2024-03-09 05:36:43 +00:00
|
|
|
import { EditorState, Extension } from '@codemirror/state';
|
2024-03-11 21:03:28 +00:00
|
|
|
import { EditorView, drawSelection, lineNumbers, highlightSpecialChars, keymap } from '@codemirror/view';
|
|
|
|
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
|
|
|
|
import { searchKeymap } from '@codemirror/search';
|
|
|
|
import { bracketMatching } from '@codemirror/language';
|
2024-03-19 03:10:11 +00:00
|
|
|
import CodeMirrorTextSelection from './codemirror.textSelection';
|
2023-09-19 17:59:29 +00:00
|
|
|
|
2024-03-13 04:24:53 +00:00
|
|
|
require( '../ext.CodeMirror.data.js' );
|
2024-03-11 18:10:08 +00:00
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
/**
|
2024-03-19 03:10:11 +00:00
|
|
|
* Interface for the CodeMirror editor.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* mw.loader.using( [
|
|
|
|
* 'ext.CodeMirror.v6',
|
|
|
|
* 'ext.CodeMirror.v6.mode.mediawiki'
|
|
|
|
* ] ).then( ( require ) => {
|
|
|
|
* const CodeMirror = require( 'ext.CodeMirror.v6' );
|
|
|
|
* const mediawikiLang = require( 'ext.CodeMirror.v6.mode.mediawiki' );
|
|
|
|
* const cm = new CodeMirror( myTextarea );
|
|
|
|
* cm.initialize( [ cm.defaultExtensions, mediawikiLang() ] );
|
|
|
|
* } );
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
2024-03-19 03:10:11 +00:00
|
|
|
class CodeMirror {
|
2023-09-19 17:59:29 +00:00
|
|
|
/**
|
2024-03-19 03:10:11 +00:00
|
|
|
* Instantiate a new CodeMirror instance.
|
|
|
|
*
|
2023-12-06 18:46:59 +00:00
|
|
|
* @param {HTMLTextAreaElement|jQuery|string} textarea Textarea to add syntax highlighting to.
|
2024-03-19 03:10:11 +00:00
|
|
|
* @constructor
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
2023-12-06 18:46:59 +00:00
|
|
|
constructor( textarea ) {
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The textarea that CodeMirror is bound to.
|
|
|
|
* @type {jQuery}
|
|
|
|
*/
|
2023-12-06 18:46:59 +00:00
|
|
|
this.$textarea = $( textarea );
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The editor user interface.
|
|
|
|
* @type {EditorView}
|
|
|
|
*/
|
2023-09-19 17:59:29 +00:00
|
|
|
this.view = null;
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The editor state.
|
|
|
|
* @type {EditorState}
|
|
|
|
*/
|
2023-09-19 17:59:29 +00:00
|
|
|
this.state = null;
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* Whether the textarea is read-only.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2024-01-10 00:02:44 +00:00
|
|
|
this.readOnly = this.$textarea.prop( 'readonly' );
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* The [edit recovery]{@link https://www.mediawiki.org/wiki/Manual:Edit_Recovery} handler.
|
|
|
|
* @type {Function|null}
|
|
|
|
*/
|
2024-03-11 21:03:28 +00:00
|
|
|
this.editRecoveryHandler = null;
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* jQuery.textSelection overrides for CodeMirror.
|
|
|
|
* @type {CodeMirrorTextSelection}
|
|
|
|
*/
|
2024-03-09 05:36:43 +00:00
|
|
|
this.textSelection = null;
|
2023-09-19 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-19 03:10:11 +00:00
|
|
|
* Default extensions used by CodeMirror.
|
2023-09-19 17:59:29 +00:00
|
|
|
* Extensions here should be applicable to all theoretical uses of CodeMirror in MediaWiki.
|
|
|
|
*
|
2023-10-11 02:00:08 +00:00
|
|
|
* @see https://codemirror.net/docs/ref/#state.Extension
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {Extension|Extension[]}
|
|
|
|
* @stable to call
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
|
|
|
get defaultExtensions() {
|
2023-10-11 02:00:08 +00:00
|
|
|
const extensions = [
|
2024-01-13 03:21:12 +00:00
|
|
|
this.contentAttributesExtension,
|
2024-01-19 00:33:58 +00:00
|
|
|
this.phrasesExtension,
|
2024-02-22 21:18:41 +00:00
|
|
|
this.specialCharsExtension,
|
2024-01-10 00:02:44 +00:00
|
|
|
this.heightExtension,
|
2024-03-11 21:03:28 +00:00
|
|
|
bracketMatching(),
|
|
|
|
EditorState.readOnly.of( this.readOnly ),
|
CM6: Add syntax highlighting preference for users without WikiEditor
This adds the `ext.CodeMirror.v6.init` ResourceLoader module which
allows use of CodeMirror on `#wpTextbox1` without the use of WikiEditor
(the 'usebetatoolbar' preference). In order for users to opt-in to using
CodeMirror, we make the existing 'usecodemirror' option into a visible
preference. In addition, with two preferences related to CodeMirror, we
group them under a new heading 'Syntax highlighting'. More preferences
may be added later to this section following T359498.
When WikiEditor is not enabled, the layout of the action=edit page has
the textarea as a sibling to other visible content, like `.editOptions`.
Because of this, we can't simply append the CodeMirror DOM to the parent
like we were before, as that would put the visible editor beneath the
edit summary, Publish button, etc. Instead we rework the CodeMirror to
first add a wrapper around the textarea and use that as the parent. This
way, `.cm-editor` is always in the same place in the DOM as the native
textarea.
Line wrapping and focus/blur events are also moved to CodeMirror, as
these are needed when not using WikiEditor.
Bug: T190108
Change-Id: I4bc069e0d398aa7088e4f50bbd0ddda458b289c3
2024-03-26 20:40:13 +00:00
|
|
|
EditorView.domEventHandlers( {
|
|
|
|
blur: () => this.$textarea.triggerHandler( 'blur' ),
|
|
|
|
focus: () => this.$textarea.triggerHandler( 'focus' )
|
|
|
|
} ),
|
|
|
|
EditorView.lineWrapping,
|
2024-03-11 21:03:28 +00:00
|
|
|
keymap.of( [
|
|
|
|
...defaultKeymap,
|
|
|
|
...searchKeymap
|
|
|
|
] ),
|
|
|
|
EditorState.allowMultipleSelections.of( true ),
|
|
|
|
drawSelection()
|
2023-10-11 02:00:08 +00:00
|
|
|
];
|
2024-03-11 21:34:36 +00:00
|
|
|
|
2024-03-11 21:03:28 +00:00
|
|
|
// Add extensions relevant to editing (not read-only).
|
|
|
|
if ( !this.readOnly ) {
|
|
|
|
extensions.push( EditorView.updateListener.of( ( update ) => {
|
|
|
|
if ( update.docChanged && typeof this.editRecoveryHandler === 'function' ) {
|
|
|
|
this.editRecoveryHandler();
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
extensions.push( history() );
|
|
|
|
extensions.push( keymap.of( historyKeymap ) );
|
|
|
|
}
|
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
// Set to [] to disable everywhere, or null to enable everywhere
|
2024-03-11 18:10:08 +00:00
|
|
|
const namespaces = mw.config.get( 'extCodeMirrorConfig' ).lineNumberingNamespaces;
|
2023-09-19 17:59:29 +00:00
|
|
|
if ( !namespaces || namespaces.includes( mw.config.get( 'wgNamespaceNumber' ) ) ) {
|
|
|
|
extensions.push( lineNumbers() );
|
|
|
|
}
|
2024-03-11 18:10:08 +00:00
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
2024-02-28 05:14:04 +00:00
|
|
|
/**
|
|
|
|
* This extension sets the height of the CodeMirror editor to match the textarea.
|
|
|
|
* Override this method to change the height of the editor.
|
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {Extension}
|
|
|
|
* @stable to call and override
|
2024-02-28 05:14:04 +00:00
|
|
|
*/
|
|
|
|
get heightExtension() {
|
|
|
|
return EditorView.theme( {
|
|
|
|
'&': {
|
|
|
|
height: `${ this.$textarea.outerHeight() }px`
|
|
|
|
},
|
|
|
|
'.cm-scroller': {
|
|
|
|
overflow: 'auto'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:00:08 +00:00
|
|
|
/**
|
2024-03-19 03:10:11 +00:00
|
|
|
* This specifies which attributes get added to the `.cm-content` and `.cm-editor` elements.
|
2023-12-06 18:46:59 +00:00
|
|
|
* Subclasses are safe to override this method, but attributes here are considered vital.
|
2023-10-11 02:00:08 +00:00
|
|
|
*
|
|
|
|
* @see https://codemirror.net/docs/ref/#view.EditorView^contentAttributes
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {Extension}
|
|
|
|
* @stable to call and override
|
2023-10-11 02:00:08 +00:00
|
|
|
*/
|
|
|
|
get contentAttributesExtension() {
|
2024-02-20 20:59:21 +00:00
|
|
|
const classList = [];
|
|
|
|
// T245568: Sync text editor font preferences with CodeMirror
|
|
|
|
const fontClass = Array.from( this.$textarea[ 0 ].classList )
|
|
|
|
.find( ( style ) => style.startsWith( 'mw-editfont-' ) );
|
|
|
|
if ( fontClass ) {
|
|
|
|
classList.push( fontClass );
|
|
|
|
}
|
|
|
|
// Add colorblind mode if preference is set.
|
|
|
|
// This currently is only to be used for the MediaWiki markup language.
|
|
|
|
if (
|
|
|
|
mw.user.options.get( 'usecodemirror-colorblind' ) &&
|
|
|
|
mw.config.get( 'wgPageContentModel' ) === 'wikitext'
|
|
|
|
) {
|
|
|
|
classList.push( 'cm-mw-colorblind-colors' );
|
|
|
|
}
|
|
|
|
|
2024-03-08 01:36:15 +00:00
|
|
|
return [
|
|
|
|
// .cm-content element (the contenteditable area)
|
|
|
|
EditorView.contentAttributes.of( {
|
|
|
|
// T259347: Use accesskey of the original textbox
|
|
|
|
accesskey: this.$textarea.attr( 'accesskey' ),
|
|
|
|
// Classes need to be on .cm-content to have precedence over .cm-scroller
|
2024-03-11 21:03:28 +00:00
|
|
|
class: classList.join( ' ' ),
|
2024-03-21 01:51:09 +00:00
|
|
|
spellcheck: 'true',
|
|
|
|
tabindex: this.$textarea.attr( 'tabindex' )
|
2024-03-08 01:36:15 +00:00
|
|
|
} ),
|
|
|
|
// .cm-editor element (contains the whole CodeMirror UI)
|
|
|
|
EditorView.editorAttributes.of( {
|
|
|
|
// Use direction and language of the original textbox.
|
|
|
|
// These should be attributes of .cm-editor, not the .cm-content (T359589)
|
|
|
|
dir: this.$textarea.attr( 'dir' ),
|
|
|
|
lang: this.$textarea.attr( 'lang' )
|
2024-03-26 00:00:26 +00:00
|
|
|
} ),
|
2024-04-15 00:05:56 +00:00
|
|
|
// The search panel should use the same direction as the interface language (T359611)
|
2024-03-26 00:00:26 +00:00
|
|
|
EditorView.theme( {
|
|
|
|
'.cm-panels': {
|
|
|
|
direction: document.dir
|
|
|
|
}
|
2024-03-08 01:36:15 +00:00
|
|
|
} )
|
|
|
|
];
|
2023-10-11 02:00:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 03:21:12 +00:00
|
|
|
/**
|
|
|
|
* These are all potential messages used in a full-featured CodeMirror setup.
|
|
|
|
* We lump them all here and supply it as default extensions because it is only a small cost
|
|
|
|
* and we don't want localization to be overlooked by CodeMirror clients and subclasses.
|
|
|
|
*
|
|
|
|
* @see https://codemirror.net/examples/translate/
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {Extension}
|
|
|
|
* @stable to call. Instead of overriding, pass in an additional `EditorState.phrases.of()`
|
|
|
|
* when calling `initialize()`.
|
2024-01-13 03:21:12 +00:00
|
|
|
*/
|
|
|
|
get phrasesExtension() {
|
|
|
|
return EditorState.phrases.of( {
|
|
|
|
Find: mw.msg( 'codemirror-find' ),
|
|
|
|
next: mw.msg( 'codemirror-next' ),
|
|
|
|
previous: mw.msg( 'codemirror-previous' ),
|
|
|
|
all: mw.msg( 'codemirror-all' ),
|
|
|
|
'match case': mw.msg( 'codemirror-match-case' ),
|
|
|
|
regexp: mw.msg( 'codemirror-regexp' ),
|
|
|
|
'by word': mw.msg( 'codemirror-by-word' ),
|
|
|
|
replace: mw.msg( 'codemirror-replace' ),
|
|
|
|
Replace: mw.msg( 'codemirror-replace-placeholder' ),
|
2024-01-19 00:33:58 +00:00
|
|
|
'replace all': mw.msg( 'codemirror-replace-all' ),
|
|
|
|
'Control character': mw.msg( 'codemirror-control-character' )
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We give a small subset of special characters a tooltip explaining what they are.
|
|
|
|
* The messages and for what characters are defined here.
|
|
|
|
* Any character that does not have a message will instead use CM6 defaults,
|
|
|
|
* which is the localization of 'codemirror-control-character' followed by the Unicode number.
|
|
|
|
*
|
|
|
|
* @see https://codemirror.net/docs/ref/#view.highlightSpecialChars
|
2024-03-19 03:10:11 +00:00
|
|
|
* @type {Extension}
|
|
|
|
* @stable to call
|
2024-01-19 00:33:58 +00:00
|
|
|
*/
|
|
|
|
get specialCharsExtension() {
|
|
|
|
// Keys are the decimal unicode number, values are the messages.
|
|
|
|
const messages = {
|
|
|
|
0: mw.msg( 'codemirror-special-char-null' ),
|
|
|
|
7: mw.msg( 'codemirror-special-char-bell' ),
|
|
|
|
8: mw.msg( 'codemirror-special-char-backspace' ),
|
|
|
|
10: mw.msg( 'codemirror-special-char-newline' ),
|
|
|
|
11: mw.msg( 'codemirror-special-char-vertical-tab' ),
|
|
|
|
13: mw.msg( 'codemirror-special-char-carriage-return' ),
|
|
|
|
27: mw.msg( 'codemirror-special-char-escape' ),
|
|
|
|
160: mw.msg( 'codemirror-special-char-nbsp' ),
|
|
|
|
8203: mw.msg( 'codemirror-special-char-zero-width-space' ),
|
|
|
|
8204: mw.msg( 'codemirror-special-char-zero-width-non-joiner' ),
|
|
|
|
8205: mw.msg( 'codemirror-special-char-zero-width-joiner' ),
|
|
|
|
8206: mw.msg( 'codemirror-special-char-left-to-right-mark' ),
|
|
|
|
8207: mw.msg( 'codemirror-special-char-right-to-left-mark' ),
|
|
|
|
8232: mw.msg( 'codemirror-special-char-line-separator' ),
|
|
|
|
8237: mw.msg( 'codemirror-special-char-left-to-right-override' ),
|
|
|
|
8238: mw.msg( 'codemirror-special-char-right-to-left-override' ),
|
|
|
|
8239: mw.msg( 'codemirror-special-char-narrow-nbsp' ),
|
|
|
|
8294: mw.msg( 'codemirror-special-char-left-to-right-isolate' ),
|
|
|
|
8295: mw.msg( 'codemirror-special-char-right-to-left-isolate' ),
|
|
|
|
8297: mw.msg( 'codemirror-special-char-pop-directional-isolate' ),
|
|
|
|
8233: mw.msg( 'codemirror-special-char-paragraph-separator' ),
|
|
|
|
65279: mw.msg( 'codemirror-special-char-zero-width-no-break-space' ),
|
|
|
|
65532: mw.msg( 'codemirror-special-char-object-replacement' )
|
|
|
|
};
|
|
|
|
|
|
|
|
return highlightSpecialChars( {
|
|
|
|
render: ( code, description, placeholder ) => {
|
|
|
|
description = messages[ code ] || mw.msg( 'codemirror-control-character', code );
|
|
|
|
const span = document.createElement( 'span' );
|
|
|
|
span.className = 'cm-specialChar';
|
|
|
|
|
|
|
|
// Special case non-breaking spaces (T181677).
|
|
|
|
if ( code === 160 || code === 8239 ) {
|
|
|
|
placeholder = '·';
|
|
|
|
span.className = 'cm-special-char-nbsp';
|
|
|
|
}
|
|
|
|
|
|
|
|
span.textContent = placeholder;
|
|
|
|
span.title = description;
|
|
|
|
span.setAttribute( 'aria-label', description );
|
|
|
|
return span;
|
|
|
|
},
|
|
|
|
// Highlight non-breaking spaces (T181677)
|
2024-03-11 21:03:28 +00:00
|
|
|
addSpecialChars: /[\u00a0\u202f]/g
|
2024-01-13 03:21:12 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
/**
|
|
|
|
* Setup CodeMirror and add it to the DOM. This will hide the original textarea.
|
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @param {Extension|Extension[]} [extensions=this.defaultExtensions] Extensions to use.
|
|
|
|
* @fires CodeMirror~'ext.CodeMirror.initialize'
|
2024-03-20 00:58:50 +00:00
|
|
|
* @fires CodeMirror~'ext.CodeMirror.ready'
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call and override
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
|
|
|
initialize( extensions = this.defaultExtensions ) {
|
2024-03-19 03:10:11 +00:00
|
|
|
/**
|
|
|
|
* Called just before CodeMirror is initialized.
|
|
|
|
* This can be used to manipulate the DOM to suit CodeMirror
|
|
|
|
* (i.e. if you manipulate WikiEditor's DOM, you may need this).
|
|
|
|
*
|
|
|
|
* @event CodeMirror~'ext.CodeMirror.initialize'
|
|
|
|
* @param {jQuery} $textarea The textarea that CodeMirror is bound to.
|
|
|
|
* @stable to use
|
|
|
|
*/
|
2024-02-22 21:18:41 +00:00
|
|
|
mw.hook( 'ext.CodeMirror.initialize' ).fire( this.$textarea );
|
2024-03-11 21:03:28 +00:00
|
|
|
mw.hook( 'editRecovery.loadEnd' ).add( ( data ) => {
|
|
|
|
this.editRecoveryHandler = data.fieldChangeHandler;
|
|
|
|
} );
|
2024-02-22 21:18:41 +00:00
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
// Set up the initial EditorState of CodeMirror with contents of the native textarea.
|
|
|
|
this.state = EditorState.create( {
|
|
|
|
doc: this.$textarea.textSelection( 'getContents' ),
|
|
|
|
extensions
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Add CodeMirror view to the DOM.
|
CM6: Add syntax highlighting preference for users without WikiEditor
This adds the `ext.CodeMirror.v6.init` ResourceLoader module which
allows use of CodeMirror on `#wpTextbox1` without the use of WikiEditor
(the 'usebetatoolbar' preference). In order for users to opt-in to using
CodeMirror, we make the existing 'usecodemirror' option into a visible
preference. In addition, with two preferences related to CodeMirror, we
group them under a new heading 'Syntax highlighting'. More preferences
may be added later to this section following T359498.
When WikiEditor is not enabled, the layout of the action=edit page has
the textarea as a sibling to other visible content, like `.editOptions`.
Because of this, we can't simply append the CodeMirror DOM to the parent
like we were before, as that would put the visible editor beneath the
edit summary, Publish button, etc. Instead we rework the CodeMirror to
first add a wrapper around the textarea and use that as the parent. This
way, `.cm-editor` is always in the same place in the DOM as the native
textarea.
Line wrapping and focus/blur events are also moved to CodeMirror, as
these are needed when not using WikiEditor.
Bug: T190108
Change-Id: I4bc069e0d398aa7088e4f50bbd0ddda458b289c3
2024-03-26 20:40:13 +00:00
|
|
|
this.#addCodeMirrorToDom();
|
2023-09-19 17:59:29 +00:00
|
|
|
|
|
|
|
// Hide native textarea and sync CodeMirror contents upon submission.
|
|
|
|
this.$textarea.hide();
|
|
|
|
if ( this.$textarea[ 0 ].form ) {
|
|
|
|
this.$textarea[ 0 ].form.addEventListener( 'submit', () => {
|
|
|
|
this.$textarea.val( this.view.state.doc.toString() );
|
2024-02-21 07:29:53 +00:00
|
|
|
const scrollTop = document.getElementById( 'wpScrolltop' );
|
|
|
|
if ( scrollTop ) {
|
|
|
|
scrollTop.value = this.view.scrollDOM.scrollTop;
|
|
|
|
}
|
2023-09-19 17:59:29 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register $.textSelection() on the .cm-editor element.
|
|
|
|
$( this.view.dom ).textSelection( 'register', this.cmTextSelection );
|
|
|
|
// Also override textSelection() functions for the "real" hidden textarea to route to
|
|
|
|
// CodeMirror. We unregister this when switching to normal textarea mode.
|
|
|
|
this.$textarea.textSelection( 'register', this.cmTextSelection );
|
2024-03-20 00:58:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called just after CodeMirror is initialized.
|
|
|
|
*
|
|
|
|
* @event CodeMirror~'ext.CodeMirror.ready'
|
|
|
|
* @param {jQuery} $view The CodeMirror view.
|
|
|
|
* @stable to use
|
|
|
|
*/
|
|
|
|
mw.hook( 'ext.CodeMirror.ready' ).fire( $( this.view.dom ) );
|
|
|
|
}
|
|
|
|
|
CM6: Add syntax highlighting preference for users without WikiEditor
This adds the `ext.CodeMirror.v6.init` ResourceLoader module which
allows use of CodeMirror on `#wpTextbox1` without the use of WikiEditor
(the 'usebetatoolbar' preference). In order for users to opt-in to using
CodeMirror, we make the existing 'usecodemirror' option into a visible
preference. In addition, with two preferences related to CodeMirror, we
group them under a new heading 'Syntax highlighting'. More preferences
may be added later to this section following T359498.
When WikiEditor is not enabled, the layout of the action=edit page has
the textarea as a sibling to other visible content, like `.editOptions`.
Because of this, we can't simply append the CodeMirror DOM to the parent
like we were before, as that would put the visible editor beneath the
edit summary, Publish button, etc. Instead we rework the CodeMirror to
first add a wrapper around the textarea and use that as the parent. This
way, `.cm-editor` is always in the same place in the DOM as the native
textarea.
Line wrapping and focus/blur events are also moved to CodeMirror, as
these are needed when not using WikiEditor.
Bug: T190108
Change-Id: I4bc069e0d398aa7088e4f50bbd0ddda458b289c3
2024-03-26 20:40:13 +00:00
|
|
|
/**
|
|
|
|
* Instantiate the EditorView, adding the CodeMirror editor to the DOM.
|
|
|
|
* We use a dummy container to ensure that the editor will
|
|
|
|
* always be placed where the textarea is.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
#addCodeMirrorToDom() {
|
|
|
|
this.$textarea.wrap( '<div class="ext-codemirror-wrapper"></div>' );
|
|
|
|
|
|
|
|
this.view = new EditorView( {
|
|
|
|
state: this.state,
|
|
|
|
parent: this.$textarea.parent()[ 0 ]
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-03-20 00:58:50 +00:00
|
|
|
/**
|
|
|
|
* Destroy the CodeMirror instance and revert to the original textarea.
|
|
|
|
*
|
|
|
|
* @fires CodeMirror~'ext.CodeMirror.destroy'
|
|
|
|
* @stable to call and override
|
|
|
|
*/
|
|
|
|
destroy() {
|
|
|
|
const scrollTop = this.view.scrollDOM.scrollTop;
|
|
|
|
const hasFocus = this.view.hasFocus;
|
|
|
|
const { from, to } = this.view.state.selection.ranges[ 0 ];
|
|
|
|
$( this.view.dom ).textSelection( 'unregister' );
|
|
|
|
this.$textarea.textSelection( 'unregister' );
|
CM6: Add syntax highlighting preference for users without WikiEditor
This adds the `ext.CodeMirror.v6.init` ResourceLoader module which
allows use of CodeMirror on `#wpTextbox1` without the use of WikiEditor
(the 'usebetatoolbar' preference). In order for users to opt-in to using
CodeMirror, we make the existing 'usecodemirror' option into a visible
preference. In addition, with two preferences related to CodeMirror, we
group them under a new heading 'Syntax highlighting'. More preferences
may be added later to this section following T359498.
When WikiEditor is not enabled, the layout of the action=edit page has
the textarea as a sibling to other visible content, like `.editOptions`.
Because of this, we can't simply append the CodeMirror DOM to the parent
like we were before, as that would put the visible editor beneath the
edit summary, Publish button, etc. Instead we rework the CodeMirror to
first add a wrapper around the textarea and use that as the parent. This
way, `.cm-editor` is always in the same place in the DOM as the native
textarea.
Line wrapping and focus/blur events are also moved to CodeMirror, as
these are needed when not using WikiEditor.
Bug: T190108
Change-Id: I4bc069e0d398aa7088e4f50bbd0ddda458b289c3
2024-03-26 20:40:13 +00:00
|
|
|
this.$textarea.unwrap( '.ext-codemirror-wrapper' );
|
2024-03-20 00:58:50 +00:00
|
|
|
this.$textarea.val( this.view.state.doc.toString() );
|
|
|
|
this.view.destroy();
|
|
|
|
this.view = null;
|
|
|
|
this.$textarea.show();
|
|
|
|
if ( hasFocus ) {
|
|
|
|
this.$textarea.trigger( 'focus' );
|
|
|
|
}
|
|
|
|
this.$textarea.prop( 'selectionStart', Math.min( from, to ) )
|
|
|
|
.prop( 'selectionEnd', Math.max( to, from ) );
|
|
|
|
this.$textarea.scrollTop( scrollTop );
|
|
|
|
this.textSelection = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called just after CodeMirror is destroyed and the original textarea is restored.
|
|
|
|
*
|
|
|
|
* @event CodeMirror~'ext.CodeMirror.destroy'
|
|
|
|
* @param {jQuery} $textarea The original textarea.
|
|
|
|
* @stable to use
|
|
|
|
*/
|
|
|
|
mw.hook( 'ext.CodeMirror.destroy' ).fire( this.$textarea );
|
2023-09-19 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log usage of CodeMirror.
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
|
|
|
logUsage( data ) {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const event = Object.assign( {
|
|
|
|
session_token: mw.user.sessionId(),
|
|
|
|
user_id: mw.user.getId()
|
|
|
|
}, data );
|
|
|
|
const editCountBucket = mw.config.get( 'wgUserEditCountBucket' );
|
|
|
|
if ( editCountBucket !== null ) {
|
|
|
|
event.user_edit_count_bucket = editCountBucket;
|
|
|
|
}
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
mw.track( 'event.CodeMirrorUsage', event );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save CodeMirror enabled preference.
|
|
|
|
*
|
|
|
|
* @param {boolean} prefValue True, if CodeMirror should be enabled by default, otherwise false.
|
2024-03-19 03:10:11 +00:00
|
|
|
* @stable to call and override
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
|
|
|
setCodeMirrorPreference( prefValue ) {
|
2023-12-06 18:46:59 +00:00
|
|
|
// Skip for unnamed users
|
|
|
|
if ( !mw.user.isNamed() ) {
|
2023-09-19 17:59:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
new mw.Api().saveOption( 'usecodemirror', prefValue ? 1 : 0 );
|
|
|
|
mw.user.options.set( 'usecodemirror', prefValue ? 1 : 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery.textSelection overrides for CodeMirror.
|
|
|
|
*
|
2024-03-19 03:10:11 +00:00
|
|
|
* @see jQuery.fn.textSelection
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
|
|
|
get cmTextSelection() {
|
2024-03-09 05:36:43 +00:00
|
|
|
if ( !this.textSelection ) {
|
2024-03-19 03:10:11 +00:00
|
|
|
this.textSelection = new CodeMirrorTextSelection( this.view );
|
2024-03-09 05:36:43 +00:00
|
|
|
}
|
2023-09-19 17:59:29 +00:00
|
|
|
return {
|
2024-03-09 05:36:43 +00:00
|
|
|
getContents: () => this.textSelection.getContents(),
|
|
|
|
setContents: ( content ) => this.textSelection.setContents( content ),
|
|
|
|
getCaretPosition: ( options ) => this.textSelection.getCaretPosition( options ),
|
|
|
|
scrollToCaretPosition: () => this.textSelection.scrollToCaretPosition(),
|
|
|
|
getSelection: () => this.textSelection.getSelection(),
|
|
|
|
setSelection: ( options ) => this.textSelection.setSelection( options ),
|
|
|
|
replaceSelection: ( value ) => this.textSelection.replaceSelection( value ),
|
|
|
|
encapsulateSelection: ( options ) => this.textSelection.encapsulateSelection( options )
|
2023-09-19 17:59:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 03:10:11 +00:00
|
|
|
|
|
|
|
export default CodeMirror;
|