UNPKG

@atlaskit/editor-plugin-selection

Version:

Selection plugin for @atlaskit/editor-core

42 lines 1.66 kB
import { pluginFactory } from '@atlaskit/editor-common/utils'; import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; import { DecorationSet } from '@atlaskit/editor-prosemirror/view'; import { CellSelection } from '@atlaskit/editor-tables/cell-selection'; import { selectionPluginKey } from '../types'; import { reducer } from './reducer'; import { getDecorations, isSelectableContainerNode } from './utils'; const handleDocChanged = (tr, pluginState) => { // in some collab edge cases mapping decorations could throw an error try { if (pluginState.decorationSet.find().length === 0 && (!tr.selectionSet || getDecorations(tr).find().length === 0)) { return pluginState; } const decorationSet = pluginState.decorationSet.map(tr.mapping, tr.doc); return { ...pluginState, decorationSet }; } catch (error) { return { ...pluginState, decorationSet: DecorationSet.empty }; } }; const handleSelectionChanged = (tr, pluginState) => { // Reset relative selection pos when user clicks to select a node if ((tr.selection instanceof NodeSelection && isSelectableContainerNode(tr.selection.node) || tr.selection instanceof CellSelection) && !tr.getMeta(selectionPluginKey)) { return { ...pluginState, selectionRelativeToNode: undefined }; } return pluginState; }; const dest = pluginFactory(selectionPluginKey, reducer, { onDocChanged: handleDocChanged, onSelectionChanged: handleSelectionChanged }); export const createCommand = dest.createCommand; export const getPluginState = dest.getPluginState; export const createPluginState = dest.createPluginState;