UNPKG

@atlaskit/editor-plugin-composition

Version:

Composition plugin for @atlaskit/editor-core

68 lines (65 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _whitespace = require("@atlaskit/editor-common/whitespace"); var _pluginKey = require("./plugin-key"); var isLinux = function isLinux() { return navigator.userAgent.indexOf('Linux') >= 0; }; var _default = exports.default = function _default() { return new _safePlugin.SafePlugin({ key: _pluginKey.pluginKey, state: { init: function init() { return { isComposing: false, zeroWidthSpacePos: undefined }; }, apply: function apply(tr, value) { var isComposing = tr.getMeta(_pluginKey.pluginKey); var zeroWidthSpacePos = tr.getMeta('zeroWidthSpacePos'); if (typeof isComposing === 'undefined') { return value; } return { isComposing: isComposing, zeroWidthSpacePos: zeroWidthSpacePos }; } }, props: { handleDOMEvents: { compositionstart: function compositionstart(view, event) { var tr = view.state.tr; tr.setMeta(_pluginKey.pluginKey, true); // only apply for linux and cursor is at start of line if (isLinux() && view.state.selection.$from.parentOffset === 0) { tr.insertText(_whitespace.ZERO_WIDTH_SPACE); // remember the position of inserted zero width space tr.setMeta('zeroWidthSpacePos', view.state.selection.$from.pos); } view.dispatch(tr); return false; }, compositionend: function compositionend(view, event) { var tr = view.state.tr; tr.setMeta(_pluginKey.pluginKey, false); if (isLinux()) { var _pluginKey$getState; var zeroWidthSpacePos = (_pluginKey$getState = _pluginKey.pluginKey.getState(view.state)) === null || _pluginKey$getState === void 0 ? void 0 : _pluginKey$getState.zeroWidthSpacePos; if (typeof zeroWidthSpacePos !== 'undefined') { tr.deleteRange(zeroWidthSpacePos, zeroWidthSpacePos + 1); } tr.setMeta('zeroWidthSpacePos', undefined); } view.dispatch(tr); return false; } } } }); };