@atlaskit/editor-plugin-highlight
Version:
Highlight plugin for @atlaskit/editor-core
52 lines • 1.8 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
import { getActiveColor } from '../editor-commands/color';
import { getDisabledState } from '../editor-commands/disabled';
export const highlightPluginKey = new PluginKey('highlight');
export let HighlightPluginAction = /*#__PURE__*/function (HighlightPluginAction) {
HighlightPluginAction[HighlightPluginAction["CHANGE_COLOR"] = 0] = "CHANGE_COLOR";
HighlightPluginAction[HighlightPluginAction["SET_PALETTE"] = 1] = "SET_PALETTE";
return HighlightPluginAction;
}({});
export const createPlugin = ({
api
}) => {
return new SafePlugin({
key: highlightPluginKey,
state: {
init: (_, editorState) => ({
activeColor: null,
disabled: getDisabledState(editorState),
isPaletteOpen: false
}),
apply: (tr, pluginState, _oldState, newState) => {
var _tr$getMeta;
const action = (_tr$getMeta = tr.getMeta(highlightPluginKey)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.type;
switch (action) {
case HighlightPluginAction.CHANGE_COLOR:
const {
color
} = tr.getMeta(highlightPluginKey);
return {
...pluginState,
activeColor: color
};
case HighlightPluginAction.SET_PALETTE:
const {
isPaletteOpen
} = tr.getMeta(highlightPluginKey);
return {
...pluginState,
isPaletteOpen
};
default:
return {
...pluginState,
activeColor: getActiveColor(tr),
disabled: getDisabledState(newState)
};
}
}
}
});
};