@weaverse/core 0.2.2 → 0.2.3

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.
@@ -11,96 +11,6 @@ var isReactNative = typeof navigator === 'object' && navigator.product === 'Reac
11
11
  var isBrowser = typeof window !== 'undefined' && !isReactNative;
12
12
  var isIframe = isBrowser && window.top !== window.self;
13
13
 
14
- /**
15
- * When the editor is ready, we'll load this studio-bridge code.
16
- * This is for processing data between the editor and the workspace project.
17
- * It will not load in production mode or not running when the editor is not detected/ready.
18
- */
19
-
20
- var StudioBridge = /*#__PURE__*/function () {
21
- function StudioBridge(weaverse) {
22
- var _this = this;
23
-
24
- /**
25
- * handle the message event from editor(parent window)
26
- * the message type will start with "weavers.",
27
- * when an item got message to update, get the item instance and setData to it
28
- * @param event {MessageEvent}
29
- */
30
- this.handleMessageEvent = function (event) {
31
- var _event$data, _event$data$type;
32
-
33
- if ((_event$data = event.data) != null && (_event$data$type = _event$data.type) != null && _event$data$type.startsWith('weaverse.')) {
34
- var type = event.data.type;
35
-
36
- switch (type) {
37
- case 'weaverse.editor.ready':
38
- _this.weaverse.isEditor = true;
39
- break;
40
-
41
- case 'weaverse.editor.update':
42
- var payload = event.data.payload;
43
- var itemId = payload.itemId,
44
- background = payload.background;
45
-
46
- var instance = _this.weaverse.itemInstances.get(itemId);
47
-
48
- if (instance) {
49
- instance.setData({
50
- css: {
51
- background: background
52
- }
53
- });
54
- }
55
-
56
- }
57
- }
58
- };
59
-
60
- this.handleMouseDown = function (e) {
61
- console.log('handleMouseDown', e);
62
-
63
- _this.sendMessageToEditor('weaverse.editor.mouseDown', {});
64
- };
65
-
66
- this.weaverse = weaverse;
67
- this.handleProps = {
68
- onMouseDownCapture: this.handleMouseDown.bind(this)
69
- };
70
- }
71
- /**
72
- * subscribe to window message event from editor(parent window), and handle the message event
73
- */
74
-
75
-
76
- var _proto = StudioBridge.prototype;
77
-
78
- _proto.subscribeMessageEvent = function subscribeMessageEvent() {
79
- var _this2 = this;
80
-
81
- if (typeof this.currentFrameSubscription === 'function') {
82
- this.currentFrameSubscription();
83
- }
84
-
85
- isBrowser && window.addEventListener('message', this.handleMessageEvent);
86
-
87
- this.currentFrameSubscription = function () {
88
- isBrowser && window.removeEventListener('message', _this2.handleMessageEvent);
89
- };
90
-
91
- return this.currentFrameSubscription;
92
- };
93
-
94
- _proto.sendMessageToEditor = function sendMessageToEditor(type, payload) {
95
- isBrowser && window.parent.postMessage({
96
- type: type,
97
- payload: payload
98
- }, '*');
99
- };
100
-
101
- return StudioBridge;
102
- }();
103
-
104
14
  // TODO: Implement Weaverse SDK class
105
15
  // using stitches core only for framework-agnostic code
106
16
 
@@ -192,20 +102,41 @@ var Weaverse = /*#__PURE__*/function () {
192
102
  bp3: '(min-width: 1024px)'
193
103
  }
194
104
  });
105
+ this.loadStudio();
195
106
  };
196
107
 
197
108
  _proto.loadStudio = function loadStudio() {
198
- console.log('"development"', "development"); // only load studio bridge if it's in editor mode
199
-
200
- {
201
- // TODO: separate studio bridge into a separate package
202
- // in development mode, we should use localhost:3000 as appUrl
203
- // this.appUrl = 'http://localhost:3000'
204
- // let StudioBridge = require('./studio-bridge')
205
- this.studioBridge = new StudioBridge(this);
206
- console.log('Weaverse: init studio bridge', this.studioBridge);
207
- this.studioBridge.subscribeMessageEvent();
208
- return true;
109
+ var _this = this;
110
+
111
+ if (isIframe) {
112
+ var initStudio = function initStudio() {
113
+ var StudioBridge = window.WeaverseStudioBridge;
114
+ _this.studioBridge = new StudioBridge(_this);
115
+
116
+ _this.studioBridge.subscribeMessageEvent();
117
+
118
+ _this.triggerUpdate();
119
+
120
+ console.log('studio bridge initialized', _this.studioBridge);
121
+ };
122
+
123
+ window.addEventListener('message', function (e) {
124
+ var _e$data;
125
+
126
+ if (((_e$data = e.data) == null ? void 0 : _e$data.type) === 'weaverse.editor.ready') {
127
+ _this.isEditor = true;
128
+
129
+ if (!window.WeaverseStudioBridge) {
130
+ // load studio bridge script by url: https://weaverse.io/assets/studio/studio-bridge.js
131
+ var studioBridgeScript = document.createElement('script');
132
+ studioBridgeScript.src = _this.appUrl + "/assets/studio/studio-bridge.js";
133
+ studioBridgeScript.onload = initStudio;
134
+ document.body.appendChild(studioBridgeScript);
135
+ } else {
136
+ initStudio();
137
+ }
138
+ }
139
+ });
209
140
  }
210
141
  };
211
142
 
@@ -265,15 +196,15 @@ var Weaverse = /*#__PURE__*/function () {
265
196
  };
266
197
 
267
198
  _proto.initItemData = function initItemData() {
268
- var _this2 = this;
199
+ var _this3 = this;
269
200
 
270
201
  var data = this.projectData;
271
202
 
272
203
  if (data.items) {
273
204
  data.items.forEach(function (item) {
274
- var itemStore = _this2.itemInstances.get(item.id) || new WeaverseItemStore(item, _this2);
205
+ var itemStore = _this3.itemInstances.get(item.id) || new WeaverseItemStore(item, _this3);
275
206
 
276
- _this2.itemInstances.set(item.id, itemStore);
207
+ _this3.itemInstances.set(item.id, itemStore);
277
208
  });
278
209
  }
279
210
  };
@@ -299,7 +230,7 @@ var Weaverse = /*#__PURE__*/function () {
299
230
  */
300
231
 
301
232
  var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
302
- var _this3 = this;
233
+ var _this4 = this;
303
234
 
304
235
  if (itemData === void 0) {
305
236
  itemData = {};
@@ -312,22 +243,22 @@ var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
312
243
  };
313
244
 
314
245
  this.setData = function (data) {
315
- _this3.data = Object.assign(_this3.data, data);
246
+ _this4.data = Object.assign(_this4.data, data);
316
247
 
317
- _this3.triggerUpdate();
248
+ _this4.triggerUpdate();
318
249
  };
319
250
 
320
251
  this.subscribe = function (fn) {
321
- _this3.listeners.add(fn);
252
+ _this4.listeners.add(fn);
322
253
  };
323
254
 
324
255
  this.unsubscribe = function (fn) {
325
- _this3.listeners["delete"](fn);
256
+ _this4.listeners["delete"](fn);
326
257
  };
327
258
 
328
259
  this.triggerUpdate = function () {
329
- _this3.listeners.forEach(function (fn) {
330
- return fn(_this3.data);
260
+ _this4.listeners.forEach(function (fn) {
261
+ return fn(_this4.data);
331
262
  });
332
263
  };
333
264
 
@@ -1 +1 @@
1
- {"version":3,"file":"core.cjs.development.js","sources":["../src/utils/index.ts","../src/studio-bridge.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","import {isBrowser} from './utils'\nimport type {Weaverse} from '.'\n\n/**\n * When the editor is ready, we'll load this studio-bridge code.\n * This is for processing data between the editor and the workspace project.\n * It will not load in production mode or not running when the editor is not detected/ready.\n */\n\nexport class StudioBridge {\n weaverse: Weaverse\n\n /**\n * instance for subscribing to window message, save it to currentFrameSubscription then can remove it when unmount\n */\n currentFrameSubscription: any\n handleProps: any\n constructor(weaverse: Weaverse) {\n this.weaverse = weaverse\n this.handleProps = {\n onMouseDownCapture: this.handleMouseDown.bind(this)\n }\n }\n\n /**\n * subscribe to window message event from editor(parent window), and handle the message event\n */\n subscribeMessageEvent() {\n if (typeof this.currentFrameSubscription === 'function') {\n this.currentFrameSubscription()\n }\n isBrowser && window.addEventListener('message', this.handleMessageEvent)\n this.currentFrameSubscription = () => {\n isBrowser && window.removeEventListener('message', this.handleMessageEvent)\n }\n return this.currentFrameSubscription\n }\n\n /**\n * handle the message event from editor(parent window)\n * the message type will start with \"weavers.\",\n * when an item got message to update, get the item instance and setData to it\n * @param event {MessageEvent}\n */\n handleMessageEvent = (event: MessageEvent) => {\n if (event.data?.type?.startsWith('weaverse.')) {\n let type = event.data.type\n switch (type) {\n case 'weaverse.editor.ready':\n this.weaverse.isEditor = true\n break\n case 'weaverse.editor.update':\n let {payload} = event.data\n let {itemId, background} = payload\n let instance = this.weaverse.itemInstances.get(itemId)\n if (instance) {\n instance.setData({\n css: {\n background\n }\n })\n }\n }\n }\n }\n\n sendMessageToEditor(type: string, payload: any) {\n isBrowser && window.parent.postMessage({type, payload}, '*')\n }\n\n handleMouseDown = (e: MouseEvent) => {\n console.log('handleMouseDown', e)\n this.sendMessageToEditor('weaverse.editor.mouseDown', {})\n }\n}\n\n\nexport default StudioBridge","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport StudioBridge from './studio-bridge'\nimport Stitches from '@stitches/core/types/stitches'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: StudioBridge\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n }\n\n loadStudio() {\n console.log('process.env.NODE_ENV', process.env.NODE_ENV)\n // only load studio bridge if it's in editor mode\n if (process.env.NODE_ENV !== 'production') {\n // TODO: separate studio bridge into a separate package\n // in development mode, we should use localhost:3000 as appUrl\n // this.appUrl = 'http://localhost:3000'\n // let StudioBridge = require('./studio-bridge')\n this.studioBridge = new StudioBridge(this)\n console.log('Weaverse: init studio bridge', this.studioBridge)\n\n this.studioBridge.subscribeMessageEvent()\n return true\n }\n return false\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","StudioBridge","weaverse","event","data","type","startsWith","isEditor","payload","itemId","background","instance","itemInstances","get","setData","css","e","console","log","sendMessageToEditor","handleProps","onMouseDownCapture","handleMouseDown","bind","subscribeMessageEvent","currentFrameSubscription","addEventListener","handleMessageEvent","removeEventListener","parent","postMessage","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","process","items","Set","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","studioBridge","subscribe","fn","listeners","add","unsubscribe","triggerUpdate","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","item","itemStore","id","WeaverseItemStore","itemData","current","Object","assign","Component"],"mappings":";;;;;;;;;IAAaA,aAAa,GAAG,OAAOC,SAAP,KAAqB,QAArB,IAAiCA,SAAS,CAACC,OAAV,KAAsB;IACvEC,SAAS,GAAG,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,CAACJ;IAC9CK,QAAQ,GAAGF,SAAS,IAAIC,MAAM,CAACE,GAAP,KAAeF,MAAM,CAACG;;ACC3D;;;;;;AAMA,IAAaC,YAAb;AAQE,wBAAYC,QAAZ;;;AAqBA;;;;;;AAMA,2BAAA,GAAqB,UAACC,KAAD;;;AACnB,yBAAIA,KAAK,CAACC,IAAV,iCAAI,YAAYC,IAAhB,aAAI,iBAAkBC,UAAlB,CAA6B,WAA7B,CAAJ,EAA+C;AAC7C,YAAID,IAAI,GAAGF,KAAK,CAACC,IAAN,CAAWC,IAAtB;;AACA,gBAAQA,IAAR;AACE,eAAK,uBAAL;AACE,YAAA,KAAI,CAACH,QAAL,CAAcK,QAAd,GAAyB,IAAzB;AACA;;AACF,eAAK,wBAAL;AACE,gBAAKC,OAAL,GAAgBL,KAAK,CAACC,IAAtB,CAAKI,OAAL;AACA,gBAAKC,MAAL,GAA2BD,OAA3B,CAAKC,MAAL;AAAA,gBAAaC,UAAb,GAA2BF,OAA3B,CAAaE,UAAb;;AACA,gBAAIC,QAAQ,GAAG,KAAI,CAACT,QAAL,CAAcU,aAAd,CAA4BC,GAA5B,CAAgCJ,MAAhC,CAAf;;AACA,gBAAIE,QAAJ,EAAc;A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AACD,KAFD;AAGD,GAJD;;AApBE,OAAKA,IAAL,GAAYwE,QAAZ;AACA,kBAAaA,QAAb;AAAA,MAAKvE,IAAL,aAAKA,IAAL;;AACA,MAAIA,IAAJ,EAAU;AACR,SAAK2E,SAAL,GAAiB9E,QAAQ,CAAC2C,gBAAT,CAA0BhC,GAA1B,CAA8BR,IAA9B,CAAjB;AACD;AACF,CAdH;;;;;;;;"}
1
+ {"version":3,"file":"core.cjs.development.js","sources":["../src/utils/index.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport Stitches from '@stitches/core/types/stitches'\nimport {isIframe} from './utils'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: any\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n this.loadStudio()\n }\n\n loadStudio() {\n if (isIframe) {\n let initStudio = () => {\n let StudioBridge = window.WeaverseStudioBridge\n this.studioBridge = new StudioBridge(this)\n this.studioBridge.subscribeMessageEvent()\n this.triggerUpdate()\n console.log('studio bridge initialized', this.studioBridge)\n }\n window.addEventListener('message', e => {\n if (e.data?.type === 'weaverse.editor.ready') {\n this.isEditor = true\n if (!window.WeaverseStudioBridge) {\n // load studio bridge script by url: https://weaverse.io/assets/studio/studio-bridge.js\n const studioBridgeScript = document.createElement('script')\n studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`\n studioBridgeScript.onload = initStudio\n document.body.appendChild(studioBridgeScript)\n } else {\n initStudio()\n }\n }\n })\n }\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","process","items","Set","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","initStudio","StudioBridge","WeaverseStudioBridge","studioBridge","subscribeMessageEvent","triggerUpdate","console","log","addEventListener","e","data","type","isEditor","studioBridgeScript","document","createElement","src","onload","body","appendChild","subscribe","fn","listeners","add","unsubscribe","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","postMessage","payload","item","itemStore","itemInstances","get","id","WeaverseItemStore","itemData","weaverse","current","Object","assign","Component"],"mappings":";;;;;;;;;IAAaA,aAAa,GAAG,OAAOC,SAAP,KAAqB,QAArB,IAAiCA,SAAS,CAACC,OAAV,KAAsB;IACvEC,SAAS,GAAG,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,CAACJ;IAC9CK,QAAQ,GAAGF,SAAS,IAAIC,MAAM,CAACE,GAAP,KAAeF,MAAM,CAACG;;ACF3D;AACA,AAOA;;AACA,IAAIC,QAAQ,gBAAGC,OAAO,CAAC,gBAAD,CAAtB;;AA2BA,IAAaC,QAAb;AA+CE;;;;;;AAMA;kCAA8D;QAAjDC,cAAAA;QAAQC,kBAAAA;QAAYC,mBAAAA;;AApDjC;;;;AAIA,yBAAA,GAAmB,IAAIC,GAAJ,EAAnB;AACA;;;;;AAIA,sBAAA,GAAgB,IAAIA,GAAJ,EA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AACnC,IAAD;AACR,IAAA,MAAI,CAACA,IAAL,GAAYoC,MAAM,CAACC,MAAP,CAAc,MAAI,CAACrC,IAAnB,EAAyBA,IAAzB,CAAZ;;AACA,IAAA,MAAI,CAACL,aAAL;AACD,GAHD;;AAKA,gBAAA,GAAY,UAACgB,EAAD;AACV,IAAA,MAAI,CAACC,SAAL,CAAeC,GAAf,CAAmBF,EAAnB;AACD,GAFD;;AAIA,kBAAA,GAAc,UAACA,EAAD;AACZ,IAAA,MAAI,CAACC,SAAL,WAAsBD,EAAtB;AACD,GAFD;;AAIA,oBAAA,GAAgB;AACd,IAAA,MAAI,CAACC,SAAL,CAAeG,OAAf,CAAuB,UAAAJ,EAAE;AACvB,aAAOA,EAAE,CAAC,MAAI,CAACX,IAAN,CAAT;AACD,KAFD;AAGD,GAJD;;AApBE,OAAKA,IAAL,GAAYiC,QAAZ;AACA,kBAAaA,QAAb;AAAA,MAAKhC,IAAL,aAAKA,IAAL;;AACA,MAAIA,IAAJ,EAAU;AACR,SAAKqC,SAAL,GAAiBJ,QAAQ,CAACtD,gBAAT,CAA0BkD,GAA1B,CAA8B7B,IAA9B,CAAjB;AACD;AACF,CAdH;;;;;;;;"}
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("isomorphic-unfetch"))&&"object"==typeof t&&"default"in t?t.default:t;require("process");var i="object"==typeof navigator&&"ReactNative"===navigator.product,s="undefined"!=typeof window&&!i,r=s&&window.top!==window.self,n=require("@stitches/core"),o=function(){function t(t){var e=void 0===t?{}:t,i=e.appUrl,s=e.projectKey,r=e.projectData;this.elementInstances=new Map,this.itemInstances=new Map,this.appUrl="https://weaverse.io",this.projectKey="",this.projectData={items:[]},this.listeners=new Set,this.isEditor=!1,this.appUrl=i||this.appUrl,this.projectKey=s||this.projectKey,r&&(this.projectData=r),this.init()}var i=t.prototype;return i.registerElement=function(t,e){this.elementInstances.set(t,e)},i.init=function(){this.stitchesInstance=n.createStitches({prefix:"weaverse",media:{bp1:"(min-width: 640px)",bp2:"(max-width: 768px)",bp3:"(min-width: 1024px)"}})},i.loadStudio=function(){return console.log('"production"',"production"),!1},i.subscribe=function(t){this.listeners.add(t)},i.unsubscribe=function(t){this.listeners.delete(t)},i.triggerUpdate=function(){this.listeners.forEach((function(t){return t()})),this.triggerEditorUpdate()},i.fetchProjectData=function(){return e(this.appUrl+"/api/public/"+this.projectKey).then((function(t){return t.json()})).catch(console.error)},i.updateProjectData=function(){console.log("this.projectData",this.projectData),this.initItemData(),this.triggerUpdate()},i.triggerEditorUpdate=function(t){var e;void 0===t&&(t="weaverse.workspace.init"),this.isEditor&&(null==(e=window.top)||e.postMessage({type:t,payload:{projectKey:this.projectKey,projectData:this.projectData}},"*"))},i.initItemData=function(){var t=this,e=this.projectData;e.items&&e.items.forEach((function(e){var i=t.itemInstances.get(e.id)||new a(e,t);t.itemInstances.set(e.id,i)}))},t}(),a=function(t,e){var i=this;void 0===t&&(t={}),this.data={},this.listeners=new Set,this.ref={current:null},this.setData=function(t){i.data=Object.assign(i.data,t),i.triggerUpdate()},this.subscribe=function(t){i.listeners.add(t)},this.unsubscribe=function(t){i.listeners.delete(t)},this.triggerUpdate=function(){i.listeners.forEach((function(t){return t(i.data)}))},this.data=t;var s=t.type;s&&(this.Component=e.elementInstances.get(s))};exports.Weaverse=o,exports.WeaverseItemStore=a,exports.isBrowser=s,exports.isIframe=r,exports.isReactNative=i;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("isomorphic-unfetch"))&&"object"==typeof t&&"default"in t?t.default:t;require("process");var i="object"==typeof navigator&&"ReactNative"===navigator.product,s="undefined"!=typeof window&&!i,r=s&&window.top!==window.self,n=require("@stitches/core"),a=function(){function t(t){var e=void 0===t?{}:t,i=e.appUrl,s=e.projectKey,r=e.projectData;this.elementInstances=new Map,this.itemInstances=new Map,this.appUrl="https://weaverse.io",this.projectKey="",this.projectData={items:[]},this.listeners=new Set,this.isEditor=!1,this.appUrl=i||this.appUrl,this.projectKey=s||this.projectKey,r&&(this.projectData=r),this.init()}var i=t.prototype;return i.registerElement=function(t,e){this.elementInstances.set(t,e)},i.init=function(){this.stitchesInstance=n.createStitches({prefix:"weaverse",media:{bp1:"(min-width: 640px)",bp2:"(max-width: 768px)",bp3:"(min-width: 1024px)"}}),this.loadStudio()},i.loadStudio=function(){var t=this;if(r){var e=function(){var e=window.WeaverseStudioBridge;t.studioBridge=new e(t),t.studioBridge.subscribeMessageEvent(),t.triggerUpdate(),console.log("studio bridge initialized",t.studioBridge)};window.addEventListener("message",(function(i){var s;if("weaverse.editor.ready"===(null==(s=i.data)?void 0:s.type))if(t.isEditor=!0,window.WeaverseStudioBridge)e();else{var r=document.createElement("script");r.src=t.appUrl+"/assets/studio/studio-bridge.js",r.onload=e,document.body.appendChild(r)}}))}},i.subscribe=function(t){this.listeners.add(t)},i.unsubscribe=function(t){this.listeners.delete(t)},i.triggerUpdate=function(){this.listeners.forEach((function(t){return t()})),this.triggerEditorUpdate()},i.fetchProjectData=function(){return e(this.appUrl+"/api/public/"+this.projectKey).then((function(t){return t.json()})).catch(console.error)},i.updateProjectData=function(){console.log("this.projectData",this.projectData),this.initItemData(),this.triggerUpdate()},i.triggerEditorUpdate=function(t){var e;void 0===t&&(t="weaverse.workspace.init"),this.isEditor&&(null==(e=window.top)||e.postMessage({type:t,payload:{projectKey:this.projectKey,projectData:this.projectData}},"*"))},i.initItemData=function(){var t=this,e=this.projectData;e.items&&e.items.forEach((function(e){var i=t.itemInstances.get(e.id)||new o(e,t);t.itemInstances.set(e.id,i)}))},t}(),o=function(t,e){var i=this;void 0===t&&(t={}),this.data={},this.listeners=new Set,this.ref={current:null},this.setData=function(t){i.data=Object.assign(i.data,t),i.triggerUpdate()},this.subscribe=function(t){i.listeners.add(t)},this.unsubscribe=function(t){i.listeners.delete(t)},this.triggerUpdate=function(){i.listeners.forEach((function(t){return t(i.data)}))},this.data=t;var s=t.type;s&&(this.Component=e.elementInstances.get(s))};exports.Weaverse=a,exports.WeaverseItemStore=o,exports.isBrowser=s,exports.isIframe=r,exports.isReactNative=i;
2
2
  //# sourceMappingURL=core.cjs.production.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.cjs.production.min.js","sources":["../src/utils/index.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport StudioBridge from './studio-bridge'\nimport Stitches from '@stitches/core/types/stitches'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: StudioBridge\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n }\n\n loadStudio() {\n console.log('process.env.NODE_ENV', process.env.NODE_ENV)\n // only load studio bridge if it's in editor mode\n if (process.env.NODE_ENV !== 'production') {\n // TODO: separate studio bridge into a separate package\n // in development mode, we should use localhost:3000 as appUrl\n // this.appUrl = 'http://localhost:3000'\n // let StudioBridge = require('./studio-bridge')\n this.studioBridge = new StudioBridge(this)\n console.log('Weaverse: init studio bridge', this.studioBridge)\n\n this.studioBridge.subscribeMessageEvent()\n return true\n }\n return false\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","items","Set","this","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","console","log","process","subscribe","fn","listeners","add","unsubscribe","triggerUpdate","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","type","isEditor","postMessage","payload","data","item","itemStore","_this2","itemInstances","get","id","WeaverseItemStore","itemData","weaverse","current","_this3","Object","assign","Component"],"mappings":"oLAAaA,EAAqC,iBAAdC,WAAgD,gBAAtBA,UAAUC,QAC3DC,EAA8B,oBAAXC,SAA2BJ,EAC9CK,EAAWF,GAAaC,OAAOE,MAAQF,OAAOG,KCOvDC,EAAWC,QAAQ,kBA2BVC,4CAqDmD,KAAjDC,IAAAA,OAAQC,IAAAA,WAAYC,IAAAA,kCAhDd,IAAIC,uBAKP,IAAIA,gBAKgE,sCAK/D,oBAKU,CAC7BC,MAAO,mBAMa,IAAIC,mBAOf,OAeJL,OAASA,GAAUM,KAAKN,YACxBC,WAAaA,GAAcK,KAAKL,WACrCC,IAAgBI,KAAKJ,YAAcA,QAC9BK,kCAQPC,gBAAA,SAAgBC,EAAcC,QACvBC,iBAAiBC,IAAIH,EAAMC,MAGlCH,KAAA,gBAEOM,iBAAmBhB,EAASiB,eAC/B,CACEC,OAAQ,WACRC,MAAO,CACLC,IAAK,qBACLC,IAAK,qBACLC,IAAK,4BAMbC,WAAA,kBACEC,QAAQC,IAAI,eAAwBC,eAa7B,KAGTC,UAAA,SAAUC,QACHC,UAAUC,IAAIF,MAGrBG,YAAA,SAAYH,QACLC,iBAAiBD,MAGxBI,cAAA,gBACOH,UAAUI,SAAQ,SAAAL,UAAMA,YACxBM,yBAMPC,iBAAA,kBACSC,EAAM3B,KAAKN,sBAAwBM,KAAKL,YAAciC,MAAK,SAAAC,UAAKA,EAAEC,gBAAcf,QAAQgB,UAMjGC,kBAAA,WACEjB,QAAQC,IAAI,mBAAoBhB,KAAKJ,kBAChCqC,oBACAV,mBAgBPE,oBAAA,SAAoBS,kBAAAA,IAAAA,EAAO,2BACrBlC,KAAKmC,oBACPhD,OAAOE,QAAK+C,YAAY,CACtBF,KAAAA,EAAMG,QAAS,CACb1C,WAAYK,KAAKL,WACjBC,YAAaI,KAAKJ,cAEnB,SAIPqC,aAAA,sBACMK,EAAOtC,KAAKJ,YACZ0C,EAAKxC,OACPwC,EAAKxC,MAAM0B,SAAQ,SAAAe,OACbC,EAAYC,EAAKC,cAAcC,IAAIJ,EAAKK,KAAO,IAAIC,EAAkBN,EAAME,GAC/EA,EAAKC,cAAcpC,IAAIiC,EAAKK,GAAIJ,YAuB3BK,EAQX,SAAYC,EAAoBC,uBAApBD,IAAAA,EAAgB,cAPhB,kBACU,IAAI/C,aAEf,CACTiD,QAAS,mBAWD,SAACV,GACTW,EAAKX,KAAOY,OAAOC,OAAOF,EAAKX,KAAMA,GACrCW,EAAK1B,gCAGK,SAACJ,GACX8B,EAAK7B,UAAUC,IAAIF,qBAGP,SAACA,GACb8B,EAAK7B,iBAAiBD,uBAGR,WACd8B,EAAK7B,UAAUI,SAAQ,SAAAL,UACdA,EAAG8B,EAAKX,eAtBZA,KAAOQ,MACPZ,EAAQY,EAARZ,KACDA,SACGkB,UAAYL,EAAS1C,iBAAiBsC,IAAIT"}
1
+ {"version":3,"file":"core.cjs.production.min.js","sources":["../src/utils/index.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport Stitches from '@stitches/core/types/stitches'\nimport {isIframe} from './utils'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: any\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n this.loadStudio()\n }\n\n loadStudio() {\n if (isIframe) {\n let initStudio = () => {\n let StudioBridge = window.WeaverseStudioBridge\n this.studioBridge = new StudioBridge(this)\n this.studioBridge.subscribeMessageEvent()\n this.triggerUpdate()\n console.log('studio bridge initialized', this.studioBridge)\n }\n window.addEventListener('message', e => {\n if (e.data?.type === 'weaverse.editor.ready') {\n this.isEditor = true\n if (!window.WeaverseStudioBridge) {\n // load studio bridge script by url: https://weaverse.io/assets/studio/studio-bridge.js\n const studioBridgeScript = document.createElement('script')\n studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`\n studioBridgeScript.onload = initStudio\n document.body.appendChild(studioBridgeScript)\n } else {\n initStudio()\n }\n }\n })\n }\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","items","Set","this","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","initStudio","StudioBridge","WeaverseStudioBridge","_this","studioBridge","subscribeMessageEvent","triggerUpdate","console","log","addEventListener","e","data","type","isEditor","studioBridgeScript","document","createElement","src","onload","body","appendChild","subscribe","fn","listeners","add","unsubscribe","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","postMessage","payload","item","itemStore","_this3","itemInstances","get","id","WeaverseItemStore","itemData","weaverse","current","_this4","Object","assign","Component"],"mappings":"oLAAaA,EAAqC,iBAAdC,WAAgD,gBAAtBA,UAAUC,QAC3DC,EAA8B,oBAAXC,SAA2BJ,EAC9CK,EAAWF,GAAaC,OAAOE,MAAQF,OAAOG,KCOvDC,EAAWC,QAAQ,kBA2BVC,4CAqDmD,KAAjDC,IAAAA,OAAQC,IAAAA,WAAYC,IAAAA,kCAhDd,IAAIC,uBAKP,IAAIA,gBAKgE,sCAK/D,oBAKU,CAC7BC,MAAO,mBAMa,IAAIC,mBAOf,OAeJL,OAASA,GAAUM,KAAKN,YACxBC,WAAaA,GAAcK,KAAKL,WACrCC,IAAgBI,KAAKJ,YAAcA,QAC9BK,kCAQPC,gBAAA,SAAgBC,EAAcC,QACvBC,iBAAiBC,IAAIH,EAAMC,MAGlCH,KAAA,gBAEOM,iBAAmBhB,EAASiB,eAC/B,CACEC,OAAQ,WACRC,MAAO,CACLC,IAAK,qBACLC,IAAK,qBACLC,IAAK,8BAINC,gBAGPA,WAAA,yBACM1B,EAAU,KACR2B,EAAa,eACXC,EAAe7B,OAAO8B,qBAC1BC,EAAKC,aAAe,IAAIH,EAAaE,GACrCA,EAAKC,aAAaC,wBAClBF,EAAKG,gBACLC,QAAQC,IAAI,4BAA6BL,EAAKC,eAEhDhC,OAAOqC,iBAAiB,WAAW,SAAAC,YACZ,oCAAjBA,EAAEC,eAAMC,SACVT,EAAKU,UAAW,EACXzC,OAAO8B,qBAOVF,QAPgC,KAE1Bc,EAAqBC,SAASC,cAAc,UAClDF,EAAmBG,IAASd,EAAKxB,yCACjCmC,EAAmBI,OAASlB,EAC5Be,SAASI,KAAKC,YAAYN,WASpCO,UAAA,SAAUC,QACHC,UAAUC,IAAIF,MAGrBG,YAAA,SAAYH,QACLC,iBAAiBD,MAGxBhB,cAAA,gBACOiB,UAAUG,SAAQ,SAAAJ,UAAMA,YACxBK,yBAMPC,iBAAA,kBACSC,EAAM5C,KAAKN,sBAAwBM,KAAKL,YAAckD,MAAK,SAAAC,UAAKA,EAAEC,gBAAczB,QAAQ0B,UAMjGC,kBAAA,WACE3B,QAAQC,IAAI,mBAAoBvB,KAAKJ,kBAChCsD,oBACA7B,mBAgBPqB,oBAAA,SAAoBf,kBAAAA,IAAAA,EAAO,2BACrB3B,KAAK4B,oBACPzC,OAAOE,QAAK8D,YAAY,CACtBxB,KAAAA,EAAMyB,QAAS,CACbzD,WAAYK,KAAKL,WACjBC,YAAaI,KAAKJ,cAEnB,SAIPsD,aAAA,sBACMxB,EAAO1B,KAAKJ,YACZ8B,EAAK5B,OACP4B,EAAK5B,MAAM2C,SAAQ,SAAAY,OACbC,EAAYC,EAAKC,cAAcC,IAAIJ,EAAKK,KAAO,IAAIC,EAAkBN,EAAME,GAC/EA,EAAKC,cAAclD,IAAI+C,EAAKK,GAAIJ,YAuB3BK,EAQX,SAAYC,EAAoBC,uBAApBD,IAAAA,EAAgB,cAPhB,kBACU,IAAI7D,aAEf,CACT+D,QAAS,mBAWD,SAACpC,GACTqC,EAAKrC,KAAOsC,OAAOC,OAAOF,EAAKrC,KAAMA,GACrCqC,EAAK1C,gCAGK,SAACgB,GACX0B,EAAKzB,UAAUC,IAAIF,qBAGP,SAACA,GACb0B,EAAKzB,iBAAiBD,uBAGR,WACd0B,EAAKzB,UAAUG,SAAQ,SAAAJ,UACdA,EAAG0B,EAAKrC,eAtBZA,KAAOkC,MACPjC,EAAQiC,EAARjC,KACDA,SACGuC,UAAYL,EAASxD,iBAAiBoD,IAAI9B"}
package/dist/core.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import StudioBridge from './studio-bridge';
2
1
  import Stitches from '@stitches/core/types/stitches';
3
2
  export interface ProjectDataItemType {
4
3
  type: string;
@@ -61,7 +60,7 @@ export declare class Weaverse {
61
60
  * stitches instance for handling CSS stylesheet, media, theme for Weaverse project
62
61
  */
63
62
  stitchesInstance: Stitches | any;
64
- studioBridge?: StudioBridge;
63
+ studioBridge?: any;
65
64
  /**
66
65
  * constructor
67
66
  * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
@@ -76,7 +75,7 @@ export declare class Weaverse {
76
75
  */
77
76
  registerElement(name: string, element: any): void;
78
77
  init(): void;
79
- loadStudio(): boolean;
78
+ loadStudio(): void;
80
79
  subscribe(fn: any): void;
81
80
  unsubscribe(fn: any): void;
82
81
  triggerUpdate(): void;
package/dist/core.esm.js CHANGED
@@ -5,96 +5,6 @@ var isReactNative = typeof navigator === 'object' && navigator.product === 'Reac
5
5
  var isBrowser = typeof window !== 'undefined' && !isReactNative;
6
6
  var isIframe = isBrowser && window.top !== window.self;
7
7
 
8
- /**
9
- * When the editor is ready, we'll load this studio-bridge code.
10
- * This is for processing data between the editor and the workspace project.
11
- * It will not load in production mode or not running when the editor is not detected/ready.
12
- */
13
-
14
- var StudioBridge = /*#__PURE__*/function () {
15
- function StudioBridge(weaverse) {
16
- var _this = this;
17
-
18
- /**
19
- * handle the message event from editor(parent window)
20
- * the message type will start with "weavers.",
21
- * when an item got message to update, get the item instance and setData to it
22
- * @param event {MessageEvent}
23
- */
24
- this.handleMessageEvent = function (event) {
25
- var _event$data, _event$data$type;
26
-
27
- if ((_event$data = event.data) != null && (_event$data$type = _event$data.type) != null && _event$data$type.startsWith('weaverse.')) {
28
- var type = event.data.type;
29
-
30
- switch (type) {
31
- case 'weaverse.editor.ready':
32
- _this.weaverse.isEditor = true;
33
- break;
34
-
35
- case 'weaverse.editor.update':
36
- var payload = event.data.payload;
37
- var itemId = payload.itemId,
38
- background = payload.background;
39
-
40
- var instance = _this.weaverse.itemInstances.get(itemId);
41
-
42
- if (instance) {
43
- instance.setData({
44
- css: {
45
- background: background
46
- }
47
- });
48
- }
49
-
50
- }
51
- }
52
- };
53
-
54
- this.handleMouseDown = function (e) {
55
- console.log('handleMouseDown', e);
56
-
57
- _this.sendMessageToEditor('weaverse.editor.mouseDown', {});
58
- };
59
-
60
- this.weaverse = weaverse;
61
- this.handleProps = {
62
- onMouseDownCapture: this.handleMouseDown.bind(this)
63
- };
64
- }
65
- /**
66
- * subscribe to window message event from editor(parent window), and handle the message event
67
- */
68
-
69
-
70
- var _proto = StudioBridge.prototype;
71
-
72
- _proto.subscribeMessageEvent = function subscribeMessageEvent() {
73
- var _this2 = this;
74
-
75
- if (typeof this.currentFrameSubscription === 'function') {
76
- this.currentFrameSubscription();
77
- }
78
-
79
- isBrowser && window.addEventListener('message', this.handleMessageEvent);
80
-
81
- this.currentFrameSubscription = function () {
82
- isBrowser && window.removeEventListener('message', _this2.handleMessageEvent);
83
- };
84
-
85
- return this.currentFrameSubscription;
86
- };
87
-
88
- _proto.sendMessageToEditor = function sendMessageToEditor(type, payload) {
89
- isBrowser && window.parent.postMessage({
90
- type: type,
91
- payload: payload
92
- }, '*');
93
- };
94
-
95
- return StudioBridge;
96
- }();
97
-
98
8
  // TODO: Implement Weaverse SDK class
99
9
  // using stitches core only for framework-agnostic code
100
10
 
@@ -186,23 +96,42 @@ var Weaverse = /*#__PURE__*/function () {
186
96
  bp3: '(min-width: 1024px)'
187
97
  }
188
98
  });
99
+ this.loadStudio();
189
100
  };
190
101
 
191
102
  _proto.loadStudio = function loadStudio() {
192
- console.log('process.env.NODE_ENV', env.NODE_ENV); // only load studio bridge if it's in editor mode
193
-
194
- if (env.NODE_ENV !== 'production') {
195
- // TODO: separate studio bridge into a separate package
196
- // in development mode, we should use localhost:3000 as appUrl
197
- // this.appUrl = 'http://localhost:3000'
198
- // let StudioBridge = require('./studio-bridge')
199
- this.studioBridge = new StudioBridge(this);
200
- console.log('Weaverse: init studio bridge', this.studioBridge);
201
- this.studioBridge.subscribeMessageEvent();
202
- return true;
203
- }
103
+ var _this = this;
104
+
105
+ if (isIframe) {
106
+ var initStudio = function initStudio() {
107
+ var StudioBridge = window.WeaverseStudioBridge;
108
+ _this.studioBridge = new StudioBridge(_this);
109
+
110
+ _this.studioBridge.subscribeMessageEvent();
111
+
112
+ _this.triggerUpdate();
204
113
 
205
- return false;
114
+ console.log('studio bridge initialized', _this.studioBridge);
115
+ };
116
+
117
+ window.addEventListener('message', function (e) {
118
+ var _e$data;
119
+
120
+ if (((_e$data = e.data) == null ? void 0 : _e$data.type) === 'weaverse.editor.ready') {
121
+ _this.isEditor = true;
122
+
123
+ if (!window.WeaverseStudioBridge) {
124
+ // load studio bridge script by url: https://weaverse.io/assets/studio/studio-bridge.js
125
+ var studioBridgeScript = document.createElement('script');
126
+ studioBridgeScript.src = _this.appUrl + "/assets/studio/studio-bridge.js";
127
+ studioBridgeScript.onload = initStudio;
128
+ document.body.appendChild(studioBridgeScript);
129
+ } else {
130
+ initStudio();
131
+ }
132
+ }
133
+ });
134
+ }
206
135
  };
207
136
 
208
137
  _proto.subscribe = function subscribe(fn) {
@@ -261,15 +190,15 @@ var Weaverse = /*#__PURE__*/function () {
261
190
  };
262
191
 
263
192
  _proto.initItemData = function initItemData() {
264
- var _this2 = this;
193
+ var _this3 = this;
265
194
 
266
195
  var data = this.projectData;
267
196
 
268
197
  if (data.items) {
269
198
  data.items.forEach(function (item) {
270
- var itemStore = _this2.itemInstances.get(item.id) || new WeaverseItemStore(item, _this2);
199
+ var itemStore = _this3.itemInstances.get(item.id) || new WeaverseItemStore(item, _this3);
271
200
 
272
- _this2.itemInstances.set(item.id, itemStore);
201
+ _this3.itemInstances.set(item.id, itemStore);
273
202
  });
274
203
  }
275
204
  };
@@ -295,7 +224,7 @@ var Weaverse = /*#__PURE__*/function () {
295
224
  */
296
225
 
297
226
  var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
298
- var _this3 = this;
227
+ var _this4 = this;
299
228
 
300
229
  if (itemData === void 0) {
301
230
  itemData = {};
@@ -308,22 +237,22 @@ var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
308
237
  };
309
238
 
310
239
  this.setData = function (data) {
311
- _this3.data = Object.assign(_this3.data, data);
240
+ _this4.data = Object.assign(_this4.data, data);
312
241
 
313
- _this3.triggerUpdate();
242
+ _this4.triggerUpdate();
314
243
  };
315
244
 
316
245
  this.subscribe = function (fn) {
317
- _this3.listeners.add(fn);
246
+ _this4.listeners.add(fn);
318
247
  };
319
248
 
320
249
  this.unsubscribe = function (fn) {
321
- _this3.listeners["delete"](fn);
250
+ _this4.listeners["delete"](fn);
322
251
  };
323
252
 
324
253
  this.triggerUpdate = function () {
325
- _this3.listeners.forEach(function (fn) {
326
- return fn(_this3.data);
254
+ _this4.listeners.forEach(function (fn) {
255
+ return fn(_this4.data);
327
256
  });
328
257
  };
329
258
 
@@ -1 +1 @@
1
- {"version":3,"file":"core.esm.js","sources":["../src/utils/index.ts","../src/studio-bridge.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","import {isBrowser} from './utils'\nimport type {Weaverse} from '.'\n\n/**\n * When the editor is ready, we'll load this studio-bridge code.\n * This is for processing data between the editor and the workspace project.\n * It will not load in production mode or not running when the editor is not detected/ready.\n */\n\nexport class StudioBridge {\n weaverse: Weaverse\n\n /**\n * instance for subscribing to window message, save it to currentFrameSubscription then can remove it when unmount\n */\n currentFrameSubscription: any\n handleProps: any\n constructor(weaverse: Weaverse) {\n this.weaverse = weaverse\n this.handleProps = {\n onMouseDownCapture: this.handleMouseDown.bind(this)\n }\n }\n\n /**\n * subscribe to window message event from editor(parent window), and handle the message event\n */\n subscribeMessageEvent() {\n if (typeof this.currentFrameSubscription === 'function') {\n this.currentFrameSubscription()\n }\n isBrowser && window.addEventListener('message', this.handleMessageEvent)\n this.currentFrameSubscription = () => {\n isBrowser && window.removeEventListener('message', this.handleMessageEvent)\n }\n return this.currentFrameSubscription\n }\n\n /**\n * handle the message event from editor(parent window)\n * the message type will start with \"weavers.\",\n * when an item got message to update, get the item instance and setData to it\n * @param event {MessageEvent}\n */\n handleMessageEvent = (event: MessageEvent) => {\n if (event.data?.type?.startsWith('weaverse.')) {\n let type = event.data.type\n switch (type) {\n case 'weaverse.editor.ready':\n this.weaverse.isEditor = true\n break\n case 'weaverse.editor.update':\n let {payload} = event.data\n let {itemId, background} = payload\n let instance = this.weaverse.itemInstances.get(itemId)\n if (instance) {\n instance.setData({\n css: {\n background\n }\n })\n }\n }\n }\n }\n\n sendMessageToEditor(type: string, payload: any) {\n isBrowser && window.parent.postMessage({type, payload}, '*')\n }\n\n handleMouseDown = (e: MouseEvent) => {\n console.log('handleMouseDown', e)\n this.sendMessageToEditor('weaverse.editor.mouseDown', {})\n }\n}\n\n\nexport default StudioBridge","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport StudioBridge from './studio-bridge'\nimport Stitches from '@stitches/core/types/stitches'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: StudioBridge\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n }\n\n loadStudio() {\n console.log('process.env.NODE_ENV', process.env.NODE_ENV)\n // only load studio bridge if it's in editor mode\n if (process.env.NODE_ENV !== 'production') {\n // TODO: separate studio bridge into a separate package\n // in development mode, we should use localhost:3000 as appUrl\n // this.appUrl = 'http://localhost:3000'\n // let StudioBridge = require('./studio-bridge')\n this.studioBridge = new StudioBridge(this)\n console.log('Weaverse: init studio bridge', this.studioBridge)\n\n this.studioBridge.subscribeMessageEvent()\n return true\n }\n return false\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","StudioBridge","weaverse","event","data","type","startsWith","isEditor","payload","itemId","background","instance","itemInstances","get","setData","css","e","console","log","sendMessageToEditor","handleProps","onMouseDownCapture","handleMouseDown","bind","subscribeMessageEvent","currentFrameSubscription","addEventListener","handleMessageEvent","removeEventListener","parent","postMessage","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","process","NODE_ENV","items","Set","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","studioBridge","subscribe","fn","listeners","add","unsubscribe","triggerUpdate","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","item","itemStore","id","WeaverseItemStore","itemData","current","Object","assign","Component"],"mappings":";;;IAAaA,aAAa,GAAG,OAAOC,SAAP,KAAqB,QAArB,IAAiCA,SAAS,CAACC,OAAV,KAAsB;IACvEC,SAAS,GAAG,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,CAACJ;IAC9CK,QAAQ,GAAGF,SAAS,IAAIC,MAAM,CAACE,GAAP,KAAeF,MAAM,CAACG;;ACC3D;;;;;;AAMA,IAAaC,YAAb;AAQE,wBAAYC,QAAZ;;;AAqBA;;;;;;AAMA,2BAAA,GAAqB,UAACC,KAAD;;;AACnB,yBAAIA,KAAK,CAACC,IAAV,iCAAI,YAAYC,IAAhB,aAAI,iBAAkBC,UAAlB,CAA6B,WAA7B,CAAJ,EAA+C;AAC7C,YAAID,IAAI,GAAGF,KAAK,CAACC,IAAN,CAAWC,IAAtB;;AACA,gBAAQA,IAAR;AACE,eAAK,uBAAL;AACE,YAAA,KAAI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,IAAA,MAAI,CAACA,IAAL,GAAY2E,MAAM,CAACC,MAAP,CAAc,MAAI,CAAC5E,IAAnB,EAAyBA,IAAzB,CAAZ;;AACA,IAAA,MAAI,CAAC0D,aAAL;AACD,GAHD;;AAKA,gBAAA,GAAY,UAACJ,EAAD;AACV,IAAA,MAAI,CAACC,SAAL,CAAeC,GAAf,CAAmBF,EAAnB;AACD,GAFD;;AAIA,kBAAA,GAAc,UAACA,EAAD;AACZ,IAAA,MAAI,CAACC,SAAL,WAAsBD,EAAtB;AACD,GAFD;;AAIA,oBAAA,GAAgB;AACd,IAAA,MAAI,CAACC,SAAL,CAAeI,OAAf,CAAuB,UAAAL,EAAE;AACvB,aAAOA,EAAE,CAAC,MAAI,CAACtD,IAAN,CAAT;AACD,KAFD;AAGD,GAJD;;AApBE,OAAKA,IAAL,GAAYyE,QAAZ;AACA,kBAAaA,QAAb;AAAA,MAAKxE,IAAL,aAAKA,IAAL;;AACA,MAAIA,IAAJ,EAAU;AACR,SAAK4E,SAAL,GAAiB/E,QAAQ,CAAC4C,gBAAT,CAA0BjC,GAA1B,CAA8BR,IAA9B,CAAjB;AACD;AACF,CAdH;;;;"}
1
+ {"version":3,"file":"core.esm.js","sources":["../src/utils/index.ts","../src/core.ts"],"sourcesContent":["export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'\nexport const isBrowser = typeof window !== 'undefined' && !isReactNative\nexport const isIframe = isBrowser && window.top !== window.self","// TODO: Implement Weaverse SDK class\n// Only core code is implemented here, avoid importing other packages,\n// the core code should be framework agnostic, no react, vue, angular, etc.\nimport fetch from 'isomorphic-unfetch'\nimport * as process from 'process'\nimport Stitches from '@stitches/core/types/stitches'\nimport {isIframe} from './utils'\n// stitches problem, we should use require instead of import\n// using stitches core only for framework-agnostic code\nlet stitches = require('@stitches/core')\n\n\nexport interface ProjectDataItemType {\n type: string\n name?: string\n id: string | number\n description?: string\n childIds?: Array<string | number>\n css?: {\n [key: string]: string\n }\n}\n\nexport interface ProjectDataType {\n items: ProjectDataItemType[]\n}\n\n\nexport type WeaverseType = {\n [key: string]: any\n appUrl?: string,\n projectKey?: string,\n projectData?: ProjectDataType\n}\n\n\nexport class Weaverse {\n /**\n * For storing, registering element React component from Weaverse or created by user/developer\n * @type {Map<string, React.Component>}\n */\n elementInstances = new Map<string, any>()\n /**\n * list of element/items store to provide data, handle state update, state sharing, etc.\n * @type {Map<string, any>}\n */\n itemInstances = new Map<string | number, WeaverseItemStore>()\n /**\n * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @type {string}\n */\n appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'\n /**\n * Weaverse project key to access project data via API\n * @type {string}\n */\n projectKey: string = ''\n /**\n * Weaverse project data, by default, user can provide project data via React Component:\n * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering\n */\n projectData: ProjectDataType = {\n items: []\n }\n /**\n * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot\n * @type {Map<string, (data: any) => void>}\n */\n listeners: Set<any> = new Set()\n /**\n * check whether the sdk is inEditor or not\n * if isEditor is true, it means the sdk is inEditor mode, render the editor UI\n * else render the preview UI, plain HTML + CSS + React hydrate\n * @type {boolean}\n */\n isEditor = false\n /**\n * stitches instance for handling CSS stylesheet, media, theme for Weaverse project\n */\n stitchesInstance: Stitches | any\n\n studioBridge?: any\n\n /**\n * constructor\n * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000\n * @param projectKey {string} Weaverse project key to access project data via API\n * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.\n */\n constructor({appUrl, projectKey, projectData}: WeaverseType = {}) {\n this.appUrl = appUrl || this.appUrl\n this.projectKey = projectKey || this.projectKey\n projectData && (this.projectData = projectData)\n this.init()\n }\n\n /**\n * register the custom React Component to Weaverse, store it into Weaverse.elementInstances\n * @param name {string} unique name of the custom React Component\n * @param element {React.Component} custom React Component\n */\n registerElement(name: string, element: any) {\n this.elementInstances.set(name, element)\n }\n\n init() {\n // init the stitches instance\n this.stitchesInstance = stitches.createStitches(\n {\n prefix: 'weaverse',\n media: {\n bp1: '(min-width: 640px)',\n bp2: '(max-width: 768px)',\n bp3: '(min-width: 1024px)'\n }\n }\n )\n this.loadStudio()\n }\n\n loadStudio() {\n if (isIframe) {\n let initStudio = () => {\n let StudioBridge = window.WeaverseStudioBridge\n this.studioBridge = new StudioBridge(this)\n this.studioBridge.subscribeMessageEvent()\n this.triggerUpdate()\n console.log('studio bridge initialized', this.studioBridge)\n }\n window.addEventListener('message', e => {\n if (e.data?.type === 'weaverse.editor.ready') {\n this.isEditor = true\n if (!window.WeaverseStudioBridge) {\n // load studio bridge script by url: https://weaverse.io/assets/studio/studio-bridge.js\n const studioBridgeScript = document.createElement('script')\n studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`\n studioBridgeScript.onload = initStudio\n document.body.appendChild(studioBridgeScript)\n } else {\n initStudio()\n }\n }\n })\n }\n }\n\n subscribe(fn: any) {\n this.listeners.add(fn)\n }\n\n unsubscribe(fn: any) {\n this.listeners.delete(fn)\n }\n\n triggerUpdate() {\n this.listeners.forEach(fn => fn())\n this.triggerEditorUpdate()\n }\n\n /**\n * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)\n */\n fetchProjectData() {\n return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json()).catch(console.error)\n }\n\n /**\n * fetch and update the project data, then trigger update to re-render the WeaverseRoot\n */\n updateProjectData() {\n console.log('this.projectData', this.projectData)\n this.initItemData()\n this.triggerUpdate()\n return\n if (this.projectKey) {\n this.fetchProjectData().then(data => {\n if (data) {\n this.projectData = data\n this.initItemData()\n this.triggerUpdate()\n }\n }).catch(err => {\n console.error(err)\n })\n }\n }\n\n\n triggerEditorUpdate(type = 'weaverse.workspace.init') {\n if (this.isEditor) {\n window.top?.postMessage({\n type, payload: {\n projectKey: this.projectKey,\n projectData: this.projectData\n }\n }, '*')\n }\n }\n\n initItemData() {\n let data = this.projectData\n if (data.items) {\n data.items.forEach(item => {\n let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)\n this.itemInstances.set(item.id, itemStore)\n })\n }\n }\n}\n\n/**\n * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data\n * @param itemData {ProjectDataItemType} Weaverse item data\n * @param weaverse {Weaverse} Weaverse instance\n * Usage:\n * ```jsx\n * useEffect(() => {\n * let handleUpdate = (update: any) => {\n * setData({...update})\n * }\n * itemInstance.subscribe(handleUpdate)\n * return () => {\n * itemInstance.unsubscribe(handleUpdate)\n * }\n * }, [])\n * ```\n */\nexport class WeaverseItemStore {\n data: any = {}\n listeners: Set<any> = new Set()\n Component: any\n ref: any = {\n current: null\n }\n\n constructor(itemData: any = {}, weaverse: Weaverse) {\n this.data = itemData\n let {type} = itemData\n if (type) {\n this.Component = weaverse.elementInstances.get(type)\n }\n }\n\n setData = (data: any) => {\n this.data = Object.assign(this.data, data)\n this.triggerUpdate()\n }\n\n subscribe = (fn: any) => {\n this.listeners.add(fn)\n }\n\n unsubscribe = (fn: any) => {\n this.listeners.delete(fn)\n }\n\n triggerUpdate = () => {\n this.listeners.forEach(fn => {\n return fn(this.data)\n })\n }\n}"],"names":["isReactNative","navigator","product","isBrowser","window","isIframe","top","self","stitches","require","Weaverse","appUrl","projectKey","projectData","Map","process","NODE_ENV","items","Set","init","registerElement","name","element","elementInstances","set","stitchesInstance","createStitches","prefix","media","bp1","bp2","bp3","loadStudio","initStudio","StudioBridge","WeaverseStudioBridge","studioBridge","subscribeMessageEvent","triggerUpdate","console","log","addEventListener","e","data","type","isEditor","studioBridgeScript","document","createElement","src","onload","body","appendChild","subscribe","fn","listeners","add","unsubscribe","forEach","triggerEditorUpdate","fetchProjectData","fetch","then","r","json","error","updateProjectData","initItemData","postMessage","payload","item","itemStore","itemInstances","get","id","WeaverseItemStore","itemData","weaverse","current","Object","assign","Component"],"mappings":";;;IAAaA,aAAa,GAAG,O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AdH;;;;"}
@@ -0,0 +1 @@
1
+ declare const loadScript: (url: string) => Promise<unknown>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.2",
2
+ "version": "0.2.3",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,29 +0,0 @@
1
- import type { Weaverse } from '.';
2
- /**
3
- * When the editor is ready, we'll load this studio-bridge code.
4
- * This is for processing data between the editor and the workspace project.
5
- * It will not load in production mode or not running when the editor is not detected/ready.
6
- */
7
- export declare class StudioBridge {
8
- weaverse: Weaverse;
9
- /**
10
- * instance for subscribing to window message, save it to currentFrameSubscription then can remove it when unmount
11
- */
12
- currentFrameSubscription: any;
13
- handleProps: any;
14
- constructor(weaverse: Weaverse);
15
- /**
16
- * subscribe to window message event from editor(parent window), and handle the message event
17
- */
18
- subscribeMessageEvent(): any;
19
- /**
20
- * handle the message event from editor(parent window)
21
- * the message type will start with "weavers.",
22
- * when an item got message to update, get the item instance and setData to it
23
- * @param event {MessageEvent}
24
- */
25
- handleMessageEvent: (event: MessageEvent) => void;
26
- sendMessageToEditor(type: string, payload: any): void;
27
- handleMouseDown: (e: MouseEvent) => void;
28
- }
29
- export default StudioBridge;