UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

148 lines 7.19 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, codeBlockWithLocalId } from '@atlaskit/adf-schema'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_CODE_BLOCK_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION_RANK } from '@atlaskit/editor-common/block-menu'; import { blockTypeMessages } from '@atlaskit/editor-common/messages'; import { IconCode } from '@atlaskit/editor-common/quick-insert'; import { fg } from '@atlaskit/platform-feature-flags'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { createInsertCodeBlockTransaction, insertCodeBlockWithAnalytics } from './editor-commands'; import { codeBlockAutoFullStopTransformPlugin } from './pm-plugins/codeBlockAutoFullStopTransformPlugin'; import { codeBlockCopySelectionPlugin, copySelectionPluginKey } 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'; import { createCodeBlockMenuItem } from './ui/CodeBlockMenuItem'; var CODE_BLOCK_NODE_NAME = 'codeBlock'; var codeBlockPlugin = function codeBlockPlugin(_ref) { var options = _ref.config, api = _ref.api; if (editorExperiment('platform_editor_block_menu', true)) { var _api$blockMenu; api === null || api === void 0 || (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 || _api$blockMenu.actions.registerBlockMenuComponents([{ type: 'block-menu-item', key: TRANSFORM_STRUCTURE_CODE_BLOCK_MENU_ITEM.key, parent: { type: 'block-menu-section', key: TRANSFORM_STRUCTURE_MENU_SECTION.key, rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_CODE_BLOCK_MENU_ITEM.key] }, component: createCodeBlockMenuItem(api), isHidden: function isHidden() { var _api$blockMenu2; return Boolean(api === null || api === void 0 || (_api$blockMenu2 = api.blockMenu) === null || _api$blockMenu2 === void 0 ? void 0 : _api$blockMenu2.actions.isTransformOptionDisabled(CODE_BLOCK_NODE_NAME)); } }]); } return { name: 'codeBlock', nodes: function nodes() { return [{ name: 'codeBlock', node: fg('platform_editor_adf_with_localid') ? codeBlockWithLocalId : codeBlock }]; }, getSharedState: function getSharedState(state) { if (!state) { return undefined; } return { copyButtonHoverNode: copySelectionPluginKey.getState(state).codeBlockNode }; }, 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); } }, { 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); } }, 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, source) { var _api$analytics3; var tr = createInsertCodeBlockTransaction({ state: state }); 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: source || INPUT_METHOD.QUICK_INSERT }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } }]; }, floatingToolbar: getToolbarConfig(options === null || options === void 0 ? void 0 : options.allowCopyToClipboard, api, options === null || options === void 0 ? void 0 : options.overrideLanguageName) } }; }; export default codeBlockPlugin;