diff --git a/src/index.ts b/src/index.ts index f3238ea2..5b2b0b9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import snapshot, { serializeNodeWithId, resetId } from './snapshot'; +import snapshot, { serializeNodeWithId } from './snapshot'; import rebuild, { buildNodeWithSN } from './rebuild'; export * from './types'; -export { snapshot, serializeNodeWithId, resetId, rebuild, buildNodeWithSN }; +export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN }; diff --git a/src/snapshot.ts b/src/snapshot.ts index dd2a5447..2c0e170e 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -13,10 +13,6 @@ function genId(): number { return _id++; } -export function resetId() { - _id = 1; -} - function getCssRulesString(s: CSSStyleSheet): string | null { try { const rules = s.rules || s.cssRules; @@ -239,7 +235,7 @@ function serializeNode( } export function serializeNodeWithId( - n: Node, + n: Node | INode, doc: Document, map: idNodeMap, blockClass: string | RegExp, @@ -252,11 +248,16 @@ export function serializeNodeWithId( console.warn(n, 'not serialized'); return null; } - const serializedNode = Object.assign(_serializedNode, { - id: genId(), - }); + let id + // Try to reuse the previous id + if ('__sn' in n) { + id = n.__sn.id + } else { + id = genId() + } + const serializedNode = Object.assign(_serializedNode, { id }); (n as INode).__sn = serializedNode; - map[serializedNode.id] = n as INode; + map[id] = (n as INode); let recordChild = !skipChild; if (serializedNode.type === NodeType.Element) { recordChild = recordChild && !serializedNode.needBlock; @@ -290,7 +291,6 @@ function snapshot( blockClass: string | RegExp = 'rr-block', inlineStylesheet = true, ): [serializedNodeWithId | null, idNodeMap] { - resetId(); const idNodeMap: idNodeMap = {}; return [ serializeNodeWithId(n, n, idNodeMap, blockClass, false, inlineStylesheet), diff --git a/typings/index.d.ts b/typings/index.d.ts index 60e49702..87f610a3 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,4 +1,4 @@ -import snapshot, { serializeNodeWithId, resetId } from './snapshot'; +import snapshot, { serializeNodeWithId } from './snapshot'; import rebuild, { buildNodeWithSN } from './rebuild'; export * from './types'; -export { snapshot, serializeNodeWithId, resetId, rebuild, buildNodeWithSN }; +export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN }; diff --git a/typings/snapshot.d.ts b/typings/snapshot.d.ts index 61ed5163..e8605417 100644 --- a/typings/snapshot.d.ts +++ b/typings/snapshot.d.ts @@ -1,6 +1,5 @@ -import { serializedNodeWithId, idNodeMap } from './types'; -export declare function resetId(): void; +import { serializedNodeWithId, INode, idNodeMap } from './types'; export declare function absoluteToStylesheet(cssText: string, href: string): string; -export declare function serializeNodeWithId(n: Node, doc: Document, map: idNodeMap, blockClass: string | RegExp, skipChild?: boolean, inlineStylesheet?: boolean): serializedNodeWithId | null; +export declare function serializeNodeWithId(n: Node | INode, doc: Document, map: idNodeMap, blockClass: string | RegExp, skipChild?: boolean, inlineStylesheet?: boolean): serializedNodeWithId | null; declare function snapshot(n: Document, blockClass?: string | RegExp, inlineStylesheet?: boolean): [serializedNodeWithId | null, idNodeMap]; export default snapshot;