UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

44 lines 1.77 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { ReplaceAroundStep, ReplaceStep } from '@atlaskit/editor-prosemirror/transform'; import { DecorationSet, Decoration } from '@atlaskit/editor-prosemirror/view'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; export var firstNodeDecPluginKey = new PluginKey('firstNodeDec'); var createFirstNodeDecSet = function createFirstNodeDecSet(state) { var firstNode = state.doc.firstChild; if (!firstNode) { return DecorationSet.empty; } var firstNodeDecoration = expValEquals('platform_editor_breakout_resizing', 'isEnabled', true) ? Decoration.node(0, firstNode.nodeSize, { style: 'margin-top: 0;', class: 'first-node-in-document' }) : Decoration.node(0, firstNode.nodeSize, { style: 'margin-top: 0' }); return DecorationSet.create(state.doc, [firstNodeDecoration]); }; export var firstNodeDecPlugin = function firstNodeDecPlugin() { return new SafePlugin({ key: firstNodeDecPluginKey, state: { init: function init(_, state) { return createFirstNodeDecSet(state); }, apply: function apply(tr, currentState, _, newState) { var isDocChanged = tr.docChanged && tr.steps.some(function (step) { return step instanceof ReplaceStep || step instanceof ReplaceAroundStep; }); // Reapply decorations if there are any steps that modify the document if (isDocChanged) { return createFirstNodeDecSet(newState); } return currentState; } }, props: { decorations: function decorations(state) { return firstNodeDecPluginKey.getState(state); } } }); };