@atlaskit/editor-plugin-content-format
Version:
ContentFormat plugin for @atlaskit/editor-core
72 lines • 2.43 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
export const contentFormatPluginKey = new PluginKey('contentFormat');
export const createPlugin = ({
initialContentMode
}) => {
return new SafePlugin({
key: contentFormatPluginKey,
state: {
init: () => ({
contentMode: initialContentMode !== null && initialContentMode !== void 0 ? initialContentMode : 'standard'
}),
apply: (tr, currentPluginState) => {
const meta = tr.getMeta(contentFormatPluginKey);
if (meta) {
const {
contentMode
} = meta;
return {
contentMode: contentMode !== null && contentMode !== void 0 ? contentMode : currentPluginState.contentMode
};
}
return currentPluginState;
}
}
});
};
export const contentFormatPlugin = ({
config: options
}) => {
let previousContentMode;
return {
name: 'contentFormat',
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'
};
}
const 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: contentMode => ({
tr
}) => {
if (contentMode === previousContentMode) {
return null;
}
previousContentMode = contentMode;
return tr.setMeta(contentFormatPluginKey, {
contentMode
});
}
},
pmPlugins() {
return [{
name: 'contentFormat',
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'
});
}
}];
}
};
};