Skip to content
Snippets Groups Projects
Commit e2b92c15 authored by dsehnal's avatar dsehnal
Browse files

PluginContext.initContainer options

parent 14614f48
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,8 @@ Note that since we don't clearly distinguish between a public and private interf ...@@ -6,6 +6,8 @@ Note that since we don't clearly distinguish between a public and private interf
## [Unreleased] ## [Unreleased]
- Make `PluginContext.initContainer` checkered canvas background optional
## [v3.23.0] - 2022-10-19 ## [v3.23.0] - 2022-10-19
- Add `PluginContext.initContainer/mount/unmount` methods; these should make it easier to reuse a plugin context with both custom and built-in UI - Add `PluginContext.initContainer/mount/unmount` methods; these should make it easier to reuse a plugin context with both custom and built-in UI
......
...@@ -38,7 +38,7 @@ export class ViewportCanvas extends PluginUIComponent<ViewportCanvasParams, View ...@@ -38,7 +38,7 @@ export class ViewportCanvas extends PluginUIComponent<ViewportCanvasParams, View
}; };
componentDidMount() { componentDidMount() {
if (!this.container.current || !this.plugin.mount(this.container.current!)) { if (!this.container.current || !this.plugin.mount(this.container.current!, { checkeredCanvasBackground: true })) {
this.setState({ noWebGl: true }); this.setState({ noWebGl: true });
return; return;
} }
......
...@@ -193,7 +193,7 @@ export class PluginContext { ...@@ -193,7 +193,7 @@ export class PluginContext {
*/ */
readonly customState: unknown = Object.create(null); readonly customState: unknown = Object.create(null);
initContainer(canvas3dContext?: Canvas3DContext) { initContainer(options?: { canvas3dContext?: Canvas3DContext, checkeredCanvasBackground?: boolean }) {
if (this.canvasContainer) return true; if (this.canvasContainer) return true;
const container = document.createElement('div'); const container = document.createElement('div');
...@@ -209,27 +209,33 @@ export class PluginContext { ...@@ -209,27 +209,33 @@ export class PluginContext {
'-webkit-touch-callout': 'none', '-webkit-touch-callout': 'none',
'touch-action': 'manipulation', 'touch-action': 'manipulation',
}); });
let canvas = canvas3dContext?.canvas; let canvas = options?.canvas3dContext?.canvas;
if (!canvas) { if (!canvas) {
canvas = document.createElement('canvas'); canvas = document.createElement('canvas');
Object.assign(canvas.style, { if (options?.checkeredCanvasBackground) {
'background-image': 'linear-gradient(45deg, lightgrey 25%, transparent 25%, transparent 75%, lightgrey 75%, lightgrey), linear-gradient(45deg, lightgrey 25%, transparent 25%, transparent 75%, lightgrey 75%, lightgrey)', Object.assign(canvas.style, {
'background-size': '60px 60px', 'background-image': 'linear-gradient(45deg, lightgrey 25%, transparent 25%, transparent 75%, lightgrey 75%, lightgrey), linear-gradient(45deg, lightgrey 25%, transparent 25%, transparent 75%, lightgrey 75%, lightgrey)',
'background-position': '0 0, 30px 30px' 'background-size': '60px 60px',
}); 'background-position': '0 0, 30px 30px'
});
}
container.appendChild(canvas); container.appendChild(canvas);
} }
if (!this.initViewer(canvas, container, canvas3dContext)) { if (!this.initViewer(canvas, container, options?.canvas3dContext)) {
return false; return false;
} }
this.canvasContainer = container; this.canvasContainer = container;
return true; return true;
} }
mount(target: HTMLElement) { /**
* Mount the plugin into the target element (assumes the target has "relative"-like positioninig).
* If initContainer wasn't called separately before, initOptions will be passed to it.
*/
mount(target: HTMLElement, initOptions?: { canvas3dContext?: Canvas3DContext, checkeredCanvasBackground?: boolean }) {
if (this.disposed) throw new Error('Cannot mount a disposed context'); if (this.disposed) throw new Error('Cannot mount a disposed context');
if (!this.initContainer()) return false; if (!this.initContainer(initOptions)) return false;
if (this.canvasContainer!.parentElement !== target) { if (this.canvasContainer!.parentElement !== target) {
this.canvasContainer!.parentElement?.removeChild(this.canvasContainer!); this.canvasContainer!.parentElement?.removeChild(this.canvasContainer!);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment