UNPKG

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

Version:

Tasks and decisions plugin for @atlaskit/editor-core

120 lines (119 loc) 6.58 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.inputRulePlugin = exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _analytics = require("@atlaskit/editor-common/analytics"); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _state = require("@atlaskit/editor-prosemirror/state"); var _utils2 = require("@atlaskit/editor-prosemirror/utils"); var _prosemirrorInputRules = require("@atlaskit/prosemirror-input-rules"); var _insertCommands = require("./insert-commands"); 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 createListRule = function createListRule(editorAnalyticsAPI, getContextIdentifierProvider) { return function (regex, listType, itemAttrs) { return (0, _utils.createRule)(regex, function (state, _match, start, end) { var paragraph = state.schema.nodes.paragraph; var _getListTypes = (0, _insertCommands.getListTypes)(listType, state.schema), list = _getListTypes.list; var $end = state.doc.resolve(end); var $endOfParent = state.doc.resolve($end.after()); // Only allow creating list in nodes that support them. // Parent must be a paragraph as we don't want this applying to headings if ($end.parent.type !== paragraph || !(0, _utils2.canInsert)($endOfParent, list.createAndFill())) { return null; } var insertTr = (0, _insertCommands.insertTaskDecisionAction)(editorAnalyticsAPI, getContextIdentifierProvider)(state, listType, _analytics.INPUT_METHOD.FORMATTING, addItem(start, end), undefined, undefined, itemAttrs); return insertTr; }); }; }; var isCursorInsideList = function isCursorInsideList($pos) { var _$pos$node; return ((_$pos$node = $pos.node($pos.depth - 1)) === null || _$pos$node === void 0 ? void 0 : _$pos$node.type.name) === 'listItem'; }; var processShortcutForNestedTask = function processShortcutForNestedTask(content, $from, tr, list, item, listLocalId, itemLocalId, itemAttrs) { //Extracting the content into the 'contentWithoutShortcut' from 'content' after removing the keyboard shortcut text, i.e., '[] '. var contentWithoutShortcut = content.cut($from.pos - $from.start(), content.size); tr.insert($from.after(), list.create({ localId: listLocalId }, [item.create(_objectSpread({ localId: itemLocalId }, itemAttrs), contentWithoutShortcut)])).setSelection(new _state.TextSelection(tr.doc.resolve($from.after()))).delete($from.start(), $from.end()); }; var addItem = function addItem(start, end) { return function (_ref) { var tr = _ref.tr, state = _ref.state, list = _ref.list, item = _ref.item, listLocalId = _ref.listLocalId, itemLocalId = _ref.itemLocalId, itemAttrs = _ref.itemAttrs; var $from = state.selection.$from, schema = state.schema; var hardBreak = schema.nodes.hardBreak; var content = $from.node($from.depth).content; var shouldBreakNode = false; content.forEach(function (node, offset) { if (node.type === hardBreak && offset < start) { shouldBreakNode = true; } }); if (!shouldBreakNode) { if (isCursorInsideList($from)) { processShortcutForNestedTask(content, $from, tr, list, item, listLocalId, itemLocalId, itemAttrs); return tr; } tr.replaceRangeWith($from.before(), $from.after(), list.create({ localId: listLocalId }, [item.create(_objectSpread({ localId: itemLocalId }, itemAttrs), content)])).delete(start + 1, end + 1).setSelection(new _state.TextSelection(tr.doc.resolve(start + 1))); return tr; } var depthAdjustment = (0, _insertCommands.changeInDepth)($from, tr.selection.$from); tr.split($from.pos).setSelection(new _state.NodeSelection(tr.doc.resolve($from.pos + 1))).replaceSelectionWith(list.create({ localId: listLocalId }, [item.create(_objectSpread({ localId: itemLocalId }, itemAttrs), tr.doc.nodeAt($from.pos + 1).content)])).setSelection(new _state.TextSelection(tr.doc.resolve($from.pos + depthAdjustment))).delete(start, end + 1); return tr; }; }; var inputRulePlugin = exports.inputRulePlugin = function inputRulePlugin(editorAnalyticsAPI, getContextIdentifierProvider) { return function (schema, featureFlags) { var rules = []; var _schema$nodes = schema.nodes, decisionList = _schema$nodes.decisionList, decisionItem = _schema$nodes.decisionItem, taskList = _schema$nodes.taskList, taskItem = _schema$nodes.taskItem; if (decisionList && decisionItem) { rules.push(createListRule(editorAnalyticsAPI, getContextIdentifierProvider)( // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp new RegExp("(^|".concat(_prosemirrorInputRules.leafNodeReplacementCharacter, ")\\<\\>\\s$")), 'decisionList')); } if (taskList && taskItem) { rules.push(createListRule(editorAnalyticsAPI, getContextIdentifierProvider)( // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp new RegExp("(^|".concat(_prosemirrorInputRules.leafNodeReplacementCharacter, ")\\[\\]\\s$")), 'taskList')); rules.push(createListRule(editorAnalyticsAPI, getContextIdentifierProvider)( // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp new RegExp("(^|".concat(_prosemirrorInputRules.leafNodeReplacementCharacter, ")\\[x\\]\\s$")), 'taskList', { state: 'DONE' })); } return new _safePlugin.SafePlugin((0, _prosemirrorInputRules.createPlugin)('tasks-and-decisions', rules, { isBlockNodeRule: true })); }; }; var _default = exports.default = inputRulePlugin;