@weaverse/react 0.1.6 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/elements/base.d.ts +3 -0
- package/dist/elements/button.d.ts +3 -0
- package/dist/elements/grid.d.ts +12 -0
- package/dist/elements/index.d.ts +8 -0
- package/dist/index.d.ts +1 -6
- package/dist/renderer.d.ts +7 -0
- package/dist/sdk-react.cjs.development.js +61 -294
- package/dist/sdk-react.cjs.development.js.map +1 -1
- package/dist/sdk-react.cjs.production.min.js +1 -1
- package/dist/sdk-react.cjs.production.min.js.map +1 -1
- package/dist/sdk-react.esm.js +61 -295
- package/dist/sdk-react.esm.js.map +1 -1
- package/package.json +11 -14
- package/dist/core.d.ts +0 -45
- package/dist/utils/index.d.ts +0 -3
- package/src/core.ts +0 -191
- package/src/index.tsx +0 -61
- package/src/types.d.ts +0 -13
- package/src/utils/index.ts +0 -3
- package/src/utils/toHash.js +0 -23
package/src/core.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
// TODO: Implement Weaverse SDK class
|
|
2
|
-
import fetch from 'isomorphic-unfetch'
|
|
3
|
-
import Elements from '@weaverse/elements'
|
|
4
|
-
import {useEffect} from 'react'
|
|
5
|
-
import {isBrowser} from './utils'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export interface ProjectDataItemType {
|
|
9
|
-
type: string
|
|
10
|
-
name: string
|
|
11
|
-
id: string | number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ProjectDataType {
|
|
15
|
-
items: ProjectDataItemType[]
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class Weaverse {
|
|
19
|
-
elementInstances = new Map<string, any>()
|
|
20
|
-
itemInstances = new Map<string | number, WeaverseItemStore>()
|
|
21
|
-
appUrl: string = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://weaverse.io'
|
|
22
|
-
projectKey: string = ''
|
|
23
|
-
projectData: ProjectDataType = {
|
|
24
|
-
items: []
|
|
25
|
-
}
|
|
26
|
-
listeners: Set<any> = new Set()
|
|
27
|
-
isEditor = false
|
|
28
|
-
currentFrameSubscription: any
|
|
29
|
-
|
|
30
|
-
constructor({
|
|
31
|
-
appUrl,
|
|
32
|
-
projectKey,
|
|
33
|
-
projectData
|
|
34
|
-
}: { appUrl?: string, projectKey?: string, projectData?: ProjectDataType } = {}) {
|
|
35
|
-
this.appUrl = appUrl || this.appUrl
|
|
36
|
-
this.projectKey = projectKey || this.projectKey
|
|
37
|
-
projectData && (this.projectData = projectData)
|
|
38
|
-
this.init()
|
|
39
|
-
console.log('process.env.NODE_ENV', process.env.NODE_ENV)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
registerElement(name: string, element: any) {
|
|
43
|
-
this.elementInstances.set(name, element)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
init() {
|
|
47
|
-
Object.keys(Elements).forEach(key => {
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
Elements[key]?.configs?.type && this.registerElement(Elements[key].configs.type, Elements[key])
|
|
50
|
-
})
|
|
51
|
-
this.subscribeMessageEvent()
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
subscribe(fn: any) {
|
|
55
|
-
this.listeners.add(fn)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
unsubscribe(fn: any) {
|
|
59
|
-
this.listeners.delete(fn)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
triggerUpdate() {
|
|
63
|
-
this.listeners.forEach(fn => fn())
|
|
64
|
-
this.triggerEditorUpdate()
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
fetchProjectData() {
|
|
68
|
-
return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json())
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
updateProjectData() {
|
|
72
|
-
if (this.projectKey) {
|
|
73
|
-
this.fetchProjectData().then(data => {
|
|
74
|
-
if (data) {
|
|
75
|
-
this.projectData = data
|
|
76
|
-
this.initItemData()
|
|
77
|
-
this.triggerUpdate()
|
|
78
|
-
}
|
|
79
|
-
}).catch(err => {
|
|
80
|
-
console.error(err)
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
subscribeMessageEvent() {
|
|
86
|
-
if (typeof this.currentFrameSubscription === 'function') {
|
|
87
|
-
this.currentFrameSubscription()
|
|
88
|
-
}
|
|
89
|
-
isBrowser && window.addEventListener('message', this.handleMessageEvent)
|
|
90
|
-
this.currentFrameSubscription = () => {
|
|
91
|
-
isBrowser && window.removeEventListener('message', this.handleMessageEvent)
|
|
92
|
-
}
|
|
93
|
-
return this.currentFrameSubscription
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
triggerEditorUpdate(type = 'weaverse.workspace.init') {
|
|
97
|
-
if (this.isEditor) {
|
|
98
|
-
window.top?.postMessage({
|
|
99
|
-
type, payload: {
|
|
100
|
-
projectKey: this.projectKey,
|
|
101
|
-
projectData: this.projectData
|
|
102
|
-
}
|
|
103
|
-
}, '*')
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
handleMessageEvent = (e: MessageEvent) => {
|
|
108
|
-
if (e.data?.type?.startsWith('weaverse.')) {
|
|
109
|
-
let type = e.data.type
|
|
110
|
-
switch (type) {
|
|
111
|
-
case 'weaverse.editor.ready':
|
|
112
|
-
this.isEditor = true
|
|
113
|
-
break
|
|
114
|
-
case 'weaverse.editor.update':
|
|
115
|
-
let {payload} = e.data
|
|
116
|
-
let {itemId, background} = payload
|
|
117
|
-
let instance = this.itemInstances.get(itemId)
|
|
118
|
-
if (instance) {
|
|
119
|
-
instance.setData({
|
|
120
|
-
style: {
|
|
121
|
-
background
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
initItemData() {
|
|
130
|
-
let data = this.projectData
|
|
131
|
-
if (data.items) {
|
|
132
|
-
data.items.forEach(item => {
|
|
133
|
-
let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)
|
|
134
|
-
this.itemInstances.set(item.id, itemStore)
|
|
135
|
-
})
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export class WeaverseItemStore {
|
|
141
|
-
data: any = {}
|
|
142
|
-
listeners: Set<any> = new Set()
|
|
143
|
-
Component: any
|
|
144
|
-
|
|
145
|
-
constructor(itemData: any = {}, weaverse: Weaverse) {
|
|
146
|
-
this.data = itemData
|
|
147
|
-
let {type} = itemData
|
|
148
|
-
if (type) {
|
|
149
|
-
this.Component = weaverse.elementInstances.get(type)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
setData = (data: any) => {
|
|
154
|
-
this.data = Object.assign(this.data, data)
|
|
155
|
-
this.triggerUpdate()
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
subscribe = (fn: any) => {
|
|
159
|
-
this.listeners.add(fn)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
unsubscribe = (fn: any) => {
|
|
163
|
-
this.listeners.delete(fn)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
triggerUpdate = () => {
|
|
167
|
-
this.listeners.forEach(fn => {
|
|
168
|
-
return fn(this.data)
|
|
169
|
-
})
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
useSubscription = (fn: any) => {
|
|
173
|
-
useEffect(() => {
|
|
174
|
-
this.subscribe(fn)
|
|
175
|
-
return () => {
|
|
176
|
-
this.unsubscribe(fn)
|
|
177
|
-
}
|
|
178
|
-
}, [])
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
// addItem(item: any) {
|
|
183
|
-
// this.items.push(item)
|
|
184
|
-
// this.triggerUpdate()
|
|
185
|
-
// }
|
|
186
|
-
//
|
|
187
|
-
// removeItem(item: any) {
|
|
188
|
-
// this.items = this.items.filter(i => i !== item)
|
|
189
|
-
// this.triggerUpdate()
|
|
190
|
-
// }
|
|
191
|
-
}
|
package/src/index.tsx
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
import {useEffect} from 'react'
|
|
3
|
-
import {Weaverse} from './core'
|
|
4
|
-
|
|
5
|
-
const WeaverseRoot = ({context, defaultData}: { context: Weaverse, defaultData: any }) => {
|
|
6
|
-
let [, setData] = React.useState<any>(context.projectData)
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
let handleUpdate = () => {
|
|
9
|
-
setData(context.projectData)
|
|
10
|
-
}
|
|
11
|
-
context.subscribe(handleUpdate)
|
|
12
|
-
context.updateProjectData()
|
|
13
|
-
|
|
14
|
-
return () => {
|
|
15
|
-
context.unsubscribe(handleUpdate)
|
|
16
|
-
}
|
|
17
|
-
}, [])
|
|
18
|
-
if (!context.projectData?.items?.length && defaultData) {
|
|
19
|
-
context.projectData = defaultData
|
|
20
|
-
context.initItemData()
|
|
21
|
-
}
|
|
22
|
-
return <RenderItem itemId={0} context={context}/>
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const Item = ({itemInstance, elementInstances, context}: any) => {
|
|
28
|
-
let [data, setData] = React.useState<any>(itemInstance.data)
|
|
29
|
-
let {id, type, childIds, ...rest} = data
|
|
30
|
-
|
|
31
|
-
let {useSubscription} = itemInstance
|
|
32
|
-
useSubscription((update: any) => {
|
|
33
|
-
console.log('update', update)
|
|
34
|
-
setData({...update})
|
|
35
|
-
})
|
|
36
|
-
let Component = elementInstances.get(type)
|
|
37
|
-
if (Component) {
|
|
38
|
-
return <Component key={id} data-wv-id={id} {...rest}>
|
|
39
|
-
{Array.isArray(childIds)
|
|
40
|
-
&& childIds.map(childId =>
|
|
41
|
-
<RenderItem
|
|
42
|
-
key={childId}
|
|
43
|
-
itemId={childId}
|
|
44
|
-
context={context}
|
|
45
|
-
/>
|
|
46
|
-
)}
|
|
47
|
-
</Component>
|
|
48
|
-
}
|
|
49
|
-
return null
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const RenderItem = ({itemId, context}: { itemId: number, context: any }): any => {
|
|
53
|
-
let {itemInstances, elementInstances} = context
|
|
54
|
-
let itemInstance = itemInstances.get(itemId)
|
|
55
|
-
if (itemInstance) {
|
|
56
|
-
return <Item itemInstance={itemInstance} elementInstances={elementInstances} context={context}/>
|
|
57
|
-
}
|
|
58
|
-
return <></>
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export {Weaverse, WeaverseRoot}
|
package/src/types.d.ts
DELETED
package/src/utils/index.ts
DELETED
package/src/utils/toHash.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const toAlphabeticChar = (/** @type {number} */ code) => String.fromCharCode(code + (code > 25 ? 39 : 97))
|
|
2
|
-
|
|
3
|
-
const toAlphabeticName = (/** @type {number} */ code) => {
|
|
4
|
-
let name = ''
|
|
5
|
-
let x
|
|
6
|
-
|
|
7
|
-
for (x = Math.abs(code); x > 52; x = (x / 52) | 0) name = toAlphabeticChar(x % 52) + name
|
|
8
|
-
|
|
9
|
-
return toAlphabeticChar(x % 52) + name
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const toPhash = (/** @type {number} */ h, /** @type {string} */ x) => {
|
|
13
|
-
let i = x.length
|
|
14
|
-
while (i) h = (h * 33) ^ x.charCodeAt(--i)
|
|
15
|
-
return h
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const toHash = (/** @type {object} */ value) => toAlphabeticName(
|
|
19
|
-
toPhash(
|
|
20
|
-
5381,
|
|
21
|
-
JSON.stringify(value)
|
|
22
|
-
) >>> 0
|
|
23
|
-
)
|