UNPKG

@atlaskit/editor-plugin-highlight

Version:

Highlight plugin for @atlaskit/editor-core

84 lines (83 loc) 3.2 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { 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 { 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 && expValEquals('platform_editor_fix_selection_text_color_change', 'isEnabled', true)) { 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 (expValEquals('platform_editor_lovability_color_schema_change', 'isEnabled', true) && !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; 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 } }; };