@atlaskit/editor-plugin-extension
Version:
editor-plugin-extension plugin for @atlaskit/editor-core
87 lines (84 loc) • 4.63 kB
JavaScript
;
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 _steps = require("@atlaskit/adf-schema/steps");
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; }
var pluginKey = new _state.PluginKey('extensionUniqueIdPlugin');
var createPlugin = exports.createPlugin = function createPlugin() {
return new _safePlugin.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 (0, _utils.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 _steps.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: _adfSchema.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
});
};