UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

198 lines (184 loc) 12.7 kB
/** * 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'; var DEFAULT_COLOR_SCHEME = 'standard'; var getColorScheme = function getColorScheme(colorScheme) { return 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. */ var nestedContentScheme = standardScheme; var lozengeStyle = buildDeletedLozengeStyle(); /** * Node-name classification shared by the style resolvers below and by the routing in * `wrapBlockNodeView.ts`. */ export var isMultiContainerBlockNode = function isMultiContainerBlockNode(nodeName) { return ['decisionList', 'layoutSection'].includes(nodeName); }; export var isTextLikeBlockNode = function isTextLikeBlockNode(nodeName) { return ['heading', 'bulletList', 'orderedList', 'listItem', 'taskList', 'blockquote'].includes(nodeName); }; var getChangedContentStyleNext = function getChangedContentStyleNext(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var isInserted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var diffType = arguments.length > 3 ? arguments[3] : undefined; var hideAddedDiffsUnderline = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; var 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'); }; var getChangedNodeStyleNext = function getChangedNodeStyleNext(nodeName, colorScheme) { var isInserted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var isActive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; var diffType = arguments.length > 4 ? arguments[4] : undefined; var hideAddedDiffsUnderline = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false; var 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: isActive, isRounded: false }); case 'panel': case 'codeBlock': return buildDeletedWrappedBlockOutline(colors, { isActive: isActive, isRounded: true }); default: return undefined; } }; var getDeletedContentStyleUnboundedNext = function getDeletedContentStyleUnboundedNext(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return buildDeletedStrikethroughLine(getColorScheme(colorScheme), isActive); }; var getInsertedContentStyleNext = function getInsertedContentStyleNext(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var hideAddedDiffsUnderline = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; return buildInsertedInlineStyle(getColorScheme(colorScheme), isActive, hideAddedDiffsUnderline); }; var getDeletedContentStyleNext = function getDeletedContentStyleNext(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var diffType = arguments.length > 2 ? arguments[2] : undefined; var colors = getColorScheme(colorScheme); var base = buildDeletedInlineContentStyle(colors, isActive ? 'active' : 'new'); // glyphTint adds a background highlight and border-bottom over the tint; strikethrough does not. var needsExtendedHighlight = colors.deletedInlineTreatment === 'glyphTint' && isExtendedEnabled(diffType); return needsExtendedHighlight ? base + buildDeletedInlineContentStyleExtended(colors) : base; }; var resolveCellOverlayStyleNext = function resolveCellOverlayStyleNext(_ref) { var colorScheme = _ref.colorScheme, isInserted = _ref.isInserted, isRoundedTable = _ref.isRoundedTable; var colors = getColorScheme(colorScheme); // Square added-cell overlays follow insertedNodeEmphasis: pressed background vs accent border. var addedCellStyle = isRoundedTable ? buildAddedCellOverlayRoundedStyle(colors) : colors.insertedNodeEmphasis === 'stateful' ? buildAddedCellOverlayStyleNew(colors) : buildAddedCellOverlayStyle(colors); var deletedCellStyle = isRoundedTable ? buildDeletedCellOverlayRoundedStyle(colors) : buildDeletedCellOverlayStyle(colors); return isInserted ? addedCellStyle : deletedCellStyle; }; var resolveRemovedLozengeStyleNext = function resolveRemovedLozengeStyleNext(colorScheme, isActive) { return isActive ? buildDeletedLozengeActiveStyle(getColorScheme(colorScheme)) : lozengeStyle; }; var resolveNestedInsertedNodeStyleNext = function resolveNestedInsertedNodeStyleNext() { return buildInsertStyleNode(nestedContentScheme); }; // Only 'stateful' schemes have a resting ring. var hasRestingDeletedRingNext = function hasRestingDeletedRingNext(colorScheme) { return getColorScheme(colorScheme).deletedNodeEmphasis === 'stateful'; }; /** Inline content style for a changed (inserted or deleted) block node. */ export var getChangedContentStyle = function getChangedContentStyle(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var isInserted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var diffType = arguments.length > 3 ? arguments[3] : undefined; var hideAddedDiffsUnderline = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; return 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 var getChangedNodeStyle = function getChangedNodeStyle(nodeName, colorScheme) { var isInserted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var isActive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; var diffType = arguments.length > 4 ? arguments[4] : undefined; var hideAddedDiffsUnderline = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false; return 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 var getDeletedContentStyleUnbounded = function getDeletedContentStyleUnbounded(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getDeletedContentStyleUnboundedNext(colorScheme, isActive) : getDeletedContentStyleUnboundedLegacy(colorScheme, isActive); }; /** Inline style for inserted content. */ export var getInsertedContentStyle = function getInsertedContentStyle(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var hideAddedDiffsUnderline = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; return isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? getInsertedContentStyleNext(colorScheme, isActive, hideAddedDiffsUnderline) : getInsertedContentStyleLegacy(colorScheme, isActive, hideAddedDiffsUnderline); }; /** Inline style for deleted content. */ export var getDeletedContentStyle = function getDeletedContentStyle(colorScheme) { var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var diffType = arguments.length > 2 ? arguments[2] : undefined; return 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 var resolveCellOverlayStyle = function resolveCellOverlayStyle(args) { return isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? resolveCellOverlayStyleNext(args) : resolveCellOverlayStyleLegacy(args); }; /** Inner style for the "Removed" lozenge on a deleted block node. */ export var resolveRemovedLozengeStyle = function resolveRemovedLozengeStyle(colorScheme, isActive) { return 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 var resolveNestedInsertedNodeStyle = function resolveNestedInsertedNodeStyle() { return 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 var hasRestingDeletedRing = function hasRestingDeletedRing(colorScheme) { return isExperimentEnabled('platform_editor_show_diff_color_scheme_refactor') ? hasRestingDeletedRingNext(colorScheme) : hasRestingDeletedRingLegacy(colorScheme); };