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

Add pointer from root node to Squire instance.

Resolves #253
This commit is contained in:
Neil Jenkins 2016-12-07 17:54:13 +11:00
parent 5252a81278
commit 504b40c857
4 changed files with 30 additions and 66 deletions

View file

@ -403,13 +403,14 @@ function fixCursor ( node, root ) {
// unfocussable if they have no content. To remedy this, a <BR> must be
// inserted. In Opera and IE, we just need a textnode in order for the
// cursor to appear.
var doc = node.ownerDocument,
originalNode = node,
fixer, child;
var self = root.__squire__;
var doc = node.ownerDocument;
var originalNode = node;
var fixer, child;
if ( node === root ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = getSquireInstance( doc ).createDefaultBlock();
fixer = self.createDefaultBlock();
if ( child ) {
node.replaceChild( fixer, child );
}
@ -435,7 +436,7 @@ function fixCursor ( node, root ) {
if ( !child ) {
if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS );
getSquireInstance( doc )._didAddZWS();
self._didAddZWS();
} else {
fixer = doc.createTextNode( '' );
}
@ -471,7 +472,7 @@ function fixCursor ( node, root ) {
try {
node.appendChild( fixer );
} catch ( error ) {
getSquireInstance( doc ).didError({
self.didError({
name: 'Squire: fixCursor  ' + error,
message: 'Parent: ' + node.nodeName + '/' + node.innerHTML +
' appendChild: ' + fixer.nodeName
@ -484,11 +485,11 @@ function fixCursor ( node, root ) {
// Recursively examine container nodes and wrap any inline children.
function fixContainer ( container, root ) {
var children = container.childNodes,
doc = container.ownerDocument,
wrapper = null,
i, l, child, isBR,
config = getSquireInstance( doc )._config;
var children = container.childNodes;
var doc = container.ownerDocument;
var wrapper = null;
var i, l, child, isBR;
var config = root.__squire__._config;
for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i];
@ -2393,20 +2394,6 @@ var onDrop = function ( event ) {
}
};
var instances = [];
function getSquireInstance ( doc ) {
var l = instances.length,
instance;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
return instance;
}
}
return null;
}
function mergeObjects ( base, extras, mayOverride ) {
var prop, value;
if ( !base ) {
@ -2547,7 +2534,7 @@ function Squire ( root, config ) {
doc.execCommand( 'enableInlineTableEditing', false, 'false' );
} catch ( error ) {}
instances.push( this );
root.__squire__ = this;
// Need to register instance before calling setHTML, so that the fixCursor
// function can lookup any default block tag options set.
@ -2698,7 +2685,6 @@ proto.fireEvent = function ( type, event ) {
};
proto.destroy = function () {
var l = instances.length;
var events = this._events;
var type;
@ -2708,11 +2694,7 @@ proto.destroy = function () {
if ( this._mutation ) {
this._mutation.disconnect();
}
while ( l-- ) {
if ( instances[l] === this ) {
instances.splice( l, 1 );
}
}
delete this._root.__squire__;
// Destroy undo stack
this._undoIndex = -1;

File diff suppressed because one or more lines are too long

View file

@ -1,19 +1,5 @@
/*jshint strict:false, undef:false, unused:false */
var instances = [];
function getSquireInstance ( doc ) {
var l = instances.length,
instance;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
return instance;
}
}
return null;
}
function mergeObjects ( base, extras, mayOverride ) {
var prop, value;
if ( !base ) {
@ -154,7 +140,7 @@ function Squire ( root, config ) {
doc.execCommand( 'enableInlineTableEditing', false, 'false' );
} catch ( error ) {}
instances.push( this );
root.__squire__ = this;
// Need to register instance before calling setHTML, so that the fixCursor
// function can lookup any default block tag options set.
@ -305,7 +291,6 @@ proto.fireEvent = function ( type, event ) {
};
proto.destroy = function () {
var l = instances.length;
var events = this._events;
var type;
@ -315,11 +300,7 @@ proto.destroy = function () {
if ( this._mutation ) {
this._mutation.disconnect();
}
while ( l-- ) {
if ( instances[l] === this ) {
instances.splice( l, 1 );
}
}
delete this._root.__squire__;
// Destroy undo stack
this._undoIndex = -1;

View file

@ -223,13 +223,14 @@ function fixCursor ( node, root ) {
// unfocussable if they have no content. To remedy this, a <BR> must be
// inserted. In Opera and IE, we just need a textnode in order for the
// cursor to appear.
var doc = node.ownerDocument,
originalNode = node,
fixer, child;
var self = root.__squire__;
var doc = node.ownerDocument;
var originalNode = node;
var fixer, child;
if ( node === root ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = getSquireInstance( doc ).createDefaultBlock();
fixer = self.createDefaultBlock();
if ( child ) {
node.replaceChild( fixer, child );
}
@ -255,7 +256,7 @@ function fixCursor ( node, root ) {
if ( !child ) {
if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS );
getSquireInstance( doc )._didAddZWS();
self._didAddZWS();
} else {
fixer = doc.createTextNode( '' );
}
@ -291,7 +292,7 @@ function fixCursor ( node, root ) {
try {
node.appendChild( fixer );
} catch ( error ) {
getSquireInstance( doc ).didError({
self.didError({
name: 'Squire: fixCursor  ' + error,
message: 'Parent: ' + node.nodeName + '/' + node.innerHTML +
' appendChild: ' + fixer.nodeName
@ -304,11 +305,11 @@ function fixCursor ( node, root ) {
// Recursively examine container nodes and wrap any inline children.
function fixContainer ( container, root ) {
var children = container.childNodes,
doc = container.ownerDocument,
wrapper = null,
i, l, child, isBR,
config = getSquireInstance( doc )._config;
var children = container.childNodes;
var doc = container.ownerDocument;
var wrapper = null;
var i, l, child, isBR;
var config = root.__squire__._config;
for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i];