@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
42 lines (41 loc) • 1.65 kB
JavaScript
import { Plugin } from '@atlaskit/editor-prosemirror/state';
// Wraper to avoid any exception during the get pos operation
// See this https://hello.atlassian.net/wiki/spaces/EDITOR/pages/2849713193/ED-19672+Extensions+Regression
// And this https://discuss.prosemirror.net/t/possible-bug-on-viewdesc-posbeforechild/5783
const wrapGetPosExceptions = spec => {
var _spec$props;
if (!(spec !== null && spec !== void 0 && (_spec$props = spec.props) !== null && _spec$props !== void 0 && _spec$props.nodeViews)) {
return spec;
}
const unsafeNodeViews = spec.props.nodeViews;
const safeNodeViews = new Proxy(unsafeNodeViews, {
get(target, prop, receiver) {
const safeNodeView = new Proxy(Reflect.get(target, prop, receiver), {
apply(target, thisArg, argumentsList) {
const [node, view, unsafeGetPos, ...more] = argumentsList;
const safeGetPos = (() => {
try {
return unsafeGetPos();
} catch (e) {
return;
}
return;
// eslint-disable-next-line no-extra-bind
}).bind(thisArg);
return Reflect.apply(target, thisArg, [node, view, safeGetPos, ...more]);
}
});
return safeNodeView;
}
});
spec.props.nodeViews = safeNodeViews;
return spec;
};
export class SafePlugin extends Plugin {
// This variable isn't (and shouldn't) be used anywhere. Its purpose is
// to distinguish Plugin from SafePlugin, thus ensuring that an 'unsafe'
// Plugin cannot be assigned as an item in EditorPlugin → pmPlugins.
constructor(spec) {
super(wrapGetPosExceptions(spec));
}
}