GithubHelp home page GithubHelp logo

jqtexter's Introduction

jqTexter

jQTexter is a jQuery plugin for formatting text that works with character indexes so you don't have to deal with DOM selection and range objects. It evens out browser inconsistencies by working with elements' text contents instead of DOM Nodes.

Unlike some other editors out there, jQTexter's formatting attemps to optimally distribute tags. It will never leave tags of the same type next to each other or have tags overlap. For instance, if you have some bold text and later you add some italic tags inside, you will get this:

<b>Some <i>italic</i> and some more <i>italic</i></b>

instead of this:

<b>Some </b><i><b>italic</b></i><b> and some more </b><i><b>italic</b></i>

Selection and formatting

Selections are described with a simple object like this:

{ start: 5, length: 15 }

Text formatting is described with list of simple objects like this:

{ B: [{start: 7, end: 33, attrs: {}}, {start: 65, end: 97, attrs: {}}],
  I: [{start: 87, end: 117, attrs: {}}, {start: 225, end: 245, attrs: {}}],
  A: [{start: 364, end: 397, attrs: {href: "http://example.com", target: "_blank"}}] }

Methods

  • textSelection( [selection] ) gets/sets selection in an element
  • textFormat( [formatting, replacementText] ) gets/sets formatting in an element
  • applyTag( selection, tagName, [attrs, remove] ) applies (or removes) tagName to selection
  • getSelectedNodes() returns a list of child nodes completely contained in selection

Compatibility

jQtexter definitely works with IE 9+, Firefox 4+ and Chrome 12+. I'm not sure about other versions, but it requires the following APIs to be present:

  • window.getSelection()
  • selection.getRangeAt()
  • selection.anchorNode, selection.focusNode
  • HTML contentEditable attribute (required for actual editing)

Demo / Project Page

jQtexter at discobot

License

MIT

jqtexter's People

Contributors

skid avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.