UNPKG

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

Version:

Tasks and decisions plugin for @atlaskit/editor-core

143 lines (141 loc) 7.18 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import { ReplaceAroundStep, ReplaceStep } from '@atlaskit/editor-prosemirror/transform'; import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure'; function getAffectedTaskListsFromTransactions(transactions, doc, schema) { var taskList = schema.nodes.taskList; if (!taskList) { return new Map(); } var result = new Map(); var _iterator = _createForOfIteratorHelper(transactions), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var tr = _step.value; var _iterator2 = _createForOfIteratorHelper(tr.steps), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var step = _step2.value; // ReplaceStep and ReplaceAroundStep both have from/to — other step types are skipped. if (!(step instanceof ReplaceStep) && !(step instanceof ReplaceAroundStep)) { continue; } // Check both the start and end of each changed range, mapped to post-transaction positions. for (var _i = 0, _arr = [step.from, step.to]; _i < _arr.length; _i++) { var rawPos = _arr[_i]; var mappedPos = Math.min(tr.mapping.map(rawPos), doc.content.size - 1); var $pos = doc.resolve(mappedPos); // Walk ancestors from inner to outer, recording the outermost taskList node. // Once we find a taskList and then exit list structure (hit a non-taskList ancestor), // break early — prevents container nodes (e.g. panel) from causing us to // return an outer taskList in a different structural context. var rootTaskListPos = null; var rootTaskListNode = null; for (var depth = $pos.depth; depth >= 0; depth--) { var node = $pos.node(depth); if (node.type === taskList) { rootTaskListPos = $pos.before(depth); rootTaskListNode = node; } else if (rootTaskListNode !== null && node.type !== schema.nodes.taskItem) { // We've exited the taskList structure — stop walking. break; } } if (rootTaskListPos !== null && rootTaskListNode !== null) { result.set(rootTaskListPos, rootTaskListNode); } } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return result; } export function applyTaskListNormalisationFixes(_ref) { var doc = _ref.doc, schema = _ref.schema, tr = _ref.tr, transactions = _ref.transactions; var _schema$nodes = schema.nodes, taskList = _schema$nodes.taskList, taskItem = _schema$nodes.taskItem; if (!taskList || !taskItem) { return tr; } var affectedTaskLists = getAffectedTaskListsFromTransactions(transactions, doc, schema); if (affectedTaskLists.size === 0) { return tr; } // Sort by position descending so we process deeper/later positions first, // preventing earlier insertions from shifting later positions. var sortedEntries = _toConsumableArray(affectedTaskLists.entries()).sort(function (_ref2, _ref3) { var _ref4 = _slicedToArray(_ref2, 1), a = _ref4[0]; var _ref5 = _slicedToArray(_ref3, 1), b = _ref5[0]; return b - a; }); var _iterator3 = _createForOfIteratorHelper(sortedEntries), _step3; try { var _loop = function _loop() { var _step3$value = _slicedToArray(_step3.value, 1), taskListPos = _step3$value[0]; // Re-resolve the taskList node from the current transaction doc (post-operation state). var mappedTaskListPos = tr.mapping.map(taskListPos); var currentTaskListNode = tr.doc.nodeAt(mappedTaskListPos); if (!currentTaskListNode) { return 1; // continue } if (!expValEqualsNoExposure('platform_editor_flexible_list_indentation', 'isEnabled', true)) { // Collect positions of all taskList nodes (at any depth) whose direct children // include a taskList — this is the invalid structure. The taskList schema requires // taskItem as children, not nested taskLists directly. // Process in reverse order so higher-position insertions don't shift lower positions. var invalidTaskListPositions = []; currentTaskListNode.descendants(function (node, offsetPos) { if (node.type === taskList) { // A taskList as the FIRST child of another taskList is invalid — it means a // delete or paste removed the leading taskItem, leaving a bare nested list. // A taskList that follows a taskItem is valid (that's normal indentation). var firstChild = node.firstChild; if (firstChild && firstChild.type === taskList) { var pos = mappedTaskListPos + 1 + offsetPos + 1; invalidTaskListPositions.push(pos); } } return true; }); // Process in reverse (highest positions first). for (var i = invalidTaskListPositions.length - 1; i >= 0; i--) { var remappedPos = tr.mapping.map(invalidTaskListPositions[i]); var emptyTaskItem = taskItem.createAndFill(); if (emptyTaskItem) { tr.insert(remappedPos, emptyTaskItem); } } } }; for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { if (_loop()) continue; } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } return tr; }