Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable Surface class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable surface observer.
9 *
10 * @class
11 *
12 * @constructor
13 * @param {ve.ce.Surface} surface Surface to observe
14 */
15 ve.ce.SurfaceObserver = function VeCeSurfaceObserver( surface ) {
16 // Properties
17 this.surface = surface;
18 this.domDocument = surface.attachedRoot.getElementDocument();
19 this.polling = false;
20 this.disabled = false;
21 this.timeoutId = null;
22 this.pollInterval = 250; // ms
23 this.rangeState = null;
24 };
25
26 /* Inheritance */
27
28 OO.initClass( ve.ce.SurfaceObserver );
29
30 /* Methods */
31
32 /**
33 * Clear polling data.
34 */
35 ve.ce.SurfaceObserver.prototype.clear = function () {
36 this.rangeState = null;
37 };
38
39 /**
40 * Detach from the document view
41 */
42 ve.ce.SurfaceObserver.prototype.detach = function () {
43 this.surface = null;
44 this.domDocument = null;
45 this.rangeState = null;
46 };
47
48 /**
49 * Start the setTimeout synchronisation loop
50 */
51 ve.ce.SurfaceObserver.prototype.startTimerLoop = function () {
52 this.polling = true;
53 this.timerLoop( true ); // Will not sync immediately, because timeoutId should be null
54 };
55
56 /**
57 * Loop once with `setTimeout`
58 *
59 * @param {boolean} firstTime Wait before polling
60 */
61 ve.ce.SurfaceObserver.prototype.timerLoop = function ( firstTime ) {
62 if ( this.timeoutId ) {
63 // In case we're not running from setTimeout
64 clearTimeout( this.timeoutId );
65 this.timeoutId = null;
66 }
67 if ( !firstTime ) {
68 this.pollOnce();
69 }
70 // Only reach this point if pollOnce does not throw an exception
71 if ( this.pollInterval !== null ) {
72 this.timeoutId = this.setTimeout(
73 this.timerLoop.bind( this ),
74 this.pollInterval
75 );
76 }
77 };
78
79 /**
80 * Stop polling
81 */
82 ve.ce.SurfaceObserver.prototype.stopTimerLoop = function () {
83 if ( this.polling === true ) {
84 this.polling = false;
85 clearTimeout( this.timeoutId );
86 this.timeoutId = null;
87 }
88 };
89
90 /**
91 * Disable the surface observer
92 */
93 ve.ce.SurfaceObserver.prototype.disable = function () {
94 this.disabled = true;
95 };
96
97 /**
98 * Enable the surface observer
99 */
100 ve.ce.SurfaceObserver.prototype.enable = function () {
101 this.disabled = false;
102 };
103
104 /**
105 * Poll for changes.
106 */
107 ve.ce.SurfaceObserver.prototype.pollOnce = function () {
108 this.pollOnceInternal( true );
109 };
110
111 /**
112 * Poll to update SurfaceObserver, but don't signal any changes back to the Surface
113 */
114 ve.ce.SurfaceObserver.prototype.pollOnceNoCallback = function () {
115 this.pollOnceInternal( false );
116 };
117
118 /**
119 * Poll to update SurfaceObserver, but only check for selection changes
120 *
121 * Used as an optimisation when you know the content hasn't changed
122 */
123 ve.ce.SurfaceObserver.prototype.pollOnceSelection = function () {
124 this.pollOnceInternal( true, true );
125 };
126
127 /**
128 * Poll for changes.
129 *
130 * @private
131 * @param {boolean} signalChanges If there changes are observed, call Surface#handleObservedChange
132 * @param {boolean} selectionOnly Check for selection changes only
133 */
134 ve.ce.SurfaceObserver.prototype.pollOnceInternal = function ( signalChanges, selectionOnly ) {
135 if ( !this.domDocument || this.disabled ) {
136 return;
137 }
138
139 const oldState = this.rangeState;
140 const newState = new ve.ce.RangeState(
141 oldState,
142 this.surface.attachedRoot,
143 selectionOnly
144 );
145 this.rangeState = newState;
146
147 if ( signalChanges && (
148 newState.contentChanged ||
149 newState.branchNodeChanged ||
150 newState.selectionChanged
151 ) ) {
152 this.surface.handleObservedChanges( oldState, newState );
153 }
154 };
155
156 /**
157 * Wrapper for setTimeout, for ease of debugging
158 *
159 * @param {Function} callback
160 * @param {number} timeout Timeout ms
161 * @return {number} Timeout ID
162 */
163 ve.ce.SurfaceObserver.prototype.setTimeout = function ( callback, timeout ) {
164 return setTimeout( callback, timeout );
165 };
166
167 /**
168 * Get the range last observed.
169 *
170 * Used when you have just polled, but don't want to wait for a 'rangeChange' event.
171 *
172 * @return {ve.Range|null}
173 */
174 ve.ce.SurfaceObserver.prototype.getRange = function () {
175 if ( !this.rangeState ) {
176 return null;
177 }
178 return this.rangeState.veRange;
179 };