@weaverse/core 0.4.1 → 0.5.0

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
@@ -12,7 +12,7 @@ declare type WeaverseElementSchema = {
12
12
  title: string;
13
13
  type: string;
14
14
  settings: WeaverseElementSetting[];
15
- toolbar: TODO;
15
+ toolbar?: TODO;
16
16
  data?: TODO;
17
17
  };
18
18
  declare type WeaverseElementSetting = {
@@ -21,10 +21,10 @@ declare type WeaverseElementSetting = {
21
21
  inspectors: WeaverseElementInspector[];
22
22
  };
23
23
  declare type WeaverseElementInspector = {
24
- binding: "data" | "style";
24
+ binding?: "data" | "style";
25
25
  key?: string;
26
26
  label?: string;
27
- type?: InspectorInputType;
27
+ type: InspectorInputType;
28
28
  options?: InspectorOption[];
29
29
  default?: TODO;
30
30
  };
@@ -32,7 +32,7 @@ declare type InspectorOption = {
32
32
  value: TODO;
33
33
  label: string;
34
34
  };
35
- declare type InspectorInputType = "alignment" | "color" | "switch" | "textarea" | "select";
35
+ declare type InspectorInputType = "alignment" | "color" | "switch" | "spacing" | "textarea" | "select" | "grid" | "flex";
36
36
  declare global {
37
37
  interface Window {
38
38
  WeaverseStudioBridge: TODO;
@@ -81,11 +81,14 @@ declare type WeaverseType = {
81
81
  * ```
82
82
  */
83
83
  declare class WeaverseItemStore {
84
- data: any;
85
84
  listeners: Set<any>;
86
- Element: WeaverseElement;
87
85
  ref: any;
86
+ weaverse: Weaverse;
88
87
  constructor(itemData: any, weaverse: Weaverse);
88
+ _data: any;
89
+ get Element(): WeaverseElement;
90
+ set data(data: any);
91
+ get data(): any;
89
92
  setData: (data: any) => any;
90
93
  subscribe: (fn: any) => void;
91
94
  unsubscribe: (fn: any) => void;
@@ -162,8 +165,14 @@ declare class Weaverse {
162
165
  /**
163
166
  * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
164
167
  * @param fetch {fetch} custom fetch function, pass in custom fetch function if you want to use your own fetch function
168
+ * @param appUrl
169
+ * @param projectKey
165
170
  */
166
- fetchProjectData(fetch?: typeof globalThis.fetch): Promise<any>;
171
+ static fetchProjectData({ fetch, appUrl, projectKey, }: {
172
+ fetch?: any;
173
+ appUrl?: string;
174
+ projectKey?: string;
175
+ }): any;
167
176
  /**
168
177
  * fetch and update the project data, then trigger update to re-render the WeaverseRoot
169
178
  */
package/dist/index.js CHANGED
@@ -53,11 +53,11 @@ var isIframe = isBrowser && window.top !== window.self;
53
53
  var stitches = __toESM(require("@stitches/core"));
54
54
  var WeaverseItemStore = class {
55
55
  constructor(itemData = {}, weaverse) {
56
- this.data = {};
57
56
  this.listeners = /* @__PURE__ */ new Set();
58
57
  this.ref = {
59
58
  current: null
60
59
  };
60
+ this._data = {};
61
61
  this.setData = (data) => {
62
62
  this.data = Object.assign(this.data, data);
63
63
  this.triggerUpdate();
@@ -74,17 +74,25 @@ var WeaverseItemStore = class {
74
74
  return fn(this.data);
75
75
  });
76
76
  };
77
- var _a;
78
77
  let { type, id } = itemData;
79
- this.Element = weaverse.elementInstances.get(type);
80
- if (id) {
81
- let defaultData = (_a = this.Element.schema) == null ? void 0 : _a.data;
78
+ this.weaverse = weaverse;
79
+ if (id && type) {
82
80
  weaverse.itemInstances.set(id, this);
83
- this.data = __spreadValues(__spreadValues(__spreadValues({}, this.Element.Component.defaultProps), defaultData), itemData);
81
+ this.data = __spreadValues({}, itemData);
84
82
  }
85
83
  }
84
+ get Element() {
85
+ return this.weaverse.elementInstances.get(this._data.type);
86
+ }
87
+ set data(data) {
88
+ this._data = __spreadValues(__spreadValues({}, this.data), data);
89
+ }
90
+ get data() {
91
+ var _a, _b, _c, _d;
92
+ return __spreadValues(__spreadValues(__spreadValues({}, (_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps), (_d = (_c = this.Element) == null ? void 0 : _c.schema) == null ? void 0 : _d.data), this._data);
93
+ }
86
94
  };
87
- var Weaverse = class {
95
+ var _Weaverse = class {
88
96
  constructor({ appUrl, projectKey, projectData, mediaBreakPoints } = {}) {
89
97
  this.elementInstances = /* @__PURE__ */ new Map();
90
98
  this.itemInstances = /* @__PURE__ */ new Map();
@@ -103,8 +111,17 @@ var Weaverse = class {
103
111
  };
104
112
  this.initStitches = () => {
105
113
  this.stitchesInstance = stitches.createStitches({
106
- prefix: "we",
107
- media: this.mediaBreakPoints
114
+ prefix: "weaverse",
115
+ media: this.mediaBreakPoints,
116
+ theme: {
117
+ sizes: {
118
+ rowSize: "48px",
119
+ columns: 16
120
+ },
121
+ space: {
122
+ gap: "8px"
123
+ }
124
+ }
108
125
  });
109
126
  };
110
127
  this.appUrl = appUrl || this.appUrl;
@@ -124,29 +141,25 @@ var Weaverse = class {
124
141
  init() {
125
142
  this.initStitches();
126
143
  this.loadStudio();
144
+ this.initProjectItemData();
127
145
  }
128
146
  loadStudio() {
129
147
  if (isIframe) {
130
- window.addEventListener("message", (e) => {
131
- var _a;
132
- if (((_a = e.data) == null ? void 0 : _a.type) === "weaverse.preview.initializeEditor") {
133
- this.isEditor = true;
134
- let initStudio = () => {
135
- this.studioBridge = new window.WeaverseStudioBridge(this);
136
- this.studioBridge.subscribeMessageEvent();
137
- this.triggerUpdate();
138
- };
139
- if (!window.WeaverseStudioBridge) {
140
- const studioBridgeScript = document.createElement("script");
141
- studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
142
- studioBridgeScript.type = "module";
143
- studioBridgeScript.onload = initStudio;
144
- document.body.appendChild(studioBridgeScript);
145
- } else {
146
- initStudio();
147
- }
148
- }
149
- });
148
+ this.isEditor = true;
149
+ let initStudio = () => {
150
+ this.studioBridge = new window.WeaverseStudioBridge(this);
151
+ this.studioBridge.subscribeMessageEvent();
152
+ this.triggerUpdate();
153
+ };
154
+ if (!window.WeaverseStudioBridge) {
155
+ const studioBridgeScript = document.createElement("script");
156
+ studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
157
+ studioBridgeScript.type = "module";
158
+ studioBridgeScript.onload = initStudio;
159
+ document.body.appendChild(studioBridgeScript);
160
+ } else {
161
+ initStudio();
162
+ }
150
163
  }
151
164
  }
152
165
  subscribe(fn) {
@@ -165,12 +178,16 @@ var Weaverse = class {
165
178
  });
166
179
  }
167
180
  }
168
- fetchProjectData(fetch = globalThis.fetch) {
169
- return fetch(this.appUrl + `/api/public/${this.projectKey}`).then((r) => r.json()).catch(console.error);
181
+ static fetchProjectData({
182
+ fetch = globalThis.fetch,
183
+ appUrl,
184
+ projectKey
185
+ }) {
186
+ return fetch(appUrl + `/api/public/${projectKey}`).then((r) => r.json()).catch(console.error);
170
187
  }
171
188
  updateProjectData() {
172
189
  if (this.projectKey) {
173
- this.fetchProjectData().then((data) => {
190
+ _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
174
191
  if (data) {
175
192
  this.projectData = data;
176
193
  this.initProjectItemData();
@@ -192,6 +209,7 @@ var Weaverse = class {
192
209
  }
193
210
  }
194
211
  };
212
+ var Weaverse = _Weaverse;
195
213
  Weaverse.WeaverseItemStore = WeaverseItemStore;
196
214
  // Annotate the CommonJS export names for ESM import in node:
197
215
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -24,11 +24,11 @@ var isIframe = isBrowser && window.top !== window.self;
24
24
  import * as stitches from "@stitches/core";
25
25
  var WeaverseItemStore = class {
26
26
  constructor(itemData = {}, weaverse) {
27
- this.data = {};
28
27
  this.listeners = /* @__PURE__ */ new Set();
29
28
  this.ref = {
30
29
  current: null
31
30
  };
31
+ this._data = {};
32
32
  this.setData = (data) => {
33
33
  this.data = Object.assign(this.data, data);
34
34
  this.triggerUpdate();
@@ -45,17 +45,25 @@ var WeaverseItemStore = class {
45
45
  return fn(this.data);
46
46
  });
47
47
  };
48
- var _a;
49
48
  let { type, id } = itemData;
50
- this.Element = weaverse.elementInstances.get(type);
51
- if (id) {
52
- let defaultData = (_a = this.Element.schema) == null ? void 0 : _a.data;
49
+ this.weaverse = weaverse;
50
+ if (id && type) {
53
51
  weaverse.itemInstances.set(id, this);
54
- this.data = __spreadValues(__spreadValues(__spreadValues({}, this.Element.Component.defaultProps), defaultData), itemData);
52
+ this.data = __spreadValues({}, itemData);
55
53
  }
56
54
  }
55
+ get Element() {
56
+ return this.weaverse.elementInstances.get(this._data.type);
57
+ }
58
+ set data(data) {
59
+ this._data = __spreadValues(__spreadValues({}, this.data), data);
60
+ }
61
+ get data() {
62
+ var _a, _b, _c, _d;
63
+ return __spreadValues(__spreadValues(__spreadValues({}, (_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps), (_d = (_c = this.Element) == null ? void 0 : _c.schema) == null ? void 0 : _d.data), this._data);
64
+ }
57
65
  };
58
- var Weaverse = class {
66
+ var _Weaverse = class {
59
67
  constructor({ appUrl, projectKey, projectData, mediaBreakPoints } = {}) {
60
68
  this.elementInstances = /* @__PURE__ */ new Map();
61
69
  this.itemInstances = /* @__PURE__ */ new Map();
@@ -74,8 +82,17 @@ var Weaverse = class {
74
82
  };
75
83
  this.initStitches = () => {
76
84
  this.stitchesInstance = stitches.createStitches({
77
- prefix: "we",
78
- media: this.mediaBreakPoints
85
+ prefix: "weaverse",
86
+ media: this.mediaBreakPoints,
87
+ theme: {
88
+ sizes: {
89
+ rowSize: "48px",
90
+ columns: 16
91
+ },
92
+ space: {
93
+ gap: "8px"
94
+ }
95
+ }
79
96
  });
80
97
  };
81
98
  this.appUrl = appUrl || this.appUrl;
@@ -95,29 +112,25 @@ var Weaverse = class {
95
112
  init() {
96
113
  this.initStitches();
97
114
  this.loadStudio();
115
+ this.initProjectItemData();
98
116
  }
99
117
  loadStudio() {
100
118
  if (isIframe) {
101
- window.addEventListener("message", (e) => {
102
- var _a;
103
- if (((_a = e.data) == null ? void 0 : _a.type) === "weaverse.preview.initializeEditor") {
104
- this.isEditor = true;
105
- let initStudio = () => {
106
- this.studioBridge = new window.WeaverseStudioBridge(this);
107
- this.studioBridge.subscribeMessageEvent();
108
- this.triggerUpdate();
109
- };
110
- if (!window.WeaverseStudioBridge) {
111
- const studioBridgeScript = document.createElement("script");
112
- studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
113
- studioBridgeScript.type = "module";
114
- studioBridgeScript.onload = initStudio;
115
- document.body.appendChild(studioBridgeScript);
116
- } else {
117
- initStudio();
118
- }
119
- }
120
- });
119
+ this.isEditor = true;
120
+ let initStudio = () => {
121
+ this.studioBridge = new window.WeaverseStudioBridge(this);
122
+ this.studioBridge.subscribeMessageEvent();
123
+ this.triggerUpdate();
124
+ };
125
+ if (!window.WeaverseStudioBridge) {
126
+ const studioBridgeScript = document.createElement("script");
127
+ studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
128
+ studioBridgeScript.type = "module";
129
+ studioBridgeScript.onload = initStudio;
130
+ document.body.appendChild(studioBridgeScript);
131
+ } else {
132
+ initStudio();
133
+ }
121
134
  }
122
135
  }
123
136
  subscribe(fn) {
@@ -136,12 +149,16 @@ var Weaverse = class {
136
149
  });
137
150
  }
138
151
  }
139
- fetchProjectData(fetch = globalThis.fetch) {
140
- return fetch(this.appUrl + `/api/public/${this.projectKey}`).then((r) => r.json()).catch(console.error);
152
+ static fetchProjectData({
153
+ fetch = globalThis.fetch,
154
+ appUrl,
155
+ projectKey
156
+ }) {
157
+ return fetch(appUrl + `/api/public/${projectKey}`).then((r) => r.json()).catch(console.error);
141
158
  }
142
159
  updateProjectData() {
143
160
  if (this.projectKey) {
144
- this.fetchProjectData().then((data) => {
161
+ _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
145
162
  if (data) {
146
163
  this.projectData = data;
147
164
  this.initProjectItemData();
@@ -163,6 +180,7 @@ var Weaverse = class {
163
180
  }
164
181
  }
165
182
  };
183
+ var Weaverse = _Weaverse;
166
184
  Weaverse.WeaverseItemStore = WeaverseItemStore;
167
185
  export {
168
186
  Weaverse,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.4.1",
2
+ "version": "0.5.0",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",