@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
116 lines (114 loc) • 3.25 kB
JavaScript
import { isExtendedEnabled } from '../isExtendedEnabled';
/**
* Decoration families produced by the show-diff plugin.
* Each family owns its own key space and spec shape.
*/
export const DecorationFamily = {
diff: 'diff',
anchor: 'anchor'
};
export const AnchorTypeKey = {
from: 'from',
to: 'to',
left: 'left',
docMargin: 'doc-margin'
};
/**
* The diff-decoration kinds produced by the show-diff plugin. Each value is
* the leading segment of the generated key, so a diff decoration's kind can be
* matched with `key?.startsWith(DiffDecorationKey.inline)` etc.
*/
export const DiffDecorationKey = {
inline: `${DecorationFamily.diff}-inline`,
block: `${DecorationFamily.diff}-block`,
widget: `${DecorationFamily.diff}-widget`
};
export const AnchorDocMarginKey = `${DecorationFamily.anchor}-${AnchorTypeKey.docMargin}`;
export const buildAnchorDecorationKey = ({
diffId,
anchorType
}) => {
return `${DecorationFamily.anchor}-${diffId}${anchorType ? `-${anchorType}` : ''}`;
};
/**
* Builds a diff decoration key. The extended experience includes the `diffId`
* in the key so independently rendered decorations for the same type remain
* distinguishable.
*/
export const buildDiffDecorationKey = ({
decorationKeyPrefix,
isActive,
diffId,
diffType
}) => {
if (isExtendedEnabled(diffType)) {
return `${decorationKeyPrefix}-${diffId}-${isActive ? 'active' : 'inactive'}`;
}
return isActive !== undefined ? `${decorationKeyPrefix}-${isActive ? 'active' : 'inactive'}` : decorationKeyPrefix;
};
export const buildDiffDecorationSpec = ({
decorationType,
diffId,
isActive,
nodeName,
side,
diffType
}) => ({
decorationFamily: DecorationFamily.diff,
decorationType,
diffId,
key: buildDiffDecorationKey({
decorationKeyPrefix: DiffDecorationKey[decorationType],
diffId,
isActive,
diffType
}),
...(nodeName ? {
nodeName
} : {}),
...(side !== undefined ? {
side
} : {})
});
export function buildAnchorDecorationSpec({
anchorType,
diffId,
side
}) {
if (anchorType === AnchorTypeKey.docMargin) {
return {
decorationFamily: DecorationFamily.anchor,
anchorType,
key: AnchorDocMarginKey,
...(side !== undefined ? {
side
} : {})
};
}
if (!diffId) {
throw new Error(`diffId is required for anchor type "${anchorType}"`);
}
return {
decorationFamily: DecorationFamily.anchor,
anchorType,
diffId,
key: buildAnchorDecorationKey({
diffId,
anchorType
}),
...(side !== undefined ? {
side
} : {})
};
}
export const isDiffDecorationSpec = spec => Boolean(spec && typeof spec === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.diff);
export const isAnchorDecorationSpec = spec => Boolean(spec && typeof spec === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.anchor);
export const isDiffDecoration = decoration => isDiffDecorationSpec(decoration.spec);
export const extractDiffDescriptors = decorations => decorations.find(undefined, undefined, isDiffDecorationSpec).filter(isDiffDecoration).map(({
spec
}) => {
return {
id: spec.diffId,
type: spec.decorationType
};
});