@atlaskit/editor-plugin-extension
Version:
editor-plugin-extension plugin for @atlaskit/editor-core
35 lines • 958 B
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { setMacroProvider } from './actions';
import { pluginKey } from './plugin-key';
export const createPlugin = (dispatch, providerFactory) => new SafePlugin({
state: {
init: () => ({
macroProvider: null
}),
apply(tr, state) {
const meta = tr.getMeta(pluginKey);
if (meta) {
const newState = {
...state,
...meta
};
dispatch(pluginKey, newState);
return newState;
}
return state;
}
},
key: pluginKey,
view: view => {
const handleProvider = (_name, provider) => provider && setMacroProvider(provider)(view);
// make sure editable DOM node is mounted
if (view.dom.parentNode) {
providerFactory.subscribe('macroProvider', handleProvider);
}
return {
destroy() {
providerFactory.unsubscribe('macroProvider', handleProvider);
}
};
}
});