UNPKG

@atlaskit/editor-plugin-width

Version:

Width plugin for @atlaskit/editor-core

56 lines 1.42 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { pluginKey } from './plugin-key'; import { useResizeWidthObserver } from './useResizeWidthObserver'; function createPlugin(dispatch) { return new SafePlugin({ key: pluginKey, state: { init: () => ({ width: document.body.offsetWidth }), apply(tr, pluginState) { const meta = tr.getMeta(pluginKey); if (!meta) { return pluginState; } const newPluginState = { ...pluginState, ...meta }; if (newPluginState && (pluginState.width !== newPluginState.width || pluginState.lineLength !== newPluginState.lineLength)) { dispatch(pluginKey, newPluginState); return newPluginState; } return pluginState; } } }); } /** * Width plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor` * from `@atlaskit/editor-core`. */ export const widthPlugin = () => ({ name: 'width', pmPlugins: () => [{ name: 'width', plugin: ({ dispatch }) => createPlugin(dispatch) }], getSharedState: editorState => { if (!editorState) { return undefined; } return pluginKey.getState(editorState); }, usePluginHook({ editorView, containerElement }) { return useResizeWidthObserver({ editorView, containerElement }); } });