UNPKG

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

Version:

Paste options toolbar for @atlaskit/editor-core

54 lines (53 loc) 1.8 kB
import { pluginFactory } from '@atlaskit/editor-common/utils'; import { PastePluginActionTypes } from '../editor-actions/actions'; import { pasteOptionsPluginKey } from '../types/types'; import { PASTE_OPTIONS_META_ID } from './constants'; import { reducer } from './reducer'; const dest = pluginFactory(pasteOptionsPluginKey, reducer, { mapping: (tr, pluginState) => { if (!tr.docChanged || !pluginState.showToolbar) { return pluginState; } const oldPasteStartPos = pluginState.pasteStartPos; const oldPasteEndPos = pluginState.pasteEndPos; const newPasteStartPos = tr.mapping.map(oldPasteStartPos); const newPasteEndPos = tr.mapping.map(oldPasteEndPos); //this is true when user changes format from the toolbar. //only change pasteEndPos in this case if (changedFormatFromToolbar(tr)) { return { ...pluginState, pasteEndPos: newPasteEndPos }; } if (oldPasteStartPos === newPasteStartPos && oldPasteEndPos === newPasteEndPos) { return pluginState; } return { ...pluginState, pasteStartPos: newPasteStartPos, pasteEndPos: newPasteEndPos }; }, onSelectionChanged: (tr, pluginState) => { // Detect click outside the editor if (tr.getMeta('outsideProsemirrorEditorClicked')) { return { ...pluginState, showToolbar: false, highlightContent: false }; } return pluginState; } }); export const createPluginState = dest.createPluginState; export const createCommand = dest.createCommand; export const getPluginState = dest.getPluginState; const changedFormatFromToolbar = tr => { const meta = tr.getMeta(PASTE_OPTIONS_META_ID); if (meta && meta.type === PastePluginActionTypes.CHANGE_FORMAT) { return true; } return false; };