UNPKG

@limetech/lime-elements

Version:
41 lines (40 loc) 1.83 kB
import { Plugin, PluginKey } from 'prosemirror-state'; import { isEqual } from 'lodash-es'; export const actionBarPluginKey = new PluginKey('actionBarPlugin'); export const getMenuItemStates = (menuTypes, menuCommandFactory, view) => { var _a, _b, _c; const activeTypes = {}; const allowedTypes = {}; for (const type of menuTypes) { const command = menuCommandFactory.getCommand(type); activeTypes[type] = !!((_a = command === null || command === void 0 ? void 0 : command.active) === null || _a === void 0 ? void 0 : _a.call(command, view.state)) || false; allowedTypes[type] = !!((_c = (_b = command === null || command === void 0 ? void 0 : command.allowed) === null || _b === void 0 ? void 0 : _b.call(command, view.state)) !== null && _c !== void 0 ? _c : true); } return { active: activeTypes, allowed: allowedTypes }; }; export const createMenuStateTrackingPlugin = (menuTypes, menuCommandFactory, updateCallback) => { return new Plugin({ key: actionBarPluginKey, state: { init: () => { return { active: {}, allowed: {} }; }, apply: (tr, menuStates) => { const newMenuStates = tr.getMeta(actionBarPluginKey); return newMenuStates !== null && newMenuStates !== void 0 ? newMenuStates : menuStates; }, }, view: () => ({ update: (view) => { const oldItemStates = actionBarPluginKey.getState(view.state); const menuItemStates = getMenuItemStates(menuTypes, menuCommandFactory, view); if (!isEqual(oldItemStates, menuItemStates)) { const tr = view.state.tr.setMeta(actionBarPluginKey, menuItemStates); view.dispatch(tr); updateCallback(menuItemStates.active, menuItemStates.allowed); } }, }), }); }; //# sourceMappingURL=menu-state-tracking-plugin.js.map