@weaverse/core 0.2.4 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,289 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var fetch = _interopDefault(require('isomorphic-unfetch'));
8
- require('process');
9
-
10
- var isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative';
11
- var isBrowser = typeof window !== 'undefined' && !isReactNative;
12
- var isIframe = isBrowser && window.top !== window.self;
13
-
14
- // TODO: Implement Weaverse SDK class
15
- // using stitches core only for framework-agnostic code
16
-
17
- var stitches = /*#__PURE__*/require('@stitches/core');
18
-
19
- var Weaverse = /*#__PURE__*/function () {
20
- /**
21
- * constructor
22
- * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
23
- * @param projectKey {string} Weaverse project key to access project data via API
24
- * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
25
- */
26
- function Weaverse(_temp) {
27
- var _ref = _temp === void 0 ? {} : _temp,
28
- appUrl = _ref.appUrl,
29
- projectKey = _ref.projectKey,
30
- projectData = _ref.projectData;
31
-
32
- /**
33
- * For storing, registering element React component from Weaverse or created by user/developer
34
- * @type {Map<string, React.Component>}
35
- */
36
- this.elementInstances = new Map();
37
- /**
38
- * list of element/items store to provide data, handle state update, state sharing, etc.
39
- * @type {Map<string, any>}
40
- */
41
-
42
- this.itemInstances = new Map();
43
- /**
44
- * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
45
- * @type {string}
46
- */
47
-
48
- this.appUrl = 'http://localhost:3000' ;
49
- /**
50
- * Weaverse project key to access project data via API
51
- * @type {string}
52
- */
53
-
54
- this.projectKey = '';
55
- /**
56
- * Weaverse project data, by default, user can provide project data via React Component:
57
- * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
58
- */
59
-
60
- this.projectData = {
61
- items: []
62
- };
63
- /**
64
- * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
65
- * @type {Map<string, (data: any) => void>}
66
- */
67
-
68
- this.listeners = new Set();
69
- /**
70
- * check whether the sdk is inEditor or not
71
- * if isEditor is true, it means the sdk is inEditor mode, render the editor UI
72
- * else render the preview UI, plain HTML + CSS + React hydrate
73
- * @type {boolean}
74
- */
75
-
76
- this.isEditor = false;
77
- this.appUrl = appUrl || this.appUrl;
78
- this.projectKey = projectKey || this.projectKey;
79
- projectData && (this.projectData = projectData);
80
- this.init();
81
- }
82
- /**
83
- * register the custom React Component to Weaverse, store it into Weaverse.elementInstances
84
- * @param name {string} unique name of the custom React Component
85
- * @param element {React.Component} custom React Component
86
- */
87
-
88
-
89
- var _proto = Weaverse.prototype;
90
-
91
- _proto.registerElement = function registerElement(name, element) {
92
- this.elementInstances.set(name, element);
93
- };
94
-
95
- _proto.init = function init() {
96
- // init the stitches instance
97
- this.stitchesInstance = stitches.createStitches({
98
- prefix: 'weaverse',
99
- media: {
100
- bp1: '(min-width: 640px)',
101
- bp2: '(max-width: 768px)',
102
- bp3: '(min-width: 1024px)'
103
- }
104
- });
105
- this.loadStudio();
106
- };
107
-
108
- _proto.loadStudio = function loadStudio() {
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
- });
140
- }
141
- };
142
-
143
- _proto.subscribe = function subscribe(fn) {
144
- this.listeners.add(fn);
145
- };
146
-
147
- _proto.unsubscribe = function unsubscribe(fn) {
148
- this.listeners["delete"](fn);
149
- };
150
-
151
- _proto.triggerUpdate = function triggerUpdate() {
152
- this.listeners.forEach(function (fn) {
153
- return fn();
154
- });
155
- this.triggerEditorUpdate();
156
- }
157
- /**
158
- * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
159
- */
160
- ;
161
-
162
- _proto.fetchProjectData = function fetchProjectData() {
163
- return fetch(this.appUrl + ("/api/public/" + this.projectKey)).then(function (r) {
164
- return r.json();
165
- })["catch"](console.error);
166
- }
167
- /**
168
- * fetch and update the project data, then trigger update to re-render the WeaverseRoot
169
- */
170
- ;
171
-
172
- _proto.updateProjectData = function updateProjectData() {
173
- var _this2 = this;
174
-
175
- // console.log('this.projectData', this.projectData)
176
- // this.initItemData()
177
- // this.triggerUpdate()
178
- // return
179
- if (this.projectKey) {
180
- this.fetchProjectData().then(function (data) {
181
- if (data) {
182
- _this2.projectData = data;
183
-
184
- _this2.initItemData();
185
-
186
- _this2.triggerUpdate();
187
- }
188
- })["catch"](function (err) {
189
- console.error(err);
190
- });
191
- }
192
- };
193
-
194
- _proto.triggerEditorUpdate = function triggerEditorUpdate(type) {
195
- if (type === void 0) {
196
- type = 'weaverse.workspace.init';
197
- }
198
-
199
- if (this.isEditor && this.studioBridge) {
200
- this.studioBridge.sendMessageToEditor(type, {
201
- projectData: this.projectData,
202
- projectKey: this.projectKey,
203
- appUrl: this.appUrl
204
- });
205
- }
206
- };
207
-
208
- _proto.initItemData = function initItemData() {
209
- var _this3 = this;
210
-
211
- var data = this.projectData;
212
-
213
- if (data.items) {
214
- data.items.forEach(function (item) {
215
- var itemStore = _this3.itemInstances.get(item.id) || new WeaverseItemStore(item, _this3);
216
-
217
- _this3.itemInstances.set(item.id, itemStore);
218
- });
219
- }
220
- };
221
-
222
- return Weaverse;
223
- }();
224
- /**
225
- * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
226
- * @param itemData {ProjectDataItemType} Weaverse item data
227
- * @param weaverse {Weaverse} Weaverse instance
228
- * Usage:
229
- * ```jsx
230
- * useEffect(() => {
231
- * let handleUpdate = (update: any) => {
232
- * setData({...update})
233
- * }
234
- * itemInstance.subscribe(handleUpdate)
235
- * return () => {
236
- * itemInstance.unsubscribe(handleUpdate)
237
- * }
238
- * }, [])
239
- * ```
240
- */
241
-
242
- var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
243
- var _this4 = this;
244
-
245
- if (itemData === void 0) {
246
- itemData = {};
247
- }
248
-
249
- this.data = {};
250
- this.listeners = new Set();
251
- this.ref = {
252
- current: null
253
- };
254
-
255
- this.setData = function (data) {
256
- _this4.data = Object.assign(_this4.data, data);
257
-
258
- _this4.triggerUpdate();
259
- };
260
-
261
- this.subscribe = function (fn) {
262
- _this4.listeners.add(fn);
263
- };
264
-
265
- this.unsubscribe = function (fn) {
266
- _this4.listeners["delete"](fn);
267
- };
268
-
269
- this.triggerUpdate = function () {
270
- _this4.listeners.forEach(function (fn) {
271
- return fn(_this4.data);
272
- });
273
- };
274
-
275
- this.data = itemData;
276
- var _itemData = itemData,
277
- type = _itemData.type;
278
-
279
- if (type) {
280
- this.Component = weaverse.elementInstances.get(type);
281
- }
282
- };
283
-
284
- exports.Weaverse = Weaverse;
285
- exports.WeaverseItemStore = WeaverseItemStore;
286
- exports.isBrowser = isBrowser;
287
- exports.isIframe = isIframe;
288
- exports.isReactNative = isReactNative;
289
- //# sourceMappingURL=core.cjs.development.js.map
@@ -1 +0,0 @@
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 && this.studioBridge) {\n this.studioBridge.sendMessageToEditor(type, {\n projectData: this.projectData,\n projectKey: this.projectKey,\n appUrl: this.appUrl\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","err","sendMessageToEditor","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,EAAhB;AACA;;;;;AAIA,eAAA,GAAiBC,CAAyC,uBAAzC,CAAjB;AACA;;;;;AAIA,mBAAA,GAAqB,EAArB;AACA;;;;;AAIA,oBAAA,GAA+B;AAC7BC,MAAAA,KAAK,EAAE;AADsB,KAA/B;AAGA;;;;;AAIA,kBAAA,GAAsB,IAAIC,GAAJ,EAAtB;AACA;;;;;;;AAMA,iBAAA,GAAW,KAAX;AAeE,SAAKN,MAAL,GAAcA,MAAM,IAAI,KAAKA,M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AACrC,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 +0,0 @@
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(){var t=this;this.projectKey&&this.fetchProjectData().then((function(e){e&&(t.projectData=e,t.initItemData(),t.triggerUpdate())})).catch((function(t){console.error(t)}))},i.triggerEditorUpdate=function(t){void 0===t&&(t="weaverse.workspace.init"),this.isEditor&&this.studioBridge&&this.studioBridge.sendMessageToEditor(t,{projectData:this.projectData,projectKey:this.projectKey,appUrl:this.appUrl})},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
- //# sourceMappingURL=core.cjs.production.min.js.map
@@ -1 +0,0 @@
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 && this.studioBridge) {\n this.studioBridge.sendMessageToEditor(type, {\n projectData: this.projectData,\n projectKey: this.projectKey,\n appUrl: this.appUrl\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","_this2","initItemData","err","sendMessageToEditor","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,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AMA,GACrCsC,EAAK3C,gCAGK,SAACgB,GACX2B,EAAK1B,UAAUC,IAAIF,qBAGP,SAACA,GACb2B,EAAK1B,iBAAiBD,uBAGR,WACd2B,EAAK1B,UAAUG,SAAQ,SAAAJ,UACdA,EAAG2B,EAAKtC,eAtBZA,KAAOmC,MACPlC,EAAQkC,EAARlC,KACDA,SACGwC,UAAYL,EAASzD,iBAAiBqD,IAAI/B"}
package/dist/core.d.ts DELETED
@@ -1,120 +0,0 @@
1
- import Stitches from '@stitches/core/types/stitches';
2
- export interface ProjectDataItemType {
3
- type: string;
4
- name?: string;
5
- id: string | number;
6
- description?: string;
7
- childIds?: Array<string | number>;
8
- css?: {
9
- [key: string]: string;
10
- };
11
- }
12
- export interface ProjectDataType {
13
- items: ProjectDataItemType[];
14
- }
15
- export declare type WeaverseType = {
16
- [key: string]: any;
17
- appUrl?: string;
18
- projectKey?: string;
19
- projectData?: ProjectDataType;
20
- };
21
- export declare class Weaverse {
22
- /**
23
- * For storing, registering element React component from Weaverse or created by user/developer
24
- * @type {Map<string, React.Component>}
25
- */
26
- elementInstances: Map<string, any>;
27
- /**
28
- * list of element/items store to provide data, handle state update, state sharing, etc.
29
- * @type {Map<string, any>}
30
- */
31
- itemInstances: Map<string | number, WeaverseItemStore>;
32
- /**
33
- * Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
34
- * @type {string}
35
- */
36
- appUrl: string;
37
- /**
38
- * Weaverse project key to access project data via API
39
- * @type {string}
40
- */
41
- projectKey: string;
42
- /**
43
- * Weaverse project data, by default, user can provide project data via React Component:
44
- * <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
45
- */
46
- projectData: ProjectDataType;
47
- /**
48
- * storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
49
- * @type {Map<string, (data: any) => void>}
50
- */
51
- listeners: Set<any>;
52
- /**
53
- * check whether the sdk is inEditor or not
54
- * if isEditor is true, it means the sdk is inEditor mode, render the editor UI
55
- * else render the preview UI, plain HTML + CSS + React hydrate
56
- * @type {boolean}
57
- */
58
- isEditor: boolean;
59
- /**
60
- * stitches instance for handling CSS stylesheet, media, theme for Weaverse project
61
- */
62
- stitchesInstance: Stitches | any;
63
- studioBridge?: any;
64
- /**
65
- * constructor
66
- * @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
67
- * @param projectKey {string} Weaverse project key to access project data via API
68
- * @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
69
- */
70
- constructor({ appUrl, projectKey, projectData }?: WeaverseType);
71
- /**
72
- * register the custom React Component to Weaverse, store it into Weaverse.elementInstances
73
- * @param name {string} unique name of the custom React Component
74
- * @param element {React.Component} custom React Component
75
- */
76
- registerElement(name: string, element: any): void;
77
- init(): void;
78
- loadStudio(): void;
79
- subscribe(fn: any): void;
80
- unsubscribe(fn: any): void;
81
- triggerUpdate(): void;
82
- /**
83
- * fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
84
- */
85
- fetchProjectData(): Promise<any>;
86
- /**
87
- * fetch and update the project data, then trigger update to re-render the WeaverseRoot
88
- */
89
- updateProjectData(): void;
90
- triggerEditorUpdate(type?: string): void;
91
- initItemData(): void;
92
- }
93
- /**
94
- * WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
95
- * @param itemData {ProjectDataItemType} Weaverse item data
96
- * @param weaverse {Weaverse} Weaverse instance
97
- * Usage:
98
- * ```jsx
99
- * useEffect(() => {
100
- * let handleUpdate = (update: any) => {
101
- * setData({...update})
102
- * }
103
- * itemInstance.subscribe(handleUpdate)
104
- * return () => {
105
- * itemInstance.unsubscribe(handleUpdate)
106
- * }
107
- * }, [])
108
- * ```
109
- */
110
- export declare class WeaverseItemStore {
111
- data: any;
112
- listeners: Set<any>;
113
- Component: any;
114
- ref: any;
115
- constructor(itemData: any, weaverse: Weaverse);
116
- setData: (data: any) => void;
117
- subscribe: (fn: any) => void;
118
- unsubscribe: (fn: any) => void;
119
- triggerUpdate: () => void;
120
- }