@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
143 lines (129 loc) • 9.94 kB
JavaScript
/**
* Style resolution for the `wrapBlockNodeView` nodeview decorations.
*
* Every exported resolver is a single gate: the `colorSchemeRegistry` + `factory.ts`
* implementation when `platform_editor_show_diff_color_scheme_refactor` is on, and the verbatim
* pre-refactor per-scheme constants from `./wrapBlockNodeViewStyles.legacy` when it is off. The two
* paths are built to emit identical style strings for both shipped schemes, so this is a kill
* switch for a refactor rather than a behaviour experiment.
*
* On cleanup (EDITOR-8281): drop the `isExperimentEnabled` dispatch and the `*Legacy` imports,
* keeping the `*Next` bodies.
*/
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { isExtendedEnabled } from '../../isExtendedEnabled';
import { buildAddedCellOverlayRoundedStyle, buildAddedCellOverlayStyle, buildAddedCellOverlayStyleNew, buildDeletedCellOverlayRoundedStyle, buildDeletedCellOverlayStyle, buildDeletedInlineContentStyle, buildDeletedInlineContentStyleExtended, buildDeletedLozengeActiveStyle, buildDeletedLozengeStyle, buildDeletedQuoteNodeWithLozengeStyle, buildDeletedStrikethroughLine, buildDeletedWrappedBlockOutline, buildInsertedBlockNodeStyle, buildInsertedInlineStyle, buildInsertStyleInBlockExtended, buildInsertStyleInBlockExtendedNoUnderline, buildInsertStyleNode } from '../colorSchemes/factory';
import { colorSchemeRegistry, standardScheme } from '../colorSchemes/schemes';
import { getChangedContentStyleLegacy, getChangedNodeStyleLegacy, getDeletedContentStyleLegacy, getDeletedContentStyleUnboundedLegacy, getInsertedContentStyleLegacy, hasRestingDeletedRingLegacy, resolveCellOverlayStyleLegacy, resolveNestedInsertedNodeStyleLegacy, resolveRemovedLozengeStyleLegacy } from './wrapBlockNodeViewStyles.legacy';
const DEFAULT_COLOR_SCHEME = 'standard';
const getColorScheme = colorScheme => colorSchemeRegistry[colorScheme !== null && colorScheme !== void 0 ? colorScheme : DEFAULT_COLOR_SCHEME];
/**
* Inserted content inside a multi-container or list node always uses the standard scheme, so a
* traditional diff shows purple here. Pre-existing: these call sites predate traditional.
* Preserved as-is; see EDITOR-8281.
*/
const nestedContentScheme = standardScheme;
const lozengeStyle = buildDeletedLozengeStyle();
/**
* Node-name classification shared by the style resolvers below and by the routing in
* `wrapBlockNodeView.ts`.
*/
export const isMultiContainerBlockNode = nodeName => {
return ['decisionList', 'layoutSection'].includes(nodeName);
};
export const isTextLikeBlockNode = nodeName => {
return ['heading', 'bulletList', 'orderedList', 'listItem', 'taskList', 'blockquote'].includes(nodeName);
};
const getChangedContentStyleNext = (colorScheme, isActive = false, isInserted = false, diffType, hideAddedDiffsUnderline = false) => {
const colors = getColorScheme(colorScheme);
if (isExtendedEnabled(diffType) && isInserted) {
return buildInsertedInlineStyle(colors, isActive, hideAddedDiffsUnderline);
}
if (isActive) {
return buildDeletedInlineContentStyle(colors, 'active');
}
return buildDeletedInlineContentStyle(colors, expValEquals('platform_editor_enghealth_a11y_jan_fixes', 'isEnabled', true) ? 'new' : 'default');
};
const getChangedNodeStyleNext = (nodeName, colorScheme, isInserted = false, isActive = false, diffType, hideAddedDiffsUnderline = false) => {
const colors = getColorScheme(colorScheme);
if (isExtendedEnabled(diffType) && isInserted) {
if (isMultiContainerBlockNode(nodeName)) {
return hideAddedDiffsUnderline ? buildInsertStyleInBlockExtendedNoUnderline(nestedContentScheme) : buildInsertStyleInBlockExtended(nestedContentScheme);
}
if (isTextLikeBlockNode(nodeName)) {
return undefined;
}
return buildInsertedBlockNodeStyle(colors, 'node', isActive);
}
switch (nodeName) {
case 'blockquote':
return buildDeletedQuoteNodeWithLozengeStyle(colors, isActive);
case 'expand':
case 'decisionList':
return buildDeletedWrappedBlockOutline(colors, {
isActive,
isRounded: false
});
case 'panel':
case 'codeBlock':
return buildDeletedWrappedBlockOutline(colors, {
isActive,
isRounded: true
});
default:
return undefined;
}
};
const getDeletedContentStyleUnboundedNext = (colorScheme, isActive = false) => buildDeletedStrikethroughLine(getColorScheme(colorScheme), isActive);
const getInsertedContentStyleNext = (colorScheme, isActive = false, hideAddedDiffsUnderline = false) => buildInsertedInlineStyle(getColorScheme(colorScheme), isActive, hideAddedDiffsUnderline);
const getDeletedContentStyleNext = (colorScheme, isActive = false, diffType) => {
const colors = getColorScheme(colorScheme);
const base = buildDeletedInlineContentStyle(colors, isActive ? 'active' : 'new');
// glyphTint adds a background highlight and border-bottom over the tint; strikethrough does not.
const needsExtendedHighlight = colors.deletedInlineTreatment === 'glyphTint' && isExtendedEnabled(diffType);
return needsExtendedHighlight ? base + buildDeletedInlineContentStyleExtended(colors) : base;
};
const resolveCellOverlayStyleNext = ({
colorScheme,
isInserted,
isRoundedTable
}) => {
const colors = getColorScheme(colorScheme);
// Square added-cell overlays follow insertedNodeEmphasis: pressed background vs accent border.
const addedCellStyle = isRoundedTable ? buildAddedCellOverlayRoundedStyle(colors) : colors.insertedNodeEmphasis === 'stateful' ? buildAddedCellOverlayStyleNew(colors) : buildAddedCellOverlayStyle(colors);
const deletedCellStyle = isRoundedTable ? buildDeletedCellOverlayRoundedStyle(colors) : buildDeletedCellOverlayStyle(colors);
return isInserted ? addedCellStyle : deletedCellStyle;
};
const resolveRemovedLozengeStyleNext = (colorScheme, isActive) => isActive ? buildDeletedLozengeActiveStyle(getColorScheme(colorScheme)) : lozengeStyle;
const resolveNestedInsertedNodeStyleNext = () => buildInsertStyleNode(nestedContentScheme);
// Only 'stateful' schemes have a resting ring.
const hasRestingDeletedRingNext = colorScheme => getColorScheme(colorScheme).deletedNodeEmphasis === 'stateful';
/** Inline content style for a changed (inserted or deleted) block node. */
export const getChangedContentStyle = (colorScheme, isActive = false, isInserted = false, diffType, hideAddedDiffsUnderline = false) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getChangedContentStyleNext(colorScheme, isActive, isInserted, diffType, hideAddedDiffsUnderline) : getChangedContentStyleLegacy(colorScheme, isActive, isInserted, diffType, hideAddedDiffsUnderline);
/** Node-specific style for a changed block node, or `undefined` when the node type has none. */
export const getChangedNodeStyle = (nodeName, colorScheme, isInserted = false, isActive = false, diffType, hideAddedDiffsUnderline = false) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getChangedNodeStyleNext(nodeName, colorScheme, isInserted, isActive, diffType, hideAddedDiffsUnderline) : getChangedNodeStyleLegacy(nodeName, colorScheme, isInserted, isActive, diffType, hideAddedDiffsUnderline);
/** The positioned line drawn across deleted content spanning an unbounded range. */
export const getDeletedContentStyleUnbounded = (colorScheme, isActive = false) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getDeletedContentStyleUnboundedNext(colorScheme, isActive) : getDeletedContentStyleUnboundedLegacy(colorScheme, isActive);
/** Inline style for inserted content. */
export const getInsertedContentStyle = (colorScheme, isActive = false, hideAddedDiffsUnderline = false) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getInsertedContentStyleNext(colorScheme, isActive, hideAddedDiffsUnderline) : getInsertedContentStyleLegacy(colorScheme, isActive, hideAddedDiffsUnderline);
/** Inline style for deleted content. */
export const getDeletedContentStyle = (colorScheme, isActive = false, diffType) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getDeletedContentStyleNext(colorScheme, isActive, diffType) : getDeletedContentStyleLegacy(colorScheme, isActive, diffType);
/**
* Overlay style for one added or deleted table cell. Invariant across the cells of a table, so the
* `querySelectorAll('td, th')` loop in `wrapBlockNodeView.ts` resolves it once.
*/
export const resolveCellOverlayStyle = args => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? resolveCellOverlayStyleNext(args) : resolveCellOverlayStyleLegacy(args);
/** Inner style for the "Removed" lozenge on a deleted block node. */
export const resolveRemovedLozengeStyle = (colorScheme, isActive) => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? resolveRemovedLozengeStyleNext(colorScheme, isActive) : resolveRemovedLozengeStyleLegacy(colorScheme, isActive);
/**
* Style for inserted content nested inside a multi-container or list node — the `decisionList` and
* `taskList` `li`s and the `layoutSection` columns. Scheme-independent: see
* `nestedContentScheme` above.
*/
export const resolveNestedInsertedNodeStyle = () => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? resolveNestedInsertedNodeStyleNext() : resolveNestedInsertedNodeStyleLegacy();
/**
* Whether the colour scheme draws a resting (non-active) ring on a deleted media/embed node, which
* is what the `show-diff-deleted-outline-new` class styles in editor-core's smartCardStyles.
*/
export const hasRestingDeletedRing = colorScheme => isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? hasRestingDeletedRingNext(colorScheme) : hasRestingDeletedRingLegacy(colorScheme);