@atlaskit/editor-plugin-paste-options-toolbar
Version:
Paste options toolbar for @atlaskit/editor-core
54 lines (53 loc) • 2.7 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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';
var dest = pluginFactory(pasteOptionsPluginKey, reducer, {
mapping: function mapping(tr, pluginState) {
if (!tr.docChanged || !pluginState.showToolbar) {
return pluginState;
}
var oldPasteStartPos = pluginState.pasteStartPos;
var oldPasteEndPos = pluginState.pasteEndPos;
var newPasteStartPos = tr.mapping.map(oldPasteStartPos);
var 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 _objectSpread(_objectSpread({}, pluginState), {}, {
pasteEndPos: newPasteEndPos
});
}
if (oldPasteStartPos === newPasteStartPos && oldPasteEndPos === newPasteEndPos) {
return pluginState;
}
return _objectSpread(_objectSpread({}, pluginState), {}, {
pasteStartPos: newPasteStartPos,
pasteEndPos: newPasteEndPos
});
},
onSelectionChanged: function onSelectionChanged(tr, pluginState) {
// Detect click outside the editor
if (tr.getMeta('outsideProsemirrorEditorClicked')) {
return _objectSpread(_objectSpread({}, pluginState), {}, {
showToolbar: false,
highlightContent: false
});
}
return pluginState;
}
});
export var createPluginState = dest.createPluginState;
export var createCommand = dest.createCommand;
export var getPluginState = dest.getPluginState;
var changedFormatFromToolbar = function changedFormatFromToolbar(tr) {
var meta = tr.getMeta(PASTE_OPTIONS_META_ID);
if (meta && meta.type === PastePluginActionTypes.CHANGE_FORMAT) {
return true;
}
return false;
};