UNPKG

@atlaskit/editor-plugin-find-replace

Version:

find replace plugin for @atlaskit/editor-core

68 lines (66 loc) • 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.viewModeShortcutPlugin = void 0; var _bindEventListener = require("bind-event-listener"); var _enums = require("@atlaskit/editor-common/analytics/types/enums"); var _keymaps = require("@atlaskit/editor-common/keymaps"); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _keymap = require("@atlaskit/editor-prosemirror/keymap"); var _commandsWithAnalytics = require("./commands-with-analytics"); /** * Whether the event came from a field the user is typing into. `Mod-f` belongs to * that field, not to us. */ var isFromTextEntry = function isFromTextEntry(event) { var target = event.target; return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable; }; var activateInViewMode = function activateInViewMode(api) { return function (state, dispatch) { var _api$editorViewMode, _api$analytics; // In edit mode `./keymap.ts` already handles `Mod-f`. Handling it here as well // would activate the dialog twice from a single keypress. if ((api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 || (_api$editorViewMode = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.mode) !== 'view') { return false; } (0, _commandsWithAnalytics.activateWithAnalytics)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, api === null || api === void 0 ? void 0 : api.editorViewMode)({ triggerMethod: _enums.TRIGGER_METHOD.SHORTCUT })(state, dispatch); return true; }; }; /** * Opens the find dialog on `Mod-f` while the editor is in view mode. * * The listener is bound on the document so that it still fires in view mode, where * ProseMirror delivers the keydown to neither `keymap()` nor `handleDOMEvents`. */ var viewModeShortcutPlugin = exports.viewModeShortcutPlugin = function viewModeShortcutPlugin(api) { var bindings = {}; if (_keymaps.find.common) { (0, _keymaps.bindKeymapWithCommand)(_keymaps.find.common, activateInViewMode(api), bindings); } var handleKeydown = (0, _keymap.keydownHandler)(bindings); return new _safePlugin.SafePlugin({ view: function view(_view) { var unbind = (0, _bindEventListener.bind)(_view.root, { type: 'keydown', listener: function listener(event) { // `view.root` is `Document | ShadowRoot`, so the binding widens the event. if (!(event instanceof KeyboardEvent) || isFromTextEntry(event)) { return; } if (handleKeydown(_view, event)) { // Without this the browser find bar opens on top of our dialog. event.preventDefault(); } } }); return { destroy: unbind }; } }); };