@weaverse/core 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/core.cjs.development.js +348 -0
- package/dist/core.cjs.development.js.map +1 -0
- package/dist/core.cjs.production.min.js +2 -0
- package/dist/core.cjs.production.min.js.map +1 -0
- package/dist/core.d.ts +121 -0
- package/dist/core.esm.js +340 -0
- package/dist/core.esm.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/studio-bridge.d.ts +29 -0
- package/dist/utils/index.d.ts +3 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Paul
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# TSDX User Guide
|
|
2
|
+
|
|
3
|
+
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with
|
|
4
|
+
what’s here and how to use it.
|
|
5
|
+
|
|
6
|
+
> This TSDX setup is meant for developing libraries (not apps!) that can be published to NPM. If you’re looking to build a Node app, you could use `ts-node-dev`, plain `ts-node`, or simple `tsc`.
|
|
7
|
+
|
|
8
|
+
> If you’re new to TypeScript, checkout [this handy cheatsheet](https://devhints.io/typescript)
|
|
9
|
+
|
|
10
|
+
## Commands
|
|
11
|
+
|
|
12
|
+
TSDX scaffolds your new library inside `/src`.
|
|
13
|
+
|
|
14
|
+
To run TSDX, use:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm start # or yarn start
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`
|
|
21
|
+
.
|
|
22
|
+
|
|
23
|
+
To do a one-off build, use `npm run build` or `yarn build`.
|
|
24
|
+
|
|
25
|
+
To run tests, use `npm test` or `yarn test`.
|
|
26
|
+
|
|
27
|
+
## Configuration
|
|
28
|
+
|
|
29
|
+
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields
|
|
30
|
+
in `package.json` accordingly.
|
|
31
|
+
|
|
32
|
+
### Jest
|
|
33
|
+
|
|
34
|
+
Jest tests are set up to run with `npm test` or `yarn test`.
|
|
35
|
+
|
|
36
|
+
### Bundle Analysis
|
|
37
|
+
|
|
38
|
+
[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of your library
|
|
39
|
+
with `npm run size` and visualize the bundle with `npm run analyze`.
|
|
40
|
+
|
|
41
|
+
#### Setup Files
|
|
42
|
+
|
|
43
|
+
This is the folder structure we set up for you:
|
|
44
|
+
|
|
45
|
+
```txt
|
|
46
|
+
/src
|
|
47
|
+
index.tsx # EDIT THIS
|
|
48
|
+
/test
|
|
49
|
+
blah.test.tsx # EDIT THIS
|
|
50
|
+
.gitignore
|
|
51
|
+
package.json
|
|
52
|
+
README.md # EDIT THIS
|
|
53
|
+
tsconfig.json
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Rollup
|
|
57
|
+
|
|
58
|
+
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats
|
|
59
|
+
and build settings. See [Optimizations](#optimizations) for details.
|
|
60
|
+
|
|
61
|
+
### TypeScript
|
|
62
|
+
|
|
63
|
+
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your
|
|
64
|
+
needs.
|
|
65
|
+
|
|
66
|
+
## Continuous Integration
|
|
67
|
+
|
|
68
|
+
### GitHub Actions
|
|
69
|
+
|
|
70
|
+
Two actions are added by default:
|
|
71
|
+
|
|
72
|
+
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
|
|
73
|
+
- `size` which comments cost comparison of your library on every pull request
|
|
74
|
+
using [`size-limit`](https://github.com/ai/size-limit)
|
|
75
|
+
|
|
76
|
+
## Optimizations
|
|
77
|
+
|
|
78
|
+
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know
|
|
79
|
+
that you can take advantage of development-only optimizations:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
// ./types/index.d.ts
|
|
83
|
+
declare var __DEV__: boolean;
|
|
84
|
+
|
|
85
|
+
// inside your code...
|
|
86
|
+
if (__DEV__) {
|
|
87
|
+
console.log('foo');
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant)
|
|
92
|
+
and [warning](https://github.com/palmerhq/tsdx#warning) functions.
|
|
93
|
+
|
|
94
|
+
## Module Formats
|
|
95
|
+
|
|
96
|
+
CJS, ESModules, and UMD module formats are supported.
|
|
97
|
+
|
|
98
|
+
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are
|
|
99
|
+
found.
|
|
100
|
+
|
|
101
|
+
## Named Exports
|
|
102
|
+
|
|
103
|
+
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split
|
|
104
|
+
inside your React app instead of your React library.
|
|
105
|
+
|
|
106
|
+
## Including Styles
|
|
107
|
+
|
|
108
|
+
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
|
|
109
|
+
|
|
110
|
+
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so
|
|
111
|
+
that it can be imported separately by your users and run through their bundler's loader.
|
|
112
|
+
|
|
113
|
+
## Publishing to NPM
|
|
114
|
+
|
|
115
|
+
We recommend using [np](https://github.com/sindresorhus/np).
|
|
@@ -0,0 +1,348 @@
|
|
|
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
|
+
/**
|
|
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
|
+
// TODO: Implement Weaverse SDK class
|
|
105
|
+
// using stitches core only for framework-agnostic code
|
|
106
|
+
|
|
107
|
+
var stitches = /*#__PURE__*/require('@stitches/core');
|
|
108
|
+
|
|
109
|
+
var Weaverse = /*#__PURE__*/function () {
|
|
110
|
+
/**
|
|
111
|
+
* constructor
|
|
112
|
+
* @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
113
|
+
* @param projectKey {string} Weaverse project key to access project data via API
|
|
114
|
+
* @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
|
|
115
|
+
*/
|
|
116
|
+
function Weaverse(_temp) {
|
|
117
|
+
var _ref = _temp === void 0 ? {} : _temp,
|
|
118
|
+
appUrl = _ref.appUrl,
|
|
119
|
+
projectKey = _ref.projectKey,
|
|
120
|
+
projectData = _ref.projectData;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* For storing, registering element React component from Weaverse or created by user/developer
|
|
124
|
+
* @type {Map<string, React.Component>}
|
|
125
|
+
*/
|
|
126
|
+
this.elementInstances = new Map();
|
|
127
|
+
/**
|
|
128
|
+
* list of element/items store to provide data, handle state update, state sharing, etc.
|
|
129
|
+
* @type {Map<string, any>}
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
this.itemInstances = new Map();
|
|
133
|
+
/**
|
|
134
|
+
* Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
135
|
+
* @type {string}
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
this.appUrl = 'http://localhost:3000' ;
|
|
139
|
+
/**
|
|
140
|
+
* Weaverse project key to access project data via API
|
|
141
|
+
* @type {string}
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
this.projectKey = '';
|
|
145
|
+
/**
|
|
146
|
+
* Weaverse project data, by default, user can provide project data via React Component:
|
|
147
|
+
* <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
this.projectData = {
|
|
151
|
+
items: []
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
|
|
155
|
+
* @type {Map<string, (data: any) => void>}
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
this.listeners = new Set();
|
|
159
|
+
/**
|
|
160
|
+
* check whether the sdk is inEditor or not
|
|
161
|
+
* if isEditor is true, it means the sdk is inEditor mode, render the editor UI
|
|
162
|
+
* else render the preview UI, plain HTML + CSS + React hydrate
|
|
163
|
+
* @type {boolean}
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
this.isEditor = false;
|
|
167
|
+
this.appUrl = appUrl || this.appUrl;
|
|
168
|
+
this.projectKey = projectKey || this.projectKey;
|
|
169
|
+
projectData && (this.projectData = projectData);
|
|
170
|
+
this.init();
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* register the custom React Component to Weaverse, store it into Weaverse.elementInstances
|
|
174
|
+
* @param name {string} unique name of the custom React Component
|
|
175
|
+
* @param element {React.Component} custom React Component
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
var _proto = Weaverse.prototype;
|
|
180
|
+
|
|
181
|
+
_proto.registerElement = function registerElement(name, element) {
|
|
182
|
+
this.elementInstances.set(name, element);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
_proto.init = function init() {
|
|
186
|
+
// init the stitches instance
|
|
187
|
+
this.stitchesInstance = stitches.createStitches({
|
|
188
|
+
prefix: 'weaverse',
|
|
189
|
+
media: {
|
|
190
|
+
bp1: '(min-width: 640px)',
|
|
191
|
+
bp2: '(max-width: 768px)',
|
|
192
|
+
bp3: '(min-width: 1024px)'
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
_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;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
_proto.subscribe = function subscribe(fn) {
|
|
213
|
+
this.listeners.add(fn);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
_proto.unsubscribe = function unsubscribe(fn) {
|
|
217
|
+
this.listeners["delete"](fn);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
_proto.triggerUpdate = function triggerUpdate() {
|
|
221
|
+
this.listeners.forEach(function (fn) {
|
|
222
|
+
return fn();
|
|
223
|
+
});
|
|
224
|
+
this.triggerEditorUpdate();
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
|
|
228
|
+
*/
|
|
229
|
+
;
|
|
230
|
+
|
|
231
|
+
_proto.fetchProjectData = function fetchProjectData() {
|
|
232
|
+
return fetch(this.appUrl + ("/api/public/" + this.projectKey)).then(function (r) {
|
|
233
|
+
return r.json();
|
|
234
|
+
})["catch"](console.error);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* fetch and update the project data, then trigger update to re-render the WeaverseRoot
|
|
238
|
+
*/
|
|
239
|
+
;
|
|
240
|
+
|
|
241
|
+
_proto.updateProjectData = function updateProjectData() {
|
|
242
|
+
|
|
243
|
+
console.log('this.projectData', this.projectData);
|
|
244
|
+
this.initItemData();
|
|
245
|
+
this.triggerUpdate();
|
|
246
|
+
return;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
_proto.triggerEditorUpdate = function triggerEditorUpdate(type) {
|
|
250
|
+
if (type === void 0) {
|
|
251
|
+
type = 'weaverse.workspace.init';
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (this.isEditor) {
|
|
255
|
+
var _window$top;
|
|
256
|
+
|
|
257
|
+
(_window$top = window.top) == null ? void 0 : _window$top.postMessage({
|
|
258
|
+
type: type,
|
|
259
|
+
payload: {
|
|
260
|
+
projectKey: this.projectKey,
|
|
261
|
+
projectData: this.projectData
|
|
262
|
+
}
|
|
263
|
+
}, '*');
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
_proto.initItemData = function initItemData() {
|
|
268
|
+
var _this2 = this;
|
|
269
|
+
|
|
270
|
+
var data = this.projectData;
|
|
271
|
+
|
|
272
|
+
if (data.items) {
|
|
273
|
+
data.items.forEach(function (item) {
|
|
274
|
+
var itemStore = _this2.itemInstances.get(item.id) || new WeaverseItemStore(item, _this2);
|
|
275
|
+
|
|
276
|
+
_this2.itemInstances.set(item.id, itemStore);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
return Weaverse;
|
|
282
|
+
}();
|
|
283
|
+
/**
|
|
284
|
+
* WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
|
|
285
|
+
* @param itemData {ProjectDataItemType} Weaverse item data
|
|
286
|
+
* @param weaverse {Weaverse} Weaverse instance
|
|
287
|
+
* Usage:
|
|
288
|
+
* ```jsx
|
|
289
|
+
* useEffect(() => {
|
|
290
|
+
* let handleUpdate = (update: any) => {
|
|
291
|
+
* setData({...update})
|
|
292
|
+
* }
|
|
293
|
+
* itemInstance.subscribe(handleUpdate)
|
|
294
|
+
* return () => {
|
|
295
|
+
* itemInstance.unsubscribe(handleUpdate)
|
|
296
|
+
* }
|
|
297
|
+
* }, [])
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
|
|
302
|
+
var _this3 = this;
|
|
303
|
+
|
|
304
|
+
if (itemData === void 0) {
|
|
305
|
+
itemData = {};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
this.data = {};
|
|
309
|
+
this.listeners = new Set();
|
|
310
|
+
this.ref = {
|
|
311
|
+
current: null
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
this.setData = function (data) {
|
|
315
|
+
_this3.data = Object.assign(_this3.data, data);
|
|
316
|
+
|
|
317
|
+
_this3.triggerUpdate();
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
this.subscribe = function (fn) {
|
|
321
|
+
_this3.listeners.add(fn);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
this.unsubscribe = function (fn) {
|
|
325
|
+
_this3.listeners["delete"](fn);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
this.triggerUpdate = function () {
|
|
329
|
+
_this3.listeners.forEach(function (fn) {
|
|
330
|
+
return fn(_this3.data);
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
this.data = itemData;
|
|
335
|
+
var _itemData = itemData,
|
|
336
|
+
type = _itemData.type;
|
|
337
|
+
|
|
338
|
+
if (type) {
|
|
339
|
+
this.Component = weaverse.elementInstances.get(type);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
exports.Weaverse = Weaverse;
|
|
344
|
+
exports.WeaverseItemStore = WeaverseItemStore;
|
|
345
|
+
exports.isBrowser = isBrowser;
|
|
346
|
+
exports.isIframe = isIframe;
|
|
347
|
+
exports.isReactNative = isReactNative;
|
|
348
|
+
//# sourceMappingURL=core.cjs.development.js.map
|
|
@@ -0,0 +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,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AyBA,IAAzB,CAAZ;;AACA,IAAA,MAAI,CAACyD,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,CAACrD,IAAN,CAAT;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;;;;;;;;"}
|
|
@@ -0,0 +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;
|
|
2
|
+
//# sourceMappingURL=core.cjs.production.min.js.map
|
|
@@ -0,0 +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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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"}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import StudioBridge from './studio-bridge';
|
|
2
|
+
import Stitches from '@stitches/core/types/stitches';
|
|
3
|
+
export interface ProjectDataItemType {
|
|
4
|
+
type: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
id: string | number;
|
|
7
|
+
description?: string;
|
|
8
|
+
childIds?: Array<string | number>;
|
|
9
|
+
css?: {
|
|
10
|
+
[key: string]: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface ProjectDataType {
|
|
14
|
+
items: ProjectDataItemType[];
|
|
15
|
+
}
|
|
16
|
+
export declare type WeaverseType = {
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
appUrl?: string;
|
|
19
|
+
projectKey?: string;
|
|
20
|
+
projectData?: ProjectDataType;
|
|
21
|
+
};
|
|
22
|
+
export declare class Weaverse {
|
|
23
|
+
/**
|
|
24
|
+
* For storing, registering element React component from Weaverse or created by user/developer
|
|
25
|
+
* @type {Map<string, React.Component>}
|
|
26
|
+
*/
|
|
27
|
+
elementInstances: Map<string, any>;
|
|
28
|
+
/**
|
|
29
|
+
* list of element/items store to provide data, handle state update, state sharing, etc.
|
|
30
|
+
* @type {Map<string, any>}
|
|
31
|
+
*/
|
|
32
|
+
itemInstances: Map<string | number, WeaverseItemStore>;
|
|
33
|
+
/**
|
|
34
|
+
* Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
35
|
+
* @type {string}
|
|
36
|
+
*/
|
|
37
|
+
appUrl: string;
|
|
38
|
+
/**
|
|
39
|
+
* Weaverse project key to access project data via API
|
|
40
|
+
* @type {string}
|
|
41
|
+
*/
|
|
42
|
+
projectKey: string;
|
|
43
|
+
/**
|
|
44
|
+
* Weaverse project data, by default, user can provide project data via React Component:
|
|
45
|
+
* <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
|
|
46
|
+
*/
|
|
47
|
+
projectData: ProjectDataType;
|
|
48
|
+
/**
|
|
49
|
+
* storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
|
|
50
|
+
* @type {Map<string, (data: any) => void>}
|
|
51
|
+
*/
|
|
52
|
+
listeners: Set<any>;
|
|
53
|
+
/**
|
|
54
|
+
* check whether the sdk is inEditor or not
|
|
55
|
+
* if isEditor is true, it means the sdk is inEditor mode, render the editor UI
|
|
56
|
+
* else render the preview UI, plain HTML + CSS + React hydrate
|
|
57
|
+
* @type {boolean}
|
|
58
|
+
*/
|
|
59
|
+
isEditor: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* stitches instance for handling CSS stylesheet, media, theme for Weaverse project
|
|
62
|
+
*/
|
|
63
|
+
stitchesInstance: Stitches | any;
|
|
64
|
+
studioBridge?: StudioBridge;
|
|
65
|
+
/**
|
|
66
|
+
* constructor
|
|
67
|
+
* @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
68
|
+
* @param projectKey {string} Weaverse project key to access project data via API
|
|
69
|
+
* @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
|
|
70
|
+
*/
|
|
71
|
+
constructor({ appUrl, projectKey, projectData }?: WeaverseType);
|
|
72
|
+
/**
|
|
73
|
+
* register the custom React Component to Weaverse, store it into Weaverse.elementInstances
|
|
74
|
+
* @param name {string} unique name of the custom React Component
|
|
75
|
+
* @param element {React.Component} custom React Component
|
|
76
|
+
*/
|
|
77
|
+
registerElement(name: string, element: any): void;
|
|
78
|
+
init(): void;
|
|
79
|
+
loadStudio(): boolean;
|
|
80
|
+
subscribe(fn: any): void;
|
|
81
|
+
unsubscribe(fn: any): void;
|
|
82
|
+
triggerUpdate(): void;
|
|
83
|
+
/**
|
|
84
|
+
* fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
|
|
85
|
+
*/
|
|
86
|
+
fetchProjectData(): Promise<any>;
|
|
87
|
+
/**
|
|
88
|
+
* fetch and update the project data, then trigger update to re-render the WeaverseRoot
|
|
89
|
+
*/
|
|
90
|
+
updateProjectData(): void;
|
|
91
|
+
triggerEditorUpdate(type?: string): void;
|
|
92
|
+
initItemData(): void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
|
|
96
|
+
* @param itemData {ProjectDataItemType} Weaverse item data
|
|
97
|
+
* @param weaverse {Weaverse} Weaverse instance
|
|
98
|
+
* Usage:
|
|
99
|
+
* ```jsx
|
|
100
|
+
* useEffect(() => {
|
|
101
|
+
* let handleUpdate = (update: any) => {
|
|
102
|
+
* setData({...update})
|
|
103
|
+
* }
|
|
104
|
+
* itemInstance.subscribe(handleUpdate)
|
|
105
|
+
* return () => {
|
|
106
|
+
* itemInstance.unsubscribe(handleUpdate)
|
|
107
|
+
* }
|
|
108
|
+
* }, [])
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare class WeaverseItemStore {
|
|
112
|
+
data: any;
|
|
113
|
+
listeners: Set<any>;
|
|
114
|
+
Component: any;
|
|
115
|
+
ref: any;
|
|
116
|
+
constructor(itemData: any, weaverse: Weaverse);
|
|
117
|
+
setData: (data: any) => void;
|
|
118
|
+
subscribe: (fn: any) => void;
|
|
119
|
+
unsubscribe: (fn: any) => void;
|
|
120
|
+
triggerUpdate: () => void;
|
|
121
|
+
}
|
package/dist/core.esm.js
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import fetch from 'isomorphic-unfetch';
|
|
2
|
+
import { env } from 'process';
|
|
3
|
+
|
|
4
|
+
var isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative';
|
|
5
|
+
var isBrowser = typeof window !== 'undefined' && !isReactNative;
|
|
6
|
+
var isIframe = isBrowser && window.top !== window.self;
|
|
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
|
+
// TODO: Implement Weaverse SDK class
|
|
99
|
+
// using stitches core only for framework-agnostic code
|
|
100
|
+
|
|
101
|
+
var stitches = /*#__PURE__*/require('@stitches/core');
|
|
102
|
+
|
|
103
|
+
var Weaverse = /*#__PURE__*/function () {
|
|
104
|
+
/**
|
|
105
|
+
* constructor
|
|
106
|
+
* @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
107
|
+
* @param projectKey {string} Weaverse project key to access project data via API
|
|
108
|
+
* @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
|
|
109
|
+
*/
|
|
110
|
+
function Weaverse(_temp) {
|
|
111
|
+
var _ref = _temp === void 0 ? {} : _temp,
|
|
112
|
+
appUrl = _ref.appUrl,
|
|
113
|
+
projectKey = _ref.projectKey,
|
|
114
|
+
projectData = _ref.projectData;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* For storing, registering element React component from Weaverse or created by user/developer
|
|
118
|
+
* @type {Map<string, React.Component>}
|
|
119
|
+
*/
|
|
120
|
+
this.elementInstances = new Map();
|
|
121
|
+
/**
|
|
122
|
+
* list of element/items store to provide data, handle state update, state sharing, etc.
|
|
123
|
+
* @type {Map<string, any>}
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
this.itemInstances = new Map();
|
|
127
|
+
/**
|
|
128
|
+
* Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
129
|
+
* @type {string}
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
this.appUrl = env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io';
|
|
133
|
+
/**
|
|
134
|
+
* Weaverse project key to access project data via API
|
|
135
|
+
* @type {string}
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
this.projectKey = '';
|
|
139
|
+
/**
|
|
140
|
+
* Weaverse project data, by default, user can provide project data via React Component:
|
|
141
|
+
* <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
this.projectData = {
|
|
145
|
+
items: []
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
|
|
149
|
+
* @type {Map<string, (data: any) => void>}
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
this.listeners = new Set();
|
|
153
|
+
/**
|
|
154
|
+
* check whether the sdk is inEditor or not
|
|
155
|
+
* if isEditor is true, it means the sdk is inEditor mode, render the editor UI
|
|
156
|
+
* else render the preview UI, plain HTML + CSS + React hydrate
|
|
157
|
+
* @type {boolean}
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
this.isEditor = false;
|
|
161
|
+
this.appUrl = appUrl || this.appUrl;
|
|
162
|
+
this.projectKey = projectKey || this.projectKey;
|
|
163
|
+
projectData && (this.projectData = projectData);
|
|
164
|
+
this.init();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* register the custom React Component to Weaverse, store it into Weaverse.elementInstances
|
|
168
|
+
* @param name {string} unique name of the custom React Component
|
|
169
|
+
* @param element {React.Component} custom React Component
|
|
170
|
+
*/
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
var _proto = Weaverse.prototype;
|
|
174
|
+
|
|
175
|
+
_proto.registerElement = function registerElement(name, element) {
|
|
176
|
+
this.elementInstances.set(name, element);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
_proto.init = function init() {
|
|
180
|
+
// init the stitches instance
|
|
181
|
+
this.stitchesInstance = stitches.createStitches({
|
|
182
|
+
prefix: 'weaverse',
|
|
183
|
+
media: {
|
|
184
|
+
bp1: '(min-width: 640px)',
|
|
185
|
+
bp2: '(max-width: 768px)',
|
|
186
|
+
bp3: '(min-width: 1024px)'
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
_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
|
+
}
|
|
204
|
+
|
|
205
|
+
return false;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
_proto.subscribe = function subscribe(fn) {
|
|
209
|
+
this.listeners.add(fn);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
_proto.unsubscribe = function unsubscribe(fn) {
|
|
213
|
+
this.listeners["delete"](fn);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
_proto.triggerUpdate = function triggerUpdate() {
|
|
217
|
+
this.listeners.forEach(function (fn) {
|
|
218
|
+
return fn();
|
|
219
|
+
});
|
|
220
|
+
this.triggerEditorUpdate();
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
|
|
224
|
+
*/
|
|
225
|
+
;
|
|
226
|
+
|
|
227
|
+
_proto.fetchProjectData = function fetchProjectData() {
|
|
228
|
+
return fetch(this.appUrl + ("/api/public/" + this.projectKey)).then(function (r) {
|
|
229
|
+
return r.json();
|
|
230
|
+
})["catch"](console.error);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* fetch and update the project data, then trigger update to re-render the WeaverseRoot
|
|
234
|
+
*/
|
|
235
|
+
;
|
|
236
|
+
|
|
237
|
+
_proto.updateProjectData = function updateProjectData() {
|
|
238
|
+
|
|
239
|
+
console.log('this.projectData', this.projectData);
|
|
240
|
+
this.initItemData();
|
|
241
|
+
this.triggerUpdate();
|
|
242
|
+
return;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
_proto.triggerEditorUpdate = function triggerEditorUpdate(type) {
|
|
246
|
+
if (type === void 0) {
|
|
247
|
+
type = 'weaverse.workspace.init';
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this.isEditor) {
|
|
251
|
+
var _window$top;
|
|
252
|
+
|
|
253
|
+
(_window$top = window.top) == null ? void 0 : _window$top.postMessage({
|
|
254
|
+
type: type,
|
|
255
|
+
payload: {
|
|
256
|
+
projectKey: this.projectKey,
|
|
257
|
+
projectData: this.projectData
|
|
258
|
+
}
|
|
259
|
+
}, '*');
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
_proto.initItemData = function initItemData() {
|
|
264
|
+
var _this2 = this;
|
|
265
|
+
|
|
266
|
+
var data = this.projectData;
|
|
267
|
+
|
|
268
|
+
if (data.items) {
|
|
269
|
+
data.items.forEach(function (item) {
|
|
270
|
+
var itemStore = _this2.itemInstances.get(item.id) || new WeaverseItemStore(item, _this2);
|
|
271
|
+
|
|
272
|
+
_this2.itemInstances.set(item.id, itemStore);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
return Weaverse;
|
|
278
|
+
}();
|
|
279
|
+
/**
|
|
280
|
+
* WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
|
|
281
|
+
* @param itemData {ProjectDataItemType} Weaverse item data
|
|
282
|
+
* @param weaverse {Weaverse} Weaverse instance
|
|
283
|
+
* Usage:
|
|
284
|
+
* ```jsx
|
|
285
|
+
* useEffect(() => {
|
|
286
|
+
* let handleUpdate = (update: any) => {
|
|
287
|
+
* setData({...update})
|
|
288
|
+
* }
|
|
289
|
+
* itemInstance.subscribe(handleUpdate)
|
|
290
|
+
* return () => {
|
|
291
|
+
* itemInstance.unsubscribe(handleUpdate)
|
|
292
|
+
* }
|
|
293
|
+
* }, [])
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
|
|
297
|
+
var WeaverseItemStore = function WeaverseItemStore(itemData, weaverse) {
|
|
298
|
+
var _this3 = this;
|
|
299
|
+
|
|
300
|
+
if (itemData === void 0) {
|
|
301
|
+
itemData = {};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
this.data = {};
|
|
305
|
+
this.listeners = new Set();
|
|
306
|
+
this.ref = {
|
|
307
|
+
current: null
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
this.setData = function (data) {
|
|
311
|
+
_this3.data = Object.assign(_this3.data, data);
|
|
312
|
+
|
|
313
|
+
_this3.triggerUpdate();
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
this.subscribe = function (fn) {
|
|
317
|
+
_this3.listeners.add(fn);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
this.unsubscribe = function (fn) {
|
|
321
|
+
_this3.listeners["delete"](fn);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
this.triggerUpdate = function () {
|
|
325
|
+
_this3.listeners.forEach(function (fn) {
|
|
326
|
+
return fn(_this3.data);
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
this.data = itemData;
|
|
331
|
+
var _itemData = itemData,
|
|
332
|
+
type = _itemData.type;
|
|
333
|
+
|
|
334
|
+
if (type) {
|
|
335
|
+
this.Component = weaverse.elementInstances.get(type);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export { Weaverse, WeaverseItemStore, isBrowser, isIframe, isReactNative };
|
|
340
|
+
//# sourceMappingURL=core.esm.js.map
|
|
@@ -0,0 +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,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;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ProjectDataItemType, ProjectDataType, Weaverse, WeaverseItemStore, WeaverseType } from './core';
|
|
2
|
+
import { isBrowser, isIframe, isReactNative } from './utils';
|
|
3
|
+
export { Weaverse, WeaverseType, WeaverseItemStore, ProjectDataType, ProjectDataItemType, isBrowser, isReactNative, isIframe };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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;
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.1",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"typings": "dist/index.d.ts",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=10"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"start": "tsdx watch",
|
|
17
|
+
"dev": "tsdx watch",
|
|
18
|
+
"build": "tsdx build",
|
|
19
|
+
"test": "tsdx test",
|
|
20
|
+
"lint": "tsdx lint",
|
|
21
|
+
"prepare": "tsdx build",
|
|
22
|
+
"size": "size-limit",
|
|
23
|
+
"analyze": "size-limit --why"
|
|
24
|
+
},
|
|
25
|
+
"husky": {
|
|
26
|
+
"hooks": {
|
|
27
|
+
"pre-commit": "tsdx lint"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"prettier": {
|
|
31
|
+
"printWidth": 80,
|
|
32
|
+
"semi": true,
|
|
33
|
+
"singleQuote": true,
|
|
34
|
+
"trailingComma": "es5"
|
|
35
|
+
},
|
|
36
|
+
"name": "@weaverse/core",
|
|
37
|
+
"author": "Paul",
|
|
38
|
+
"module": "dist/core.esm.js",
|
|
39
|
+
"size-limit": [
|
|
40
|
+
{
|
|
41
|
+
"path": "dist/core.cjs.production.min.js",
|
|
42
|
+
"limit": "10 KB"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"path": "dist/core.esm.js",
|
|
46
|
+
"limit": "10 KB"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@stitches/core": "^1.2.6",
|
|
51
|
+
"isomorphic-unfetch": "^3.1.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@size-limit/preset-small-lib": "^7.0.8",
|
|
55
|
+
"size-limit": "^7.0.8",
|
|
56
|
+
"tsdx": "^0.14.1",
|
|
57
|
+
"tslib": "^2.3.1",
|
|
58
|
+
"typescript": "^4.5.5"
|
|
59
|
+
}
|
|
60
|
+
}
|