@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
50 lines (49 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.firstNodeDecPluginKey = exports.firstNodeDecPlugin = void 0;
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _state = require("@atlaskit/editor-prosemirror/state");
var _transform = require("@atlaskit/editor-prosemirror/transform");
var _view = require("@atlaskit/editor-prosemirror/view");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var firstNodeDecPluginKey = exports.firstNodeDecPluginKey = new _state.PluginKey('firstNodeDec');
var createFirstNodeDecSet = function createFirstNodeDecSet(state) {
var firstNode = state.doc.firstChild;
if (!firstNode) {
return _view.DecorationSet.empty;
}
var firstNodeDecoration = (0, _expValEquals.expValEquals)('platform_editor_breakout_resizing', 'isEnabled', true) ? _view.Decoration.node(0, firstNode.nodeSize, {
style: 'margin-top: 0;',
class: 'first-node-in-document'
}) : _view.Decoration.node(0, firstNode.nodeSize, {
style: 'margin-top: 0'
});
return _view.DecorationSet.create(state.doc, [firstNodeDecoration]);
};
var firstNodeDecPlugin = exports.firstNodeDecPlugin = function firstNodeDecPlugin() {
return new _safePlugin.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 _transform.ReplaceStep || step instanceof _transform.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);
}
}
});
};