outer-labs / react-forge-viewer

React component wrapper for Autodesk's Forge Viewer
MIT License
47 stars 23 forks source link

How to add extension to viewer and register extesion for viewer #20

Open bogdanAndrushchenko opened 3 years ago

bogdanAndrushchenko commented 3 years ago

Hi guys! I already investigated the library documentation. But still do not understand how can I add and register an extension for the viewer in case of React app. How can I receive the class of Autodesk.ADN.Viewing.Extension without CDN? Appreciate your help!

sanzoghenzo commented 3 years ago

Hi, I was just trying to figure it out myself; after some documenting and trial & error, I think I'm on the right path...

The key here is dynamic/lazy loading.

First, you need to setup webpack (or the layer above it, for me it was umijs) for code splitting/dynamic loading.

then you can add the following code in the function that you use as onDocumentLoad or onModelLoad:

const { default: ext } = await import('./path/to/your/extension');
this.viewer.loadExtension('yourExtensionName');
// do something with ext, if needed

obviously you will need some try/catch somewhere to catch import errors.