@plattar/plattar-ar-adapter 1.120.1 → 1.122.2

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.
@@ -0,0 +1,32 @@
1
+ import { LauncherAR } from "./launcher-ar";
2
+ /**
3
+ * Performs AT Functionality using Plattar FileModel types
4
+ */
5
+ export declare class ModelAR extends LauncherAR {
6
+ private _analytics;
7
+ private readonly _modelID;
8
+ private _ar;
9
+ constructor(modelID?: string | undefined | null);
10
+ get modelID(): string;
11
+ private _SetupAnalytics;
12
+ /**
13
+ * Initialise the ModelAR instance. This returns a Promise that resolves
14
+ * successfully if initialisation is successful, otherwise it will fail.
15
+ *
16
+ * filure can occur for a number of reasons but it generally means that AR
17
+ * cannot be performed.
18
+ */
19
+ init(): Promise<LauncherAR>;
20
+ /**
21
+ * Initialise and launch with a single function call. this is mostly for convenience.
22
+ * Use .init() and .start() separately for fine-grained control
23
+ */
24
+ launch(): Promise<void>;
25
+ /**
26
+ * Launches the internal AR instance using an appropriate version of AR Viewers
27
+ */
28
+ start(): void;
29
+ canQuicklook(): boolean;
30
+ canRealityViewer(): boolean;
31
+ canSceneViewer(): boolean;
32
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ModelAR = void 0;
7
+ const plattar_api_1 = require("@plattar/plattar-api");
8
+ const plattar_analytics_1 = require("@plattar/plattar-analytics");
9
+ const util_1 = require("../util/util");
10
+ const quicklook_viewer_1 = __importDefault(require("../viewers/quicklook-viewer"));
11
+ const reality_viewer_1 = __importDefault(require("../viewers/reality-viewer"));
12
+ const scene_viewer_1 = __importDefault(require("../viewers/scene-viewer"));
13
+ const launcher_ar_1 = require("./launcher-ar");
14
+ /**
15
+ * Performs AT Functionality using Plattar FileModel types
16
+ */
17
+ class ModelAR extends launcher_ar_1.LauncherAR {
18
+ constructor(modelID = null) {
19
+ super();
20
+ // analytics instance
21
+ this._analytics = null;
22
+ if (!modelID) {
23
+ throw new Error("ModelAR.constructor(modelID) - modelID must be defined");
24
+ }
25
+ this._modelID = modelID;
26
+ this._ar = null;
27
+ }
28
+ get modelID() {
29
+ return this._modelID;
30
+ }
31
+ _SetupAnalytics(model) {
32
+ let analytics = null;
33
+ const project = model.relationships.find(plattar_api_1.Project);
34
+ // setup scene stuff (if any)
35
+ if (project) {
36
+ analytics = new plattar_analytics_1.Analytics(project.id);
37
+ analytics.origin = plattar_api_1.Server.location().type;
38
+ analytics.data.push("applicationId", project.id);
39
+ analytics.data.push("applicationTitle", project.attributes.title);
40
+ analytics.data.push("modelId", model.id);
41
+ analytics.data.push("modelTitle", model.attributes.title);
42
+ this._analytics = analytics;
43
+ }
44
+ }
45
+ /**
46
+ * Initialise the ModelAR instance. This returns a Promise that resolves
47
+ * successfully if initialisation is successful, otherwise it will fail.
48
+ *
49
+ * filure can occur for a number of reasons but it generally means that AR
50
+ * cannot be performed.
51
+ */
52
+ init() {
53
+ return new Promise((accept, reject) => {
54
+ if (!util_1.Util.canAugment()) {
55
+ return reject(new Error("ModelAR.init() - cannot proceed as AR not available in context"));
56
+ }
57
+ const model = new plattar_api_1.FileModel(this.modelID);
58
+ model.include(plattar_api_1.Project);
59
+ model.get().then((model) => {
60
+ // setup the analytics data
61
+ this._SetupAnalytics(model);
62
+ // we need to define our AR module here
63
+ // we are in Safari/Quicklook mode here
64
+ if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
65
+ // model needs to have either USDZ or REALITY files defined
66
+ // we load REALITY stuff first if available
67
+ if (model.attributes.reality_filename && util_1.Util.canRealityViewer()) {
68
+ this._ar = new reality_viewer_1.default();
69
+ this._ar.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.reality_filename;
70
+ return accept(this);
71
+ }
72
+ // otherwise, load the USDZ stuff second if available
73
+ if (model.attributes.usdz_filename && util_1.Util.canQuicklook()) {
74
+ this._ar = new quicklook_viewer_1.default();
75
+ this._ar.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.usdz_filename;
76
+ return accept(this);
77
+ }
78
+ return reject(new Error("ModelAR.init() - cannot proceed as ModelFile does not have a defined .usdz or .reality file"));
79
+ }
80
+ // check android
81
+ if (util_1.Util.canSceneViewer()) {
82
+ this._ar = new scene_viewer_1.default();
83
+ this._ar.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.original_filename;
84
+ return accept(this);
85
+ }
86
+ // otherwise, we didn't have AR available - it should never really reach this stage as this should be caught
87
+ // earlier in the process
88
+ return reject(new Error("ModelAR.init() - could not initialise AR correctly, check values"));
89
+ }).catch(reject);
90
+ });
91
+ }
92
+ /**
93
+ * Initialise and launch with a single function call. this is mostly for convenience.
94
+ * Use .init() and .start() separately for fine-grained control
95
+ */
96
+ launch() {
97
+ return new Promise((accept, reject) => {
98
+ this.init().then((value) => {
99
+ value.start();
100
+ return accept();
101
+ }).catch(reject);
102
+ });
103
+ }
104
+ /**
105
+ * Launches the internal AR instance using an appropriate version of AR Viewers
106
+ */
107
+ start() {
108
+ if (!this._ar) {
109
+ throw new Error("ModelAR.start() - cannot proceed as AR instance is null");
110
+ }
111
+ const analytics = this._analytics;
112
+ if (analytics) {
113
+ analytics.data.push("device", this._ar.device);
114
+ analytics.data.push("eventCategory", this._ar.nodeType);
115
+ analytics.data.push("eventAction", "Start Model Augment");
116
+ analytics.write();
117
+ analytics.startRecordEngagement();
118
+ }
119
+ // this was initialised via the init() function
120
+ this._ar.start();
121
+ }
122
+ canQuicklook() {
123
+ return this._ar && this._ar.nodeType === "Quick Look" ? true : false;
124
+ }
125
+ canRealityViewer() {
126
+ return this._ar && this._ar.nodeType === "Reality Viewer" ? true : false;
127
+ }
128
+ canSceneViewer() {
129
+ return this._ar && this._ar.nodeType === "Scene Viewer" ? true : false;
130
+ }
131
+ }
132
+ exports.ModelAR = ModelAR;
@@ -4,6 +4,8 @@ import { LauncherAR } from "../ar/launcher-ar";
4
4
  * of Plattar related content
5
5
  */
6
6
  export default class PlattarEmbed extends HTMLElement {
7
+ private _currentState;
8
+ private _qrCodeOptions;
7
9
  private _sceneID;
8
10
  private _productID;
9
11
  private _variationID;
@@ -19,4 +21,9 @@ export default class PlattarEmbed extends HTMLElement {
19
21
  startAR(): Promise<void>;
20
22
  startViewer(): Promise<HTMLElement>;
21
23
  startQRCode(options?: any | undefined | null): Promise<HTMLElement>;
24
+ /**
25
+ * This is called by the observer if any of the embed attributes have changed
26
+ * based on the state of the embed, we update the internal structure accordingly
27
+ */
28
+ private _OnAttributesUpdated;
22
29
  }
@@ -2,6 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const plattar_api_1 = require("@plattar/plattar-api");
4
4
  const product_ar_1 = require("../ar/product-ar");
5
+ /**
6
+ * This tracks the current state of the Embed
7
+ */
8
+ var EmbedState;
9
+ (function (EmbedState) {
10
+ EmbedState[EmbedState["None"] = 0] = "None";
11
+ EmbedState[EmbedState["SceneViewer"] = 1] = "SceneViewer";
12
+ EmbedState[EmbedState["ProductViewer"] = 2] = "ProductViewer";
13
+ EmbedState[EmbedState["ProductAR"] = 3] = "ProductAR";
14
+ EmbedState[EmbedState["QRCode"] = 4] = "QRCode";
15
+ })(EmbedState || (EmbedState = {}));
5
16
  /**
6
17
  * This is the primary <plattar-embed /> node that allows easy embedding
7
18
  * of Plattar related content
@@ -9,6 +20,13 @@ const product_ar_1 = require("../ar/product-ar");
9
20
  class PlattarEmbed extends HTMLElement {
10
21
  constructor() {
11
22
  super();
23
+ // this is the current state of the embed, none by default
24
+ this._currentState = EmbedState.None;
25
+ this._qrCodeOptions = {
26
+ color: "#101721",
27
+ qrType: "default",
28
+ margin: 0
29
+ };
12
30
  this._sceneID = null;
13
31
  this._productID = null;
14
32
  this._variationID = null;
@@ -22,6 +40,35 @@ class PlattarEmbed extends HTMLElement {
22
40
  return this._viewer;
23
41
  }
24
42
  connectedCallback() {
43
+ const observer = new MutationObserver((mutations) => {
44
+ mutations.forEach((mutation) => {
45
+ if (mutation.type === "attributes") {
46
+ const sceneID = this.hasAttribute("scene-id") ? this.getAttribute("scene-id") : null;
47
+ const productID = this.hasAttribute("product-id") ? this.getAttribute("product-id") : null;
48
+ const variationID = this.hasAttribute("variation-id") ? this.getAttribute("variation-id") : null;
49
+ let updated = false;
50
+ if (sceneID !== this._sceneID) {
51
+ this._sceneID = sceneID;
52
+ updated = true;
53
+ }
54
+ if (productID !== this._productID) {
55
+ this._productID = productID;
56
+ updated = true;
57
+ }
58
+ if (variationID !== this._variationID) {
59
+ this._variationID = variationID;
60
+ updated = true;
61
+ }
62
+ if (updated) {
63
+ // re-render based on internal state
64
+ this._OnAttributesUpdated();
65
+ }
66
+ }
67
+ });
68
+ });
69
+ observer.observe(this, {
70
+ attributes: true
71
+ });
25
72
  const server = this.hasAttribute("server") ? this.getAttribute("server") : "production";
26
73
  plattar_api_1.Server.create(plattar_api_1.Server.match(server || "production"));
27
74
  if (server) {
@@ -74,6 +121,13 @@ class PlattarEmbed extends HTMLElement {
74
121
  const product = new product_ar_1.ProductAR(this._productID, this._variationID);
75
122
  return product.init().then(accept).catch(reject);
76
123
  }
124
+ // If Product is set (under any scenario) then use ProductAR
125
+ // NOTE: At some point this should check for Scenes when SceneAR
126
+ // is implemented
127
+ if (this._productID) {
128
+ const product = new product_ar_1.ProductAR(this._productID, this._variationID);
129
+ return product.init().then(accept).catch(reject);
130
+ }
77
131
  // otherwise, scene was set so use SceneAR
78
132
  if (this._sceneID) {
79
133
  return reject(new Error("PlattarEmbed.initAR() - scene-id not yet supported"));
@@ -108,12 +162,19 @@ class PlattarEmbed extends HTMLElement {
108
162
  viewer.setAttribute("height", this._height);
109
163
  viewer.setAttribute("server", this._server);
110
164
  viewer.setAttribute("scene-id", this._sceneID);
165
+ if (this._productID) {
166
+ viewer.setAttribute("product-id", this._productID);
167
+ }
168
+ if (this._variationID) {
169
+ viewer.setAttribute("variation-id", this._variationID);
170
+ }
111
171
  viewer.onload = () => {
112
172
  return accept(viewer);
113
173
  };
114
174
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
115
175
  shadow.append(viewer);
116
176
  this._viewer = viewer;
177
+ this._currentState = EmbedState.SceneViewer;
117
178
  return;
118
179
  }
119
180
  // if product is set, we use <plattar-product /> node from plattar-web
@@ -132,6 +193,7 @@ class PlattarEmbed extends HTMLElement {
132
193
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
133
194
  shadow.append(viewer);
134
195
  this._viewer = viewer;
196
+ this._currentState = EmbedState.ProductViewer;
135
197
  return;
136
198
  }
137
199
  return reject(new Error("PlattarEmbed.startViewer() - minimum required attributes not set, use scene-id or product-id as a minimum"));
@@ -142,11 +204,9 @@ class PlattarEmbed extends HTMLElement {
142
204
  if (!this._isReady) {
143
205
  return reject(new Error("PlattarEmbed.startQRCode() - cannot execute as page has not loaded yet"));
144
206
  }
145
- const opt = options || {
146
- color: "#101721",
147
- qrType: "default",
148
- margin: 0
149
- };
207
+ const opt = options || this._qrCodeOptions;
208
+ // reset instance for later use
209
+ this._qrCodeOptions = opt;
150
210
  if (this._viewer) {
151
211
  this._viewer.remove();
152
212
  this._viewer = null;
@@ -165,7 +225,13 @@ class PlattarEmbed extends HTMLElement {
165
225
  if (opt.qrType) {
166
226
  viewer.setAttribute("qr-type", opt.qrType);
167
227
  }
168
- const dst = plattar_api_1.Server.location().base + "renderer/viewer.html?scene_id=" + this._sceneID;
228
+ let dst = plattar_api_1.Server.location().base + "renderer/viewer.html?scene_id=" + this._sceneID;
229
+ if (this._productID) {
230
+ dst += "&productId=" + this._productID;
231
+ }
232
+ if (this._variationID) {
233
+ dst += "&variationId=" + this._variationID;
234
+ }
169
235
  viewer.setAttribute("url", dst);
170
236
  viewer.onload = () => {
171
237
  return accept(viewer);
@@ -173,6 +239,7 @@ class PlattarEmbed extends HTMLElement {
173
239
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
174
240
  shadow.append(viewer);
175
241
  this._viewer = viewer;
242
+ this._currentState = EmbedState.QRCode;
176
243
  return;
177
244
  }
178
245
  // if product is set, we embed a QR code that takes us to product.html
@@ -200,10 +267,41 @@ class PlattarEmbed extends HTMLElement {
200
267
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
201
268
  shadow.append(viewer);
202
269
  this._viewer = viewer;
270
+ this._currentState = EmbedState.QRCode;
203
271
  return;
204
272
  }
205
273
  return reject(new Error("PlattarEmbed.startQRCode() - minimum required attributes not set, use scene-id or product-id as a minimum"));
206
274
  });
207
275
  }
276
+ /**
277
+ * This is called by the observer if any of the embed attributes have changed
278
+ * based on the state of the embed, we update the internal structure accordingly
279
+ */
280
+ _OnAttributesUpdated() {
281
+ // nothing to update in these scenarios
282
+ if (this._currentState === EmbedState.None || this._currentState === EmbedState.ProductAR) {
283
+ return;
284
+ }
285
+ // re-render the QR Code when attributes have changed
286
+ if (this._currentState === EmbedState.QRCode) {
287
+ this.startQRCode(this._qrCodeOptions);
288
+ return;
289
+ }
290
+ // use the messenger function to change variation when attributes have changed
291
+ if (this._currentState === EmbedState.SceneViewer) {
292
+ const viewer = this.viewer;
293
+ if (viewer) {
294
+ viewer.messenger.selectVariation(this._productID, this._variationID);
295
+ }
296
+ return;
297
+ }
298
+ if (this._currentState === EmbedState.ProductViewer) {
299
+ const viewer = this.viewer;
300
+ if (viewer) {
301
+ viewer.messenger.selectVariation(this._variationID);
302
+ }
303
+ return;
304
+ }
305
+ }
208
306
  }
209
307
  exports.default = PlattarEmbed;
package/dist/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export * as version from "./version";
4
4
  export { ConfiguratorAR } from "./ar/configurator-ar";
5
5
  export { ProductAR } from "./ar/product-ar";
6
6
  export { SceneAR } from "./ar/scene-ar";
7
+ export { ModelAR } from "./ar/model-ar";
7
8
  export { Util } from "./util/util";
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.Util = exports.SceneAR = exports.ProductAR = exports.ConfiguratorAR = exports.version = exports.PlattarQRCode = exports.PlattarWeb = void 0;
25
+ exports.Util = exports.ModelAR = exports.SceneAR = exports.ProductAR = exports.ConfiguratorAR = exports.version = exports.PlattarQRCode = exports.PlattarWeb = void 0;
26
26
  exports.PlattarWeb = __importStar(require("@plattar/plattar-web"));
27
27
  exports.PlattarQRCode = __importStar(require("@plattar/plattar-qrcode"));
28
28
  exports.version = __importStar(require("./version"));
@@ -32,11 +32,15 @@ var product_ar_1 = require("./ar/product-ar");
32
32
  Object.defineProperty(exports, "ProductAR", { enumerable: true, get: function () { return product_ar_1.ProductAR; } });
33
33
  var scene_ar_1 = require("./ar/scene-ar");
34
34
  Object.defineProperty(exports, "SceneAR", { enumerable: true, get: function () { return scene_ar_1.SceneAR; } });
35
+ var model_ar_1 = require("./ar/model-ar");
36
+ Object.defineProperty(exports, "ModelAR", { enumerable: true, get: function () { return model_ar_1.ModelAR; } });
35
37
  var util_1 = require("./util/util");
36
38
  Object.defineProperty(exports, "Util", { enumerable: true, get: function () { return util_1.Util; } });
37
39
  const plattar_embed_1 = __importDefault(require("./embed/plattar-embed"));
38
40
  const version_1 = __importDefault(require("./version"));
39
41
  if (customElements) {
40
- customElements.define("plattar-embed", plattar_embed_1.default);
42
+ if (customElements.get("plattar-embed") === undefined) {
43
+ customElements.define("plattar-embed", plattar_embed_1.default);
44
+ }
41
45
  }
42
46
  console.log("using @plattar/plattar-ar-adapter v" + version_1.default);
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: "1.120.1";
1
+ declare const _default: "1.122.2";
2
2
  export default _default;
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = "1.120.1";
3
+ exports.default = "1.122.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plattar/plattar-ar-adapter",
3
- "version": "1.120.1",
3
+ "version": "1.122.2",
4
4
  "description": "Plattar AR Adapter for interfacing with Google & Apple WebAR",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -40,14 +40,14 @@
40
40
  "@plattar/plattar-analytics": "^1.117.2",
41
41
  "@plattar/plattar-api": "^1.120.1",
42
42
  "@plattar/plattar-qrcode": "1.120.3",
43
- "@plattar/plattar-web": "^1.117.1"
43
+ "@plattar/plattar-web": "^1.122.2"
44
44
  },
45
45
  "devDependencies": {
46
- "@babel/cli": "^7.16.0",
47
- "@babel/core": "^7.16.0",
48
- "@babel/preset-env": "^7.16.4",
46
+ "@babel/cli": "^7.16.8",
47
+ "@babel/core": "^7.16.12",
48
+ "@babel/preset-env": "^7.16.11",
49
49
  "browserify": "^17.0.0",
50
- "typescript": "^4.5.2",
50
+ "typescript": "^4.5.5",
51
51
  "uglify-es": "^3.3.9"
52
52
  },
53
53
  "publishConfig": {