UNPKG

@atlaskit/editor-plugin-composition

Version:

Composition plugin for @atlaskit/editor-core

60 lines (58 loc) 1.91 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { ZERO_WIDTH_SPACE } from '@atlaskit/editor-common/whitespace'; import { pluginKey } from './plugin-key'; const isLinux = () => navigator.userAgent.indexOf('Linux') >= 0; export default (() => new SafePlugin({ key: pluginKey, state: { init: () => ({ isComposing: false, zeroWidthSpacePos: undefined }), apply: (tr, value) => { const isComposing = tr.getMeta(pluginKey); const zeroWidthSpacePos = tr.getMeta('zeroWidthSpacePos'); if (typeof isComposing === 'undefined') { return value; } return { isComposing, zeroWidthSpacePos }; } }, props: { handleDOMEvents: { compositionstart: (view, event) => { const { tr } = view.state; 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: (view, event) => { const { tr } = view.state; tr.setMeta(pluginKey, false); if (isLinux()) { var _pluginKey$getState; const 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; } } } }));