@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
58 lines (54 loc) • 2.85 kB
JavaScript
import { getBaseNodeTypeName } from '@atlaskit/editor-common/utils/node-type-utils';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { getDiffableAttrNames } from '../decorations/utils/diffableAttrs';
import { getComparableExcerptIncludeAttrs, isExcerptInclude } from '../decorations/utils/excerptIncludeAttrs';
import { encodeCharacterWithMarks } from './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`.
export var attrAwareTokenEncoder = {
encodeCharacter: function encodeCharacter(char, marks) {
return 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 = fg('platform_editor_normalize_excerpt_diff') && node.type.name === 'extension' && isExcerptInclude(node.attrs) ? getComparableExcerptIncludeAttrs(node.attrs) : node.attrs;
// Normalise variants (e.g. `panel_c1` → `panel`) so `diffableAttrs` needs one entry.
var attrNames = getDiffableAttrNames(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;
}