UNPKG

@atlaskit/editor-plugin-paste-options-toolbar

Version:

Paste options toolbar for @atlaskit/editor-core

78 lines 3.29 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { Slice } from '@atlaskit/editor-prosemirror/model'; import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view'; import { fg } from '@atlaskit/platform-feature-flags'; import { checkAndHideToolbar } from '../editor-commands/commands'; import { pasteOptionsPluginKey, ToolbarDropdownOption } from '../types/types'; import { PASTE_HIGHLIGHT_DECORATION_KEY, TEXT_HIGHLIGHT_CLASS } from './constants'; import { createPluginState } from './plugin-factory'; var MODIFIER_KEYS = new Set(['Shift', 'Control', 'Alt', 'Meta', // Cmd on Mac, Win on Windows 'CapsLock', 'NumLock', 'ScrollLock', 'Fn', 'FnLock']); function isModifierKey(event) { return MODIFIER_KEYS.has(event.key); } export function createPlugin(dispatch, options) { return new SafePlugin({ key: pasteOptionsPluginKey, state: createPluginState(dispatch, { showToolbar: false, showLegacyOptions: false, pasteAncestorNodeNames: [], pasteStartPos: 0, pasteEndPos: 0, plaintext: '', isPlainText: false, highlightContent: false, highlightDecorationSet: DecorationSet.empty, richTextSlice: Slice.empty, selectedOption: ToolbarDropdownOption.None }), view: function view(_editorView) { return { update: function update(_view, prevState) { return prevState; } }; }, props: { handleDOMEvents: { blur: function blur(view) { if (options !== null && options !== void 0 && options.useNewPasteMenu) { return false; } checkAndHideToolbar(view); return false; }, // Hide toolbar when clicked anywhere within the editor, tr.getMeta('pointer') does not work if clicked on the same line after pasting so relying on mousedown event mousedown: checkAndHideToolbar }, handleKeyDown: function handleKeyDown(view, event) { // Don't hide toolbar when pressing modifier keys alone (Ctrl, Shift, Alt, Meta/Cmd) if (isModifierKey(event) && fg('platform_editor_paste_actions_keypress_fix')) { return false; } checkAndHideToolbar(view); return false; }, decorations: function decorations(state) { var _pasteOptionsPluginKe, _pasteOptionsPluginKe2; var _ref = pasteOptionsPluginKey.getState(state) || {}, highlightContent = _ref.highlightContent, pasteStartPos = _ref.pasteStartPos; var decorationSet = (_pasteOptionsPluginKe = (_pasteOptionsPluginKe2 = pasteOptionsPluginKey.getState(state)) === null || _pasteOptionsPluginKe2 === void 0 ? void 0 : _pasteOptionsPluginKe2.highlightDecorationSet) !== null && _pasteOptionsPluginKe !== void 0 ? _pasteOptionsPluginKe : DecorationSet.empty; if (!highlightContent) { return decorationSet; } var selection = state.tr.selection; var pasteEndPos = selection.$anchor.pos; var highlightDecoration = Decoration.inline(pasteStartPos, pasteEndPos, { class: TEXT_HIGHLIGHT_CLASS }, { key: PASTE_HIGHLIGHT_DECORATION_KEY }); return decorationSet.add(state.doc, [highlightDecoration]); } } }); }