Extends
Constructor
new CodeMirrorChild(textarea, primaryInstance, [langExtension])
#
Instantiate a child CodeMirror instance given a primary instance.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
textarea |
HTMLTextAreaElement | Child textarea element. |
|
primaryInstance |
CodeMirror | The primary CodeMirror instance to sync with. |
|
langExtension |
LanguageSupport | Extension |
optional |
Language support and its extension(s). If not provided, the primary instance's language support will be used. |
- Source:
Properties
$textarea :jQuery.<(HTMLTextAreaElement|ve.ui.Surface)>
#
jQuery instance of the textarea or VE surface for use with jQuery plugins.
Type:
- Overrides:
- Source:
activeLineExtension :Extension
#
Extension for highlighting the active line.
Type:
- Overrides:
- Source:
bracketMatchingConfig :Config|undefined
#
undefined
#
The extended configuration for bracket matching.
Setting this will update the
bracket matching extension
Example
// Highlight matching angle brackets in addition to the default.
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
const config = cm.bracketMatchingConfig || {};
config.brackets = ( config.brackets || '()[]{}' ) + '<>';
cm.bracketMatchingConfig = config;
} );
Type:
-
Config
|
undefined
- Overrides:
- Source:
bracketMatchingExtension :Extension
#
This extension adds bracket matching to the CodeMirror editor.
Type:
- Overrides:
- Source:
child :CodeMirrorChild
#
Get a CodeMirrorChild object for use on other textareas that
should have preferences synced with this CodeMirror instance.
Type:
- Overrides:
- Source:
Get a CodeMirrorChild object for use on other textareas that
should have preferences synced with this CodeMirror instance.
closeBracketsExtension :Extension
#
This extension adds automatic closing of brackets to the CodeMirror editor.
Type:
- Overrides:
- Source:
columnGuideExtension :Extensionprotected
#
This extension adds a vertical column guide to the editor at column 80.
Type:
- Overrides:
- Source:
compactPanelsExtension :Extension
#
Extension for the compact panel design. The class it adds makes the search and go-to line panels float over the editor instead of pushing the contents down.
Type:
- Overrides:
- Source:
container :HTMLDivElement
#
The .ext-codemirror-wrapper container. This houses both the original textarea and the CodeMirror editor.
Type:
- Overrides:
- Source:
contentAttributesExtension :Extensionprotected
#
This specifies which attributes get added to the CodeMirror contenteditable .cm-content.
Subclasses are safe to override this method, but attributes here are considered vital.
Type:
.cm-content.
defaultExtensions :Extension
#
Default extensions used by CodeMirror.
Extensions here should be applicable to all theoretical uses of CodeMirror in MediaWiki.
This getter can be overridden to apply additional extensions before
initialization. To apply a new extension after initialization,
use applyExtension(), or through
CodeMirrorExtensionRegistry using
register() if it needs
to be reconfigured (such as toggling on and off).
Type:
- Overrides:
- Source:
- See:
dirCompartment :Compartment
#
Compartment to control the direction of the editor.
Type:
- Overrides:
- Source:
dirExtension :Extensionprotected
#
This extension adds the ability to change the direction of the editor.
Type:
- Overrides:
- Source:
domEventHandlersExtension :Extensionprotected
#
Extension to bubble some DOM events to the original textarea.
The CodeMirror events are natively fired on the EditorView's
.cm-content element, which is accessible through
. The view.contentDOMscroll event is fired on
the .cm-scroller element, which is accessible through
.view.scrollDOM
Type:
- Overrides:
- Source:
editRecoveryExtension
#
Edit Recovery support.
- Overrides:
- Source:
editorAttributesExtension :Extensionprotected
#
This specifies which attributes get added to the .cm-editor element (the entire editor).
Subclasses are safe to override this method, but attributes here are considered vital.
Type:
.cm-editor element (the entire editor).
extensionRegistry :CodeMirrorExtensionRegistry
#
Extensions.
extensionRegistryDefaults :Object.<Extension>protected
#
The Extensions to register with the
CodeMirrorExtensionRegistry at construction, keyed by preference name.
Subclasses may override this to restrict or extend which preference-backed
extensions are available in their integration.
NOTE: This is called by the CodeMirror constructor, so overrides must not
reference instance properties that are only set after super() has been called.
Type:
- Overrides:
- Source:
The Extensions to register with the
CodeMirrorExtensionRegistry at construction, keyed by preference name.
gotoLine :CodeMirrorGotoLine
#
The go-to line panel.
Type:
- Overrides:
- Source:
hasFocus :boolean
#
Whether the editor has focus.
Type:
- boolean
- Overrides:
- Source:
heightExtension :Extension
#
This extension sets the height of the CodeMirror editor to match the
textarea. This getter can be overridden to
change the height of the editor, but it's usually simpler to set the
height of the textarea using CSS prior to initialization.
Type:
- Overrides:
- Source:
This extension sets the height of the CodeMirror editor to match the
textarea.
indentGuidesExtension :Extension
#
This extension adds indent guides to the CodeMirror editor.
Type:
- Overrides:
- Source:
initialReadOnly :booleanprotected
#
The read-only state to use before there is a view, which is
what readOnlyExtension seeds the state with.
Integrations bound to something other than a textarea should override this.
Type:
- boolean
- Overrides:
- Source:
The read-only state to use before there is a view, which is
what readOnlyExtension seeds the state with.
isActive :boolean
#
Whether the CodeMirror instance is active.
Type:
- boolean
- Overrides:
- Source:
keymap :CodeMirrorKeymap
#
CodeMirror key mappings and help dialog.
Type:
- Overrides:
- Source:
langExtension :LanguageSupport
#
Language support and its extension(s).
Type:
- Overrides:
- Source:
lintApi :LintSource|undefined
#
undefined
#
The function to lint the code in the editor using a MediaWiki API.
It can be disabled by setting this.lintApi.disabled = true.
Example
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
if ( cm.lintApi ) {
cm.lintApi.disabled = true;
}
} );
Type:
-
LintSource
|
undefined
- Overrides:
- Source:
lintExtension :Extensionprotected
#
This extension adds the ability to lint the code in the editor.
Type:
- Overrides:
- Source:
lintSource :LintSource|undefined
#
undefined
#
The function to lint the code in the editor using a web worker.
It can be disabled by setting this.lintSource.disabled = true.
Example
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
if ( cm.lintSource ) {
cm.lintSource.disabled = true;
}
} );
Type:
-
LintSource
|
undefined
- Overrides:
- Source:
mode :string
#
The CodeMirror "mode" (language).
Type:
- string
- Overrides:
- Source:
persistPreferences :booleanprotected
#
Whether to save preference changes to the user's options. Subclasses that are not the
user's main editor should override this and return false, so that the editor does not
change preferences the user set elsewhere. Preferences are still read, and still apply
for the life of the editor.
Note this is distinct from locked preferences,
which force a specific PrefValue for the life of the editor.
Type:
- boolean
- Overrides:
- Source:
phrasesExtension :Extensionprotected
#
Overrides for the CodeMirror library's internalization system.
Type:
- Overrides:
- Source:
- See:
preferences :CodeMirrorPreferences
#
The CodeMirror preferences dialog.
Type:
- Overrides:
- Source:
primaryInstance :CodeMirror
#
readOnly :boolean
#
Whether the editor is read-only. Assigning to this reconfigures the editor in place,
so it can be changed at any time after initialization.
Before then it comes from initialReadOnly.
A read-only editor is still focusable and its contents still selectable. Use
EditorView.editable to make it non-editable.
Type:
- boolean
- Overrides:
- Source:
readOnlyExtension :Extensionprotected
#
The read-only state of the editor, in a Compartment so that
readOnly can reconfigure it after initialization.
Type:
- Overrides:
- Source:
The read-only state of the editor, in a Compartment so that
readOnly can reconfigure it after initialization.
search :CodeMirrorSearch
#
The CodeMirror search panel.
Type:
- Overrides:
- Source:
searchExtension :Extension
#
Extension for search and goto line functionality.
Type:
- Overrides:
- Source:
sortLines :CodeMirrorSortLines
#
CodeMirror line sorting functionality.
Type:
- Overrides:
- Source:
specialCharsExtension :Extensionprotected
#
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.
Type:
- Overrides:
- Source:
- See:
state :EditorState|undefined
#
undefined
#
The current EditorState of the CodeMirror editor.
This is given by the EditorView instance, which is only available
after initialization.
Type:
-
EditorState
|
undefined
- Overrides:
- Source:
EditorState of the CodeMirror editor.
supportedExtensions :Array.<string>|nullprotected
#
nullprotected
#
Names that CodeMirrorExtensionRegistry#register will accept, or null to accept
any name. Subclasses that can only support some extensions should override this, so that
extensions registered later — by a gadget, or by
applyLinter() — are refused as well.
NOTE: This is called by the CodeMirror constructor, so overrides must not
reference instance properties that are only set after super() has been called.
Type:
-
Array.<string>
|
null
- Overrides:
- Source:
Names that CodeMirrorExtensionRegistry#register will accept, or null to accept
any name.
textSelection :CodeMirrorTextSelection
#
jQuery.textSelection overrides for CodeMirror.
Type:
- Overrides:
- Source:
textarea :HTMLTextAreaElement|ve.ui.Surface
#
The textarea or VE surface that CodeMirror is bound to.
Type:
- Overrides:
- Source:
themes :CodeMirrorThemes
#
The CodeMirror themes manager.
Type:
- Overrides:
- Source:
trailingWhitespaceExtension
#
This extension highlights trailing whitespace characters.
- Overrides:
- Source:
updateExtension :Extension
#
This extension listens for changes in the CodeMirror editor and fires
the ext.CodeMirror.input hook with the ViewUpdate object.
Type:
- Overrides:
- Source:
Fires:
This extension listens for changes in the CodeMirror editor and fires
the ext.CodeMirror.input hook with the ViewUpdate object.
view :EditorView
#
The editor user interface.
Type:
- Overrides:
- Source:
whitespaceExtension
#
This extension highlights whitespace characters.
- Overrides:
- Source:
wrappedElement :HTMLElementprotected
#
The element that container is wrapped around, and which
the container hides. Integrations bound to something other than a textarea should
override this, so that the view is added beside the right element.
Type:
- Overrides:
- Source:
The element that container is wrapped around, and which
the container hides.
Methods
activate()protected
#
Activate CodeMirror on the textarea.
This shows the editor view, hiding the original textarea.
initialize is expected to be called before this method.
- Overrides:
- Source:
textarea.
addEditRecoveryHandler()protected
#
Set a new edit recovery handler.
- Overrides:
- Source:
addFormSubmitHandler()protected
#
Sync the CodeMirror editor with the original textarea on form submission.
- Overrides:
- Source:
addHashChangeHandler()protected
#
Scroll the CodeMirror editor on hashchange.
- Overrides:
- Source:
addMwHook(hook, fn)protected
#
Add a handler for the given Hook.
This method is used to ensure no hook handlers are duplicated across lifecycle methods,
All handlers will be removed during deactivation.
Parameters:
| Name | Type | Description |
|---|---|---|
hook |
string | |
fn |
function |
- Overrides:
- Source:
Hook.
addTextAreaJQueryHook()protected
#
Define jQuery hook for .val() on the textarea.
- Overrides:
- Source:
- See:
addToDOM(extensions)protected
#
Add the CodeMirror EditorView to the DOM, wrapping
wrappedElement in the
container element.
Parameters:
| Name | Type | Description |
|---|---|---|
extensions |
Extension |
- Overrides:
- Source:
Add the CodeMirror EditorView to the DOM, wrapping
wrappedElement in the
container element.
applyExtension(extension)
#
Apply an Extension to the CodeMirror editor.
This is accomplished through
top-level reconfiguration
of the EditorView.
If the extension needs to be reconfigured (such as toggling on and off), use the
extensionRegistry instead.
Example
const require = await mw.loader.using( 'ext.CodeMirror' );
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
const { EditorView, Prec } = require( 'ext.CodeMirror.lib' );
// Disable spellchecking. Use Prec.high() to override the
// contentAttributesExtension which adds spellcheck="true".
cm.applyExtension( Prec.high( EditorView.contentAttributes.of( {
spellcheck: 'false'
} ) ) );
} );
Parameters:
| Name | Type | Description |
|---|---|---|
extension |
Extension |
- Overrides:
- Source:
- See:
Extension to the CodeMirror editor.
applyLinter(lint, [config])
#
Apply an extra linter to the CodeMirror editor.
This linter respects the user preference for enabling or disabling linting.
Example
// Report Ukrainian letter 'і' among Latin letters as a warning.
const lint = {
pattern: /[a-zA-Z]і|і(?=[a-zA-Z])/g,
callback( match ) {
return {
from: match.index + match[ 0 ].indexOf( 'і' ),
to: match.index + match[ 0 ].indexOf( 'і' ) + 1,
severity: 'warning',
message: 'Ukrainian letter "і" found among Latin letters.',
actions: [ {
name: 'To Latin',
apply( view, from, to ) {
view.dispatch( {
changes: { from, to, insert: 'i' }
} );
}
} ]
};
}
};
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
cm.applyLinter( lint );
} );
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
lint |
function | Object | Function that takes a string and returns an array of
|
|
config |
Object |
optional |
Optional configuration. |
- Overrides:
- Source:
linter to the CodeMirror editor.
deactivate()protected
#
Deactivate CodeMirror on the textarea, restoring the original
textarea and hiding the editor.
destroy()
#
Destroy the CodeMirror instance and revert to the original textarea. This action should be considered irreversible.
Fires:
- Overrides:
- Source:
dispatch(spec)
#
Apply a transaction
to the editor. Together with state, this is what makes a
CodeMirror instance an Editor.
Parameters:
| Name | Type | Description |
|---|---|---|
spec |
Object |
- Overrides:
- Source:
Apply a transaction to the editor.
focus([isActive])
#
Focus the CodeMirror editor.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
isActive |
boolean |
optional |
Whether the CodeMirror editor is active. |
- Overrides:
- Source:
getLineNumberingExtension([firstLineNumber]) → {Extension}
#
Line numbering, starting at the given number.
Use this instead of CodeMirror#lineNumberingExtension when the code in the editor
is part of a larger file, and the first line is not line 1.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
firstLineNumber |
number |
optional |
1 | Number to show for the first line. |
Returns:
- Type
- Extension
- Overrides:
- Source:
getNewEditorState(extensions) → {EditorState}protected
#
Create a new EditorState with the given extensions and the contents
of the element that CodeMirror is bound to.
Parameters:
| Name | Type | Description |
|---|---|---|
extensions |
Extension |
Returns:
- Type
- EditorState
- Overrides:
- Source:
Create a new EditorState with the given extensions and the contents
of the element that CodeMirror is bound to.
getSourceContents() → {string}protected
#
Get the contents of the element that CodeMirror is bound to. This is used to populate the initial document, and to re-sync the contents when re-activating.
Returns:
- Type
- string
- Overrides:
- Source:
hideEditor()protected
#
Hide the editor and give jQuery.textSelection back to the textarea, as
deactivate() does on the way out. Integrations that render
no editor of their own should override this.
- Overrides:
- Source:
Hide the editor and give jQuery.textSelection back to the textarea, as
deactivate() does on the way out.
initialize([extensions])
#
Setup CodeMirror and add it to the DOM. This will hide the original textarea.
This method should only be called once per instance. Use toggle,
activate, and deactivate
to enable or disable the same CodeMirror instance programmatically, and restore or hide
the original textarea.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
extensions |
Extension |
optional |
this.defaultExtensions
|
Extensions to use. |
Fires:
- Overrides:
- Source:
logEditFeature(action)protected
#
Log usage of CodeMirror.
Parameters:
| Name | Type | Description |
|---|---|---|
action |
string |
- Overrides:
- Source:
- See:
removeFromDOM()protected
#
Remove the CodeMirror EditorView from the DOM, unwrapping the element it was
attached to, as the inverse of addToDOM(). Integrations that
attach CodeMirror to something other than a textarea should override this.
- Overrides:
- Source:
Remove the CodeMirror EditorView from the DOM, unwrapping the element it was
attached to, as the inverse of addToDOM().
removeMwHooks([hook], [fn])protected
#
Remove handlers registered with #addMwHook for the given Hook,
or remove all handlers if no hook is specified.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
hook |
string |
optional |
|
fn |
function |
optional |
If specified, only this handler will be removed for the given hook. |
- Overrides:
- Source:
Remove handlers registered with #addMwHook for the given Hook,
or remove all handlers if no hook is specified.
restoreSelectionAndScrollPosition(selectionStart, selectionEnd, scrollTop, hadFocus)protected
#
Hand the hidden textarea's state over to the editor: disable IME on the textarea so it can't conflict with CodeMirror, then restore the selection, scroll position and focus state that were captured before the textarea was hidden.
This is the activate counterpart to
syncSelectionAndScrollPosition().
Integrations that aren't bound to a textarea should override it as a no-op.
Parameters:
| Name | Type | Description |
|---|---|---|
selectionStart |
number | |
selectionEnd |
number | |
scrollTop |
number | |
hadFocus |
boolean |
- Overrides:
- Source:
Hand the hidden textarea's state over to the editor: disable IME on the textarea so it can't conflict with CodeMirror, then restore the selection, scroll position and focus state that were captured before the textarea was hidden.
setCodeMirrorPreference(prefValue)
#
Save CodeMirror enabled preference.
Parameters:
| Name | Type | Description |
|---|---|---|
prefValue |
boolean |
|
- Overrides:
- Source:
setupFeatureLogging()protected
#
Add hook handlers to log usage of CodeMirror features.
- Overrides:
- Source:
showEditor()protected
#
Reveal the editor and route jQuery.textSelection through it, as
activate() does on the way in. Integrations that render no
editor of their own should override this.
- Overrides:
- Source:
Reveal the editor and route jQuery.textSelection through it, as
activate() does on the way in.
syncEditorContentsToSource()protected
#
Sync the CodeMirror editor contents to the original textarea.
- Overrides:
- Source:
syncSelectionAndScrollPosition(from, to, scrollTop)protected
#
Sync the selection and scroll position to the original textarea.
Parameters:
| Name | Type | Description |
|---|---|---|
from |
number | |
to |
number | |
scrollTop |
number |
- Overrides:
- Source:
toggle([force])
#
Toggle CodeMirror on or off from the textarea.
This will call initialize if CodeMirror
is being enabled for the first time.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
force |
boolean |
optional |
|
Fires:
- Overrides:
- Source: