UNPKG

@atlaskit/editor-plugin-extension

Version:

editor-plugin-extension plugin for @atlaskit/editor-core

81 lines (79 loc) 4.36 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 { uuid } from '@atlaskit/adf-schema'; import { SetAttrsStep } from '@atlaskit/adf-schema/steps'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { stepAddsOneOf } from '@atlaskit/editor-common/utils'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; var pluginKey = new PluginKey('extensionUniqueIdPlugin'); var createPlugin = function createPlugin() { return new SafePlugin({ appendTransaction: function appendTransaction(transactions, _oldState, newState) { var tr = newState.tr; var selectionBookmark = tr.selection.getBookmark(); var modified = false; var _newState$schema$node = newState.schema.nodes, extension = _newState$schema$node.extension, bodiedExtension = _newState$schema$node.bodiedExtension, inlineExtension = _newState$schema$node.inlineExtension, multiBodiedExtension = _newState$schema$node.multiBodiedExtension; var extensionTypes = new Set([extension, bodiedExtension, inlineExtension, multiBodiedExtension]); var idsObserved = new Set(); transactions.forEach(function (transaction) { if (!transaction.docChanged) { return; } var isAddingExtension = transaction.steps.some(function (step) { var _step$attrs; return stepAddsOneOf(step, extensionTypes) || // There are instances where the localId will be reset to null on publish due to extension // not existing in the Storage format (eg. Legacy Content Extensions) or not having a localId // eslint-disable-next-line @atlaskit/platform/no-preconditioning step instanceof SetAttrsStep && // @ts-expect-error Bad ProseMirror step types ((_step$attrs = step.attrs) === null || _step$attrs === void 0 ? void 0 : _step$attrs.localId) === null; }); if (isAddingExtension) { // Can't simply look at changed nodes, as we could be adding an extension newState.doc.descendants(function (node, pos) { var localId = node.attrs.localId; // Dealing with an extension - make sure it's a unique ID if (!!node.type && extensionTypes.has(node.type)) { if (localId && !idsObserved.has(localId)) { idsObserved.add(localId); // Also add a localId if it happens to not have one, } else if (!localId || idsObserved.has(localId)) { modified = true; tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, { localId: uuid.generate() })); } /** * If it's a multiBodiedExtension or bodiedExtension, we'll need to keep digging; * since we can have more extension nodes within the contents of that */ if ([multiBodiedExtension, bodiedExtension].includes(node.type)) { return true; } return false; } /** * Otherwise continue traversing, we can encounter extensions nested in * expands/bodiedExtensions */ return true; }); } }); if (modified) { // We want to restore to the original selection but w/o applying the mapping // @see https://github.com/ProseMirror/prosemirror/issues/645 return tr.setSelection(selectionBookmark.resolve(tr.doc)); } return; }, key: pluginKey }); }; export { createPlugin };