UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

122 lines 6.03 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 { codeBlock } from '@atlaskit/adf-schema'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { blockTypeMessages } from '@atlaskit/editor-common/messages'; import { IconCode } from '@atlaskit/editor-common/quick-insert'; import { fg } from '@atlaskit/platform-feature-flags'; import { createInsertCodeBlockTransaction, insertCodeBlockWithAnalytics } from './editor-commands'; import { codeBlockAutoFullStopTransformPlugin } from './pm-plugins/codeBlockAutoFullStopTransformPlugin'; import { codeBlockCopySelectionPlugin } from './pm-plugins/codeBlockCopySelectionPlugin'; import ideUX from './pm-plugins/ide-ux'; import { createCodeBlockInputRule } from './pm-plugins/input-rule'; import keymap from './pm-plugins/keymaps'; import { createPlugin } from './pm-plugins/main'; import refreshBrowserSelectionOnChange from './pm-plugins/refresh-browser-selection'; import { getToolbarConfig } from './pm-plugins/toolbar'; var codeBlockPlugin = function codeBlockPlugin(_ref) { var _api$featureFlags; var options = _ref.config, api = _ref.api; var isNestingInQuoteSupported = (api === null || api === void 0 || (_api$featureFlags = api.featureFlags) === null || _api$featureFlags === void 0 || (_api$featureFlags = _api$featureFlags.sharedState.currentState()) === null || _api$featureFlags === void 0 ? void 0 : _api$featureFlags.nestMediaAndCodeblockInQuote) || fg('editor_nest_media_and_codeblock_in_quotes_jira'); return { name: 'codeBlock', nodes: function nodes() { return [{ name: 'codeBlock', node: codeBlock }]; }, pmPlugins: function pmPlugins() { return [{ name: 'codeBlock', plugin: function plugin(_ref2) { var getIntl = _ref2.getIntl; return createPlugin(_objectSpread(_objectSpread({}, options), {}, { getIntl: getIntl, api: api })); } }, { name: 'codeBlockInputRule', plugin: function plugin(_ref3) { var _api$analytics; var schema = _ref3.schema; return createCodeBlockInputRule(schema, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, isNestingInQuoteSupported); } }, { name: 'codeBlockIDEKeyBindings', plugin: function plugin() { return ideUX(api); } }, { name: 'codeBlockKeyMap', plugin: function plugin(_ref4) { var schema = _ref4.schema; return keymap(schema); } }, { name: 'codeBlockCopySelection', plugin: function plugin() { return codeBlockCopySelectionPlugin(); } }, { name: 'codeBlockAutoFullStopTransform', plugin: function plugin() { return codeBlockAutoFullStopTransformPlugin(); } }]; }, // Workaround for a firefox issue where dom selection is off sync // https://product-fabric.atlassian.net/browse/ED-12442 onEditorViewStateUpdated: function onEditorViewStateUpdated(props) { refreshBrowserSelectionOnChange(props.originalTransaction, props.newEditorState); }, actions: { /* * Function will insert code block at current selection if block is empty or below current selection and set focus on it. */ insertCodeBlock: function insertCodeBlock(inputMethod) { var _api$analytics2; return insertCodeBlockWithAnalytics(inputMethod, api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions, isNestingInQuoteSupported); } }, pluginsOptions: { quickInsert: function quickInsert(_ref5) { var formatMessage = _ref5.formatMessage; return [{ id: 'codeblock', title: formatMessage(blockTypeMessages.codeblock), description: formatMessage(blockTypeMessages.codeblockDescription), keywords: ['code block'], priority: 700, keyshortcut: '```', icon: function icon() { return /*#__PURE__*/React.createElement(IconCode, null); }, action: function action(_insert, state) { var _api$analytics3; var tr = createInsertCodeBlockTransaction({ state: state, isNestingInQuoteSupported: isNestingInQuoteSupported }); api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 || _api$analytics3.actions.attachAnalyticsEvent({ action: ACTION.INSERTED, actionSubject: ACTION_SUBJECT.DOCUMENT, actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK, attributes: { inputMethod: INPUT_METHOD.QUICK_INSERT }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } }]; }, floatingToolbar: getToolbarConfig(options === null || options === void 0 ? void 0 : options.allowCopyToClipboard, api) } }; }; export default codeBlockPlugin;