UNPKG

@atlaskit/editor-plugin-max-content-size

Version:

max-content-size plugin for @atlaskit/editor-core

65 lines (64 loc) 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPlugin = createPlugin; exports.pluginKey = exports.default = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _state = require("@atlaskit/editor-prosemirror/state"); var pluginKey = exports.pluginKey = new _state.PluginKey('maxContentSizePlugin'); function createPlugin(dispatch, maxContentSize) { if (!maxContentSize) { return; } var maxContentSizeReached = false; return new _safePlugin.SafePlugin({ state: { init: function init() { return { maxContentSizeReached: false }; }, apply: function apply(tr, state) { var result = tr.doc && tr.doc.nodeSize > maxContentSize - 1; return { maxContentSizeReached: result }; } }, key: pluginKey, filterTransaction: function filterTransaction(tr) { var result = tr.doc && tr.doc.nodeSize > maxContentSize; if (result || result !== maxContentSizeReached) { dispatch(pluginKey, { maxContentSizeReached: result }); } maxContentSizeReached = result; return !result; } }); } var maxContentSizePlugin = function maxContentSizePlugin(_ref) { var maxContentSize = _ref.config, api = _ref.api; return { name: 'maxContentSize', getSharedState: function getSharedState(editorState) { if (!editorState) { return undefined; } return pluginKey.getState(editorState); }, pmPlugins: function pmPlugins() { return [{ name: 'maxContentSize', plugin: function plugin(_ref2) { var dispatch = _ref2.dispatch; return createPlugin(dispatch, maxContentSize); } }]; } }; }; var _default = exports.default = maxContentSizePlugin;