@atlaskit/editor-plugin-editor-viewmode
Version:
Editor viewmode plugin for @atlaskit/editor-core
78 lines (77 loc) • 2.85 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
var viewModePluginKey = new PluginKey('editorViewMode');
var createPlugin = function createPlugin(_ref) {
var initialMode = _ref.initialMode;
return new SafePlugin({
key: viewModePluginKey,
state: {
init: function init() {
return {
mode: initialMode !== null && initialMode !== void 0 ? initialMode : 'edit'
};
},
apply: function apply(tr, pluginState) {
var 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: function editable(state) {
var _viewModePluginKey$ge;
var 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 var editorViewModePlugin = function editorViewModePlugin(_ref2) {
var options = _ref2.config,
api = _ref2.api;
return {
name: 'editorViewMode',
getSharedState: function 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: function updateViewMode(mode) {
return function (_ref3) {
var tr = _ref3.tr;
return tr.setMeta(viewModePluginKey, {
mode: mode
});
};
}
},
pmPlugins: function pmPlugins() {
return [{
name: 'editorViewMode',
plugin: function plugin() {
return createPlugin({
initialMode: options === null || options === void 0 ? void 0 : options.mode
});
}
}];
}
};
};