UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

74 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var plugins_1 = require("../../../plugins"); var codeMirrorPlugin_1 = require("./codeMirrorPlugin"); require("./styles"); var CodeBlock = (function () { function CodeBlock(node, view, getPos) { var _this = this; this.updateLanguage = function (state) { var language = state.language, uniqueId = state.uniqueId; if (language && uniqueId === _this.uniqueId) { _this.codeMirrorPlugin.updateLanguage(language); } }; this.focusCodeEditor = function (uniqueId) { if (uniqueId === _this.uniqueId) { _this.codeMirrorPlugin.focusCodeEditor(); } }; this.node = node; this.view = view; this.pluginState = plugins_1.codeBlockStateKey.getState(this.view.state); this.codeMirrorPlugin = new codeMirrorPlugin_1.default(node, view, getPos, this.pluginState); this.uniqueId = this.node.attrs['uniqueId'] || generateId(); this.node.attrs['uniqueId'] = this.uniqueId; this.node.attrs['isCodeMirror'] = true; this.pluginState.subscribe(this.updateLanguage); this.pluginState.subscribeFocusHandlers(this.focusCodeEditor); } Object.defineProperty(CodeBlock.prototype, "dom", { get: function () { return this.codeMirrorPlugin.dom; }, enumerable: true, configurable: true }); CodeBlock.prototype.update = function (node) { if (!this.node || node.type !== this.node.type) { return false; } this.node = node; this.codeMirrorPlugin.update(node.textContent); return true; }; CodeBlock.prototype.setSelection = function (anchor, head) { this.codeMirrorPlugin.setSelection(anchor, head); }; CodeBlock.prototype.selectNode = function () { this.cm.focus(); }; CodeBlock.prototype.stopEvent = function () { return true; }; CodeBlock.prototype.destroy = function () { this.codeMirrorPlugin.destroy(); this.node.attrs['uniqueId'] = undefined; this.node.attrs['isCodeMirror'] = undefined; this.pluginState.unsubscribe(this.updateLanguage); this.pluginState.unsubscribeFocusHandlers(this.focusCodeEditor); }; return CodeBlock; }()); function generateId() { var text = ''; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (var i = 0; i < 5; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)); } return text; } exports.default = function (node, view, getPos) { return new CodeBlock(node, view, getPos); }; //# sourceMappingURL=index.js.map