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.

Methods that read or change the configuration take an Editor, so integrations that have no EditorView can pass themselves 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.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.

reconfigValueMap :Map.<string, Map> #

Map of reconfiguration values and the extensions that should be applied when a compartmentalized extension is reconfigured with that value.

Keyed by extension name, then by 'reconfig value' and then the implementing Extension.

This is used when we need to pass around the CodeMirrorExtensionRegistry but keep track of the Extension values elsewhere.

Type:

Source:
See:

Map of reconfiguration values and the extensions that should be applied when a compartmentalized extension is reconfigured with that value.

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

Returns:

Type
Extension | undefined
Source:
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

Returns:

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

isEnabled(name, editor) → {boolean} #

Check if an extension is enabled.

Parameters:

Name Type Description
name string
editor Editor

Returns:

Type
boolean
Source:
Check if an extension is enabled.

isRegistered(name, editor) → {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
editor Editor

Returns:

Type
boolean
Source:

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

reconfigure(name, editor, 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
editor Editor
extension Extension
Source:
Reconfigure a compartmentalized extension with a new Extension.

reconfigureFromValueMap(name, editor, reconfigValue) #

Reconfigure a compartmentalized extension with a value from the reconfiguration value map.

Parameters:

Name Type Description
name string
editor Editor
reconfigValue string
Source:

Reconfigure a compartmentalized extension with a value from the reconfiguration value map.

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

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

Integrations that can only support some extensions declare which through CodeMirror#supportedExtensions, and anything else is refused here.

Parameters:

Name Type Attributes Description
name string
extension Extension
editor Editor
enable boolean optional

true to enable the extension immediately.

Source:
Register an Extension, creating a corresponding Compartment.

registerFromValueMap(name, editor, reconfigValue) #

Register an extension with an initial value from the reconfiguration value map.

Parameters:

Name Type Description
name string
editor Editor
reconfigValue string
Source:

Register an extension with an initial value from the reconfiguration value map.

toggle(name, editor, [force]) #

Toggle on or off an extension.

Parameters:

Name Type Attributes Description
name string
editor Editor
force PrefValue optional

true to enable, false to disable, undefined to toggle, or a string value to force-enable with the given value from the reconfiguration value map.

Source:
Toggle on or off an extension.