UNPKG

@atlaskit/editor-plugin-width

Version:

Width plugin for @atlaskit/editor-core

62 lines 2.54 kB
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 './plugin-key'; import { useResizeWidthObserver } from './useResizeWidthObserver'; function createPlugin(dispatch) { return new SafePlugin({ key: pluginKey, state: { init: function init() { return { width: document.body.offsetWidth }; }, apply: function apply(tr, pluginState) { var meta = tr.getMeta(pluginKey); if (!meta) { return pluginState; } var newPluginState = _objectSpread(_objectSpread({}, 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 var widthPlugin = function widthPlugin() { return { name: 'width', pmPlugins: function pmPlugins() { return [{ name: 'width', plugin: function plugin(_ref) { var dispatch = _ref.dispatch; return createPlugin(dispatch); } }]; }, getSharedState: function getSharedState(editorState) { if (!editorState) { return undefined; } return pluginKey.getState(editorState); }, usePluginHook: function usePluginHook(_ref2) { var editorView = _ref2.editorView, containerElement = _ref2.containerElement; return useResizeWidthObserver({ editorView: editorView, containerElement: containerElement }); } }; };