@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
44 lines (41 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.encodeCharacterWithMarks = void 0;
/** U+0000 cannot occur in ProseMirror text content, so a token can never be ambiguous. */
var MARK_KEY_SEPARATOR = "\0";
var buildMarkKey = function buildMarkKey(marks) {
return marks.map(function (mark) {
var _mark$attrs;
var attrs = (_mark$attrs = mark.attrs) !== null && _mark$attrs !== void 0 ? _mark$attrs : {};
var attrParts = Object.keys(attrs).sort().map(function (key) {
var _attrs$key;
return "".concat(key, "=").concat(JSON.stringify((_attrs$key = attrs[key]) !== null && _attrs$key !== void 0 ? _attrs$key : null));
});
return attrParts.length > 0 ? "".concat(mark.type.name, "(").concat(attrParts.join(','), ")") : mark.type.name;
}).sort().join('&');
};
/**
* Keyed on array identity: `prosemirror-changeset` passes the same `child.marks` reference for
* every character in a text node, so this collapses the key building to once per node.
*/
var markKeyCache = new WeakMap();
/**
* Folds a character's marks into its `prosemirror-changeset` token. The library default returns
* only the char code, so marked and unmarked text tokenise identically and a formatting-only edit
* is invisible to the changeset.
*
* Marks and attrs are sorted so the token is order-independent.
*/
var encodeCharacterWithMarks = exports.encodeCharacterWithMarks = function encodeCharacterWithMarks(char, marks) {
if (marks.length === 0) {
return char;
}
var markKey = markKeyCache.get(marks);
if (markKey === undefined) {
markKey = buildMarkKey(marks);
markKeyCache.set(marks, markKey);
}
return "".concat(char).concat(MARK_KEY_SEPARATOR).concat(markKey);
};