UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

81 lines 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var prosemirror_1 = require("../../prosemirror"); var commands_1 = require("./commands"); var keymap_1 = require("./keymap"); var ClearFormattingState = (function () { function ClearFormattingState(state) { var _this = this; this.formattingIsPresent = false; this.markTypes = [ 'em', 'code', 'strike', 'strong', 'underline', 'link', 'textColor' ]; this.changeHandlers = []; this.blockStylingIsPresent = function () { var state = _this.state; var _a = state.selection, from = _a.from, to = _a.to; var isBlockStyling = false; state.doc.nodesBetween(from, to, function (node, pos) { if (node.isBlock && node.type !== state.schema.nodes.paragraph) { isBlockStyling = true; } }); return isBlockStyling; }; this.changeHandlers = []; this.update(state); } ClearFormattingState.prototype.subscribe = function (cb) { this.changeHandlers.push(cb); cb(this); }; ClearFormattingState.prototype.unsubscribe = function (cb) { this.changeHandlers = this.changeHandlers.filter(function (ch) { return ch !== cb; }); }; ClearFormattingState.prototype.update = function (newEditorState) { var _this = this; this.state = newEditorState; var state = this.state; this.activeMarkTypes = this.markTypes.filter(function (mark) { return state.schema.marks[mark] && _this.markIsActive(state.schema.marks[mark]); }); var formattingIsPresent = this.activeMarkTypes.length > 0 || this.blockStylingIsPresent(); if (formattingIsPresent !== this.formattingIsPresent) { this.formattingIsPresent = formattingIsPresent; this.triggerOnChange(); } }; ClearFormattingState.prototype.clearFormatting = function (view) { commands_1.clearFormatting(this.markTypes)(view.state, view.dispatch); }; ClearFormattingState.prototype.triggerOnChange = function () { var _this = this; this.changeHandlers.forEach(function (cb) { return cb(_this); }); }; ClearFormattingState.prototype.markIsActive = function (mark) { var state = this.state; var _a = state.selection, from = _a.from, to = _a.to, empty = _a.empty; if (empty) { return !!mark.isInSet(state.selection.$from.marks()); } return state.doc.rangeHasMark(from, to, mark); }; return ClearFormattingState; }()); exports.ClearFormattingState = ClearFormattingState; exports.stateKey = new prosemirror_1.PluginKey('clearFormattingPlugin'); exports.plugin = new prosemirror_1.Plugin({ state: { init: function (config, state) { return new ClearFormattingState(state); }, apply: function (tr, pluginState, oldState, newState) { pluginState.update(newState); return pluginState; } }, key: exports.stateKey }); var plugins = function (schema) { return [exports.plugin, keymap_1.default(schema)].filter(function (plugin) { return !!plugin; }); }; exports.default = plugins; //# sourceMappingURL=index.js.map