@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
63 lines (59 loc) • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.attrAwareTokenEncoder = void 0;
var _nodeTypeUtils = require("@atlaskit/editor-common/utils/node-type-utils");
var _fg = require("@atlaskit/platform-feature-flags/fg");
var _diffableAttrs = require("../decorations/utils/diffableAttrs");
var _excerptIncludeAttrs = require("../decorations/utils/excerptIncludeAttrs");
var _encodeCharacterWithMarks = require("./encodeCharacterWithMarks");
/**
* Attribute-aware token encoder for `prosemirror-changeset`. The default encoder
* reduces a node to `node.type.name`, so an attribute-only change (e.g. a table
* cell recolour) tokenises identically on both sides and goes undetected. This
* folds the diffable attributes (from the shared `diffableAttrs` map) into the
* open token so such changes register.
*/
// Stable composite token; attr names are pre-ordered by the caller.
var encodeNodeWithAttrs = function encodeNodeWithAttrs(nodeTypeName, attrs, attrNames) {
var parts = attrNames.map(function (name) {
var _attrs$name;
return "".concat(name, "=").concat(JSON.stringify((_attrs$name = attrs[name]) !== null && _attrs$name !== void 0 ? _attrs$name : null));
});
return "".concat(nodeTypeName, "|").concat(parts.join('|'));
};
// Like the library default, but node types with a `diffableAttrs` rule fold their
// attrs into the open token, and text characters fold in their marks. Node-end is
// unchanged, hence `string | number`.
var attrAwareTokenEncoder = exports.attrAwareTokenEncoder = {
encodeCharacter: function encodeCharacter(char, marks) {
return (0, _encodeCharacterWithMarks.encodeCharacterWithMarks)(char, marks);
},
encodeNodeStart: function encodeNodeStart(node) {
// A one-sided reference is not comparable from an isolated token. Attribute-step comparison
// checks the reference when both versions have one.
var attrs = (0, _fg.fg)('platform_editor_normalize_excerpt_diff') && node.type.name === 'extension' && (0, _excerptIncludeAttrs.isExcerptInclude)(node.attrs) ? (0, _excerptIncludeAttrs.getComparableExcerptIncludeAttrs)(node.attrs) : node.attrs;
// Normalise variants (e.g. `panel_c1` → `panel`) so `diffableAttrs` needs one entry.
var attrNames = (0, _diffableAttrs.getDiffableAttrNames)((0, _nodeTypeUtils.getBaseNodeTypeName)(node.type), attrs !== null && attrs !== void 0 ? attrs : {});
if (attrNames && attrNames.length > 0) {
return encodeNodeWithAttrs(node.type.name, attrs, attrNames);
}
return node.type.name;
},
encodeNodeEnd: function encodeNodeEnd(node) {
return -typeID(node.type);
},
compareTokens: function compareTokens(a, b) {
return a === b;
}
};
// Mirrors the library's private (unexported) `typeID` so node-end tokens match.
function typeID(type) {
var cache = type.schema.cached.changeSetIDs || (type.schema.cached.changeSetIDs = Object.create(null));
var id = cache[type.name];
if (id == null) {
cache[type.name] = id = Object.keys(type.schema.nodes).indexOf(type.name) + 1;
}
return id;
}