@atlaskit/editor-plugin-context-panel
Version:
Context panel plugin for @atlaskit/editor-core
89 lines (88 loc) • 4.23 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
import { fg } from '@atlaskit/platform-feature-flags';
import { applyChange } from './pm-plugins/transforms';
export var pluginKey = new PluginKey('contextPanelPluginKey');
function contextPanelPluginFactory(contextPanels, dispatch) {
return new SafePlugin({
key: pluginKey,
state: {
init: function init(_config, state) {
return {
handlers: contextPanels,
contents: contextPanels.map(function (panelContent) {
return panelContent(state);
})
};
},
apply: function apply(tr, pluginState, _oldState, newState) {
var newPluginState = pluginState;
var meta = tr.getMeta(pluginKey);
if (tr.docChanged || tr.selectionSet || meta && meta.changed) {
var newContents = pluginState.handlers.map(function (panelContent) {
return panelContent(newState);
});
var contentsLengthChanged = newContents.length !== newPluginState.contents.length;
var contentChanged = newContents.some(function (node, index) {
return newPluginState.contents[index] !== node;
});
if (contentsLengthChanged || contentChanged) {
newPluginState = _objectSpread(_objectSpread({}, newPluginState), {}, {
contents: newContents
});
}
}
if (newPluginState !== pluginState) {
dispatch(pluginKey, newPluginState);
}
return newPluginState;
}
}
});
}
/**
* Context panel plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor`
* from `@atlaskit/editor-core`.
*/
export var contextPanelPlugin = function contextPanelPlugin(_ref) {
var config = _ref.config;
return {
name: 'contextPanel',
actions: {
applyChange: applyChange,
showPanel: config !== null && config !== void 0 && config.objectSideBar.showPanel && fg('platform_editor_ai_object_sidebar_injection') ? function (panel, behavior, panelWidth) {
return config.objectSideBar.showPanel(panel, behavior, panelWidth);
} : undefined,
closePanel: config !== null && config !== void 0 && config.objectSideBar.closePanel && fg('platform_editor_ai_object_sidebar_injection') ? function () {
return config.objectSideBar.closePanel();
} : undefined,
closePanelById: config !== null && config !== void 0 && config.objectSideBar.closePanelById && fg('platform_editor_ai_object_sidebar_injection') ? function (id) {
return config.objectSideBar.closePanelById(id);
} : undefined
},
getSharedState: function getSharedState(state) {
var _pluginKey$getState;
if (!state) {
return undefined;
}
var _ref2 = (_pluginKey$getState = pluginKey.getState(state)) !== null && _pluginKey$getState !== void 0 ? _pluginKey$getState : {},
contents = _ref2.contents;
return {
contents: contents
};
},
pmPlugins: function pmPlugins() {
var contextPanels = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return [{
name: 'contextPanel',
plugin: function plugin(_ref3) {
var dispatch = _ref3.dispatch;
return contextPanelPluginFactory(contextPanels.filter(Boolean), dispatch);
}
}];
}
};
};