From 358854d7a5a1a4ca72dbc81ac50bcbe5982e223c Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Mon, 4 Dec 2023 14:55:56 +0800 Subject: [PATCH 1/2] fix: cannot find document if group destroyed --- .gitignore | 2 +- src/runtime/library.ts | 20 +++++++++++++++----- src/runtime/render.ts | 5 +++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 10458210b6..3dac8330d1 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ site/.dumi/tmp-production # Editor .vscode - +.history # Other !/src/lib diff --git a/src/runtime/library.ts b/src/runtime/library.ts index 4c2db349db..d97ffffe1b 100644 --- a/src/runtime/library.ts +++ b/src/runtime/library.ts @@ -15,7 +15,7 @@ export function useLibrary< >( namespace: G2ComponentNamespaces, publicLibrary: G2Library, -): [(options: O, context?) => V, (type: O['type']) => C] { +): [(options: O, context?: G2Context) => V, (type: O['type']) => C] { const library = { ...builtinlib(), ...publicLibrary }; const create = (type: O['type']) => { @@ -23,15 +23,25 @@ export function useLibrary< const key = `${namespace}.${type}`; return library[key] || error(`Unknown Component: ${key}`); }; - const use = (options: O, context?) => { + + const use = (options: O, context?: G2Context) => { const { type, ...rest } = options; - return create(type)(rest, context); + if (!type) { + error(`Plot type is required!`); + } + + const currentLibrary = create(type); + return currentLibrary?.(rest, context); }; + return [use, create]; } export function documentOf(library: G2Context): IDocument { const { canvas, group } = library; - if (group) return group.ownerDocument; - return canvas.document; + return ( + canvas?.document || + group?.ownerDocument || + error(`Cannot find library document`) + ); } diff --git a/src/runtime/render.ts b/src/runtime/render.ts index 0cfa9a2189..b3d6fefe16 100644 --- a/src/runtime/render.ts +++ b/src/runtime/render.ts @@ -137,17 +137,18 @@ export function renderToMountedElement( error(`renderToMountedElement can't render chart to unmounted group.`); } + const canvas = context.canvas || group?.ownerDocument?.defaultView; const selection = select(group); context.group = group; context.emitter = emitter; + context.canvas ??= canvas as GCanvas; emitter.emit(ChartEvent.BEFORE_RENDER); // Plot the chart and mutate context. // Make sure that plot chart after container is ready for every time. plot({ ...keyed, width, height }, selection, library, context) .then(() => { - const canvas = group.ownerDocument.defaultView; - canvas.requestAnimationFrame(() => { + canvas?.requestAnimationFrame(() => { emitter.emit(ChartEvent.AFTER_RENDER); resolve?.(); }); From dea2c1ab071defc1453ead336749db099642b989 Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Tue, 5 Dec 2023 10:24:43 +0800 Subject: [PATCH 2/2] fix: fallback --- src/runtime/render.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/render.ts b/src/runtime/render.ts index b3d6fefe16..0e20e43838 100644 --- a/src/runtime/render.ts +++ b/src/runtime/render.ts @@ -137,18 +137,18 @@ export function renderToMountedElement( error(`renderToMountedElement can't render chart to unmounted group.`); } - const canvas = context.canvas || group?.ownerDocument?.defaultView; const selection = select(group); context.group = group; context.emitter = emitter; - context.canvas ??= canvas as GCanvas; + context.canvas = + context.canvas || (group?.ownerDocument?.defaultView as GCanvas); emitter.emit(ChartEvent.BEFORE_RENDER); // Plot the chart and mutate context. // Make sure that plot chart after container is ready for every time. plot({ ...keyed, width, height }, selection, library, context) .then(() => { - canvas?.requestAnimationFrame(() => { + context.canvas?.requestAnimationFrame(() => { emitter.emit(ChartEvent.AFTER_RENDER); resolve?.(); });