diff --git a/index.d.ts b/index.d.ts index fa9eae71..85dc9653 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,3 +3,8 @@ export * from './src/types'; export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap]; export function rebuild(n: serializedNodeWithId): Node | null; +export function serializeNodeWithId( + n: Node, + doc: Document, + map: idNodeMap, +): serializedNodeWithId | null; diff --git a/src/index.ts b/src/index.ts index 6b8e1450..fd969465 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import snapshot from './snapshot'; +import snapshot, { serializeNodeWithId } from './snapshot'; import rebuild from './rebuild'; export * from './types'; -export { snapshot, rebuild }; +export { snapshot, serializeNodeWithId, rebuild }; diff --git a/src/snapshot.ts b/src/snapshot.ts index 15642b7f..04eddcf3 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -117,9 +117,10 @@ function serializeNode(n: Node, doc: Document): serializedNode | false { } } -function serializeNodeWithId( +export function serializeNodeWithId( n: Node, doc: Document, + map: idNodeMap, ): serializedNodeWithId | null { const _serializedNode = serializeNode(n, doc); if (!_serializedNode) { @@ -127,9 +128,12 @@ function serializeNodeWithId( console.warn(n, 'not serialized'); return null; } - return Object.assign(_serializedNode, { + const serializedNode = Object.assign(_serializedNode, { id: genId(), }); + (n as INode).__sn = serializedNode; + map[serializedNode.id] = n as INode; + return serializedNode; } function _snapshot( @@ -137,12 +141,10 @@ function _snapshot( doc: Document, map: idNodeMap, ): serializedNodeWithId | null { - const serializedNode = serializeNodeWithId(n, doc); + const serializedNode = serializeNodeWithId(n, doc, map); if (!serializedNode) { return null; } - (n as INode).__sn = serializedNode; - map[serializedNode.id] = n as INode; if ( serializedNode.type === NodeType.Document || serializedNode.type === NodeType.Element