@atlaskit/editor-plugin-rule
Version:
Rule plugin for @atlaskit/editor-core
97 lines • 5.34 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
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) { _defineProperty(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; }
import React from 'react';
import { rule, ruleWithAttrs, ruleWithLocalId } from '@atlaskit/adf-schema/rule';
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums';
import { IconDivider } from '@atlaskit/editor-common/assets';
import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages/insert-block';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { insertHorizontalRule } from './pm-plugins/commands';
// Ignored via go/ees005
// eslint-disable-next-line import/no-named-as-default
import inputRulePlugin from './pm-plugins/input-rule';
// Ignored via go/ees005
// eslint-disable-next-line import/no-named-as-default
import keymapPlugin from './pm-plugins/keymap';
import ruleNodeViewPlugin from './pm-plugins/main';
import { getRuleQuickInsertComponents } from './ui/quick-insert/getRuleQuickInsertComponents';
import { getToolbarConfig } from './ui/toolbar';
export var rulePlugin = function rulePlugin(_ref) {
var _api$analytics;
var api = _ref.api;
var isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command');
if (isRegisteredSlashCommandEnabled) {
var _api$uiControlRegistr;
api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 || _api$uiControlRegistr.actions.register(getRuleQuickInsertComponents({
api: api
}));
}
return {
name: 'rule',
nodes: function nodes() {
return [{
name: 'rule',
node: isExperimentEnabled('platform_editor_lovability_dividers_attributes') ? ruleWithAttrs : fg('platform_editor_adf_with_localid') ? ruleWithLocalId : rule
}];
},
actions: {
insertHorizontalRule: insertHorizontalRule(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)
},
pmPlugins: function pmPlugins() {
return [].concat(_toConsumableArray(isExperimentEnabled('platform_editor_lovability_dividers') ? [{
name: 'ruleNodeView',
plugin: ruleNodeViewPlugin
}] : []), [{
name: 'ruleInputRule',
plugin: function plugin(_ref2) {
var _api$analytics2;
var schema = _ref2.schema;
return inputRulePlugin(schema, api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions);
}
}, {
name: 'ruleKeymap',
plugin: function plugin() {
var _api$analytics3;
return keymapPlugin(api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions);
}
}]);
},
pluginsOptions: _objectSpread(_objectSpread({}, isExperimentEnabled('platform_editor_lovability_dividers') ? {
floatingToolbar: function floatingToolbar(state, intl) {
return getToolbarConfig(state, intl, api);
}
} : {}), {}, {
quickInsert: isRegisteredSlashCommandEnabled ? undefined : function (_ref3) {
var formatMessage = _ref3.formatMessage;
return [{
id: 'rule',
title: formatMessage(messages.horizontalRule),
description: formatMessage(messages.horizontalRuleDescription),
keywords: ['horizontal', 'rule', 'line', 'hr'],
priority: 1200,
keyshortcut: '---',
icon: function icon() {
return /*#__PURE__*/React.createElement(IconDivider, null);
},
action: function action(insert, state, source) {
var _api$analytics4;
var tr = insert(state.schema.nodes.rule.createChecked());
api === null || api === void 0 || (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 || _api$analytics4.actions.attachAnalyticsEvent({
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.DOCUMENT,
actionSubjectId: ACTION_SUBJECT_ID.DIVIDER,
attributes: {
inputMethod: source !== null && source !== void 0 ? source : INPUT_METHOD.QUICK_INSERT
},
eventType: EVENT_TYPE.TRACK
})(tr);
return tr;
}
}];
}
})
};
};