export serializeNodeWithId so rrweb could serialize newly added nodes

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent cfc8798b53
commit 9cf2c4c7ed
3 changed files with 14 additions and 7 deletions

5
index.d.ts vendored
View File

@@ -3,3 +3,8 @@ export * from './src/types';
export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap]; export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap];
export function rebuild(n: serializedNodeWithId): Node | null; export function rebuild(n: serializedNodeWithId): Node | null;
export function serializeNodeWithId(
n: Node,
doc: Document,
map: idNodeMap,
): serializedNodeWithId | null;

View File

@@ -1,5 +1,5 @@
import snapshot from './snapshot'; import snapshot, { serializeNodeWithId } from './snapshot';
import rebuild from './rebuild'; import rebuild from './rebuild';
export * from './types'; export * from './types';
export { snapshot, rebuild }; export { snapshot, serializeNodeWithId, rebuild };

View File

@@ -117,9 +117,10 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
} }
} }
function serializeNodeWithId( export function serializeNodeWithId(
n: Node, n: Node,
doc: Document, doc: Document,
map: idNodeMap,
): serializedNodeWithId | null { ): serializedNodeWithId | null {
const _serializedNode = serializeNode(n, doc); const _serializedNode = serializeNode(n, doc);
if (!_serializedNode) { if (!_serializedNode) {
@@ -127,9 +128,12 @@ function serializeNodeWithId(
console.warn(n, 'not serialized'); console.warn(n, 'not serialized');
return null; return null;
} }
return Object.assign(_serializedNode, { const serializedNode = Object.assign(_serializedNode, {
id: genId(), id: genId(),
}); });
(n as INode).__sn = serializedNode;
map[serializedNode.id] = n as INode;
return serializedNode;
} }
function _snapshot( function _snapshot(
@@ -137,12 +141,10 @@ function _snapshot(
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
): serializedNodeWithId | null { ): serializedNodeWithId | null {
const serializedNode = serializeNodeWithId(n, doc); const serializedNode = serializeNodeWithId(n, doc, map);
if (!serializedNode) { if (!serializedNode) {
return null; return null;
} }
(n as INode).__sn = serializedNode;
map[serializedNode.id] = n as INode;
if ( if (
serializedNode.type === NodeType.Document || serializedNode.type === NodeType.Document ||
serializedNode.type === NodeType.Element serializedNode.type === NodeType.Element