Expand all

CodeMirrorExtensionRegistry

Container class for housing CodeMirror Extensions. Each Extension is wrapped in a Compartment so that it can be reconfigured.

If an Extension doesn't need to be reconfigured, it should instead be added during CodeMirror initialization, or by using CodeMirror#applyExtension().

The constructor is internal. The class can be accessed via CodeMirror#extensionRegistry.

Example

mw.loader.using( 'ext.CodeMirror.v6' ).then( ( require ) => {
  mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
    const { EditorView, Prec } = require( 'ext.CodeMirror.v6.lib' );
    // Disable spellchecking. Use Prec.high() to override the
    // contentAttributesExtension which adds spellcheck="true".
    cm.extensionRegistry.register(
      'spellcheck',
      Prec.high( EditorView.contentAttributes.of( {
        spellcheck: 'false'
      } ) ),
      cm.view
    );

    const toggleButton = document.querySelector( '#toggle-spellcheck' );
    toggleButton.addEventListener( 'click', () => {
      cm.extensionRegistry.toggle( 'spellcheck', cm.view );
    } );
  } );
} );

Properties

names :Array.<string> #

The names of all registered Extensions.

Type:

Source:
The names of all registered Extensions.

veSupportedExtensions :Array.<string> #

Allowlist of names of CodeMirror extensions supported by the 2017 wikitext editor. Do not include Extensions that make changes to the document text, or visually change the placement of text.

Note also that there is no UI to toggle or reconfigure CodeMirror Extensions in VE.

Type:

Source:
Allowlist of names of CodeMirror extensions supported by the 2017 wikitext editor.

Methods

get(name) → {Extension|undefined} #

Get the compartmentalized Extension with the given name.

This should only be used when including registered extensions during CodeMirror initialization such as with CodeMirrorPreferences#extension.

Parameters:

Name Type Description
name string
Source:

Returns:

Type
Extension | undefined
Get the compartmentalized Extension with the given name.

getCompartment(name) → {Compartment|undefined} #

Get the Compartment for the extension with the given name.

Parameters:

Name Type Description
name string
Source:

Returns:

Type
Compartment | undefined
Get the Compartment for the extension with the given name.

isEnabled(name, view) → {boolean} #

Check if an extension is enabled.

Parameters:

Name Type Description
name string
view EditorView
Source:

Returns:

Type
boolean
Check if an extension is enabled.

isRegistered(name, view) → {boolean} #

Check if the Extension with the given name has been appended to the EditorState configuration. In the context of CodeMirror, this means that the extension has been "registered", but not necessarily enabled.

Parameters:

Name Type Description
name string
view EditorView
Source:

Returns:

Type
boolean

Check if the Extension with the given name has been appended to the EditorState configuration.

reconfigure(name, view, extension) #

Reconfigure a compartmentalized extension with a new Extension.

Example

const cm = new CodeMirror( ... );
// Register an Extension that sets the tab size to 5 spaces.
cm.extensionRegistry.register( 'tabSize', EditorState.tabSize.of( 5 ), cm.view, true );
// Reconfigure the tab size to 10 spaces.
cm.extensionRegistry.reconfigure( 'tabSize', cm.view, EditorState.tabSize.of( 10 ) );

Parameters:

Name Type Description
name string
view EditorView
extension Extension
Source:
Reconfigure a compartmentalized extension with a new Extension.

register(name, extension, view, [enable]) #

Register an Extension, creating a corresponding Compartment. The Extension can then be reconfigured such as toggling on and off.

Parameters:

Name Type Attributes Description
name string
extension Extension
view EditorView
enable boolean optional

true to enable the extension immediately.

Source:
Register an Extension, creating a corresponding Compartment.

toggle(name, view, [force]) #

Toggle on or off an extension.

Parameters:

Name Type Attributes Description
name string
view EditorView
force boolean optional

true to enable, false to disable, undefined to toggle.

Source:
Toggle on or off an extension.