Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.RangeState.js

raw symbols SCIP
1 /*!
2 * VisualEditor Content Editable Range State class
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable range state (a snapshot of CE selection/content state)
9 *
10 * @class
11 *
12 * @constructor
13 * @param {ve.ce.RangeState|null} old Previous range state
14 * @param {ve.ce.BranchNode} root Surface root
15 * @param {boolean} selectionOnly The caller promises the content has not changed from old
16 */
17 ve.ce.RangeState = function VeCeRangeState( old, root, selectionOnly ) {
18 /**
19 * @property {boolean} branchNodeChanged Whether the CE branch node changed
20 */
21 this.branchNodeChanged = false;
22
23 /**
24 * @property {boolean} selectionChanged Whether the DOM range changed
25 */
26 this.selectionChanged = false;
27
28 /**
29 * @property {boolean} contentChanged Whether the content changed
30 *
31 * This is only set to true if both the old and new states have the
32 * same current branch node, whose content has changed
33 */
34 this.contentChanged = false;
35
36 /**
37 * @property {ve.Range|null} veRange The current selection range
38 */
39 this.veRange = null;
40
41 /**
42 * @property {ve.ce.BranchNode|null} node The current branch node
43 */
44 this.node = null;
45
46 /**
47 * @property {string|null} text Plain text of current branch node
48 */
49 this.text = null;
50
51 /**
52 * @property {string|null} DOM Hash of current branch node
53 */
54 this.hash = null;
55
56 /**
57 * @property {ve.ce.TextState|null} Current branch node's annotated content
58 */
59 this.textState = null;
60
61 /**
62 * @property {boolean|null} focusIsAfterAnnotationBoundary Focus lies after annotation tag
63 */
64 this.focusIsAfterAnnotationBoundary = null;
65
66 /**
67 * Saved selection for future comparisons. (But it is not properly frozen, because the
68 * nodes are live and mutable, and therefore the offsets may come to point to places that
69 * are misleadingly different from when the selection was saved).
70 *
71 * @property {ve.SelectionState} misleadingSelection Saved selection (but with live nodes)
72 */
73 this.misleadingSelection = null;
74
75 this.saveState( old, root, selectionOnly );
76 };
77
78 /* Inheritance */
79
80 OO.initClass( ve.ce.RangeState );
81
82 /* Methods */
83
84 /**
85 * Saves a snapshot of the current range state
86 *
87 * @param {ve.ce.RangeState|null} old Previous range state
88 * @param {ve.ce.BranchNode} root Surface root
89 * @param {boolean} selectionOnly The caller promises the content has not changed from old
90 */
91 ve.ce.RangeState.prototype.saveState = function ( old, root, selectionOnly ) {
92 const oldSelection = old ? old.misleadingSelection : ve.SelectionState.static.newNullSelection(),
93 nativeSelection = root.getElementDocument().getSelection();
94
95 let selection;
96 if (
97 nativeSelection.rangeCount &&
98 OO.ui.contains( root.$element[ 0 ], nativeSelection.focusNode, true )
99 ) {
100 // Freeze selection out of live object.
101 selection = new ve.SelectionState( nativeSelection );
102 } else {
103 // Use a blank selection if the selection is outside the document
104 selection = ve.SelectionState.static.newNullSelection();
105 }
106
107 // Get new range information
108 if ( selection.equalsSelection( oldSelection ) ) {
109 // No change; use old values for speed
110 this.selectionChanged = false;
111 this.veRange = old && old.veRange;
112 } else {
113 this.selectionChanged = true;
114 this.veRange = ve.ce.veRangeFromSelection( selection );
115 }
116
117 const focusNodeChanged = oldSelection.focusNode !== selection.focusNode;
118
119 if ( !focusNodeChanged ) {
120 this.node = old && old.node;
121 } else {
122 const $node = $( selection.focusNode ).closest( '.ve-ce-branchNode' );
123 if ( $node.length === 0 ) {
124 this.node = null;
125 } else {
126 this.node = $node.data( 'view' );
127 // Check this node belongs to our document
128 if ( this.node && this.node.root !== root.root ) {
129 this.node = null;
130 this.veRange = null;
131 }
132 }
133 }
134
135 this.branchNodeChanged = ( old && old.node ) !== this.node;
136
137 // Compute text/hash/textState, for change comparison
138 if ( !this.node ) {
139 this.text = null;
140 this.hash = null;
141 this.textState = null;
142 } else if ( selectionOnly && !focusNodeChanged ) {
143 this.text = old.text;
144 this.hash = old.hash;
145 this.textState = old.textState;
146 } else {
147 this.text = ve.ce.getDomText( this.node.$element[ 0 ] );
148 this.hash = ve.ce.getDomHash( this.node.$element[ 0 ] );
149 this.textState = new ve.ce.TextState( this.node.$element[ 0 ] );
150 }
151
152 // Only set contentChanged if we're still in the same branch node
153 this.contentChanged =
154 !selectionOnly &&
155 !this.branchNodeChanged && (
156 ( old && old.hash ) !== this.hash ||
157 ( old && old.text ) !== this.text ||
158 ( !this.textState && old && old.textState ) ||
159 ( !!this.textState && !this.textState.isEqual( old && old.textState ) )
160 );
161
162 if ( old && !this.selectionChanged && !this.contentChanged ) {
163 this.focusIsAfterAnnotationBoundary = old.focusIsAfterAnnotationBoundary;
164 } else {
165 // Will be null if there is no selection
166 this.focusIsAfterAnnotationBoundary = selection.focusNode &&
167 ve.ce.isAfterAnnotationBoundary(
168 selection.focusNode,
169 selection.focusOffset
170 );
171 }
172
173 // Save selection for future comparisons. (But it is not properly frozen, because the nodes
174 // are live and mutable, and therefore the offsets may come to point to places that are
175 // misleadingly different from when the selection was saved).
176 this.misleadingSelection = selection;
177 };