UNPKG

@atlaskit/editor-plugin-tasks-and-decisions

Version:

Tasks and decisions plugin for @atlaskit/editor-core

353 lines (352 loc) 17.9 kB
/* tasksAndDecisionsPlugin.tsx generated by @compiled/babel-plugin v0.39.1 */ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import "./tasksAndDecisionsPlugin.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { decisionList, taskList } from '@atlaskit/adf-schema'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_TASK_LIST_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION_RANK, TRANSFORM_STRUCTURE_DECISION_MENU_ITEM } from '@atlaskit/editor-common/block-menu'; import { MAX_INDENTATION_LEVEL } from '@atlaskit/editor-common/indentation'; import { toolbarInsertBlockMessages as insertBlockMessages } from '@atlaskit/editor-common/messages'; import { IconAction, IconDecision } from '@atlaskit/editor-common/quick-insert'; import { useSharedPluginStateSelector } from '@atlaskit/editor-common/use-shared-plugin-state-selector'; import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { taskItemNodeSpec, blockTaskItemNodeSpec } from './nodeviews/taskItemNodeSpec'; import { decisionItemSpecWithFixedToDOM } from './nodeviews/toDOM-fixes/decisionItem'; import { closeRequestEditPopupAt, getCurrentIndentLevel, getTaskItemIndex, isInsideTask } from './pm-plugins/helpers'; // Ignored via go/ees005 // eslint-disable-next-line import/no-named-as-default import inputRulePlugin from './pm-plugins/input-rules'; import { getListTypes, insertTaskDecisionAction, insertTaskDecisionCommand, setProvider as _setProvider } from './pm-plugins/insert-commands'; import keymap, { getIndentCommand, getUnindentCommand } from './pm-plugins/keymaps'; import { createPlugin } from './pm-plugins/main'; import { stateKey as taskPluginKey } from './pm-plugins/plugin-key'; import { toggleTaskList as _toggleTaskList } from './pm-plugins/toggle-tasklist-commands'; import { DecisionListBlockMenuItem } from './ui/DecisionListBlockMenuItem/DecisionListBlockMenuItem'; import { RequestToEditPopup } from './ui/Task/RequestToEditPopup'; import { TaskListBlockMenuItem } from './ui/TaskListBlockMenuItem/TaskListBlockMenuItem'; import { getTasksAndDecisionsToolbarComponents } from './ui/toolbar-components'; import ToolbarDecision from './ui/ToolbarDecision'; import ToolbarTask from './ui/ToolbarTask'; var taskDecisionToolbarGroupStyles = null; var TASK_LIST_NODE_NAME = 'taskList'; var DECISION_LIST_NODE_NAME = 'decisionList'; var addItem = function addItem(insert, listType, schema) { return function (_ref) { var listLocalId = _ref.listLocalId, itemLocalId = _ref.itemLocalId; var _getListTypes = getListTypes(listType, schema), list = _getListTypes.list, item = _getListTypes.item; return insert(list.createChecked({ localId: listLocalId }, item.createChecked({ localId: itemLocalId }))); }; }; function ContentComponent(_ref2) { var editorView = _ref2.editorView, popupsMountPoint = _ref2.popupsMountPoint, popupsBoundariesElement = _ref2.popupsBoundariesElement, popupsScrollableElement = _ref2.popupsScrollableElement, dependencyApi = _ref2.dependencyApi; var openRequestToEditPopupAt = useSharedPluginStateSelector(dependencyApi, 'taskDecision.openRequestToEditPopupAt'); var hasEditPermission = useSharedPluginStateSelector(dependencyApi, 'taskDecision.hasEditPermission'); if (!editorView || hasEditPermission || !openRequestToEditPopupAt) { return null; } var domAtPos = editorView.domAtPos.bind(editorView); // eslint-disable-next-line @atlaskit/editor/no-as-casting var element = findDomRefAtPos(openRequestToEditPopupAt, domAtPos); var handleOnClose = function handleOnClose() { closeRequestEditPopupAt(editorView); editorView.focus(); }; return /*#__PURE__*/React.createElement(RequestToEditPopup, { key: openRequestToEditPopupAt, api: dependencyApi, editorView: editorView, mountTo: popupsMountPoint, boundariesElement: popupsBoundariesElement, scrollableElement: popupsScrollableElement, element: element, onClose: handleOnClose }); } export var tasksAndDecisionsPlugin = function tasksAndDecisionsPlugin(_ref3) { var _api$analytics2, _api$analytics3, _api$analytics4; var _ref3$config = _ref3.config, _ref3$config2 = _ref3$config === void 0 ? {} : _ref3$config, allowNestedTasks = _ref3$config2.allowNestedTasks, consumeTabs = _ref3$config2.consumeTabs, useLongPressSelection = _ref3$config2.useLongPressSelection, _ref3$config2$hasEdit = _ref3$config2.hasEditPermission, hasEditPermission = _ref3$config2$hasEdit === void 0 ? true : _ref3$config2$hasEdit, hasRequestedEditPermission = _ref3$config2.hasRequestedEditPermission, quickInsertActionDescription = _ref3$config2.quickInsertActionDescription, requestToEditContent = _ref3$config2.requestToEditContent, taskDecisionProvider = _ref3$config2.taskDecisionProvider, taskPlaceholder = _ref3$config2.taskPlaceholder, allowBlockTaskItem = _ref3$config2.allowBlockTaskItem, api = _ref3.api; var getIdentifierProvider = function getIdentifierProvider() { var _api$contextIdentifie; return api === null || api === void 0 || (_api$contextIdentifie = api.contextIdentifier) === null || _api$contextIdentifie === void 0 || (_api$contextIdentifie = _api$contextIdentifie.sharedState.currentState()) === null || _api$contextIdentifie === void 0 ? void 0 : _api$contextIdentifie.contextIdentifierProvider; }; var previousTaskAndDecisionProvider; var isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar); if (isToolbarAIFCEnabled) { var _api$toolbar; api === null || api === void 0 || (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 || _api$toolbar.actions.registerComponents(getTasksAndDecisionsToolbarComponents({ api: api })); } if (taskDecisionProvider) { taskDecisionProvider.then(function (provider) { api === null || api === void 0 || api.core.actions.execute(function (_ref4) { var tr = _ref4.tr; return _setProvider(provider)(tr); }); }); } 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_TASK_LIST_MENU_ITEM.key, parent: { type: 'block-menu-section', key: TRANSFORM_STRUCTURE_MENU_SECTION.key, rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_TASK_LIST_MENU_ITEM.key] }, component: function component() { return /*#__PURE__*/React.createElement(TaskListBlockMenuItem, { api: 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(TASK_LIST_NODE_NAME)); } }, { type: 'block-menu-item', key: TRANSFORM_STRUCTURE_DECISION_MENU_ITEM.key, parent: { type: 'block-menu-section', key: TRANSFORM_STRUCTURE_MENU_SECTION.key, rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_DECISION_MENU_ITEM.key] }, component: function component() { return /*#__PURE__*/React.createElement(DecisionListBlockMenuItem, { api: api }); }, isHidden: function isHidden() { var _api$blockMenu3; return Boolean(api === null || api === void 0 || (_api$blockMenu3 = api.blockMenu) === null || _api$blockMenu3 === void 0 ? void 0 : _api$blockMenu3.actions.isTransformOptionDisabled(DECISION_LIST_NODE_NAME)); } }]); } return { name: 'taskDecision', nodes: function nodes() { return [{ name: 'decisionList', node: decisionList }, { name: 'decisionItem', node: decisionItemSpecWithFixedToDOM() }, { name: 'taskList', node: taskList }, { name: 'taskItem', node: taskItemNodeSpec() }].concat(_toConsumableArray(expValEquals('platform_editor_blocktaskitem_node_tenantid', 'isEnabled', true) && allowBlockTaskItem ? [{ name: 'blockTaskItem', node: blockTaskItemNodeSpec() }] : [])); }, getSharedState: function getSharedState(editorState) { if (!editorState) { return undefined; } var pluginState = taskPluginKey.getState(editorState); var indentLevel = getCurrentIndentLevel(editorState.selection) || 0; var itemIndex = getTaskItemIndex(editorState); return { focusedTaskItemLocalId: (pluginState === null || pluginState === void 0 ? void 0 : pluginState.focusedTaskItemLocalId) || null, isInsideTask: isInsideTask(editorState), indentDisabled: itemIndex === 0 || indentLevel >= MAX_INDENTATION_LEVEL, outdentDisabled: indentLevel <= 1, // hasEditPermission is assumed to be true if pluginState.hasEditPermission is undefined // this allows the default plugin state to initialise as true if the extra configuration is not provided hasEditPermission: (pluginState === null || pluginState === void 0 ? void 0 : pluginState.hasEditPermission) !== undefined ? pluginState === null || pluginState === void 0 ? void 0 : pluginState.hasEditPermission : true, requestToEditContent: pluginState === null || pluginState === void 0 ? void 0 : pluginState.requestToEditContent, hasRequestedEditPermission: pluginState === null || pluginState === void 0 ? void 0 : pluginState.hasRequestedEditPermission, taskDecisionProvider: pluginState === null || pluginState === void 0 ? void 0 : pluginState.taskDecisionProvider, openRequestToEditPopupAt: pluginState === null || pluginState === void 0 ? void 0 : pluginState.openRequestToEditPopupAt }; }, commands: { toggleTaskList: function toggleTaskList(targetType) { var _api$analytics; return _toggleTaskList(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(targetType); }, updateEditPermission: function updateEditPermission(hasEditPermission) { return function (_ref5) { var tr = _ref5.tr; return tr.setMeta(taskPluginKey, { hasEditPermission: hasEditPermission }); }; }, updateHasRequestedEditPermission: function updateHasRequestedEditPermission(hasRequestedEditPermission) { return function (_ref6) { var tr = _ref6.tr; return tr.setMeta(taskPluginKey, { hasRequestedEditPermission: hasRequestedEditPermission }); }; } }, actions: { insertTaskDecision: insertTaskDecisionCommand(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions, getIdentifierProvider), indentTaskList: getIndentCommand(api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions), outdentTaskList: getUnindentCommand(api === null || api === void 0 || (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : _api$analytics4.actions), setProvider: function () { var _setProvider2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(providerPromise) { var _api$core$actions$exe; var provider; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return providerPromise; case 2: provider = _context.sent; if (!(previousTaskAndDecisionProvider === provider || taskDecisionProvider === providerPromise)) { _context.next = 5; break; } return _context.abrupt("return", false); case 5: previousTaskAndDecisionProvider = provider; return _context.abrupt("return", (_api$core$actions$exe = api === null || api === void 0 ? void 0 : api.core.actions.execute(function (_ref7) { var tr = _ref7.tr; return _setProvider(provider)(tr); })) !== null && _api$core$actions$exe !== void 0 ? _api$core$actions$exe : false); case 7: case "end": return _context.stop(); } }, _callee); })); function setProvider(_x) { return _setProvider2.apply(this, arguments); } return setProvider; }() }, pmPlugins: function pmPlugins() { return [{ name: 'tasksAndDecisions', plugin: function plugin(_ref8) { var portalProviderAPI = _ref8.portalProviderAPI, eventDispatcher = _ref8.eventDispatcher, dispatch = _ref8.dispatch, getIntl = _ref8.getIntl; return createPlugin(portalProviderAPI, eventDispatcher, dispatch, api, getIntl, useLongPressSelection, hasEditPermission, hasRequestedEditPermission, requestToEditContent, taskPlaceholder); } }, { name: 'tasksAndDecisionsInputRule', plugin: function plugin(_ref9) { var _api$analytics5; var schema = _ref9.schema, featureFlags = _ref9.featureFlags; return inputRulePlugin(api === null || api === void 0 || (_api$analytics5 = api.analytics) === null || _api$analytics5 === void 0 ? void 0 : _api$analytics5.actions, getIdentifierProvider)(schema, featureFlags); } }, { name: 'tasksAndDecisionsKeyMap', plugin: function plugin(_ref0) { var schema = _ref0.schema; return keymap(schema, api, allowNestedTasks, consumeTabs); } } // Needs to be after "save-on-enter" ]; }, secondaryToolbarComponent: function secondaryToolbarComponent(_ref1) { var editorView = _ref1.editorView, disabled = _ref1.disabled; return /*#__PURE__*/React.createElement("div", { className: ax(["_1e0c1txw"]) }, /*#__PURE__*/React.createElement(ToolbarDecision, { editorView: editorView, isDisabled: disabled, isReducedSpacing: true, editorAPI: api }), /*#__PURE__*/React.createElement(ToolbarTask, { editorView: editorView, isDisabled: disabled, isReducedSpacing: true, editorAPI: api })); }, contentComponent: function contentComponent(_ref10) { var editorView = _ref10.editorView, dispatchAnalyticsEvent = _ref10.dispatchAnalyticsEvent, popupsMountPoint = _ref10.popupsMountPoint, popupsBoundariesElement = _ref10.popupsBoundariesElement, popupsScrollableElement = _ref10.popupsScrollableElement; if (!editorView) { return null; } return /*#__PURE__*/React.createElement(ContentComponent, { dependencyApi: api, editorView: editorView, dispatchAnalyticsEvent: dispatchAnalyticsEvent, popupsMountPoint: popupsMountPoint, popupsBoundariesElement: popupsBoundariesElement, popupsScrollableElement: popupsScrollableElement }); }, pluginsOptions: { quickInsert: function quickInsert(_ref11) { var formatMessage = _ref11.formatMessage; return [{ id: 'action', title: formatMessage(insertBlockMessages.action), description: quickInsertActionDescription !== null && quickInsertActionDescription !== void 0 ? quickInsertActionDescription : formatMessage(insertBlockMessages.actionDescription), priority: 100, keywords: ['checkbox', 'task', 'todo'], keyshortcut: '[]', icon: function icon() { return /*#__PURE__*/React.createElement(IconAction, null); }, action: function action(insert, state, source) { var _api$analytics6; return insertTaskDecisionAction(api === null || api === void 0 || (_api$analytics6 = api.analytics) === null || _api$analytics6 === void 0 ? void 0 : _api$analytics6.actions, getIdentifierProvider)(state, 'taskList', source !== null && source !== void 0 ? source : INPUT_METHOD.QUICK_INSERT, addItem(insert, 'taskList', state.schema)); } }, { id: 'decision', title: formatMessage(insertBlockMessages.decision), description: formatMessage(insertBlockMessages.decisionDescription), priority: 900, keyshortcut: '<>', icon: function icon() { return /*#__PURE__*/React.createElement(IconDecision, null); }, action: function action(insert, state, source) { var _api$analytics7; return insertTaskDecisionAction(api === null || api === void 0 || (_api$analytics7 = api.analytics) === null || _api$analytics7 === void 0 ? void 0 : _api$analytics7.actions, getIdentifierProvider)(state, 'decisionList', source !== null && source !== void 0 ? source : INPUT_METHOD.QUICK_INSERT, addItem(insert, 'decisionList', state.schema)); } }]; } } }; };