@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
40 lines • 1.58 kB
JavaScript
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 const firstNodeDecPluginKey = new PluginKey('firstNodeDec');
const createFirstNodeDecSet = state => {
const firstNode = state.doc.firstChild;
if (!firstNode) {
return DecorationSet.empty;
}
const 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 const firstNodeDecPlugin = () => new SafePlugin({
key: firstNodeDecPluginKey,
state: {
init(_, state) {
return createFirstNodeDecSet(state);
},
apply(tr, currentState, _, newState) {
const isDocChanged = tr.docChanged && tr.steps.some(step => 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(state) {
return firstNodeDecPluginKey.getState(state);
}
}
});