@weaverse/react 0.1.5 → 0.1.9

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/src/core.ts DELETED
@@ -1,190 +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 = 'http://localhost:3000'
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
- }
40
-
41
- registerElement(name: string, element: any) {
42
- this.elementInstances.set(name, element)
43
- }
44
-
45
- init() {
46
- Object.keys(Elements).forEach(key => {
47
- // @ts-ignore
48
- Elements[key]?.configs?.type && this.registerElement(Elements[key].configs.type, Elements[key])
49
- })
50
- this.subscribeMessageEvent()
51
- }
52
-
53
- subscribe(fn: any) {
54
- this.listeners.add(fn)
55
- }
56
-
57
- unsubscribe(fn: any) {
58
- this.listeners.delete(fn)
59
- }
60
-
61
- triggerUpdate() {
62
- this.listeners.forEach(fn => fn())
63
- this.triggerEditorUpdate()
64
- }
65
-
66
- fetchProjectData() {
67
- return fetch(this.appUrl + `/api/public/${this.projectKey}`).then(r => r.json())
68
- }
69
-
70
- updateProjectData() {
71
- if (this.projectKey) {
72
- this.fetchProjectData().then(data => {
73
- if (data) {
74
- this.projectData = data
75
- this.initItemData()
76
- this.triggerUpdate()
77
- }
78
- }).catch(err => {
79
- console.error(err)
80
- })
81
- }
82
- }
83
-
84
- subscribeMessageEvent() {
85
- if (typeof this.currentFrameSubscription === 'function') {
86
- this.currentFrameSubscription()
87
- }
88
- isBrowser && window.addEventListener('message', this.handleMessageEvent)
89
- this.currentFrameSubscription = () => {
90
- isBrowser && window.removeEventListener('message', this.handleMessageEvent)
91
- }
92
- return this.currentFrameSubscription
93
- }
94
-
95
- triggerEditorUpdate(type = 'weaverse.workspace.init') {
96
- if (this.isEditor) {
97
- window.top?.postMessage({
98
- type, payload: {
99
- projectKey: this.projectKey,
100
- projectData: this.projectData
101
- }
102
- }, '*')
103
- }
104
- }
105
-
106
- handleMessageEvent = (e: MessageEvent) => {
107
- if (e.data?.type?.startsWith('weaverse.')) {
108
- let type = e.data.type
109
- switch (type) {
110
- case 'weaverse.editor.ready':
111
- this.isEditor = true
112
- break
113
- case 'weaverse.editor.update':
114
- let {payload} = e.data
115
- let {itemId, background} = payload
116
- let instance = this.itemInstances.get(itemId)
117
- if (instance) {
118
- instance.setData({
119
- style: {
120
- background
121
- }
122
- })
123
- }
124
- }
125
- }
126
- }
127
-
128
- initItemData() {
129
- let data = this.projectData
130
- if (data.items) {
131
- data.items.forEach(item => {
132
- let itemStore = this.itemInstances.get(item.id) || new WeaverseItemStore(item, this)
133
- this.itemInstances.set(item.id, itemStore)
134
- })
135
- }
136
- }
137
- }
138
-
139
- export class WeaverseItemStore {
140
- data: any = {}
141
- listeners: Set<any> = new Set()
142
- Component: any
143
-
144
- constructor(itemData: any = {}, weaverse: Weaverse) {
145
- this.data = itemData
146
- let {type} = itemData
147
- if (type) {
148
- this.Component = weaverse.elementInstances.get(type)
149
- }
150
- }
151
-
152
- setData = (data: any) => {
153
- this.data = Object.assign(this.data, data)
154
- this.triggerUpdate()
155
- }
156
-
157
- subscribe = (fn: any) => {
158
- this.listeners.add(fn)
159
- }
160
-
161
- unsubscribe = (fn: any) => {
162
- this.listeners.delete(fn)
163
- }
164
-
165
- triggerUpdate = () => {
166
- this.listeners.forEach(fn => {
167
- return fn(this.data)
168
- })
169
- }
170
-
171
- useSubscription = (fn: any) => {
172
- useEffect(() => {
173
- this.subscribe(fn)
174
- return () => {
175
- this.unsubscribe(fn)
176
- }
177
- }, [])
178
- }
179
-
180
-
181
- // addItem(item: any) {
182
- // this.items.push(item)
183
- // this.triggerUpdate()
184
- // }
185
- //
186
- // removeItem(item: any) {
187
- // this.items = this.items.filter(i => i !== item)
188
- // this.triggerUpdate()
189
- // }
190
- }
package/src/index.tsx DELETED
@@ -1,60 +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
- const RenderItem = ({itemId, context}: { itemId: number, context: any }): any => {
52
- let {itemInstances, elementInstances} = context
53
- let itemInstance = itemInstances.get(itemId)
54
- if (itemInstance) {
55
- return <Item itemInstance={itemInstance} elementInstances={elementInstances} context={context}/>
56
- }
57
- return <></>
58
- }
59
-
60
- export {Weaverse, WeaverseRoot}
package/src/types.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import {ReactChild} from 'react'
2
-
3
- export interface HWSDKConfig {
4
- initialData?: any
5
- token?: string
6
- projectId?: string
7
- hydrate?: boolean
8
- apiUrl?: string
9
- }
10
-
11
- export interface HWProps extends HWSDKConfig {
12
- children?: ReactChild;
13
- }
@@ -1,3 +0,0 @@
1
- export const isReactNative = typeof navigator === 'object' && navigator.product === 'ReactNative'
2
- export const isBrowser = typeof window !== 'undefined' && !isReactNative
3
- export const isIframe = isBrowser && window.top !== window.self
@@ -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
- )