@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
140 lines • 5.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../../prosemirror");
var commands = require("../../commands");
var keymaps_1 = require("./keymaps");
var CodeBlockState = (function () {
function CodeBlockState(state) {
this.toolbarVisible = false;
this.domEvent = false;
this.uniqueId = undefined;
this.editorFocused = false;
this.changeHandlers = [];
this.focusHandlers = [];
this.changeHandlers = [];
this.focusHandlers = [];
this.state = state;
}
CodeBlockState.prototype.subscribe = function (cb) {
this.changeHandlers.push(cb);
cb(this);
};
CodeBlockState.prototype.unsubscribe = function (cb) {
this.changeHandlers = this.changeHandlers.filter(function (ch) { return ch !== cb; });
};
CodeBlockState.prototype.subscribeFocusHandlers = function (cb) {
this.focusHandlers.push(cb);
};
CodeBlockState.prototype.unsubscribeFocusHandlers = function (cb) {
this.focusHandlers = this.focusHandlers.filter(function (ch) { return ch !== cb; });
};
CodeBlockState.prototype.updateLanguage = function (language, view) {
if (this.activeCodeBlock) {
commands.setBlockType(view.state.schema.nodes.codeBlock, { language: language, uniqueId: this.uniqueId })(view.state, view.dispatch);
if (this.focusHandlers.length > 0) {
this.triggerFocus();
}
else {
view.focus();
}
}
};
CodeBlockState.prototype.removeCodeBlock = function (view) {
var state = view.state, dispatch = view.dispatch;
var _a = state.selection, $from = _a.$from, $to = _a.$to;
var range = $from.blockRange($to);
dispatch(state.tr.delete(range.start, range.end));
view.focus();
};
CodeBlockState.prototype.updateEditorFocused = function (editorFocused) {
this.editorFocused = editorFocused;
};
CodeBlockState.prototype.setLanguages = function (supportedLanguages) {
this.supportedLanguages = supportedLanguages;
};
CodeBlockState.prototype.update = function (state, docView, domEvent) {
if (domEvent === void 0) { domEvent = false; }
this.state = state;
var codeBlockNode = this.activeCodeBlockNode();
if (domEvent && codeBlockNode || codeBlockNode !== this.activeCodeBlock) {
this.domEvent = domEvent;
var newElement = codeBlockNode && this.activeCodeBlockElement(docView);
this.toolbarVisible = this.editorFocused && !!codeBlockNode && (domEvent || this.element !== newElement);
this.activeCodeBlock = codeBlockNode;
this.language = codeBlockNode && codeBlockNode.attrs['language'] || undefined;
this.element = newElement;
this.uniqueId = codeBlockNode && codeBlockNode.attrs['uniqueId'];
this.triggerOnChange();
}
};
CodeBlockState.prototype.triggerOnChange = function () {
var _this = this;
this.changeHandlers.forEach(function (cb) { return cb(_this); });
};
CodeBlockState.prototype.triggerFocus = function () {
var _this = this;
this.focusHandlers.forEach(function (cb) { return cb(_this.uniqueId); });
};
CodeBlockState.prototype.activeCodeBlockElement = function (docView) {
var offset = this.nodeStartPos();
var node = docView.domFromPos(offset).node;
return node;
};
CodeBlockState.prototype.nodeStartPos = function () {
var $from = this.state.selection.$from;
return $from.start($from.depth);
};
CodeBlockState.prototype.activeCodeBlockNode = function () {
var state = this.state;
var $from = state.selection.$from;
var node = $from.parent;
if (node.type === state.schema.nodes.codeBlock) {
return node;
}
};
return CodeBlockState;
}());
exports.CodeBlockState = CodeBlockState;
exports.stateKey = new prosemirror_1.PluginKey('codeBlockPlugin');
exports.plugin = new prosemirror_1.Plugin({
state: {
init: function (config, state) {
return new CodeBlockState(state);
},
apply: function (tr, pluginState, oldState, newState) {
var stored = tr.getMeta(exports.stateKey);
if (stored) {
pluginState.update(newState, stored.docView, stored.domEvent);
}
return pluginState;
}
},
key: exports.stateKey,
view: function (editorView) {
exports.stateKey.getState(editorView.state).update(editorView.state, editorView.docView);
return {
update: function (view, prevState) {
exports.stateKey.getState(view.state).update(view.state, view.docView);
}
};
},
props: {
handleClick: function (view, event) {
exports.stateKey.getState(view.state).update(view.state, view.docView, true);
return false;
},
onFocus: function (view, event) {
exports.stateKey.getState(view.state).updateEditorFocused(true);
},
onBlur: function (view, event) {
var pluginState = exports.stateKey.getState(view.state);
pluginState.updateEditorFocused(false);
pluginState.update(view.state, view.docView, true);
},
}
});
var plugins = function (schema) {
return [exports.plugin, keymaps_1.default(schema)].filter(function (plugin) { return !!plugin; });
};
exports.default = plugins;
//# sourceMappingURL=index.js.map