@atlaskit/editor-plugin-width
Version:
Width plugin for @atlaskit/editor-core
33 lines • 1.24 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { pluginKey } from './plugin-key';
export function createPlugin(dispatch) {
return new SafePlugin({
key: pluginKey,
state: {
init: () => {
return {
// The init value of width is a placeholder, it cannot be 0,
// we use the window dimension, which will not cause reflow at the start.
// `innerWidth` excludes browser chrome and follows page zoom, unlike `outerWidth`.
width: isExperimentEnabled('platform_editor_reduce_forced_layout') ? window.innerWidth : 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;
}
}
});
}