@weaverse/core 0.6.24 → 0.6.26

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.
package/dist/index.d.ts CHANGED
@@ -48,6 +48,13 @@ interface ProjectDataType {
48
48
  css: string;
49
49
  js: string;
50
50
  };
51
+ pageId?: string;
52
+ }
53
+ interface InitializeData {
54
+ id: string;
55
+ handle: string;
56
+ data: ProjectDataType;
57
+ published: boolean;
51
58
  }
52
59
  interface WeaverseType {
53
60
  mediaBreakPoints?: any;
@@ -122,7 +129,7 @@ declare type BasicGroup = {
122
129
  label: string;
123
130
  inputs: BasicInput[];
124
131
  };
125
- declare type AdvancedInput = "alignment" | "background" | "border" | "countdown" | "dimensions" | "form" | "instagram" | "product" | "spacing" | "visibility";
132
+ declare type AdvancedInput = "alignment" | "background" | "border" | "countdown" | "dimensions" | "form" | "product" | "spacing" | "visibility";
126
133
  interface BasicInput {
127
134
  type: InputType;
128
135
  label: string;
@@ -133,10 +140,14 @@ interface BasicInput {
133
140
  options?: {
134
141
  value: string;
135
142
  label: string;
143
+ icon?: string;
136
144
  }[];
145
+ min?: number;
146
+ max?: number;
147
+ step?: number;
137
148
  conditions?: TODO[];
138
149
  }
139
- declare type InputType = "select" | "radio" | "range" | "button" | "image" | "color" | "text" | "switch" | "textarea";
150
+ declare type InputType = "color" | "image" | "instagram" | "range" | "select" | "switch" | "text" | "textarea" | "toggle-group";
140
151
  declare global {
141
152
  interface Window {
142
153
  WeaverseStudioBridge: TODO;
@@ -211,6 +222,13 @@ declare class Weaverse {
211
222
  * @type {Map<string, (data: any) => void>}
212
223
  */
213
224
  listeners: Set<any>;
225
+ /**
226
+ * check whether the sdk is isDesignMode or not
227
+ * if isDesignMode is true, it means the sdk is isDesignMode mode, render the editor UI
228
+ * else render the preview UI, plain HTML + CSS + React hydrate
229
+ * @type {boolean}
230
+ */
231
+ isDesignMode: boolean;
214
232
  /**
215
233
  * Use in element to optionally render special HTML for hydration
216
234
  * @type {boolean}
@@ -232,16 +250,19 @@ declare class Weaverse {
232
250
  * @param projectKey {string} Weaverse project key to access project data via API
233
251
  * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
234
252
  * @param mediaBreakPoints {object} Pass down custom media query breakpoints or just use the default.
253
+ * @param isDesignMode {boolean} check whether the sdk is isDesignMode or not
235
254
  * @param ssrMode {boolean} Use in element to optionally render special HTML for hydration
236
255
  */
237
- constructor({ appUrl, projectKey, projectData, mediaBreakPoints, ssrMode }?: WeaverseType);
256
+ constructor({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode }?: WeaverseType);
257
+ init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode }?: WeaverseType): void;
258
+ initialized: boolean;
259
+ initializeData: (data: InitializeData) => void;
238
260
  loadStudio(): void;
239
261
  /**
240
262
  * register the custom React Component to Weaverse, store it into Weaverse.elementInstances
241
263
  * @param element {WeaverseElement} custom React Component
242
264
  */
243
265
  registerElement(element: WeaverseElement): void;
244
- init(): void;
245
266
  initStitches: () => void;
246
267
  subscribe(fn: any): void;
247
268
  unsubscribe(fn: any): void;
@@ -261,7 +282,6 @@ declare class Weaverse {
261
282
  /**
262
283
  * fetch and update the project data, then trigger update to re-render the WeaverseRoot
263
284
  */
264
- updateProjectData(): void;
265
285
  initProjectItemData(): void;
266
286
  }
267
287
 
@@ -269,4 +289,4 @@ declare const isReactNative: boolean;
269
289
  declare const isBrowser: boolean;
270
290
  declare const isIframe: boolean;
271
291
 
272
- export { AdvancedGroup, AdvancedInput, BasicGroup, BasicInput, CatalogGroup, ChildElement, ChildElementCSS, ElementCSS, ElementCatalog, ElementData, ElementFlags, ElementInspector, ElementSchema, ElementsMap, FlagType, GridSize, InputType, ParentType, ProjectDataType, TODO, ToolbarAction, Weaverse, WeaverseCSSProperties, WeaverseElement, WeaverseItemStore, WeaverseType, isBrowser, isIframe, isReactNative };
292
+ export { AdvancedGroup, AdvancedInput, BasicGroup, BasicInput, CatalogGroup, ChildElement, ChildElementCSS, ElementCSS, ElementCatalog, ElementData, ElementFlags, ElementInspector, ElementSchema, ElementsMap, FlagType, GridSize, InitializeData, InputType, ParentType, ProjectDataType, TODO, ToolbarAction, Weaverse, WeaverseCSSProperties, WeaverseElement, WeaverseItemStore, WeaverseType, isBrowser, isIframe, isReactNative };
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
7
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
8
  var __getProtoOf = Object.getPrototypeOf;
@@ -18,6 +20,7 @@ var __spreadValues = (a, b) => {
18
20
  }
19
21
  return a;
20
22
  };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
24
  var __export = (target, all) => {
22
25
  for (var name in all)
23
26
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -141,8 +144,8 @@ var WeaverseItemStore = class {
141
144
  return __spreadValues(__spreadValues({}, (_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps), this._data);
142
145
  }
143
146
  };
144
- var _Weaverse = class {
145
- constructor({ appUrl, projectKey, projectData, mediaBreakPoints, ssrMode } = {}) {
147
+ var Weaverse = class {
148
+ constructor({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
146
149
  this.elementInstances = /* @__PURE__ */ new Map();
147
150
  this.itemInstances = /* @__PURE__ */ new Map();
148
151
  this.appUrl = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://weaverse.io";
@@ -153,28 +156,46 @@ var _Weaverse = class {
153
156
  script: { css: "", js: "" }
154
157
  };
155
158
  this.listeners = /* @__PURE__ */ new Set();
159
+ this.isDesignMode = false;
156
160
  this.ssrMode = false;
157
161
  this.mediaBreakPoints = {
158
162
  desktop: "all",
159
163
  mobile: "(max-width: 767.98px)"
160
164
  };
165
+ this.initialized = false;
166
+ this.initializeData = (data) => {
167
+ if (!this.initialized) {
168
+ let { data: pageData, published, id } = data;
169
+ this.projectData = __spreadProps(__spreadValues({}, pageData), { pageId: id });
170
+ this.isDesignMode = !published;
171
+ this.initProjectItemData();
172
+ this.triggerUpdate();
173
+ this.loadStudio();
174
+ }
175
+ this.initialized = true;
176
+ };
161
177
  this.initStitches = () => {
162
- this.stitchesInstance = stitches.createStitches({
178
+ this.stitchesInstance = this.stitchesInstance || stitches.createStitches({
163
179
  prefix: "weaverse",
164
180
  media: this.mediaBreakPoints,
165
181
  utils: stichesUtils
166
182
  });
167
183
  };
184
+ this.init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode });
185
+ }
186
+ init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
168
187
  this.appUrl = appUrl || this.appUrl;
169
188
  this.projectKey = projectKey || this.projectKey;
170
189
  this.mediaBreakPoints = mediaBreakPoints || this.mediaBreakPoints;
190
+ this.isDesignMode = isDesignMode || this.isDesignMode;
171
191
  this.ssrMode = ssrMode || this.ssrMode;
172
- projectData && (this.projectData = projectData);
173
- this.init();
192
+ this.projectData = projectData || this.projectData;
193
+ this.initStitches();
194
+ this.initProjectItemData();
174
195
  }
175
196
  loadStudio() {
176
197
  setTimeout(() => {
177
- if (isIframe) {
198
+ if (isIframe && this.isDesignMode && !this.studioBridge) {
178
199
  const initStudio = () => {
179
200
  this.studioBridge = new window.WeaverseStudioBridge(this);
180
201
  this.triggerUpdate();
@@ -201,10 +222,6 @@ var _Weaverse = class {
201
222
  throw new Error("Weaverse: registerElement: `type` is required");
202
223
  }
203
224
  }
204
- init() {
205
- this.initStitches();
206
- this.initProjectItemData();
207
- }
208
225
  subscribe(fn) {
209
226
  this.listeners.add(fn);
210
227
  }
@@ -219,7 +236,6 @@ var _Weaverse = class {
219
236
  appUrl,
220
237
  projectKey
221
238
  }) {
222
- console.log("Weaverse: fetchProjectData", appUrl, projectKey);
223
239
  return fetch(appUrl + `/api/public/${projectKey}`).then((r) => r.json()).catch(console.error);
224
240
  }
225
241
  setProjectData(projectData) {
@@ -227,22 +243,9 @@ var _Weaverse = class {
227
243
  this.initProjectItemData();
228
244
  this.triggerUpdate();
229
245
  }
230
- updateProjectData() {
231
- if (this.projectKey && !this.projectData.rootId) {
232
- _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
233
- if (data) {
234
- this.projectData = data;
235
- this.initProjectItemData();
236
- this.triggerUpdate();
237
- }
238
- }).catch((err) => {
239
- console.error(err);
240
- });
241
- }
242
- }
243
246
  initProjectItemData() {
244
247
  const data = this.projectData;
245
- if (data.items) {
248
+ if (data == null ? void 0 : data.items) {
246
249
  data.items.forEach((item) => {
247
250
  if (!this.itemInstances.get(item.id)) {
248
251
  new WeaverseItemStore(item, this);
@@ -251,7 +254,6 @@ var _Weaverse = class {
251
254
  }
252
255
  }
253
256
  };
254
- var Weaverse = _Weaverse;
255
257
  Weaverse.WeaverseItemStore = WeaverseItemStore;
256
258
  // Annotate the CommonJS export names for ESM import in node:
257
259
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
2
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
4
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -14,6 +16,7 @@ var __spreadValues = (a, b) => {
14
16
  }
15
17
  return a;
16
18
  };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
17
20
 
18
21
  // src/core.ts
19
22
  import * as stitches from "@stitches/core";
@@ -112,8 +115,8 @@ var WeaverseItemStore = class {
112
115
  return __spreadValues(__spreadValues({}, (_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps), this._data);
113
116
  }
114
117
  };
115
- var _Weaverse = class {
116
- constructor({ appUrl, projectKey, projectData, mediaBreakPoints, ssrMode } = {}) {
118
+ var Weaverse = class {
119
+ constructor({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
117
120
  this.elementInstances = /* @__PURE__ */ new Map();
118
121
  this.itemInstances = /* @__PURE__ */ new Map();
119
122
  this.appUrl = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://weaverse.io";
@@ -124,28 +127,46 @@ var _Weaverse = class {
124
127
  script: { css: "", js: "" }
125
128
  };
126
129
  this.listeners = /* @__PURE__ */ new Set();
130
+ this.isDesignMode = false;
127
131
  this.ssrMode = false;
128
132
  this.mediaBreakPoints = {
129
133
  desktop: "all",
130
134
  mobile: "(max-width: 767.98px)"
131
135
  };
136
+ this.initialized = false;
137
+ this.initializeData = (data) => {
138
+ if (!this.initialized) {
139
+ let { data: pageData, published, id } = data;
140
+ this.projectData = __spreadProps(__spreadValues({}, pageData), { pageId: id });
141
+ this.isDesignMode = !published;
142
+ this.initProjectItemData();
143
+ this.triggerUpdate();
144
+ this.loadStudio();
145
+ }
146
+ this.initialized = true;
147
+ };
132
148
  this.initStitches = () => {
133
- this.stitchesInstance = stitches.createStitches({
149
+ this.stitchesInstance = this.stitchesInstance || stitches.createStitches({
134
150
  prefix: "weaverse",
135
151
  media: this.mediaBreakPoints,
136
152
  utils: stichesUtils
137
153
  });
138
154
  };
155
+ this.init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode });
156
+ }
157
+ init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
139
158
  this.appUrl = appUrl || this.appUrl;
140
159
  this.projectKey = projectKey || this.projectKey;
141
160
  this.mediaBreakPoints = mediaBreakPoints || this.mediaBreakPoints;
161
+ this.isDesignMode = isDesignMode || this.isDesignMode;
142
162
  this.ssrMode = ssrMode || this.ssrMode;
143
- projectData && (this.projectData = projectData);
144
- this.init();
163
+ this.projectData = projectData || this.projectData;
164
+ this.initStitches();
165
+ this.initProjectItemData();
145
166
  }
146
167
  loadStudio() {
147
168
  setTimeout(() => {
148
- if (isIframe) {
169
+ if (isIframe && this.isDesignMode && !this.studioBridge) {
149
170
  const initStudio = () => {
150
171
  this.studioBridge = new window.WeaverseStudioBridge(this);
151
172
  this.triggerUpdate();
@@ -172,10 +193,6 @@ var _Weaverse = class {
172
193
  throw new Error("Weaverse: registerElement: `type` is required");
173
194
  }
174
195
  }
175
- init() {
176
- this.initStitches();
177
- this.initProjectItemData();
178
- }
179
196
  subscribe(fn) {
180
197
  this.listeners.add(fn);
181
198
  }
@@ -190,7 +207,6 @@ var _Weaverse = class {
190
207
  appUrl,
191
208
  projectKey
192
209
  }) {
193
- console.log("Weaverse: fetchProjectData", appUrl, projectKey);
194
210
  return fetch(appUrl + `/api/public/${projectKey}`).then((r) => r.json()).catch(console.error);
195
211
  }
196
212
  setProjectData(projectData) {
@@ -198,22 +214,9 @@ var _Weaverse = class {
198
214
  this.initProjectItemData();
199
215
  this.triggerUpdate();
200
216
  }
201
- updateProjectData() {
202
- if (this.projectKey && !this.projectData.rootId) {
203
- _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
204
- if (data) {
205
- this.projectData = data;
206
- this.initProjectItemData();
207
- this.triggerUpdate();
208
- }
209
- }).catch((err) => {
210
- console.error(err);
211
- });
212
- }
213
- }
214
217
  initProjectItemData() {
215
218
  const data = this.projectData;
216
- if (data.items) {
219
+ if (data == null ? void 0 : data.items) {
217
220
  data.items.forEach((item) => {
218
221
  if (!this.itemInstances.get(item.id)) {
219
222
  new WeaverseItemStore(item, this);
@@ -222,7 +225,6 @@ var _Weaverse = class {
222
225
  }
223
226
  }
224
227
  };
225
- var Weaverse = _Weaverse;
226
228
  Weaverse.WeaverseItemStore = WeaverseItemStore;
227
229
  export {
228
230
  Weaverse,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.6.24",
2
+ "version": "0.6.26",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",