@weaverse/core 5.17.1 → 5.18.0

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 ADDED
@@ -0,0 +1,251 @@
1
+ /*
2
+ @weaverse/core is the core package of Weaverse SDKs, it contains the core logic of Weaverse.
3
+ It is a singleton class that can be used to register item, store project data, trigger update on item or project data, etc.
4
+
5
+ v5.8.4 Architectural Changes:
6
+ - Removed Stitches CSS-in-JS library for simplified styling approach
7
+ - Removed platformType property for unified component rendering
8
+ - Uses className-based styling instead of dynamic CSS generation
9
+
10
+ ---
11
+ Licensed under MIT License.
12
+ Source: https://github.com/weaverse/weaverse
13
+ About Weaverse: https://weaverse.io
14
+ */
15
+
16
+ import { createRef, type RefObject } from 'react'
17
+ import { version } from '../package.json'
18
+ import type {
19
+ BreakPoints,
20
+ ElementCSS,
21
+ ElementData,
22
+ WeaverseCoreParams,
23
+ WeaverseProjectDataType,
24
+ } from './types'
25
+ import { merge } from './utils'
26
+ import { EventEmitter } from './utils/event-emitter'
27
+
28
+ /** Holds the mutable runtime state for one rendered Weaverse element. */
29
+ export class WeaverseItemStore extends EventEmitter {
30
+ /** The Core runtime that owns this item. */
31
+ weaverse: Weaverse
32
+
33
+ /** React ref for the element's rendered HTML node. */
34
+ ref: RefObject<HTMLElement | null> = createRef<HTMLElement | null>()
35
+
36
+ /** The current serialized state of the element. */
37
+ _store: ElementData = { id: '', type: '' }
38
+
39
+ /** Creates an item store and registers it with the owning runtime. */
40
+ constructor(initialData: ElementData, weaverse: Weaverse) {
41
+ super()
42
+ const { type, id } = initialData || {}
43
+ this.weaverse = weaverse
44
+ if (id && type) {
45
+ Weaverse.itemInstances.set(id, this)
46
+ Object.assign(this._store, initialData)
47
+ } else {
48
+ throw new Error(
49
+ `'id' and 'type' are required to create a new Weaverse item.`
50
+ )
51
+ }
52
+ this._store.css = this.getDefaultCss()
53
+ }
54
+
55
+ /** The element's unique identifier. */
56
+ get _id() {
57
+ return this._store.id
58
+ }
59
+
60
+ /** The element's current rendered HTML node, if mounted. */
61
+ get _element() {
62
+ return this.ref.current
63
+ }
64
+
65
+ /** The registered component definition for this element type. */
66
+ get Element() {
67
+ return this.weaverse.elementRegistry.get(this._store.type)
68
+ }
69
+
70
+ /** Returns registered default styles merged with the element's saved styles. */
71
+ getDefaultCss = (): ElementCSS => {
72
+ const defaultCss = this.Element?.defaultCss || {}
73
+ const currentCss = this._store.css || {}
74
+ return merge(defaultCss, currentCss)
75
+ }
76
+
77
+ /**
78
+ * The element's current serialized data. Assigning a value merges it into
79
+ * the existing data.
80
+ */
81
+ get data(): ElementData {
82
+ return this._store
83
+ }
84
+
85
+ // Setter behavior is documented on the getter, which owns the property's JSDoc.
86
+ set data(update: Omit<ElementData, 'id' | 'type'>) {
87
+ this._store = { ...this._store, ...update }
88
+ }
89
+
90
+ /** Applies an element data update and notifies subscribers. */
91
+ setData = (update: Omit<ElementData, 'id' | 'type'>) => {
92
+ this.data = update
93
+ this.triggerUpdate()
94
+ return this.data
95
+ }
96
+
97
+ /** Returns the element's current serialized data. */
98
+ getSnapShot = () => this.data
99
+
100
+ /** Notifies subscribers with the element's current data. */
101
+ triggerUpdate = () => {
102
+ this.emit(this._store)
103
+ }
104
+ }
105
+
106
+ /** Coordinates project data, registered elements, and item stores at runtime. */
107
+ export class Weaverse extends EventEmitter {
108
+ /** The DOM node containing the rendered project, if mounted. */
109
+ contentRootElement: HTMLElement | null = null
110
+
111
+ /** Item stores keyed by element identifier. */
112
+ static itemInstances = new Map()
113
+
114
+ /** Base URL of the Weaverse Studio host. */
115
+ weaverseHost = 'https://studio.weaverse.io'
116
+
117
+ /** Version of the Weaverse runtime. */
118
+ weaverseVersion = version
119
+
120
+ /** Version of the installed Core SDK. */
121
+ sdkVersion = version
122
+
123
+ /** Identifier of the project being rendered. */
124
+ projectId = ''
125
+
126
+ /** Whether the runtime is connected to Studio design mode. */
127
+ isDesignMode = false
128
+
129
+ /** Whether the runtime is rendering a preview. */
130
+ isPreviewMode = false
131
+
132
+ /** Optional bridge used to communicate with Weaverse Studio. */
133
+ studioBridge?: any
134
+
135
+ /** Item store class instantiated for serialized project items. */
136
+ declare static ItemConstructor: typeof WeaverseItemStore
137
+
138
+ /** The project's current serialized data. */
139
+ declare data: WeaverseProjectDataType
140
+
141
+ /** Optional application data exposed while rendering the project. */
142
+ declare dataContext: Record<string, unknown> | null
143
+
144
+ /** Component definitions keyed by element type. */
145
+ static elementRegistry = new Map()
146
+
147
+ /** Media queries used for responsive element styles. */
148
+ static mediaBreakPoints: BreakPoints = {
149
+ desktop: 'all',
150
+ // max-width need to subtract 0.02px to prevent bug
151
+ // ref: https://getbootstrap.com/docs/5.1/layout/breakpoints/#max-width
152
+ mobile: '(max-width: 767.98px)',
153
+ }
154
+
155
+ /** Creates a Core runtime from serialized project data. */
156
+ constructor(params: WeaverseCoreParams) {
157
+ super()
158
+ // Note: platformType parameter was removed in v5.8.4
159
+ // Components now use unified rendering approach without platform-specific logic
160
+ for (const param of Object.entries(params)) {
161
+ const [key, value] = param
162
+ this[key] = value || this[key]
163
+ }
164
+ this.initProject()
165
+ }
166
+
167
+ /** Returns the project's current serialized data. */
168
+ getSnapShot = () => this.data
169
+
170
+ /** Creates or updates an item store for every serialized project item. */
171
+ initProject = () => {
172
+ const { data } = this
173
+ const itemInstances = Weaverse.itemInstances
174
+ if (data?.items) {
175
+ // data.items.forEach((item) => {
176
+ // let itemInstance = itemInstances.get(item.id)
177
+ // if (itemInstance) {
178
+ // itemInstance.setData(item)
179
+ // } else {
180
+ // this.createItemInstance(item)
181
+ // }
182
+ // })
183
+ for (const item of data.items) {
184
+ const itemInstance = itemInstances.get(item.id)
185
+ if (itemInstance) {
186
+ itemInstance.setData(item)
187
+ } else {
188
+ this.createItemInstance(item)
189
+ }
190
+ }
191
+ }
192
+ }
193
+
194
+ /** Item stores keyed by element identifier. */
195
+ get itemInstances() {
196
+ return Weaverse.itemInstances
197
+ }
198
+
199
+ /** Creates an item store for serialized element data. */
200
+ createItemInstance = (data: ElementData) =>
201
+ new Weaverse.ItemConstructor(data, this)
202
+
203
+ /** Registers a component definition for an element type. */
204
+ static registerElement = registerElement
205
+
206
+ /** Component definitions keyed by element type. */
207
+ get elementRegistry() {
208
+ return Weaverse.elementRegistry
209
+ }
210
+
211
+ /** Replaces the project data reference and notifies subscribers. */
212
+ triggerUpdate = () => {
213
+ // make new copy of data to trigger update
214
+ this.data = { ...this.data }
215
+ this.emit(this.data)
216
+ }
217
+
218
+ /** Notifies subscribers of every registered item store. */
219
+ refreshAllItems() {
220
+ for (const item of Weaverse.itemInstances.values()) {
221
+ item.triggerUpdate()
222
+ }
223
+ }
224
+
225
+ /** Replaces the project data and initializes its item stores. */
226
+ setProjectData = (data: WeaverseProjectDataType) => {
227
+ this.data = data
228
+ this.initProject()
229
+ }
230
+ }
231
+
232
+ /** Component definition accepted by {@link registerElement}. */
233
+ export interface RegisterElementDefinition {
234
+ /** Unique element type used as the registry key. */
235
+ type: string
236
+ /** Additional framework-specific component metadata. */
237
+ [key: string]: any
238
+ }
239
+
240
+ /** Registers a component definition for its declared element type. */
241
+ export function registerElement(element: RegisterElementDefinition) {
242
+ if (element?.type) {
243
+ if (!Weaverse.elementRegistry.has(element.type)) {
244
+ Weaverse.elementRegistry.set(element?.type, element)
245
+ }
246
+ } else {
247
+ console.error("Cannot register element without 'type'.")
248
+ }
249
+ }
250
+
251
+ export { EventEmitter }
@@ -0,0 +1,215 @@
1
+ // This file is generated from Weaverse Studio
2
+
3
+ /** Icon identifiers supported by Weaverse Studio controls. */
4
+ export type IconName =
5
+ | 'accessibility'
6
+ | 'activity-square'
7
+ | 'alarm-clock-off'
8
+ | 'alarm-clock'
9
+ | 'alarm-minus'
10
+ | 'alarm-plus'
11
+ | 'album'
12
+ | 'alert-circle'
13
+ | 'alert-octagon'
14
+ | 'alert-triangle'
15
+ | 'align-box-center-bottom-outline'
16
+ | 'align-box-center-middle-outline'
17
+ | 'align-box-center-top-outline'
18
+ | 'align-box-left-bottom-outline'
19
+ | 'align-box-left-middle-outline'
20
+ | 'align-box-left-top-outline'
21
+ | 'align-box-right-bottom-outline'
22
+ | 'align-box-right-middle-outline'
23
+ | 'align-box-right-top-outline'
24
+ | 'align-center-vertical'
25
+ | 'align-end-vertical'
26
+ | 'align-left'
27
+ | 'align-right'
28
+ | 'align-start-vertical'
29
+ | 'arrow-down-a-z'
30
+ | 'arrow-down-narrow-wide'
31
+ | 'arrow-left-from-line'
32
+ | 'arrow-left-right'
33
+ | 'arrow-up-a-z'
34
+ | 'arrow-up-narrow-wide'
35
+ | 'atom'
36
+ | 'blocks'
37
+ | 'book-image'
38
+ | 'book-marked'
39
+ | 'book-text'
40
+ | 'book'
41
+ | 'box-select'
42
+ | 'box'
43
+ | 'boxes'
44
+ | 'brackets'
45
+ | 'calendar-minus'
46
+ | 'calendar-plus'
47
+ | 'calendar'
48
+ | 'check-check'
49
+ | 'check-circle'
50
+ | 'check'
51
+ | 'chevron-down'
52
+ | 'chevron-left'
53
+ | 'chevron-right'
54
+ | 'chevron-up'
55
+ | 'chevrons-up-down'
56
+ | 'circle-dot'
57
+ | 'circle'
58
+ | 'code'
59
+ | 'columns-2'
60
+ | 'columns-3'
61
+ | 'concierge-bell'
62
+ | 'contact'
63
+ | 'copy'
64
+ | 'corner-up-left'
65
+ | 'expand'
66
+ | 'external-link'
67
+ | 'eye-off'
68
+ | 'eye'
69
+ | 'facebook'
70
+ | 'file-code-2'
71
+ | 'file-plus-2'
72
+ | 'file-search'
73
+ | 'file-text'
74
+ | 'file'
75
+ | 'filter'
76
+ | 'flag'
77
+ | 'flask-conical'
78
+ | 'focus'
79
+ | 'fold-horizontal'
80
+ | 'fold-vertical'
81
+ | 'folder-git-2'
82
+ | 'folder-open'
83
+ | 'folder-plus'
84
+ | 'folder-search'
85
+ | 'folder-symlink'
86
+ | 'folder'
87
+ | 'form-input'
88
+ | 'frown'
89
+ | 'gallery-horizontal-end'
90
+ | 'gallery-horizontal'
91
+ | 'gauge'
92
+ | 'github'
93
+ | 'globe'
94
+ | 'grid-2x2'
95
+ | 'grip-vertical'
96
+ | 'hammer'
97
+ | 'hard-drive-download'
98
+ | 'help-circle'
99
+ | 'home'
100
+ | 'hourglass'
101
+ | 'image-off'
102
+ | 'image-plus'
103
+ | 'image'
104
+ | 'images'
105
+ | 'info'
106
+ | 'languages'
107
+ | 'layers'
108
+ | 'layout-dashboard'
109
+ | 'layout-grid'
110
+ | 'layout-list'
111
+ | 'layout-panel-left'
112
+ | 'layout-template'
113
+ | 'life-buoy'
114
+ | 'lightbulb'
115
+ | 'link'
116
+ | 'linkedin'
117
+ | 'list-end'
118
+ | 'list-start'
119
+ | 'list-todo'
120
+ | 'loader-2'
121
+ | 'loader'
122
+ | 'log-out'
123
+ | 'map-pinned'
124
+ | 'map'
125
+ | 'maximize'
126
+ | 'message-circle'
127
+ | 'message-square'
128
+ | 'messages-square'
129
+ | 'minimize'
130
+ | 'minus'
131
+ | 'monitor-play'
132
+ | 'monitor'
133
+ | 'moon'
134
+ | 'more-horizontal'
135
+ | 'more-vertical'
136
+ | 'mouse-pointer-2'
137
+ | 'mouse-pointer-click'
138
+ | 'mouse-pointer-square-dashed'
139
+ | 'mouse-pointer-square'
140
+ | 'mouse-pointer'
141
+ | 'mouse'
142
+ | 'move-down-left'
143
+ | 'move-down-right'
144
+ | 'move-down'
145
+ | 'move-horizontal'
146
+ | 'move-left'
147
+ | 'move-right'
148
+ | 'move-up-left'
149
+ | 'move-up-right'
150
+ | 'move-up'
151
+ | 'newspaper'
152
+ | 'notebook-tabs'
153
+ | 'package-check'
154
+ | 'package-open'
155
+ | 'package'
156
+ | 'paintbrush-2'
157
+ | 'paintbrush'
158
+ | 'palette'
159
+ | 'panel-left'
160
+ | 'pen'
161
+ | 'pencil-line'
162
+ | 'pencil'
163
+ | 'phone'
164
+ | 'plane-landing'
165
+ | 'play-circle'
166
+ | 'plus-circle'
167
+ | 'plus-square'
168
+ | 'plus'
169
+ | 'presentation'
170
+ | 'puzzle'
171
+ | 'rectangle-horizontal'
172
+ | 'rocket'
173
+ | 'search'
174
+ | 'settings'
175
+ | 'shield-question'
176
+ | 'shopping-bag'
177
+ | 'shopping-cart'
178
+ | 'shrink'
179
+ | 'slack'
180
+ | 'sliders-horizontal'
181
+ | 'sliders'
182
+ | 'slideshow-outline'
183
+ | 'smartphone'
184
+ | 'sparkles'
185
+ | 'square'
186
+ | 'squircle'
187
+ | 'star-filled'
188
+ | 'star-half-filled'
189
+ | 'star'
190
+ | 'store'
191
+ | 'stretch-vertical'
192
+ | 'sun'
193
+ | 'tag'
194
+ | 'tags'
195
+ | 'terminal'
196
+ | 'text-cursor-input'
197
+ | 'timer'
198
+ | 'trash'
199
+ | 'truck'
200
+ | 'twitter'
201
+ | 'type'
202
+ | 'undo-2'
203
+ | 'undo'
204
+ | 'upload-cloud'
205
+ | 'upload'
206
+ | 'user'
207
+ | 'users'
208
+ | 'video'
209
+ | 'wand-2'
210
+ | 'wand-sparkles'
211
+ | 'x-logo'
212
+ | 'x-octagon'
213
+ | 'x'
214
+ | 'youtube'
215
+ | 'zap'
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Framework-agnostic runtime, element state, and shared types for Weaverse SDKs.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export * from './core'
8
+ export type { IconName } from './icon-name'
9
+ export * from './types'
10
+ export * from './utils'
11
+ export type { AnyFunction } from './utils/event-emitter'