UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

43 lines (42 loc) 2.33 kB
import { BLOCK_CONTROLS_SURFACE_INVALIDATION } from '@atlaskit/editor-common/block-controls/surface-candidate-invalidation'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { DecorationSet } from '@atlaskit/editor-prosemirror/view'; import { key as blockControlsKey } from './main'; import { emptySparseSurfaceCandidateState } from './utils/sparse-surface-candidates'; import { applySparseSurfacesTransaction, createInitialSparseSurfacesState } from './utils/sparse-surfaces-state'; import { createSparseSurfacesView } from './utils/sparse-surfaces-view'; export var sparseSurfacesKey = new PluginKey('blockControlsSparseSurfaces'); /** Owns sparse anchors independently of drag/drop decorations and their lifecycle. */ export var createSparseSurfacesPlugin = function createSparseSurfacesPlugin(api) { return new SafePlugin({ key: sparseSurfacesKey, state: { init: function init() { return createInitialSparseSurfacesState(emptySparseSurfaceCandidateState); }, apply: function apply(tr, previous, _oldState, newState) { return applySparseSurfacesTransaction({ invalidation: tr.getMeta(BLOCK_CONTROLS_SURFACE_INVALIDATION), meta: tr.getMeta(sparseSurfacesKey), newState: newState, previous: previous, tr: tr }); } }, props: { decorations: function decorations(state) { var _api$limitedMode, _blockControlsKey$get, _sparseSurfacesKey$ge; return api !== null && api !== void 0 && (_api$limitedMode = api.limitedMode) !== null && _api$limitedMode !== void 0 && (_api$limitedMode = _api$limitedMode.sharedState.currentState()) !== null && _api$limitedMode !== void 0 && _api$limitedMode.enabled || (_blockControlsKey$get = blockControlsKey.getState(state)) !== null && _blockControlsKey$get !== void 0 && _blockControlsKey$get.isDragging ? DecorationSet.empty : (_sparseSurfacesKey$ge = sparseSurfacesKey.getState(state)) === null || _sparseSurfacesKey$ge === void 0 ? void 0 : _sparseSurfacesKey$ge.decorations; } }, view: function view(_view) { return createSparseSurfacesView({ api: api, pluginKey: sparseSurfacesKey, view: _view }); } }); };