UNPKG

@atlaskit/editor-plugin-selection

Version:

Selection plugin for @atlaskit/editor-core

126 lines 3.54 kB
import { getSelectionFragment, getSelectionLocalIds } from './editor-actions'; import { createAutoExpandSelectionRangeOnInlineNodePlugin } from './pm-plugins/auto-expand-selection-range-on-inline-node-main'; import { selectNearNode } from './pm-plugins/commands'; import gapCursorKeymapPlugin from './pm-plugins/gap-cursor-keymap'; import gapCursorPlugin from './pm-plugins/gap-cursor-main'; import { gapCursorPluginKey } from './pm-plugins/gap-cursor-plugin-key'; import selectionKeymapPlugin from './pm-plugins/keymap'; import { createMarkBoundaryCursorPlugin } from './pm-plugins/mark-boundary-cursor-main'; import { createPlugin } from './pm-plugins/selection-main'; import { selectionPluginKey } from './types'; const displayGapCursor = toggle => ({ tr }) => { return tr.setMeta(gapCursorPluginKey, { displayGapCursor: toggle }); }; const clearManualSelection = () => ({ tr }) => { const currMeta = tr.getMeta(selectionPluginKey); return tr.setMeta(selectionPluginKey, { ...currMeta, manualSelection: {} }); }; const setManualSelection = (anchor, head) => ({ tr }) => { const currMeta = tr.getMeta(selectionPluginKey); return tr.setMeta(selectionPluginKey, { ...currMeta, manualSelection: { anchor, head } }); }; const hideCursor = hide => ({ tr }) => { const currMeta = tr.getMeta(selectionPluginKey); return tr.setMeta(selectionPluginKey, { ...currMeta, hideCursor: hide }); }; const setBlockSelection = selection => ({ tr }) => { const currMeta = tr.getMeta(selectionPluginKey); return tr.setMeta(selectionPluginKey, { ...currMeta, setBlockSelection: selection }); }; const clearBlockSelection = () => ({ tr }) => { const currMeta = tr.getMeta(selectionPluginKey); return tr.setMeta(selectionPluginKey, { ...currMeta, clearBlockSelection: true }); }; export const selectionPlugin = ({ api, config: options }) => ({ name: 'selection', commands: { displayGapCursor, clearManualSelection, hideCursor, setManualSelection, setBlockSelection, clearBlockSelection }, actions: { selectNearNode: ({ selectionRelativeToNode, selection }) => state => { return selectNearNode(selectionRelativeToNode, selection)({ tr: state.tr }) || state.tr; }, getSelectionFragment: getSelectionFragment(api), getSelectionLocalIds: getSelectionLocalIds(api) }, getSharedState(editorState) { if (!editorState) { return undefined; } const pluginState = selectionPluginKey.getState(editorState); return { selectionRelativeToNode: pluginState === null || pluginState === void 0 ? void 0 : pluginState.selectionRelativeToNode, selection: pluginState === null || pluginState === void 0 ? void 0 : pluginState.selection }; }, pmPlugins() { return [{ name: 'selection', plugin: ({ dispatch, dispatchAnalyticsEvent }) => createPlugin(api, dispatch, dispatchAnalyticsEvent, options) }, { name: 'selectionKeymap', plugin: selectionKeymapPlugin }, { name: 'gapCursorKeymap', plugin: () => gapCursorKeymapPlugin() }, { name: 'gapCursor', plugin: () => gapCursorPlugin }, { name: 'markBoundaryCursor', plugin: () => createMarkBoundaryCursorPlugin() }, { name: 'autoExpandSelectionRangeOnInlineNode', plugin: () => createAutoExpandSelectionRangeOnInlineNodePlugin() }]; } }); export default selectionPlugin;