Isolate build step to CM6 library and restructure files to work with RL
CodeMirror 6 requires the use of NPM, but we can still bundle all CM
packages into one file, and then everything else (i.e. our code) is
managed by ResourceLoader as per usual. This makes contribution
considerably easier as we no longer need a build step for each change.
CM5 files are now under resources/legacy, and the CM6 files are moved to
the root of the resources/ directory. Only one file,
codemirror.bundle.js, is managed by Rollup, while everything else is RL.
The Rollup output for now is put under resources/lib/ alongside the CM5
upstream files.
This patch is *mostly* renames of files, along with changing ECMAScript
Module (ESM) syntax into the CommonJS style that ResourceLoader prefers.
We also remove more modern JS syntax (i.e. private class methods) that
we were able to use before because we had a build step with Babel.
This patch should effectively make no user-facing changes, or to the
ResourceLoader modules we offer in Extension:CodeMirror.
Finally, bump version in extension.json to 6, to match the upstream lib,
and add Bhsd as an author :-)
Bug: T368053
Change-Id: Ie258e49f5df8db23a7344ac3c4c9300aaa991042
2024-06-21 03:21:09 +00:00
|
|
|
const { EditorSelection, EditorView } = require( 'ext.CodeMirror.v6.lib' );
|
2024-03-09 05:36:43 +00:00
|
|
|
|
|
|
|
/**
|
2024-03-19 03:10:11 +00:00
|
|
|
* [jQuery.textSelection]{@link jQuery.fn.textSelection} implementation for CodeMirror.
|
|
|
|
* This is registered to both the textarea and the `.cm-editor` element.
|
2024-03-09 05:36:43 +00:00
|
|
|
*
|
|
|
|
* @see jQuery.fn.textSelection
|
|
|
|
*/
|
2024-03-19 03:10:11 +00:00
|
|
|
class CodeMirrorTextSelection {
|
2024-03-09 05:36:43 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {EditorView} view
|
|
|
|
*/
|
|
|
|
constructor( view ) {
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The CodeMirror view.
|
2024-04-23 19:13:38 +00:00
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {EditorView}
|
|
|
|
*/
|
2024-03-09 05:36:43 +00:00
|
|
|
this.view = view;
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The CodeMirror DOM.
|
2024-04-23 19:13:38 +00:00
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {jQuery}
|
|
|
|
*/
|
2024-03-09 05:36:43 +00:00
|
|
|
this.$cmDom = $( view.dom );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contents of the editor.
|
|
|
|
*
|
|
|
|
* @return {string}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
getContents() {
|
|
|
|
return this.view.state.doc.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the contents of the editor.
|
|
|
|
*
|
|
|
|
* @param {string} content
|
|
|
|
* @return {jQuery}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
setContents( content ) {
|
|
|
|
this.view.dispatch( {
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: this.view.state.doc.length,
|
|
|
|
insert: content
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current caret position.
|
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {boolean} [options.startAndEnd] Whether to return the start and end of the selection
|
|
|
|
* instead of the caret position.
|
2024-03-09 05:36:43 +00:00
|
|
|
* @return {number[]|number}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
getCaretPosition( options ) {
|
|
|
|
if ( !options.startAndEnd ) {
|
|
|
|
return this.view.state.selection.main.head;
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
this.view.state.selection.main.from,
|
|
|
|
this.view.state.selection.main.to
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll the editor to the current caret position.
|
|
|
|
*
|
|
|
|
* @return {jQuery}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
scrollToCaretPosition() {
|
|
|
|
const scrollEffect = EditorView.scrollIntoView( this.view.state.selection.main.head );
|
|
|
|
scrollEffect.value.isSnapshot = true;
|
|
|
|
this.view.dispatch( {
|
|
|
|
effects: scrollEffect
|
|
|
|
} );
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the selected text.
|
|
|
|
*
|
|
|
|
* @return {string}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
getSelection() {
|
|
|
|
return this.view.state.sliceDoc(
|
|
|
|
this.view.state.selection.main.from,
|
|
|
|
this.view.state.selection.main.to
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the selected text.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
2024-03-19 03:10:11 +00:00
|
|
|
* @param {number} options.start The start of the selection.
|
|
|
|
* @param {number} [options.end=options.start] The end of the selection.
|
2024-03-09 05:36:43 +00:00
|
|
|
* @return {jQuery}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
setSelection( options ) {
|
|
|
|
this.view.dispatch( {
|
|
|
|
selection: { anchor: options.start, head: ( options.end || options.start ) }
|
|
|
|
} );
|
|
|
|
this.view.focus();
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the selected text with the given value.
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
* @return {jQuery}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
replaceSelection( value ) {
|
|
|
|
this.view.dispatch(
|
|
|
|
this.view.state.replaceSelection( value )
|
|
|
|
);
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulate the selected text with the given values.
|
|
|
|
*
|
|
|
|
* This is intentionally a near-identical implementation to jQuery.textSelection,
|
2024-03-19 03:10:11 +00:00
|
|
|
* except it uses CodeMirror's
|
|
|
|
* [EditorState.changeByRange](https://codemirror.net/docs/ref/#state.EditorState.changeByRange)
|
|
|
|
* when there are multiple selections.
|
2024-03-09 05:36:43 +00:00
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @todo Add support for 'ownline' and 'splitlines' options.
|
2024-03-09 05:36:43 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options
|
2024-03-19 03:10:11 +00:00
|
|
|
* @param {string} [options.pre] The text to insert before the cursor/selection.
|
|
|
|
* @param {string} [options.post] The text to insert after the cursor/selection.
|
|
|
|
* @param {string} [options.peri] Text to insert between pre and post and select afterwards.
|
|
|
|
* @param {boolean} [options.replace=false] If there is a selection, replace it with peri
|
|
|
|
* instead of leaving it alone.
|
|
|
|
* @param {boolean} [options.selectPeri=true] Select the peri text if it was inserted.
|
|
|
|
* @param {number} [options.selectionStart] Position to start selection at.
|
|
|
|
* @param {number} [options.selectionEnd=options.selectionStart] Position to end selection at.
|
2024-03-09 05:36:43 +00:00
|
|
|
* @return {jQuery}
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2024-03-09 05:36:43 +00:00
|
|
|
*/
|
|
|
|
encapsulateSelection( options ) {
|
|
|
|
let selectedText,
|
|
|
|
isSample = false;
|
|
|
|
|
|
|
|
const checkSelectedText = () => {
|
|
|
|
if ( !selectedText ) {
|
|
|
|
selectedText = options.peri;
|
|
|
|
isSample = true;
|
|
|
|
} else if ( options.replace ) {
|
|
|
|
selectedText = options.peri;
|
|
|
|
} else {
|
|
|
|
while ( selectedText.charAt( selectedText.length - 1 ) === ' ' ) {
|
|
|
|
// Exclude ending space char
|
|
|
|
selectedText = selectedText.slice( 0, -1 );
|
|
|
|
options.post += ' ';
|
|
|
|
}
|
|
|
|
while ( selectedText.charAt( 0 ) === ' ' ) {
|
|
|
|
// Exclude prepending space char
|
|
|
|
selectedText = selectedText.slice( 1 );
|
|
|
|
options.pre = ' ' + options.pre;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.view.focus();
|
|
|
|
|
|
|
|
// Set the selection, if applicable.
|
|
|
|
if ( options.selectionStart !== undefined ) {
|
|
|
|
this.setSelection( {
|
|
|
|
start: options.selectionStart,
|
|
|
|
end: options.selectionEnd || options.selectionStart
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedText = this.getSelection();
|
|
|
|
const [ startPos ] = this.getCaretPosition( { startAndEnd: true } );
|
|
|
|
checkSelectedText();
|
|
|
|
const insertText = options.pre + selectedText + options.post;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use CodeMirror's API when there are multiple selections.
|
|
|
|
*
|
|
|
|
* @see https://codemirror.net/examples/change/
|
|
|
|
*/
|
|
|
|
if ( this.view.state.selection.ranges.length > 1 ) {
|
|
|
|
this.view.dispatch( this.view.state.changeByRange( ( range ) => ( {
|
|
|
|
changes: [
|
|
|
|
{ from: range.from, insert: options.pre },
|
|
|
|
{ from: range.to, insert: options.post }
|
|
|
|
],
|
|
|
|
range: EditorSelection.range(
|
|
|
|
range.to + options.pre.length + options.post.length,
|
|
|
|
range.to + options.pre.length + options.post.length
|
|
|
|
)
|
|
|
|
} ) ) );
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replaceSelection( insertText );
|
|
|
|
|
|
|
|
if ( isSample && options.selectPeri ) {
|
|
|
|
this.setSelection( {
|
|
|
|
start: startPos + options.pre.length,
|
|
|
|
end: startPos + options.pre.length + selectedText.length
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.setSelection( {
|
|
|
|
start: startPos + insertText.length
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.$cmDom;
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 03:10:11 +00:00
|
|
|
|
Isolate build step to CM6 library and restructure files to work with RL
CodeMirror 6 requires the use of NPM, but we can still bundle all CM
packages into one file, and then everything else (i.e. our code) is
managed by ResourceLoader as per usual. This makes contribution
considerably easier as we no longer need a build step for each change.
CM5 files are now under resources/legacy, and the CM6 files are moved to
the root of the resources/ directory. Only one file,
codemirror.bundle.js, is managed by Rollup, while everything else is RL.
The Rollup output for now is put under resources/lib/ alongside the CM5
upstream files.
This patch is *mostly* renames of files, along with changing ECMAScript
Module (ESM) syntax into the CommonJS style that ResourceLoader prefers.
We also remove more modern JS syntax (i.e. private class methods) that
we were able to use before because we had a build step with Babel.
This patch should effectively make no user-facing changes, or to the
ResourceLoader modules we offer in Extension:CodeMirror.
Finally, bump version in extension.json to 6, to match the upstream lib,
and add Bhsd as an author :-)
Bug: T368053
Change-Id: Ie258e49f5df8db23a7344ac3c4c9300aaa991042
2024-06-21 03:21:09 +00:00
|
|
|
module.exports = CodeMirrorTextSelection;
|