UNPKG

@atlaskit/editor-plugin-fragment

Version:

Fragment plugin for @atlaskit/editor-core

143 lines (138 loc) 7.07 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.createPlugin = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _adfSchema = require("@atlaskit/adf-schema"); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _state = require("@atlaskit/editor-prosemirror/state"); 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; } 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; } /** * This plugin ensures that certain nodes (such as tables, and various extension ones) * have a unique `localId` attribute value for `fragment` marks. * It also ensures the preservation of these IDs when nodes are being cut-and-pasted * around the document. * * The implementation has been _heavily_ borrowed from * - packages/editor/editor-core/src/plugins/table/pm-plugins/table-local-id.ts */ var pluginKey = new _state.PluginKey('fragmentMarkConsistencyPlugin'); var getNodesSupportingFragmentMark = function getNodesSupportingFragmentMark(schema) { var _schema$nodes = schema.nodes, table = _schema$nodes.table, extension = _schema$nodes.extension, bodiedExtension = _schema$nodes.bodiedExtension, inlineExtension = _schema$nodes.inlineExtension; return [table, extension, bodiedExtension, inlineExtension]; }; /** * Ensures presence of `fragment` mark on certain node types and the uniqueness of their `localId` attributes */ var createPlugin = exports.createPlugin = function createPlugin(dispatch) { return new _safePlugin.SafePlugin({ key: pluginKey, appendTransaction: function appendTransaction(transactions, _oldState, newState) { var modified = false; var tr = newState.tr; var fragment = newState.schema.marks.fragment; var supportedNodeTypes = getNodesSupportingFragmentMark(newState.schema); var addedSupportedNodes = new Set(); var addedSupportedNodesPos = new Map(); var localIds = new Set(); transactions.forEach(function (transaction) { if (!transaction.docChanged) { return; } // Don't interfere with cut as it clashes with fixTables & we don't need // to handle any extra cut cases in this plugin var uiEvent = transaction.getMeta('uiEvent'); if (uiEvent === 'cut') { return; } var changedNodes = (0, _utils.getChangedNodes)(transaction); var _iterator = _createForOfIteratorHelper(changedNodes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var node = _step.value.node; if (!supportedNodeTypes.includes(node.type)) { continue; } addedSupportedNodes.add(node); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }); if (!addedSupportedNodes.size) { return; } // Get existing fragment marks localIds on the page newState.doc.descendants(function (node, pos) { if (addedSupportedNodes.has(node)) { addedSupportedNodesPos.set(node, pos); return true; } if (!supportedNodeTypes.includes(node.type)) { return true; } var existingFragmentMark = node.marks.find(function (mark) { return mark.type === fragment; }); if (!existingFragmentMark) { // continue traversing return true; } localIds.add(existingFragmentMark.attrs.localId); return true; }); // If an added node has localId that collides with existing node, generate new localId var _iterator2 = _createForOfIteratorHelper(addedSupportedNodes), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var node = _step2.value; var pos = addedSupportedNodesPos.get(node); if (pos === undefined) { continue; } var existingFragmentMark = node.marks.find(function (mark) { return mark.type === fragment; }); if (!existingFragmentMark) { continue; } if (localIds.has(existingFragmentMark.attrs.localId)) { tr.setNodeMarkup(pos, undefined, node.attrs, node.marks.map(function (mark) { if (mark.type !== fragment) { return mark; } var fragmentMark = fragment.create(_objectSpread(_objectSpread({}, mark.attrs), {}, { localId: _adfSchema.uuid.generate(), name: null })); return fragmentMark; })); modified = true; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } if (modified) { return tr; } return; } }); };