Debian Codegraph @

resources/codemirror.textSelection.js

raw
1 const { EditorSelection, EditorView } = require( 'ext.CodeMirror.v6.lib' );
2
3 /**
4 * [jQuery.textSelection]{@link jQuery.fn.textSelection} implementation for CodeMirror.
5 * This is registered to both the textarea and the `.cm-editor` element.
6 *
7 * @see jQuery.fn.textSelection
8 */
9 class CodeMirrorTextSelection {
10 /**
11 * @constructor
12 * @param {EditorView} view
13 */
14 constructor( view ) {
15 /**
16 * The CodeMirror view.
17 *
18 * @type {EditorView}
19 */
20 this.view = view;
21 /**
22 * The CodeMirror DOM.
23 *
24 * @type {jQuery}
25 */
26 this.$cmDom = $( view.dom );
27 }
28
29 /**
30 * Get the contents of the editor.
31 *
32 * @return {string}
33 * @stable to call
34 */
35 getContents() {
36 return this.view.state.doc.toString();
37 }
38
39 /**
40 * Set the contents of the editor.
41 *
42 * @param {string} content
43 * @return {jQuery}
44 * @stable to call
45 */
46 setContents( content ) {
47 this.view.dispatch( {
48 changes: {
49 from: 0,
50 to: this.view.state.doc.length,
51 insert: content
52 }
53 } );
54 return this.$cmDom;
55 }
56
57 /**
58 * Get the current caret position.
59 *
60 * @param {Object} [options]
61 * @param {boolean} [options.startAndEnd] Whether to return the start and end of the selection
62 * instead of the caret position.
63 * @return {number[]|number}
64 * @stable to call
65 */
66 getCaretPosition( options ) {
67 if ( !options.startAndEnd ) {
68 return this.view.state.selection.main.head;
69 }
70 return [
71 this.view.state.selection.main.from,
72 this.view.state.selection.main.to
73 ];
74 }
75
76 /**
77 * Scroll the editor to the current caret position.
78 *
79 * @return {jQuery}
80 * @stable to call
81 */
82 scrollToCaretPosition() {
83 const scrollEffect = EditorView.scrollIntoView( this.view.state.selection.main.head );
84 scrollEffect.value.isSnapshot = true;
85 this.view.dispatch( {
86 effects: scrollEffect
87 } );
88 return this.$cmDom;
89 }
90
91 /**
92 * Get the selected text.
93 *
94 * @return {string}
95 * @stable to call
96 */
97 getSelection() {
98 return this.view.state.sliceDoc(
99 this.view.state.selection.main.from,
100 this.view.state.selection.main.to
101 );
102 }
103
104 /**
105 * Set the selected text.
106 *
107 * @param {Object} options
108 * @param {number} options.start The start of the selection.
109 * @param {number} [options.end=options.start] The end of the selection.
110 * @return {jQuery}
111 * @stable to call
112 */
113 setSelection( options ) {
114 this.view.dispatch( {
115 selection: { anchor: options.start, head: ( options.end || options.start ) }
116 } );
117 this.view.focus();
118 return this.$cmDom;
119 }
120
121 /**
122 * Replace the selected text with the given value.
123 *
124 * @param {string} value
125 * @return {jQuery}
126 * @stable to call
127 */
128 replaceSelection( value ) {
129 this.view.dispatch(
130 this.view.state.replaceSelection( value )
131 );
132 return this.$cmDom;
133 }
134
135 /**
136 * Encapsulate the selected text with the given values.
137 *
138 * This is intentionally a near-identical implementation to jQuery.textSelection,
139 * except it uses CodeMirror's
140 * [EditorState.changeByRange](https://codemirror.net/docs/ref/#state.EditorState.changeByRange)
141 * when there are multiple selections.
142 *
143 * @todo Add support for 'ownline' and 'splitlines' options.
144 *
145 * @param {Object} options
146 * @param {string} [options.pre] The text to insert before the cursor/selection.
147 * @param {string} [options.post] The text to insert after the cursor/selection.
148 * @param {string} [options.peri] Text to insert between pre and post and select afterwards.
149 * @param {boolean} [options.replace=false] If there is a selection, replace it with peri
150 * instead of leaving it alone.
151 * @param {boolean} [options.selectPeri=true] Select the peri text if it was inserted.
152 * @param {number} [options.selectionStart] Position to start selection at.
153 * @param {number} [options.selectionEnd=options.selectionStart] Position to end selection at.
154 * @return {jQuery}
155 * @stable to call
156 */
157 encapsulateSelection( options ) {
158 let selectedText,
159 isSample = false;
160
161 const checkSelectedText = () => {
162 if ( !selectedText ) {
163 selectedText = options.peri;
164 isSample = true;
165 } else if ( options.replace ) {
166 selectedText = options.peri;
167 } else {
168 while ( selectedText.charAt( selectedText.length - 1 ) === ' ' ) {
169 // Exclude ending space char
170 selectedText = selectedText.slice( 0, -1 );
171 options.post += ' ';
172 }
173 while ( selectedText.charAt( 0 ) === ' ' ) {
174 // Exclude prepending space char
175 selectedText = selectedText.slice( 1 );
176 options.pre = ' ' + options.pre;
177 }
178 }
179 };
180
181 this.view.focus();
182
183 // Set the selection, if applicable.
184 if ( options.selectionStart !== undefined ) {
185 this.setSelection( {
186 start: options.selectionStart,
187 end: options.selectionEnd || options.selectionStart
188 } );
189 }
190
191 selectedText = this.getSelection();
192 const [ startPos ] = this.getCaretPosition( { startAndEnd: true } );
193 checkSelectedText();
194 const insertText = options.pre + selectedText + options.post;
195
196 /**
197 * Use CodeMirror's API when there are multiple selections.
198 *
199 * @see https://codemirror.net/examples/change/
200 */
201 if ( this.view.state.selection.ranges.length > 1 ) {
202 this.view.dispatch( this.view.state.changeByRange( ( range ) => ( {
203 changes: [
204 { from: range.from, insert: options.pre },
205 { from: range.to, insert: options.post }
206 ],
207 range: EditorSelection.range(
208 range.to + options.pre.length + options.post.length,
209 range.to + options.pre.length + options.post.length
210 )
211 } ) ) );
212 return this.$cmDom;
213 }
214
215 this.replaceSelection( insertText );
216
217 if ( isSample && options.selectPeri ) {
218 this.setSelection( {
219 start: startPos + options.pre.length,
220 end: startPos + options.pre.length + selectedText.length
221 } );
222 } else {
223 this.setSelection( {
224 start: startPos + insertText.length
225 } );
226 }
227
228 return this.$cmDom;
229 }
230 }
231
232 module.exports = CodeMirrorTextSelection;