@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
29 lines (28 loc) • 1.46 kB
JavaScript
import { isDiffDecorationSpec } from './decorations/decorationKeys';
import { getWidgetElement } from './decorations/utils/getWidgetElement';
import { showDiffPluginKey } from './main';
/**
* The rendered deleted-content widgets in `editorState`, optionally restricted to `[from, to]`,
* ordered by position. Deleted content is rendered as widget decorations rather than document
* nodes, so this is how the element and the position it is anchored at are recovered.
*
* Kept internal and surfaced only through the `getDeletedWidgets` plugin action, so consumers never
* receive the plugin key or the decoration set (which would let them mutate plugin behaviour).
*/
export const getDeletedWidgets = (editorState, range) => {
var _showDiffPluginKey$ge;
const decorations = (_showDiffPluginKey$ge = showDiffPluginKey.getState(editorState)) === null || _showDiffPluginKey$ge === void 0 ? void 0 : _showDiffPluginKey$ge.decorations;
if (!decorations) {
return [];
}
return decorations.find(range === null || range === void 0 ? void 0 : range.from, range === null || range === void 0 ? void 0 : range.to, spec => isDiffDecorationSpec(spec) && spec.decorationType === 'widget').reduce((widgets, decoration) => {
const element = getWidgetElement(decoration);
if (element) {
widgets.push({
element,
position: decoration.from
});
}
return widgets;
}, []).sort((a, b) => a.position - b.position);
};