0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Make into a JS class for multiple instantiation.

* If you load the squire.js script into a top-level page rather than an iframe,
  it will add a Squire constructor to the global scope.
* The Squire constructor can be used to instantiate multiple instances on the
  same page without having to load/parse/execute the full code every time.
* For each instance, create a new iframe, then call `new Squire( document )`,
  with the document node for each iframe.
This commit is contained in:
Neil Jenkins 2013-06-20 23:15:18 +10:00
parent 7812b8db23
commit 6b4dda816e
7 changed files with 1455 additions and 1409 deletions

View file

@ -7,7 +7,7 @@ Unlike other HTML5 rich text editors, Squire was written as a component for writ
### Lightweight ###
* Only 9KB of JS after minification and gzip (27KB before gzip).
* Only 10KB of JS after minification and gzip (33KB before gzip).
* IE8 support does not add extra bloat to the core library; instead, a separate
3KB (7KB before gzip) file patches the browser to support the W3C APIs.
* Does not include its own XHR wrapper, widget library or lightbox overlays.
@ -40,6 +40,15 @@ Installation and usage
5. Use the API below with the `editor` object to set and get data and integrate
with your application or framework.
Advanced usage
--------------
If you load the library into a top-level document (rather than an iframe), it
will not turn the page into an editable document, but will instead add a
function named `Squire` to the global scope. Call `new Squire( document )`, with
the `document` from an iframe to instantiate multiple rich text areas on the
same page efficiently.
License
-------

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,5 @@
/*global doc, navigator */
/*jshint strict:false */
var DOCUMENT_POSITION_PRECEDING = 2; // Node.DOCUMENT_POSITION_PRECEDING
var ELEMENT_NODE = 1; // Node.ELEMENT_NODE;
@ -14,7 +15,6 @@ var END_TO_END = 2; // Range.END_TO_END
var END_TO_START = 3; // Range.END_TO_START
var win = doc.defaultView;
var body = doc.body;
var ua = navigator.userAgent;
@ -32,6 +32,13 @@ var ctrlKey = isMac ? 'meta-' : 'ctrl-';
var useTextFixer = isIE || isOpera;
var cantFocusEmptyTextNodes = isIE || isWebKit;
var losesSelectionOnBlur = isIE;
var hasBuggySplit = ( function () {
var div = doc.createElement( 'div' ),
text = doc.createTextNode( '12' );
div.appendChild( text );
text.splitText( 2 );
return div.childNodes.length !== 2;
}() );
var notWS = /\S/;

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,6 @@
SHOW_ELEMENT,
FILTER_ACCEPT,
FILTER_SKIP,
doc,
isOpera,
useTextFixer,
cantFocusEmptyTextNodes,
@ -232,7 +231,7 @@ function split ( node, offset, stopNode ) {
}
// Clone node without children
parent = node.parentNode,
parent = node.parentNode;
clone = node.cloneNode( false );
// Add right-hand siblings to the clone
@ -374,7 +373,7 @@ function mergeContainers ( node ) {
}
}
function createElement ( tag, props, children ) {
function createElement ( doc, tag, props, children ) {
var el = doc.createElement( tag ),
attr, i, l;
if ( props instanceof Array ) {
@ -393,34 +392,3 @@ function createElement ( tag, props, children ) {
}
return el;
}
// Fix IE8/9's buggy implementation of Text#splitText.
// If the split is at the end of the node, it doesn't insert the newly split
// node into the document, and sets its value to undefined rather than ''.
// And even if the split is not at the end, the original node is removed from
// the document and replaced by another, rather than just having its data
// shortened.
if ( function () {
var div = doc.createElement( 'div' ),
text = doc.createTextNode( '12' );
div.appendChild( text );
text.splitText( 2 );
return div.childNodes.length !== 2;
}() ) {
Text.prototype.splitText = function ( offset ) {
var afterSplit = this.ownerDocument.createTextNode(
this.data.slice( offset ) ),
next = this.nextSibling,
parent = this.parentNode,
toDelete = this.length - offset;
if ( next ) {
parent.insertBefore( afterSplit, next );
} else {
parent.appendChild( afterSplit );
}
if ( toDelete ) {
this.deleteData( offset, toDelete );
}
return afterSplit;
};
}

View file

@ -1 +1,13 @@
/*global top, win, doc, Squire */
if ( top !== win ) {
win.editor = new Squire( doc );
if ( win.onEditorLoad ) {
win.onEditorLoad( win.editor );
win.onEditorLoad = null;
}
} else {
win.Squire = Squire;
}
}( document ) );