@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
28 lines • 1.5 kB
JavaScript
import React from 'react';
import { trackChangesMessages } from '@atlaskit/editor-common/messages/track-changes';
import { Decoration } from '@atlaskit/editor-prosemirror/view';
import VisuallyHidden from '@atlaskit/visually-hidden/visually-hidden';
export const mountDiffBoundary = (host, intl, isInserted, isStart, portalProviderAPI) => {
const key = `diff-boundary-${crypto.randomUUID()}`;
const message = isInserted ? isStart ? trackChangesMessages.diffInsertStart : trackChangesMessages.diffInsertEnd : isStart ? trackChangesMessages.diffDeleteStart : trackChangesMessages.diffDeleteEnd;
portalProviderAPI.render(() => /*#__PURE__*/React.createElement(VisuallyHidden, null, intl.formatMessage(message)), host, key);
return () => portalProviderAPI.remove(key);
};
export const createDiffBoundaryWidget = (pos, intl, isInserted, isStart, portalProviderAPI, side = isStart ? -0.5 : -2) => {
let unmount;
return Decoration.widget(pos, view => {
// ShowDiff has no cue API; ADS owns hidden content, while ProseMirror requires a DOM host.
const host = view.dom.ownerDocument.createElement('span');
unmount = mountDiffBoundary(host, intl, isInserted, isStart, portalProviderAPI);
return host;
}, {
// Deleted widgets precede the surviving text; its start cue must follow them.
side,
marks: [],
ignoreSelection: true,
destroy: () => {
var _unmount;
return (_unmount = unmount) === null || _unmount === void 0 ? void 0 : _unmount();
}
});
};