|
|
@@ -9,11 +9,11 @@
|
|
|
|
|
|
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
|
import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
|
|
|
-import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
|
|
|
import diff from '@ckeditor/ckeditor5-utils/src/diff';
|
|
|
-import diffToChanges from '@ckeditor/ckeditor5-utils/src/difftochanges';
|
|
|
import DomConverter from '@ckeditor/ckeditor5-engine/src/view/domconverter';
|
|
|
|
|
|
+import { getSingleTextNodeChange, containerChildrenMutated } from '../utils';
|
|
|
+
|
|
|
/**
|
|
|
* Handles mutations caused by normal typing.
|
|
|
*
|
|
|
@@ -250,45 +250,6 @@ class MutationHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Helper function that compares whether two given view nodes are same. It is used in `diff` when it's passed an array
|
|
|
-// with child nodes.
|
|
|
-function compareChildNodes( oldChild, newChild ) {
|
|
|
- if ( oldChild instanceof ViewText && newChild instanceof ViewText ) {
|
|
|
- return oldChild.data === newChild.data;
|
|
|
- } else {
|
|
|
- return oldChild === newChild;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// Returns change made to a single text node. Returns `undefined` if more than a single text node was changed.
|
|
|
-//
|
|
|
-// @private
|
|
|
-// @param mutation
|
|
|
-function getSingleTextNodeChange( mutation ) {
|
|
|
- // One new node.
|
|
|
- if ( mutation.newChildren.length - mutation.oldChildren.length != 1 ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Which is text.
|
|
|
- const diffResult = diff( mutation.oldChildren, mutation.newChildren, compareChildNodes );
|
|
|
- const changes = diffToChanges( diffResult, mutation.newChildren );
|
|
|
-
|
|
|
- // In case of [ delete, insert, insert ] the previous check will not exit.
|
|
|
- if ( changes.length > 1 ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const change = changes[ 0 ];
|
|
|
-
|
|
|
- // Which is text.
|
|
|
- if ( !( change.values[ 0 ] instanceof ViewText ) ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- return change;
|
|
|
-}
|
|
|
-
|
|
|
// Returns first common ancestor of all mutations that is either {@link module:engine/view/containerelement~ContainerElement}
|
|
|
// or {@link module:engine/view/rootelement~RootElement}.
|
|
|
//
|
|
|
@@ -313,30 +274,6 @@ function getMutationsContainer( mutations ) {
|
|
|
.find( element => element.is( 'containerElement' ) || element.is( 'rootElement' ) );
|
|
|
}
|
|
|
|
|
|
-// Returns true if container children have mutated or more than a single text node was changed.
|
|
|
-//
|
|
|
-// Single text node child insertion is handled in {@link module:typing/input~MutationHandler#_handleTextNodeInsertion}
|
|
|
-// while text mutation is handled in {@link module:typing/input~MutationHandler#_handleTextMutation}.
|
|
|
-//
|
|
|
-// @private
|
|
|
-// @param {Array.<module:engine/view/observer/mutationobserver~MutatedText|
|
|
|
-// module:engine/view/observer/mutationobserver~MutatedChildren>} mutations
|
|
|
-// @returns {Boolean}
|
|
|
-function containerChildrenMutated( mutations ) {
|
|
|
- if ( mutations.length == 0 ) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // Check if there is any mutation of `children` type or any mutation that changes more than one text node.
|
|
|
- for ( const mutation of mutations ) {
|
|
|
- if ( mutation.type === 'children' && !getSingleTextNodeChange( mutation ) ) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
// Returns true if provided array contains content that won't be problematic during diffing and text mutation handling.
|
|
|
//
|
|
|
// @param {Array.<module:engine/model/node~Node>} children
|