index.ts 981 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as React from 'react';
  8. import * as ReactDOM from 'react-dom';
  9. import { Plugin } from './plugin';
  10. import { PluginUIContext } from './context';
  11. import { DefaultPluginUISpec, PluginUISpec } from './spec';
  12. export async function createPluginUI(target: HTMLElement, spec?: PluginUISpec, options?: { onBeforeUIRender?: (ctx: PluginUIContext) => (Promise<void> | void) }) {
  13. const ctx = new PluginUIContext(spec || DefaultPluginUISpec());
  14. await ctx.init();
  15. if (options?.onBeforeUIRender) {
  16. await options.onBeforeUIRender(ctx);
  17. }
  18. ReactDOM.render(React.createElement(Plugin, { plugin: ctx }), target);
  19. try {
  20. await ctx.canvas3dInitialized;
  21. } catch {
  22. // Error reported in UI/console elsewhere.
  23. }
  24. return ctx;
  25. }