@atlaskit/editor-plugin-highlight
Version:
Highlight plugin for @atlaskit/editor-core
98 lines (96 loc) • 4.95 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.changeColor = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _mark = require("@atlaskit/editor-common/mark");
var _selection = require("@atlaskit/editor-common/selection");
var _uiColor = require("@atlaskit/editor-common/ui-color");
var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _main = require("../pm-plugins/main");
var _color = require("./color");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var maybeSyncSelectionAfterFormat = function maybeSyncSelectionAfterFormat(tr) {
if (tr.docChanged) {
tr.setMeta(_selection.FORMAT_SELECTION_SYNC_META, true);
}
};
var changeColor = exports.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 === _uiColor.REMOVE_HIGHLIGHT_COLOR) {
(0, _mark.removeMark)(backgroundColor)({
tr: tr
});
} else {
if ((0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_lovability_color_schema_change') && !(0, _expValEquals.expValEquals)('platform_editor_lovability_text_bg_color', 'isEnabled', true)) {
var overrideMarks = ['textColor'];
overrideMarks.forEach(function (mark) {
if (marks[mark]) {
(0, _mark.removeMark)(marks[mark])({
tr: tr
});
}
});
}
tr.setMeta(_main.highlightPluginKey, {
type: _main.HighlightPluginAction.CHANGE_COLOR,
color: color
});
(0, _mark.toggleMark)(backgroundColor, {
color: color
})({
tr: tr
});
}
maybeSyncSelectionAfterFormat(tr);
return tr;
};
};
};
var createAnalyticsEvent = function createAnalyticsEvent(color, inputMethod, tr) {
var _getActiveColor;
var previousColor = (_getActiveColor = (0, _color.getActiveColor)(tr)) !== null && _getActiveColor !== void 0 ? _getActiveColor : _uiColor.REMOVE_HIGHLIGHT_COLOR;
var highlightPalette = (0, _expValEqualsNoExposure.expValEqualsNoExposure)('platform_editor_lovability_text_bg_color', 'isEnabled', true) ? _uiColor.highlightColorPaletteNew : _uiColor.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;
var _tr$doc$type$schema$m = tr.doc.type.schema.marks,
textColor = _tr$doc$type$schema$m.textColor,
link = _tr$doc$type$schema$m.link;
return {
action: _analytics.ACTION.FORMATTED,
actionSubject: _analytics.ACTION_SUBJECT.TEXT,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_BACKGROUND_COLOR,
eventType: _analytics.EVENT_TYPE.TRACK,
attributes: _objectSpread({
newColor: newColorLabel.toLowerCase(),
previousColor: previousColorLabel ? previousColorLabel.toLowerCase() : '',
inputMethod: inputMethod
}, color === _uiColor.REMOVE_HIGHLIGHT_COLOR ? {} : (0, _mark.getHadMarkAttributes)(tr, [textColor, link]))
};
};