0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -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 // 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 // inserted. In Opera and IE, we just need a textnode in order for the
// cursor to appear. // cursor to appear.
var doc = node.ownerDocument, var self = root.__squire__;
originalNode = node, var doc = node.ownerDocument;
fixer, child; var originalNode = node;
var fixer, child;
if ( node === root ) { if ( node === root ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) { if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = getSquireInstance( doc ).createDefaultBlock(); fixer = self.createDefaultBlock();
if ( child ) { if ( child ) {
node.replaceChild( fixer, child ); node.replaceChild( fixer, child );
} }
@ -435,7 +436,7 @@ function fixCursor ( node, root ) {
if ( !child ) { if ( !child ) {
if ( cantFocusEmptyTextNodes ) { if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS ); fixer = doc.createTextNode( ZWS );
getSquireInstance( doc )._didAddZWS(); self._didAddZWS();
} else { } else {
fixer = doc.createTextNode( '' ); fixer = doc.createTextNode( '' );
} }
@ -471,7 +472,7 @@ function fixCursor ( node, root ) {
try { try {
node.appendChild( fixer ); node.appendChild( fixer );
} catch ( error ) { } catch ( error ) {
getSquireInstance( doc ).didError({ self.didError({
name: 'Squire: fixCursor  ' + error, name: 'Squire: fixCursor  ' + error,
message: 'Parent: ' + node.nodeName + '/' + node.innerHTML + message: 'Parent: ' + node.nodeName + '/' + node.innerHTML +
' appendChild: ' + fixer.nodeName ' appendChild: ' + fixer.nodeName
@ -484,11 +485,11 @@ function fixCursor ( node, root ) {
// Recursively examine container nodes and wrap any inline children. // Recursively examine container nodes and wrap any inline children.
function fixContainer ( container, root ) { function fixContainer ( container, root ) {
var children = container.childNodes, var children = container.childNodes;
doc = container.ownerDocument, var doc = container.ownerDocument;
wrapper = null, var wrapper = null;
i, l, child, isBR, var i, l, child, isBR;
config = getSquireInstance( doc )._config; var config = root.__squire__._config;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; 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 ) { function mergeObjects ( base, extras, mayOverride ) {
var prop, value; var prop, value;
if ( !base ) { if ( !base ) {
@ -2547,7 +2534,7 @@ function Squire ( root, config ) {
doc.execCommand( 'enableInlineTableEditing', false, 'false' ); doc.execCommand( 'enableInlineTableEditing', false, 'false' );
} catch ( error ) {} } catch ( error ) {}
instances.push( this ); root.__squire__ = this;
// Need to register instance before calling setHTML, so that the fixCursor // Need to register instance before calling setHTML, so that the fixCursor
// function can lookup any default block tag options set. // function can lookup any default block tag options set.
@ -2698,7 +2685,6 @@ proto.fireEvent = function ( type, event ) {
}; };
proto.destroy = function () { proto.destroy = function () {
var l = instances.length;
var events = this._events; var events = this._events;
var type; var type;
@ -2708,11 +2694,7 @@ proto.destroy = function () {
if ( this._mutation ) { if ( this._mutation ) {
this._mutation.disconnect(); this._mutation.disconnect();
} }
while ( l-- ) { delete this._root.__squire__;
if ( instances[l] === this ) {
instances.splice( l, 1 );
}
}
// Destroy undo stack // Destroy undo stack
this._undoIndex = -1; 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 */ /*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 ) { function mergeObjects ( base, extras, mayOverride ) {
var prop, value; var prop, value;
if ( !base ) { if ( !base ) {
@ -154,7 +140,7 @@ function Squire ( root, config ) {
doc.execCommand( 'enableInlineTableEditing', false, 'false' ); doc.execCommand( 'enableInlineTableEditing', false, 'false' );
} catch ( error ) {} } catch ( error ) {}
instances.push( this ); root.__squire__ = this;
// Need to register instance before calling setHTML, so that the fixCursor // Need to register instance before calling setHTML, so that the fixCursor
// function can lookup any default block tag options set. // function can lookup any default block tag options set.
@ -305,7 +291,6 @@ proto.fireEvent = function ( type, event ) {
}; };
proto.destroy = function () { proto.destroy = function () {
var l = instances.length;
var events = this._events; var events = this._events;
var type; var type;
@ -315,11 +300,7 @@ proto.destroy = function () {
if ( this._mutation ) { if ( this._mutation ) {
this._mutation.disconnect(); this._mutation.disconnect();
} }
while ( l-- ) { delete this._root.__squire__;
if ( instances[l] === this ) {
instances.splice( l, 1 );
}
}
// Destroy undo stack // Destroy undo stack
this._undoIndex = -1; 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 // 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 // inserted. In Opera and IE, we just need a textnode in order for the
// cursor to appear. // cursor to appear.
var doc = node.ownerDocument, var self = root.__squire__;
originalNode = node, var doc = node.ownerDocument;
fixer, child; var originalNode = node;
var fixer, child;
if ( node === root ) { if ( node === root ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) { if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = getSquireInstance( doc ).createDefaultBlock(); fixer = self.createDefaultBlock();
if ( child ) { if ( child ) {
node.replaceChild( fixer, child ); node.replaceChild( fixer, child );
} }
@ -255,7 +256,7 @@ function fixCursor ( node, root ) {
if ( !child ) { if ( !child ) {
if ( cantFocusEmptyTextNodes ) { if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS ); fixer = doc.createTextNode( ZWS );
getSquireInstance( doc )._didAddZWS(); self._didAddZWS();
} else { } else {
fixer = doc.createTextNode( '' ); fixer = doc.createTextNode( '' );
} }
@ -291,7 +292,7 @@ function fixCursor ( node, root ) {
try { try {
node.appendChild( fixer ); node.appendChild( fixer );
} catch ( error ) { } catch ( error ) {
getSquireInstance( doc ).didError({ self.didError({
name: 'Squire: fixCursor  ' + error, name: 'Squire: fixCursor  ' + error,
message: 'Parent: ' + node.nodeName + '/' + node.innerHTML + message: 'Parent: ' + node.nodeName + '/' + node.innerHTML +
' appendChild: ' + fixer.nodeName ' appendChild: ' + fixer.nodeName
@ -304,11 +305,11 @@ function fixCursor ( node, root ) {
// Recursively examine container nodes and wrap any inline children. // Recursively examine container nodes and wrap any inline children.
function fixContainer ( container, root ) { function fixContainer ( container, root ) {
var children = container.childNodes, var children = container.childNodes;
doc = container.ownerDocument, var doc = container.ownerDocument;
wrapper = null, var wrapper = null;
i, l, child, isBR, var i, l, child, isBR;
config = getSquireInstance( doc )._config; var config = root.__squire__._config;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];