0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-08 16:00:06 -05:00

Squire options to a private object with a getter

Changing tag block and properties on the way could mix things up. Made
them a private property that is set on initialization and accessible via
getSettings() public method
This commit is contained in:
dryoma 2015-04-23 16:40:46 +08:00
parent a314e25322
commit 1570bca9ec
3 changed files with 42 additions and 24 deletions

View file

@ -1114,13 +1114,19 @@ function Squire ( doc, options ) {
this._doc = doc; this._doc = doc;
this._body = body; this._body = body;
var defaults = { var settings = {
blockTag: 'DIV', blockTag: 'DIV',
blockProperties: null tagAttributes: {
block: null
}
} }
this.options = MergeObjects(defaults, options); settings = MergeObjects(settings, options);
// To prevent mistakes if the tag is set in lowercase by a user // To prevent mistakes if the tag is set in lowercase by a user
this.options.blockTag = this.options.blockTag.toUpperCase(); settings.blockTag = settings.blockTag.toUpperCase();
this.getSettings = function (){
return settings;
}
this._events = {}; this._events = {};
@ -1221,9 +1227,10 @@ proto.createElement = function ( tag, props, children ) {
}; };
proto.createDefaultBlock = function ( children ) { proto.createDefaultBlock = function ( children ) {
var settings = this.getSettings();
return fixCursor( return fixCursor(
this.createElement( this.createElement(
this.options.blockTag, this.options.blockProperties, children ) settings.blockTag, settings.tagAttributes.block, children )
); );
}; };
@ -2006,11 +2013,12 @@ var tagAfterSplit = {
var splitBlock = function ( self, block, node, offset ) { var splitBlock = function ( self, block, node, offset ) {
var splitTag = tagAfterSplit[ block.nodeName ], var splitTag = tagAfterSplit[ block.nodeName ],
splitProperties = null, splitProperties = null,
nodeAfterSplit = split( node, offset, block.parentNode ); nodeAfterSplit = split( node, offset, block.parentNode ),
settings = self.getSettings();
if ( !splitTag ) { if ( !splitTag ) {
splitTag = self.options.blockTag; splitTag = settings.blockTag;
splitProperties = self.options.blockProperties; splitProperties = settings.tagAttributes.block;
} }
// Make sure the new node is the correct type. // Make sure the new node is the correct type.
@ -2617,7 +2625,7 @@ var cleanupBRs = function ( root ) {
// If in a <div>, split, but anywhere else we might change // If in a <div>, split, but anywhere else we might change
// the formatting too much (e.g. <li> -> to two list items!) // the formatting too much (e.g. <li> -> to two list items!)
// so just play it safe and leave it. // so just play it safe and leave it.
if ( block.nodeName !== this.options.blockTag ) { if ( block.nodeName !== this.getSettings().blockTag ) {
continue; continue;
} }
split( br.parentNode, br, block.parentNode ); split( br.parentNode, br, block.parentNode );
@ -2630,7 +2638,7 @@ var cleanupBRs = function ( root ) {
proto._ensureBottomLine = function () { proto._ensureBottomLine = function () {
var body = this._body, var body = this._body,
last = body.lastElementChild; last = body.lastElementChild;
if ( !last || last.nodeName !== this.options.blockTag || !isBlock( last ) ) { if ( !last || last.nodeName !== this.getSettings().blockTag || !isBlock( last ) ) {
body.appendChild( this.createDefaultBlock() ); body.appendChild( this.createDefaultBlock() );
} }
}; };
@ -3256,7 +3264,8 @@ proto.getHTML = function ( withBookMark ) {
proto.setHTML = function ( html ) { proto.setHTML = function ( html ) {
var frag = this._doc.createDocumentFragment(), var frag = this._doc.createDocumentFragment(),
div = this.createElement(this.options.blockTag, this.options.blockProperties), settings = this.getSettings(),
div = this.createElement(settings.blockTag, settings.tagAttributes.block),
child; child;
// Parse HTML into DOM tree // Parse HTML into DOM tree

File diff suppressed because one or more lines are too long

View file

@ -43,13 +43,19 @@ function Squire ( doc, options ) {
this._doc = doc; this._doc = doc;
this._body = body; this._body = body;
var defaults = { var settings = {
blockTag: 'DIV', blockTag: 'DIV',
blockProperties: null tagAttributes: {
block: null
}
} }
this.options = MergeObjects(defaults, options); settings = MergeObjects(settings, options);
// To prevent mistakes if the tag is set in lowercase by a user // To prevent mistakes if the tag is set in lowercase by a user
this.options.blockTag = this.options.blockTag.toUpperCase(); settings.blockTag = settings.blockTag.toUpperCase();
this.getSettings = function (){
return settings;
}
this._events = {}; this._events = {};
@ -150,9 +156,10 @@ proto.createElement = function ( tag, props, children ) {
}; };
proto.createDefaultBlock = function ( children ) { proto.createDefaultBlock = function ( children ) {
var settings = this.getSettings();
return fixCursor( return fixCursor(
this.createElement( this.createElement(
this.options.blockTag, this.options.blockProperties, children ) settings.blockTag, settings.tagAttributes.block, children )
); );
}; };
@ -935,11 +942,12 @@ var tagAfterSplit = {
var splitBlock = function ( self, block, node, offset ) { var splitBlock = function ( self, block, node, offset ) {
var splitTag = tagAfterSplit[ block.nodeName ], var splitTag = tagAfterSplit[ block.nodeName ],
splitProperties = null, splitProperties = null,
nodeAfterSplit = split( node, offset, block.parentNode ); nodeAfterSplit = split( node, offset, block.parentNode ),
settings = self.getSettings();
if ( !splitTag ) { if ( !splitTag ) {
splitTag = self.options.blockTag; splitTag = settings.blockTag;
splitProperties = self.options.blockProperties; splitProperties = settings.tagAttributes.block;
} }
// Make sure the new node is the correct type. // Make sure the new node is the correct type.
@ -1546,7 +1554,7 @@ var cleanupBRs = function ( root ) {
// If in a <div>, split, but anywhere else we might change // If in a <div>, split, but anywhere else we might change
// the formatting too much (e.g. <li> -> to two list items!) // the formatting too much (e.g. <li> -> to two list items!)
// so just play it safe and leave it. // so just play it safe and leave it.
if ( block.nodeName !== this.options.blockTag ) { if ( block.nodeName !== this.getSettings().blockTag ) {
continue; continue;
} }
split( br.parentNode, br, block.parentNode ); split( br.parentNode, br, block.parentNode );
@ -1559,7 +1567,7 @@ var cleanupBRs = function ( root ) {
proto._ensureBottomLine = function () { proto._ensureBottomLine = function () {
var body = this._body, var body = this._body,
last = body.lastElementChild; last = body.lastElementChild;
if ( !last || last.nodeName !== this.options.blockTag || !isBlock( last ) ) { if ( !last || last.nodeName !== this.getSettings().blockTag || !isBlock( last ) ) {
body.appendChild( this.createDefaultBlock() ); body.appendChild( this.createDefaultBlock() );
} }
}; };
@ -2185,7 +2193,8 @@ proto.getHTML = function ( withBookMark ) {
proto.setHTML = function ( html ) { proto.setHTML = function ( html ) {
var frag = this._doc.createDocumentFragment(), var frag = this._doc.createDocumentFragment(),
div = this.createElement(this.options.blockTag, this.options.blockProperties), settings = this.getSettings(),
div = this.createElement(settings.blockTag, settings.tagAttributes.block),
child; child;
// Parse HTML into DOM tree // Parse HTML into DOM tree