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

Add a modifyDocument method

There are certain times when it is necessary to modify the document
programmatically, such as to set attributes, add tooltips, etc. Such
edits should not be observed as inputs. This method allows edits to be
ignored by the editor.
This commit is contained in:
Andy Kauffman 2016-04-28 12:01:20 -04:00 committed by Neil Jenkins
parent bcb24f1865
commit f50962db7f
4 changed files with 55 additions and 2 deletions

View file

@ -439,3 +439,8 @@ Change the **inline** formatting of the current selection. This is a high-level
3. A Range object with the range to apply the formatting changes to (or `null`/omit to apply to current selection).
4. A boolean (defaults to `false` if omitted). If `true`, any formatting nodes that cover at least part of the selected range will be removed entirely (so will potentially be removed from text outside the selected range as well). If `false`, the formatting nodes will continue to apply to any text outside the selection. This is useful, for example, when removing links. If any of the text in the selection is part of a link, the whole link is removed, rather than the link continuing to apply to bits of text outside the selection.
### modifyDocument
Takes in a function that can modify the document without the modifications being treated as input.
This is useful when the document needs to be changed programmatically, but those changes should not raise input events or modify the undo state.

View file

@ -2278,6 +2278,7 @@ function Squire ( root, config ) {
this._undoStackLength = 0;
this._isInUndoState = false;
this._ignoreChange = false;
this._ignoreAllChanges = false;
if ( canObserveMutations ) {
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
@ -2408,6 +2409,25 @@ proto.getRoot = function () {
return this._root;
};
proto.modifyDocument = function ( modificationCallback ) {
this._ignoreAllChanges = true;
if ( this._mutation ) {
this._mutation.disconnect();
}
modificationCallback();
this._ignoreAllChanges = false;
if ( this._mutation ) {
this._mutation.observe( this._root, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
}
};
// --- Events ---
// Subscribing to these events won't automatically add a listener to the
@ -2863,6 +2883,10 @@ proto._keyUpDetectChange = function ( event ) {
};
proto._docWasChanged = function () {
if ( this._ignoreAllChanges ) {
return;
}
if ( canObserveMutations && this._ignoreChange ) {
this._ignoreChange = false;
return;

File diff suppressed because one or more lines are too long

View file

@ -64,6 +64,7 @@ function Squire ( root, config ) {
this._undoStackLength = 0;
this._isInUndoState = false;
this._ignoreChange = false;
this._ignoreAllChanges = false;
if ( canObserveMutations ) {
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
@ -194,6 +195,25 @@ proto.getRoot = function () {
return this._root;
};
proto.modifyDocument = function ( modificationCallback ) {
this._ignoreAllChanges = true;
if ( this._mutation ) {
this._mutation.disconnect();
}
modificationCallback();
this._ignoreAllChanges = false;
if ( this._mutation ) {
this._mutation.observe( this._root, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
}
};
// --- Events ---
// Subscribing to these events won't automatically add a listener to the
@ -649,6 +669,10 @@ proto._keyUpDetectChange = function ( event ) {
};
proto._docWasChanged = function () {
if ( this._ignoreAllChanges ) {
return;
}
if ( canObserveMutations && this._ignoreChange ) {
this._ignoreChange = false;
return;