UNPKG

@atlaskit/editor-plugin-composition

Version:

Composition plugin for @atlaskit/editor-core

62 lines (60 loc) 2.13 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { ZERO_WIDTH_SPACE } from '@atlaskit/editor-common/whitespace'; import { pluginKey } from './plugin-key'; var isLinux = function isLinux() { return navigator.userAgent.indexOf('Linux') >= 0; }; export default (function () { return new SafePlugin({ key: pluginKey, state: { init: function init() { return { isComposing: false, zeroWidthSpacePos: undefined }; }, apply: function apply(tr, value) { var isComposing = tr.getMeta(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, true); // only apply for linux and cursor is at start of line if (isLinux() && view.state.selection.$from.parentOffset === 0) { tr.insertText(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, false); if (isLinux()) { var _pluginKey$getState; var zeroWidthSpacePos = (_pluginKey$getState = 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; } } } }); });