UNPKG

@atlaskit/editor-plugin-content-format

Version:

ContentFormat plugin for @atlaskit/editor-core

71 lines 2.7 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; export var contentFormatPluginKey = new PluginKey('contentFormat'); export var createPlugin = function createPlugin(_ref) { var initialContentMode = _ref.initialContentMode; return new SafePlugin({ key: contentFormatPluginKey, state: { init: function init() { return { contentMode: initialContentMode !== null && initialContentMode !== void 0 ? initialContentMode : 'standard' }; }, apply: function apply(tr, currentPluginState) { var meta = tr.getMeta(contentFormatPluginKey); if (meta) { var contentMode = meta.contentMode; return { contentMode: contentMode !== null && contentMode !== void 0 ? contentMode : currentPluginState.contentMode }; } return currentPluginState; } } }); }; export var contentFormatPlugin = function contentFormatPlugin(_ref2) { var options = _ref2.config; var previousContentMode; return { name: 'contentFormat', getSharedState: function getSharedState(editorState) { var _pluginState$contentM; if (!editorState) { var _options$initialConte; return { contentMode: (_options$initialConte = options === null || options === void 0 ? void 0 : options.initialContentMode) !== null && _options$initialConte !== void 0 ? _options$initialConte : 'standard' }; } var pluginState = contentFormatPluginKey.getState(editorState); return { contentMode: (_pluginState$contentM = pluginState === null || pluginState === void 0 ? void 0 : pluginState.contentMode) !== null && _pluginState$contentM !== void 0 ? _pluginState$contentM : 'standard' }; }, commands: { updateContentMode: function updateContentMode(contentMode) { return function (_ref3) { var tr = _ref3.tr; if (contentMode === previousContentMode) { return null; } previousContentMode = contentMode; return tr.setMeta(contentFormatPluginKey, { contentMode: contentMode }); }; } }, pmPlugins: function pmPlugins() { return [{ name: 'contentFormat', plugin: function plugin() { var _options$initialConte2; return createPlugin({ initialContentMode: (_options$initialConte2 = options === null || options === void 0 ? void 0 : options.initialContentMode) !== null && _options$initialConte2 !== void 0 ? _options$initialConte2 : 'standard' }); } }]; } }; };