UNPKG

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

Version:

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

56 lines 1.42 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; export const pluginKey = new PluginKey('maxContentSizePlugin'); export function createPlugin(dispatch, maxContentSize) { if (!maxContentSize) { return; } let maxContentSizeReached = false; return new SafePlugin({ state: { init: () => ({ maxContentSizeReached: false }), apply(tr, state) { const result = tr.doc && tr.doc.nodeSize > maxContentSize - 1; return { maxContentSizeReached: result }; } }, key: pluginKey, filterTransaction(tr) { const result = tr.doc && tr.doc.nodeSize > maxContentSize; if (result || result !== maxContentSizeReached) { dispatch(pluginKey, { maxContentSizeReached: result }); } maxContentSizeReached = result; return !result; } }); } const maxContentSizePlugin = ({ config: maxContentSize, api }) => { return { name: 'maxContentSize', getSharedState(editorState) { if (!editorState) { return undefined; } return pluginKey.getState(editorState); }, pmPlugins() { return [{ name: 'maxContentSize', plugin: ({ dispatch }) => createPlugin(dispatch, maxContentSize) }]; } }; }; export default maxContentSizePlugin;