@weaverse/core 0.4.0 → 0.4.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.
package/dist/index.d.ts CHANGED
@@ -1,16 +1,19 @@
1
1
  import Stitches from '@stitches/core/types/stitches';
2
2
 
3
+ declare type TODO = any;
3
4
  declare type WeaverseElementMap = {
4
5
  [key: string]: WeaverseElement;
5
6
  };
6
7
  declare type WeaverseElement = {
7
- Component: any;
8
+ Component: TODO;
8
9
  schema: WeaverseElementSchema;
9
10
  };
10
11
  declare type WeaverseElementSchema = {
11
12
  title: string;
12
13
  type: string;
13
14
  settings: WeaverseElementSetting[];
15
+ toolbar?: TODO;
16
+ data?: TODO;
14
17
  };
15
18
  declare type WeaverseElementSetting = {
16
19
  tab: string;
@@ -18,15 +21,21 @@ declare type WeaverseElementSetting = {
18
21
  inspectors: WeaverseElementInspector[];
19
22
  };
20
23
  declare type WeaverseElementInspector = {
21
- binding: 'data' | 'style';
22
- key: string;
23
- label: string;
24
+ binding?: "data" | "style";
25
+ key?: string;
26
+ label?: string;
24
27
  type: InspectorInputType;
28
+ options?: InspectorOption[];
29
+ default?: TODO;
30
+ };
31
+ declare type InspectorOption = {
32
+ value: TODO;
33
+ label: string;
25
34
  };
26
- declare type InspectorInputType = "alignment" | "color" | "switch" | "textarea";
35
+ declare type InspectorInputType = "alignment" | "color" | "switch" | "spacing" | "textarea" | "select" | "grid" | "flex";
27
36
  declare global {
28
37
  interface Window {
29
- WeaverseStudioBridge: any;
38
+ WeaverseStudioBridge: TODO;
30
39
  }
31
40
  }
32
41
 
@@ -72,12 +81,15 @@ declare type WeaverseType = {
72
81
  * ```
73
82
  */
74
83
  declare class WeaverseItemStore {
75
- data: any;
76
84
  listeners: Set<any>;
77
- Component: any;
78
85
  ref: any;
86
+ weaverse: Weaverse;
79
87
  constructor(itemData: any, weaverse: Weaverse);
80
- setData: (data: any) => void;
88
+ _data: any;
89
+ get Element(): WeaverseElement;
90
+ set data(data: any);
91
+ get data(): any;
92
+ setData: (data: any) => any;
81
93
  subscribe: (fn: any) => void;
82
94
  unsubscribe: (fn: any) => void;
83
95
  triggerUpdate: () => void;
@@ -153,8 +165,14 @@ declare class Weaverse {
153
165
  /**
154
166
  * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
155
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
156
170
  */
157
- fetchProjectData(fetch?: typeof globalThis.fetch): Promise<any>;
171
+ static fetchProjectData({ fetch, appUrl, projectKey, }: {
172
+ fetch?: any;
173
+ appUrl?: string;
174
+ projectKey?: string;
175
+ }): any;
158
176
  /**
159
177
  * fetch and update the project data, then trigger update to re-render the WeaverseRoot
160
178
  */
@@ -166,4 +184,4 @@ declare const isReactNative: boolean;
166
184
  declare const isBrowser: boolean;
167
185
  declare const isIframe: boolean;
168
186
 
169
- export { InspectorInputType, MediaBreakpoints, ProjectDataItemType, ProjectDataType, Weaverse, WeaverseElement, WeaverseElementInspector, WeaverseElementMap, WeaverseElementSchema, WeaverseElementSetting, WeaverseItemStore, WeaverseType, isBrowser, isIframe, isReactNative };
187
+ export { InspectorInputType, InspectorOption, MediaBreakpoints, ProjectDataItemType, ProjectDataType, TODO, Weaverse, WeaverseElement, WeaverseElementInspector, WeaverseElementMap, WeaverseElementSchema, WeaverseElementSetting, WeaverseItemStore, WeaverseType, isBrowser, isIframe, isReactNative };
package/dist/index.js CHANGED
@@ -2,8 +2,22 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
6
  var __getProtoOf = Object.getPrototypeOf;
6
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
7
21
  var __export = (target, all) => {
8
22
  for (var name in all)
9
23
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -39,14 +53,15 @@ var isIframe = isBrowser && window.top !== window.self;
39
53
  var stitches = __toESM(require("@stitches/core"));
40
54
  var WeaverseItemStore = class {
41
55
  constructor(itemData = {}, weaverse) {
42
- this.data = {};
43
56
  this.listeners = /* @__PURE__ */ new Set();
44
57
  this.ref = {
45
58
  current: null
46
59
  };
60
+ this._data = {};
47
61
  this.setData = (data) => {
48
62
  this.data = Object.assign(this.data, data);
49
63
  this.triggerUpdate();
64
+ return this.data;
50
65
  };
51
66
  this.subscribe = (fn) => {
52
67
  this.listeners.add(fn);
@@ -59,15 +74,25 @@ var WeaverseItemStore = class {
59
74
  return fn(this.data);
60
75
  });
61
76
  };
62
- this.data = itemData;
63
77
  let { type, id } = itemData;
64
- if (type && id) {
65
- this.Component = weaverse.elementInstances.get(type);
78
+ this.weaverse = weaverse;
79
+ if (id && type) {
66
80
  weaverse.itemInstances.set(id, this);
81
+ this.data = __spreadValues({}, itemData);
67
82
  }
68
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
+ }
69
94
  };
70
- var Weaverse = class {
95
+ var _Weaverse = class {
71
96
  constructor({ appUrl, projectKey, projectData, mediaBreakPoints } = {}) {
72
97
  this.elementInstances = /* @__PURE__ */ new Map();
73
98
  this.itemInstances = /* @__PURE__ */ new Map();
@@ -86,8 +111,17 @@ var Weaverse = class {
86
111
  };
87
112
  this.initStitches = () => {
88
113
  this.stitchesInstance = stitches.createStitches({
89
- prefix: "we",
90
- 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
+ }
91
125
  });
92
126
  };
93
127
  this.appUrl = appUrl || this.appUrl;
@@ -107,29 +141,25 @@ var Weaverse = class {
107
141
  init() {
108
142
  this.initStitches();
109
143
  this.loadStudio();
144
+ this.initProjectItemData();
110
145
  }
111
146
  loadStudio() {
112
147
  if (isIframe) {
113
- window.addEventListener("message", (e) => {
114
- var _a;
115
- if (((_a = e.data) == null ? void 0 : _a.type) === "weaverse.preview.initializeEditor") {
116
- this.isEditor = true;
117
- let initStudio = () => {
118
- this.studioBridge = new window.WeaverseStudioBridge(this);
119
- this.studioBridge.subscribeMessageEvent();
120
- this.triggerUpdate();
121
- };
122
- if (!window.WeaverseStudioBridge) {
123
- const studioBridgeScript = document.createElement("script");
124
- studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
125
- studioBridgeScript.type = "module";
126
- studioBridgeScript.onload = initStudio;
127
- document.body.appendChild(studioBridgeScript);
128
- } else {
129
- initStudio();
130
- }
131
- }
132
- });
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
+ }
133
163
  }
134
164
  }
135
165
  subscribe(fn) {
@@ -148,12 +178,16 @@ var Weaverse = class {
148
178
  });
149
179
  }
150
180
  }
151
- fetchProjectData(fetch = globalThis.fetch) {
152
- 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);
153
187
  }
154
188
  updateProjectData() {
155
189
  if (this.projectKey) {
156
- this.fetchProjectData().then((data) => {
190
+ _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
157
191
  if (data) {
158
192
  this.projectData = data;
159
193
  this.initProjectItemData();
@@ -175,6 +209,7 @@ var Weaverse = class {
175
209
  }
176
210
  }
177
211
  };
212
+ var Weaverse = _Weaverse;
178
213
  Weaverse.WeaverseItemStore = WeaverseItemStore;
179
214
  // Annotate the CommonJS export names for ESM import in node:
180
215
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,3 +1,20 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
1
18
  // src/utils/index.ts
2
19
  var isReactNative = typeof navigator === "object" && navigator.product === "ReactNative";
3
20
  var isBrowser = typeof window !== "undefined" && !isReactNative;
@@ -7,14 +24,15 @@ var isIframe = isBrowser && window.top !== window.self;
7
24
  import * as stitches from "@stitches/core";
8
25
  var WeaverseItemStore = class {
9
26
  constructor(itemData = {}, weaverse) {
10
- this.data = {};
11
27
  this.listeners = /* @__PURE__ */ new Set();
12
28
  this.ref = {
13
29
  current: null
14
30
  };
31
+ this._data = {};
15
32
  this.setData = (data) => {
16
33
  this.data = Object.assign(this.data, data);
17
34
  this.triggerUpdate();
35
+ return this.data;
18
36
  };
19
37
  this.subscribe = (fn) => {
20
38
  this.listeners.add(fn);
@@ -27,15 +45,25 @@ var WeaverseItemStore = class {
27
45
  return fn(this.data);
28
46
  });
29
47
  };
30
- this.data = itemData;
31
48
  let { type, id } = itemData;
32
- if (type && id) {
33
- this.Component = weaverse.elementInstances.get(type);
49
+ this.weaverse = weaverse;
50
+ if (id && type) {
34
51
  weaverse.itemInstances.set(id, this);
52
+ this.data = __spreadValues({}, itemData);
35
53
  }
36
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
+ }
37
65
  };
38
- var Weaverse = class {
66
+ var _Weaverse = class {
39
67
  constructor({ appUrl, projectKey, projectData, mediaBreakPoints } = {}) {
40
68
  this.elementInstances = /* @__PURE__ */ new Map();
41
69
  this.itemInstances = /* @__PURE__ */ new Map();
@@ -54,8 +82,17 @@ var Weaverse = class {
54
82
  };
55
83
  this.initStitches = () => {
56
84
  this.stitchesInstance = stitches.createStitches({
57
- prefix: "we",
58
- 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
+ }
59
96
  });
60
97
  };
61
98
  this.appUrl = appUrl || this.appUrl;
@@ -75,29 +112,25 @@ var Weaverse = class {
75
112
  init() {
76
113
  this.initStitches();
77
114
  this.loadStudio();
115
+ this.initProjectItemData();
78
116
  }
79
117
  loadStudio() {
80
118
  if (isIframe) {
81
- window.addEventListener("message", (e) => {
82
- var _a;
83
- if (((_a = e.data) == null ? void 0 : _a.type) === "weaverse.preview.initializeEditor") {
84
- this.isEditor = true;
85
- let initStudio = () => {
86
- this.studioBridge = new window.WeaverseStudioBridge(this);
87
- this.studioBridge.subscribeMessageEvent();
88
- this.triggerUpdate();
89
- };
90
- if (!window.WeaverseStudioBridge) {
91
- const studioBridgeScript = document.createElement("script");
92
- studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
93
- studioBridgeScript.type = "module";
94
- studioBridgeScript.onload = initStudio;
95
- document.body.appendChild(studioBridgeScript);
96
- } else {
97
- initStudio();
98
- }
99
- }
100
- });
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
+ }
101
134
  }
102
135
  }
103
136
  subscribe(fn) {
@@ -116,12 +149,16 @@ var Weaverse = class {
116
149
  });
117
150
  }
118
151
  }
119
- fetchProjectData(fetch = globalThis.fetch) {
120
- 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);
121
158
  }
122
159
  updateProjectData() {
123
160
  if (this.projectKey) {
124
- this.fetchProjectData().then((data) => {
161
+ _Weaverse.fetchProjectData({ appUrl: this.appUrl, projectKey: this.projectKey }).then((data) => {
125
162
  if (data) {
126
163
  this.projectData = data;
127
164
  this.initProjectItemData();
@@ -143,6 +180,7 @@ var Weaverse = class {
143
180
  }
144
181
  }
145
182
  };
183
+ var Weaverse = _Weaverse;
146
184
  Weaverse.WeaverseItemStore = WeaverseItemStore;
147
185
  export {
148
186
  Weaverse,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.4.0",
2
+ "version": "0.4.4",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",