@atlaskit/editor-plugin-paste-options-toolbar
Version:
Paste options toolbar for @atlaskit/editor-core
84 lines (83 loc) • 3.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPlugin = createPlugin;
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _model = require("@atlaskit/editor-prosemirror/model");
var _view2 = require("@atlaskit/editor-prosemirror/view");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _commands = require("../editor-commands/commands");
var _types = require("../types/types");
var _constants = require("./constants");
var _pluginFactory = require("./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);
}
function createPlugin(dispatch, options) {
return new _safePlugin.SafePlugin({
key: _types.pasteOptionsPluginKey,
state: (0, _pluginFactory.createPluginState)(dispatch, {
showToolbar: false,
showLegacyOptions: false,
pasteAncestorNodeNames: [],
pasteStartPos: 0,
pasteEndPos: 0,
plaintext: '',
isPlainText: false,
highlightContent: false,
highlightDecorationSet: _view2.DecorationSet.empty,
richTextSlice: _model.Slice.empty,
selectedOption: _types.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;
}
(0, _commands.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: _commands.checkAndHideToolbar
},
handleKeyDown: function handleKeyDown(view, event) {
// Don't hide toolbar when pressing modifier keys alone (Ctrl, Shift, Alt, Meta/Cmd)
if (isModifierKey(event) && (0, _platformFeatureFlags.fg)('platform_editor_paste_actions_keypress_fix')) {
return false;
}
(0, _commands.checkAndHideToolbar)(view);
return false;
},
decorations: function decorations(state) {
var _pasteOptionsPluginKe, _pasteOptionsPluginKe2;
var _ref = _types.pasteOptionsPluginKey.getState(state) || {},
highlightContent = _ref.highlightContent,
pasteStartPos = _ref.pasteStartPos;
var decorationSet = (_pasteOptionsPluginKe = (_pasteOptionsPluginKe2 = _types.pasteOptionsPluginKey.getState(state)) === null || _pasteOptionsPluginKe2 === void 0 ? void 0 : _pasteOptionsPluginKe2.highlightDecorationSet) !== null && _pasteOptionsPluginKe !== void 0 ? _pasteOptionsPluginKe : _view2.DecorationSet.empty;
if (!highlightContent) {
return decorationSet;
}
var selection = state.tr.selection;
var pasteEndPos = selection.$anchor.pos;
var highlightDecoration = _view2.Decoration.inline(pasteStartPos, pasteEndPos, {
class: _constants.TEXT_HIGHLIGHT_CLASS
}, {
key: _constants.PASTE_HIGHLIGHT_DECORATION_KEY
});
return decorationSet.add(state.doc, [highlightDecoration]);
}
}
});
}