Class: Extensions

ft/core/extensions. Extensions

Extensions add new behavior to FoldingText.

This class manages those extensions. Use it to add your extension with to a specific extension point. The extension logic is different for each extension point, see each extension points example code to get started.

Each extension point may have multiple extensions registered with it. When you add an extension use the optional priority parameter to help determine the order that your extension is called in.

Members

Methods




Member Details

<static> PriorityNormal

Default extension processing priority.

<static> PriorityFirst

Extensions added with PriorityFirst priority are processed before other extensions.

<static> PriorityLast

Extensions added with PriorityLast priority are processed before other extensions.

Method Details

<static> addCommand(extension, priority)

Make a new command available to the editor. If you include a description then the command will be included in the Edit > Run Command... popup.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addCommand({ name: 'date', description: 'Insert the current date', performCommand: function (editor) { editor.replaceSelection(new Date().format('mediumDate'), 'around'); } });

<static> addInit(extension, priority)

Get notified when the Editor is first initialized.

Parameters:
Name Type Argument Description
extension function
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addInit(function (editor) { });

<static> addMode(extension, priority)

Declare a new mode. Modes are whitelisted, so only modes declared using this extension point will get recognized by the classification process. See the source code for FoldingText's existing todo and timer modes for an examples of how to attach special styling and behavior to a mode.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addMode({ name: 'mymodeextension' });

<static> addState(extension, priority)

Use this extension point to store additional state with the saved document. If you want to save system level defaults then use UserDefaults instead.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addState({ readState: function (editor, state) { var mystate = state.mystate; }, writeState: function (editor, state) { state.mystate = 'some special state I want to save'; } });

<static> addSelectionDidChange(extension, priority)

Learn when the selection changes in the editor.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addSelectionDidChange(function (editor) { console.log(editor.selectedText()); });

<static> addNodePathDidChange(extension, priority)

Learn when the node path changes in the editor.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addNodePathDidChange(function (editor) { console.log(editor.nodePathString()); });

<static> addRenderNodeElementStyles(extension, priority)

Adjust DOM element attributes and styles for nodes rendered in the viewport. This is called in response to CodeMirror's renderLine event.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addRenderNodeElementStyles(function (editor, node, elementRenderer) { if (node.mode() === 'mycustommode') { elementRenderer.addWrapClass('cm-mymodewrapper'); } });

<static> addTreeChanged(extension, priority)

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addTreeChanged(function (editor, e) { e.deltas.forEach(function (eachDelta) { // process each delta }) });

<static> addRenderNode(extension, priority)

Add DOM widgets to nodes when they are displayed. For example this code adds a "current time" overlay widget in the whitespace before lines that have a clock mode.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addRenderNode(function (editor, node, nodeRenderer) { if (node.mode() === 'clock') { var widget = document.createElement('span'); widget.textContent = formatDate(node); nodeRenderer.renderLineWidget(widget, { overlay: true, positionWidget : function (widgetWidth, widgetHeight) { var line = node.lineNumber(), leadingSpace = node.line().match(/\s*\/)[0], coords = editor.cursorCoords({ line : line, ch : leadingSpace.length}, 'div'); return { left: Math.round(coords.left - (widgetWidth + editor.defaultSpaceWidth())) + 'px' }; } }); } });

<static> addSnippet(extension, priority)

Add a text snippet to the editor.

Parameters:
Name Type Argument Description
extension Object
priority ExtensionPriority <optional>

Processing priority.

Example

var Extensions = require('ft/core/extensions').Extensions; Extensions.addSnippet({ 'omw' : 'On my way!' });