From bdf1affeeefe8d55bd66dbc9a8d97048e3343116 Mon Sep 17 00:00:00 2001 From: arjxn-py Date: Mon, 14 Oct 2024 15:55:28 +0530 Subject: [PATCH 1/2] Make `viewHelperDiv` a class property --- packages/base/src/3dview/mainview.tsx | 40 +++++++++++---------- packages/base/src/panelview/formbuilder.tsx | 2 +- python/jupytercad_core/src/factory.ts | 4 +-- python/jupytercad_core/src/plugin.ts | 8 +++++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/packages/base/src/3dview/mainview.tsx b/packages/base/src/3dview/mainview.tsx index 8d4fe961..f8c8e826 100644 --- a/packages/base/src/3dview/mainview.tsx +++ b/packages/base/src/3dview/mainview.tsx @@ -362,29 +362,29 @@ export class MainView extends React.Component { this._transformControls.enabled = false; this._transformControls.visible = false; + this._createViewHelper(); + } + }; - // ViewHelper setup - this._viewHelper = new ViewHelper( - this._camera, - this._renderer.domElement - ); - this._viewHelper.center = this._controls.target; + private _createViewHelper() { + this._viewHelper = new ViewHelper(this._camera, this._renderer.domElement); + this._viewHelper.center = this._controls.target; - const viewHelperDiv = document.createElement('div'); - viewHelperDiv.id = 'viewHelper'; - viewHelperDiv.style.position = 'absolute'; - viewHelperDiv.style.right = '0px'; - viewHelperDiv.style.bottom = '0px'; - viewHelperDiv.style.height = '128px'; - viewHelperDiv.style.width = '128px'; + const viewHelperDiv = document.createElement('div'); + viewHelperDiv.style.position = 'absolute'; + viewHelperDiv.style.right = '0px'; + viewHelperDiv.style.bottom = '0px'; + viewHelperDiv.style.height = '128px'; + viewHelperDiv.style.width = '128px'; - this.divRef.current.appendChild(viewHelperDiv); + this._viewHelperDiv = viewHelperDiv; - viewHelperDiv.addEventListener('pointerup', event => - this._viewHelper.handleClick(event) - ); - } - }; + this.divRef.current?.appendChild(this._viewHelperDiv); + + this._viewHelperDiv.addEventListener('pointerup', event => + this._viewHelper.handleClick(event) + ); + } animate = (): void => { this._requestID = window.requestAnimationFrame(this.animate); @@ -1312,6 +1312,7 @@ export class MainView extends React.Component { } this._camera.add(this._cameraLight); + this._createViewHelper(); this._scene.add(this._camera); this._controls.object = this._camera; @@ -1476,6 +1477,7 @@ export class MainView extends React.Component { private _pointer3D: IPointer | null = null; private _clock: THREE.Clock; private _viewHelper: ViewHelper; + private _viewHelperDiv: HTMLDivElement | null = null; private _collaboratorPointers: IDict; private _pointerGeometry: THREE.SphereGeometry; private _contextMenu: ContextMenu; diff --git a/packages/base/src/panelview/formbuilder.tsx b/packages/base/src/panelview/formbuilder.tsx index 8bd2e9f7..dcf55c8c 100644 --- a/packages/base/src/panelview/formbuilder.tsx +++ b/packages/base/src/panelview/formbuilder.tsx @@ -46,7 +46,7 @@ export const LuminoSchemaForm = ( Widget.detach(widget); } } catch (e) { - console.warn('Exception while detaching Lumino widget.', e); + // The widget is destroyed already by React. } }; }, [children]); diff --git a/python/jupytercad_core/src/factory.ts b/python/jupytercad_core/src/factory.ts index 26e4f331..70f07ff5 100644 --- a/python/jupytercad_core/src/factory.ts +++ b/python/jupytercad_core/src/factory.ts @@ -1,9 +1,9 @@ import { ConsolePanel, IConsoleTracker } from '@jupyterlab/console'; import { JupyterCadModel, - IJupyterCadTracker, IJCadWorkerRegistry, - IJCadExternalCommandRegistry + IJCadExternalCommandRegistry, + IJupyterCadTracker } from '@jupytercad/schema'; import { ABCWidgetFactory, DocumentRegistry } from '@jupyterlab/docregistry'; import { CommandRegistry } from '@lumino/commands'; diff --git a/python/jupytercad_core/src/plugin.ts b/python/jupytercad_core/src/plugin.ts index b3c2b2d7..dc5e89c7 100644 --- a/python/jupytercad_core/src/plugin.ts +++ b/python/jupytercad_core/src/plugin.ts @@ -39,6 +39,14 @@ export const trackerPlugin: JupyterFrontEndPlugin = { const tracker = new WidgetTracker({ namespace: NAME_SPACE }); + tracker.currentChanged.connect(() => { + const currentWidget = tracker.currentWidget; + + if (currentWidget) { + const resizeEvent = new Event('resize'); + window.dispatchEvent(resizeEvent); + } + }); console.log('jupytercad:core:tracker is activated!'); return tracker; } From 4602e00cff6d62640a924ed800558c256c3c22fe Mon Sep 17 00:00:00 2001 From: arjxn-py Date: Tue, 15 Oct 2024 14:32:15 +0530 Subject: [PATCH 2/2] Remove the existing ViewHelperDiv if it already exists --- packages/base/src/3dview/mainview.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/base/src/3dview/mainview.tsx b/packages/base/src/3dview/mainview.tsx index f8c8e826..a860a1c4 100644 --- a/packages/base/src/3dview/mainview.tsx +++ b/packages/base/src/3dview/mainview.tsx @@ -367,6 +367,15 @@ export class MainView extends React.Component { }; private _createViewHelper() { + // Remove the existing ViewHelperDiv if it already exists + if ( + this._viewHelperDiv && + this.divRef.current?.contains(this._viewHelperDiv) + ) { + this.divRef.current.removeChild(this._viewHelperDiv); + } + + // Create new ViewHelper this._viewHelper = new ViewHelper(this._camera, this._renderer.domElement); this._viewHelper.center = this._controls.target;