UNPKG

@atlaskit/editor-plugin-highlight

Version:

Highlight plugin for @atlaskit/editor-core

84 lines (83 loc) 3.64 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'; var maybeSyncSelectionAfterFormat = function maybeSyncSelectionAfterFormat(tr) { if (tr.docChanged && expValEquals('platform_editor_fix_selection_text_color_change', 'isEnabled', true)) { tr.setMeta(FORMAT_SELECTION_SYNC_META, true); } }; export var changeColor = function changeColor(editorAnalyticsAPI) { return function (_ref) { var color = _ref.color, inputMethod = _ref.inputMethod; return function (_ref2) { var tr = _ref2.tr; var marks = tr.doc.type.schema.marks; var backgroundColor = tr.doc.type.schema.marks.backgroundColor; if (!backgroundColor) { return null; } editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent(createAnalyticsEvent(color, inputMethod, tr))(tr); tr.scrollIntoView(); if (color === REMOVE_HIGHLIGHT_COLOR) { removeMark(backgroundColor)({ tr: tr }); } else { if (expValEquals('platform_editor_lovability_color_schema_change', 'isEnabled', true) && !expValEquals('platform_editor_lovability_text_bg_color', 'isEnabled', true)) { var overrideMarks = ['textColor']; overrideMarks.forEach(function (mark) { if (marks[mark]) { removeMark(marks[mark])({ tr: tr }); } }); } tr.setMeta(highlightPluginKey, { type: HighlightPluginAction.CHANGE_COLOR, color: color }); toggleMark(backgroundColor, { color: color })({ tr: tr }); } maybeSyncSelectionAfterFormat(tr); return tr; }; }; }; var createAnalyticsEvent = function createAnalyticsEvent(color, inputMethod, tr) { var _getActiveColor; var previousColor = (_getActiveColor = getActiveColor(tr)) !== null && _getActiveColor !== void 0 ? _getActiveColor : REMOVE_HIGHLIGHT_COLOR; var highlightPalette = expValEqualsNoExposure('platform_editor_lovability_text_bg_color', 'isEnabled', true) ? highlightColorPaletteNew : highlightColorPalette; // get color names from palette var newColorFromPalette = highlightPalette.find(function (_ref3) { var value = _ref3.value; return value === color; }); var previousColorFromPalette = highlightPalette.find(function (_ref4) { var value = _ref4.value; return value === previousColor; }); var newColorLabel = newColorFromPalette ? newColorFromPalette.label : color; var 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: inputMethod } }; };