UNPKG

@atlaskit/editor-plugin-editor-viewmode

Version:

Editor viewmode plugin for @atlaskit/editor-core

75 lines (74 loc) 2.52 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; const viewModePluginKey = new PluginKey('editorViewMode'); const createPlugin = ({ initialMode }) => { return new SafePlugin({ key: viewModePluginKey, state: { init: () => ({ mode: initialMode !== null && initialMode !== void 0 ? initialMode : 'edit' }), apply: (tr, pluginState) => { const meta = tr.getMeta(viewModePluginKey); if (meta) { return meta; } return pluginState; } }, props: { // If we set to undefined it respects the previous value. // Prosemirror doesn't have this typed correctly for this type of behaviour // We will fast-follow to consolidate the logic with `editor-disabled` so we don't // need this workaround. // @ts-expect-error editable: state => { var _viewModePluginKey$ge; const mode = (_viewModePluginKey$ge = viewModePluginKey.getState(state)) === null || _viewModePluginKey$ge === void 0 ? void 0 : _viewModePluginKey$ge.mode; return mode === 'view' ? false : undefined; } } }); }; /** * View Mode plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor` * from `@atlaskit/editor-core`. */ export const editorViewModePlugin = ({ config: options, api }) => { return { name: 'editorViewMode', getSharedState(editorState) { var _viewModePluginKey$ge2, _viewModePluginKey$ge3; if (!editorState) { return { mode: (options === null || options === void 0 ? void 0 : options.mode) === 'view' ? 'view' : 'edit' }; // Skipping type safety for the deprecated mode property } return { mode: (_viewModePluginKey$ge2 = (_viewModePluginKey$ge3 = viewModePluginKey.getState(editorState)) === null || _viewModePluginKey$ge3 === void 0 ? void 0 : _viewModePluginKey$ge3.mode) !== null && _viewModePluginKey$ge2 !== void 0 ? _viewModePluginKey$ge2 : 'edit' }; // Skipping type safety for the deprecated mode property }, commands: { updateViewMode: mode => ({ tr }) => { return tr.setMeta(viewModePluginKey, { mode }); } }, pmPlugins() { return [{ name: 'editorViewMode', plugin: () => createPlugin({ initialMode: options === null || options === void 0 ? void 0 : options.mode }) }]; } }; };