UNPKG

@atlaskit/editor-plugin-highlight

Version:

Highlight plugin for @atlaskit/editor-core

90 lines (89 loc) • 3.39 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { getHadMarkAttributes, removeMark, toggleMark } from '@atlaskit/editor-common/mark'; import { FORMAT_SELECTION_SYNC_META } from '@atlaskit/editor-common/selection'; import { REMOVE_HIGHLIGHT_COLOR, highlightColorPalette, highlightColorPaletteNew } from '@atlaskit/editor-common/ui-color'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure'; import { HighlightPluginAction, highlightPluginKey } from '../pm-plugins/main'; import { getActiveColor } from './color'; const maybeSyncSelectionAfterFormat = tr => { if (tr.docChanged) { tr.setMeta(FORMAT_SELECTION_SYNC_META, true); } }; export const changeColor = editorAnalyticsAPI => ({ color, inputMethod }) => ({ tr }) => { const { marks } = tr.doc.type.schema; const { backgroundColor } = tr.doc.type.schema.marks; if (!backgroundColor) { return null; } editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent(createAnalyticsEvent(color, inputMethod, tr))(tr); tr.scrollIntoView(); if (color === REMOVE_HIGHLIGHT_COLOR) { removeMark(backgroundColor)({ tr }); } else { if (isExperimentEnabled('platform_editor_lovability_color_schema_change') && !expValEquals('platform_editor_lovability_text_bg_color', 'isEnabled', true)) { const overrideMarks = ['textColor']; overrideMarks.forEach(mark => { if (marks[mark]) { removeMark(marks[mark])({ tr }); } }); } tr.setMeta(highlightPluginKey, { type: HighlightPluginAction.CHANGE_COLOR, color }); toggleMark(backgroundColor, { color })({ tr }); } maybeSyncSelectionAfterFormat(tr); return tr; }; const createAnalyticsEvent = (color, inputMethod, tr) => { var _getActiveColor; const previousColor = (_getActiveColor = getActiveColor(tr)) !== null && _getActiveColor !== void 0 ? _getActiveColor : REMOVE_HIGHLIGHT_COLOR; const highlightPalette = expValEqualsNoExposure('platform_editor_lovability_text_bg_color', 'isEnabled', true) ? highlightColorPaletteNew : highlightColorPalette; // get color names from palette const newColorFromPalette = highlightPalette.find(({ value }) => value === color); const previousColorFromPalette = highlightPalette.find(({ value }) => value === previousColor); const newColorLabel = newColorFromPalette ? newColorFromPalette.label : color; const previousColorLabel = previousColorFromPalette ? previousColorFromPalette.label : previousColor; const { textColor, link } = tr.doc.type.schema.marks; return { action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_BACKGROUND_COLOR, eventType: EVENT_TYPE.TRACK, attributes: { newColor: newColorLabel.toLowerCase(), previousColor: previousColorLabel ? previousColorLabel.toLowerCase() : '', inputMethod, ...(color === REMOVE_HIGHLIGHT_COLOR ? {} : getHadMarkAttributes(tr, [textColor, link])) } }; };