@preference-sl/pref-viewer 2.5.3 → 2.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +15 -42
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preference-sl/pref-viewer",
3
- "version": "2.5.3",
3
+ "version": "2.5.4",
4
4
  "description": "Web Component to preview GLTF models with Babylon.js",
5
5
  "author": "Alex Moreno Palacio <amoreno@preference.es>",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -128,10 +128,7 @@ class PrefViewer extends HTMLElement {
128
128
  "$1"
129
129
  );
130
130
  const fixedSlashes = stripped.replace(/\\/g, "/");
131
- if (/^https?:\/\//i.test(fixedSlashes)) {
132
- return fixedSlashes;
133
- }
134
- return fixedSlashes;
131
+ return /^https?:\/\//i.test(fixedSlashes) ? fixedSlashes : fixedSlashes;
135
132
  };
136
133
 
137
134
  SceneLoader.OnPluginActivatedObservable.add((plugin) => {
@@ -172,13 +169,8 @@ class PrefViewer extends HTMLElement {
172
169
  this._createLights();
173
170
  this._setupEventListeners();
174
171
 
175
- this.engine.runRenderLoop(() => {
176
- if (this.scene) this.scene.render();
177
- });
178
-
179
- this._onWindowResize = () => {
180
- if (this.engine) this.engine.resize();
181
- };
172
+ this.engine.runRenderLoop(() => this.scene && this.scene.render());
173
+ this._onWindowResize = () => this.engine && this.engine.resize();
182
174
  window.addEventListener("resize", this._onWindowResize);
183
175
  }
184
176
 
@@ -201,7 +193,6 @@ class PrefViewer extends HTMLElement {
201
193
  this.scene
202
194
  );
203
195
  this.hemiLight.intensity = 0.6;
204
-
205
196
  this.dirLight = new DirectionalLight(
206
197
  "dirLight",
207
198
  new Vector3(-0.5, -1, -0.5),
@@ -214,16 +205,11 @@ class PrefViewer extends HTMLElement {
214
205
  _setupEventListeners() {
215
206
  this.canvas.addEventListener("wheel", (evt) => {
216
207
  if (!this.scene || !this.camera) return;
217
- const pickResult = this.scene.pick(
218
- this.scene.pointerX,
219
- this.scene.pointerY
220
- );
221
- const pivotPoint = pickResult.hit
222
- ? pickResult.pickedPoint.clone()
223
- : this.camera.target.clone();
224
- this.camera.target = pivotPoint;
225
- this.camera.inertialRadiusOffset +=
226
- evt.deltaY * this.camera.wheelPrecision * 0.01;
208
+ const pick = this.scene.pick(this.scene.pointerX, this.scene.pointerY);
209
+ this.camera.target = pick.hit
210
+ ? pick.pickedPoint.clone()
211
+ : this.camera.target;
212
+ this.camera.inertialRadiusOffset += evt.deltaY * this.camera.wheelPrecision * 0.01;
227
213
  evt.preventDefault();
228
214
  });
229
215
  }
@@ -234,7 +220,6 @@ class PrefViewer extends HTMLElement {
234
220
  console.warn("PrefViewer: _reloadModel aborted (no scene or no model)");
235
221
  return;
236
222
  }
237
-
238
223
  this._disposePreviousMeshes();
239
224
 
240
225
  try {
@@ -242,10 +227,13 @@ class PrefViewer extends HTMLElement {
242
227
  if (this.modelBase64) {
243
228
  const blob = this._createBlobFromBase64(this.modelBase64);
244
229
  const ext = this._getExtensionFromMimeType(blob.type);
230
+ const fileName = `model${ext}`;
231
+ const file = new File([blob], fileName, { type: blob.type });
232
+ console.log('[PrefViewer] Importing from File:', fileName, blob);
245
233
  result = await SceneLoader.ImportMeshAsync(
246
234
  null,
247
235
  "",
248
- blob,
236
+ file,
249
237
  this.scene,
250
238
  undefined,
251
239
  ext
@@ -261,14 +249,10 @@ class PrefViewer extends HTMLElement {
261
249
  ext
262
250
  );
263
251
  }
264
-
265
252
  this.scene.createDefaultCameraOrLight(true, true, true);
266
253
  this.dispatchEvent(
267
254
  new CustomEvent("model-loaded", {
268
- detail: {
269
- meshes: result.meshes,
270
- particleSystems: result.particleSystems
271
- },
255
+ detail: { meshes: result.meshes, particleSystems: result.particleSystems },
272
256
  bubbles: true,
273
257
  composed: true
274
258
  })
@@ -287,9 +271,7 @@ class PrefViewer extends HTMLElement {
287
271
 
288
272
  _disposePreviousMeshes() {
289
273
  if (!this.scene) return;
290
- this.scene.meshes.slice().forEach((mesh) => {
291
- if (mesh.getClassName() === "Mesh") mesh.dispose();
292
- });
274
+ this.scene.meshes.slice().forEach((m) => m.getClassName() === "Mesh" && m.dispose());
293
275
  }
294
276
 
295
277
  _createBlobFromBase64(base64) {
@@ -334,16 +316,7 @@ class PrefViewer extends HTMLElement {
334
316
  }
335
317
 
336
318
  // ====== Cleanup ======
337
- _disposeEngine() {
338
- if (this.engine) {
339
- this.engine.dispose();
340
- this.engine = null;
341
- this.scene = null;
342
- this.camera = null;
343
- this.hemiLight = null;
344
- this.dirLight = null;
345
- }
346
- }
319
+ _disposeEngine() { if (this.engine) { this.engine.dispose(); this.engine = null; this.scene = null; this.camera = null; this.hemiLight = null; this.dirLight = null; } }
347
320
  }
348
321
 
349
322
  customElements.define("pref-viewer", PrefViewer);