mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -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:
parent
e3d0576a38
commit
c766333f0f
4 changed files with 56 additions and 2 deletions
|
@ -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).
|
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.
|
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.
|
||||||
|
|
|
@ -2276,6 +2276,7 @@ function Squire ( root, config ) {
|
||||||
this._undoStackLength = 0;
|
this._undoStackLength = 0;
|
||||||
this._isInUndoState = false;
|
this._isInUndoState = false;
|
||||||
this._ignoreChange = false;
|
this._ignoreChange = false;
|
||||||
|
this._ignoreAllChanges = false;
|
||||||
|
|
||||||
if ( canObserveMutations ) {
|
if ( canObserveMutations ) {
|
||||||
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
|
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
|
||||||
|
@ -2406,6 +2407,25 @@ proto.getRoot = function () {
|
||||||
return this._root;
|
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 ---
|
// --- Events ---
|
||||||
|
|
||||||
// Subscribing to these events won't automatically add a listener to the
|
// Subscribing to these events won't automatically add a listener to the
|
||||||
|
@ -2861,6 +2881,10 @@ proto._keyUpDetectChange = function ( event ) {
|
||||||
};
|
};
|
||||||
|
|
||||||
proto._docWasChanged = function () {
|
proto._docWasChanged = function () {
|
||||||
|
if(this._ignoreAllChanges) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ( canObserveMutations && this._ignoreChange ) {
|
if ( canObserveMutations && this._ignoreChange ) {
|
||||||
this._ignoreChange = false;
|
this._ignoreChange = false;
|
||||||
return;
|
return;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -64,6 +64,7 @@ function Squire ( root, config ) {
|
||||||
this._undoStackLength = 0;
|
this._undoStackLength = 0;
|
||||||
this._isInUndoState = false;
|
this._isInUndoState = false;
|
||||||
this._ignoreChange = false;
|
this._ignoreChange = false;
|
||||||
|
this._ignoreAllChanges = false;
|
||||||
|
|
||||||
if ( canObserveMutations ) {
|
if ( canObserveMutations ) {
|
||||||
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
|
mutation = new MutationObserver( this._docWasChanged.bind( this ) );
|
||||||
|
@ -194,6 +195,25 @@ proto.getRoot = function () {
|
||||||
return this._root;
|
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 ---
|
// --- Events ---
|
||||||
|
|
||||||
// Subscribing to these events won't automatically add a listener to the
|
// Subscribing to these events won't automatically add a listener to the
|
||||||
|
@ -649,6 +669,10 @@ proto._keyUpDetectChange = function ( event ) {
|
||||||
};
|
};
|
||||||
|
|
||||||
proto._docWasChanged = function () {
|
proto._docWasChanged = function () {
|
||||||
|
if(this._ignoreAllChanges) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ( canObserveMutations && this._ignoreChange ) {
|
if ( canObserveMutations && this._ignoreChange ) {
|
||||||
this._ignoreChange = false;
|
this._ignoreChange = false;
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue