@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
26 lines (24 loc) • 1.1 kB
JavaScript
import { encodeCharacterWithMarks } from './encodeCharacterWithMarks';
/** Mirrors the library's private (unexported) `typeID` so node-end tokens match. */
const typeID = type => {
const cache = type.schema.cached.changeSetIDs || (type.schema.cached.changeSetIDs = Object.create(null));
let id = cache[type.name];
if (id == null) {
cache[type.name] = id = Object.keys(type.schema.nodes).indexOf(type.name) + 1;
}
return id;
};
/**
* The library default encoder plus mark folding on text characters.
*
* Deliberately narrower than `attrAwareTokenEncoder`: node-start tokens keep the library default
* and do not fold in diffable attributes, which `getAttrChangeRanges` already reports for every
* diff type. The NCS version-history path also uses `attrAwareTokenEncoder` so main diff calculation
* detects attribute-only node replacements.
*/
export const markAwareTokenEncoder = {
encodeCharacter: (char, marks) => encodeCharacterWithMarks(char, marks),
encodeNodeStart: node => node.type.name,
encodeNodeEnd: node => -typeID(node.type),
compareTokens: (a, b) => a === b
};