@weaverse/core 0.6.31 → 0.7.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/dist/index.d.ts +293 -0
- package/dist/index.js +9 -22
- package/dist/index.mjs +218 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { ForwardRefExoticComponent, RefObject } from 'react';
|
|
2
|
+
import * as Stitches from '@stitches/core';
|
|
3
|
+
import Stitches$1 from '@stitches/core/types/stitches';
|
|
4
|
+
|
|
5
|
+
declare const stichesUtils: {
|
|
6
|
+
m: (value: string) => {
|
|
7
|
+
margin: string;
|
|
8
|
+
};
|
|
9
|
+
mt: (value: string) => {
|
|
10
|
+
marginTop: string;
|
|
11
|
+
};
|
|
12
|
+
mr: (value: string) => {
|
|
13
|
+
marginRight: string;
|
|
14
|
+
};
|
|
15
|
+
mb: (value: string) => {
|
|
16
|
+
marginBottom: string;
|
|
17
|
+
};
|
|
18
|
+
ml: (value: string) => {
|
|
19
|
+
marginLeft: string;
|
|
20
|
+
};
|
|
21
|
+
mx: (value: string) => {
|
|
22
|
+
marginLeft: string;
|
|
23
|
+
marginRight: string;
|
|
24
|
+
};
|
|
25
|
+
my: (value: string) => {
|
|
26
|
+
marginTop: string;
|
|
27
|
+
marginBottom: string;
|
|
28
|
+
};
|
|
29
|
+
size: (value: string) => {
|
|
30
|
+
width: string;
|
|
31
|
+
height: string;
|
|
32
|
+
};
|
|
33
|
+
px: (value: string) => {
|
|
34
|
+
paddingLeft: string;
|
|
35
|
+
paddingRight: string;
|
|
36
|
+
};
|
|
37
|
+
py: (value: string) => {
|
|
38
|
+
paddingTop: string;
|
|
39
|
+
paddingBottom: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
declare type TODO = any;
|
|
44
|
+
interface ProjectDataType {
|
|
45
|
+
items: ElementData[];
|
|
46
|
+
rootId: string;
|
|
47
|
+
script: {
|
|
48
|
+
css: string;
|
|
49
|
+
js: string;
|
|
50
|
+
};
|
|
51
|
+
pageId?: string;
|
|
52
|
+
}
|
|
53
|
+
interface InitializeData {
|
|
54
|
+
id: string;
|
|
55
|
+
handle: string;
|
|
56
|
+
data: ProjectDataType;
|
|
57
|
+
published: boolean;
|
|
58
|
+
projectKey?: string;
|
|
59
|
+
}
|
|
60
|
+
interface WeaverseType {
|
|
61
|
+
mediaBreakPoints?: any;
|
|
62
|
+
appUrl?: string;
|
|
63
|
+
projectKey?: string;
|
|
64
|
+
projectData?: ProjectDataType;
|
|
65
|
+
isDesignMode?: boolean;
|
|
66
|
+
ssrMode?: boolean;
|
|
67
|
+
}
|
|
68
|
+
declare type WeaverseElement = {
|
|
69
|
+
Component: ForwardRefExoticComponent<any>;
|
|
70
|
+
type: string;
|
|
71
|
+
schema?: ElementSchema;
|
|
72
|
+
};
|
|
73
|
+
interface ElementsMap {
|
|
74
|
+
[key: string]: WeaverseElement;
|
|
75
|
+
}
|
|
76
|
+
declare type CatalogGroup = "essential" | "composition" | "other";
|
|
77
|
+
interface ElementCatalog {
|
|
78
|
+
name: string;
|
|
79
|
+
icon?: string;
|
|
80
|
+
group?: CatalogGroup;
|
|
81
|
+
data?: ElementData[];
|
|
82
|
+
}
|
|
83
|
+
declare type FlagType = "draggable" | "resizable" | "sortable" | "droppable";
|
|
84
|
+
declare type ElementFlags = Partial<Record<FlagType, boolean>>;
|
|
85
|
+
declare type ToolbarAction = "duplicate" | "delete" | "edit-button" | "insert-link" | "select-template" | "copy-styles" | "paste-styles";
|
|
86
|
+
interface ChildElement {
|
|
87
|
+
label: string;
|
|
88
|
+
selector: string;
|
|
89
|
+
}
|
|
90
|
+
declare type ParentType = "container" | "layout" | "root";
|
|
91
|
+
declare type GridSize = {
|
|
92
|
+
rowSpan: number;
|
|
93
|
+
colSpan: number;
|
|
94
|
+
};
|
|
95
|
+
interface ElementSchema {
|
|
96
|
+
title: string;
|
|
97
|
+
type: string;
|
|
98
|
+
parentType: ParentType;
|
|
99
|
+
gridSize?: GridSize;
|
|
100
|
+
inspector?: ElementInspector;
|
|
101
|
+
toolbar?: (ToolbarAction | ToolbarAction[])[];
|
|
102
|
+
childElements?: ChildElement[];
|
|
103
|
+
catalog?: ElementCatalog;
|
|
104
|
+
flags?: ElementFlags;
|
|
105
|
+
}
|
|
106
|
+
interface ElementData {
|
|
107
|
+
id: string;
|
|
108
|
+
type: string;
|
|
109
|
+
childIds?: (string | number)[];
|
|
110
|
+
css?: ElementCSS;
|
|
111
|
+
[key: string]: any;
|
|
112
|
+
}
|
|
113
|
+
declare type WeaverseCSSProperties = Stitches.CSS & Partial<Record<keyof typeof stichesUtils, string | number>>;
|
|
114
|
+
declare type ChildElementCSS = Partial<{
|
|
115
|
+
[selector: string]: WeaverseCSSProperties & ChildElementCSS;
|
|
116
|
+
}>;
|
|
117
|
+
interface ElementCSS {
|
|
118
|
+
"@desktop"?: WeaverseCSSProperties | ChildElementCSS;
|
|
119
|
+
"@mobile"?: WeaverseCSSProperties | ChildElementCSS;
|
|
120
|
+
}
|
|
121
|
+
interface ElementInspector {
|
|
122
|
+
settings?: (AdvancedGroup | BasicGroup)[];
|
|
123
|
+
styles?: (AdvancedGroup | BasicGroup)[];
|
|
124
|
+
}
|
|
125
|
+
declare type AdvancedGroup = {
|
|
126
|
+
type: AdvancedInput;
|
|
127
|
+
};
|
|
128
|
+
declare type BasicGroup = {
|
|
129
|
+
type: "basic";
|
|
130
|
+
label: string;
|
|
131
|
+
inputs: BasicInput[];
|
|
132
|
+
};
|
|
133
|
+
declare type AdvancedInput = "alignment" | "background" | "border" | "countdown" | "dimensions" | "form" | "product" | "spacing" | "visibility";
|
|
134
|
+
interface BasicInput {
|
|
135
|
+
type: InputType;
|
|
136
|
+
label: string;
|
|
137
|
+
name: string;
|
|
138
|
+
defaultValue?: string;
|
|
139
|
+
placeholder?: string;
|
|
140
|
+
helpText?: string;
|
|
141
|
+
options?: {
|
|
142
|
+
value: string;
|
|
143
|
+
label: string;
|
|
144
|
+
icon?: string;
|
|
145
|
+
}[];
|
|
146
|
+
min?: number;
|
|
147
|
+
max?: number;
|
|
148
|
+
step?: number;
|
|
149
|
+
conditions?: TODO[];
|
|
150
|
+
}
|
|
151
|
+
declare type InputType = "color" | "image" | "instagram" | "range" | "select" | "switch" | "text" | "textarea" | "toggle-group";
|
|
152
|
+
declare global {
|
|
153
|
+
interface Window {
|
|
154
|
+
WeaverseStudioBridge: TODO;
|
|
155
|
+
weaverseShopifyProducts: TODO;
|
|
156
|
+
weaverseGlobalSettings: TODO;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* WeaverseItemStore is a store for Weaverse item, it can be used to subscribe/update the item data
|
|
162
|
+
* @param itemData {ElementData} Weaverse item data
|
|
163
|
+
* @param weaverse {Weaverse} Weaverse instance
|
|
164
|
+
* @example
|
|
165
|
+
* useEffect(() => {
|
|
166
|
+
* let handleUpdate = (update: any) => {
|
|
167
|
+
* setData({...update})
|
|
168
|
+
* }
|
|
169
|
+
* itemInstance.subscribe(handleUpdate)
|
|
170
|
+
* return () => {
|
|
171
|
+
* itemInstance.unsubscribe(handleUpdate)
|
|
172
|
+
* }
|
|
173
|
+
* }, [])
|
|
174
|
+
*/
|
|
175
|
+
declare class WeaverseItemStore {
|
|
176
|
+
listeners: Set<any>;
|
|
177
|
+
ref: RefObject<HTMLElement>;
|
|
178
|
+
weaverse: Weaverse;
|
|
179
|
+
stitchesClass: string;
|
|
180
|
+
_data: ElementData;
|
|
181
|
+
_flags: ElementFlags;
|
|
182
|
+
constructor(itemData: ElementData, weaverse: Weaverse);
|
|
183
|
+
get _id(): string;
|
|
184
|
+
get _element(): HTMLElement | null;
|
|
185
|
+
get Element(): WeaverseElement | undefined;
|
|
186
|
+
set data(data: Omit<ElementData, "id" | "type">);
|
|
187
|
+
get data(): ElementData;
|
|
188
|
+
setData: (data: Omit<ElementData, "id" | "type">) => ElementData;
|
|
189
|
+
subscribe: (fn: any) => void;
|
|
190
|
+
unsubscribe: (fn: any) => void;
|
|
191
|
+
triggerUpdate: () => void;
|
|
192
|
+
}
|
|
193
|
+
declare class Weaverse {
|
|
194
|
+
/**
|
|
195
|
+
* The `weaverse-content-root` element of Weaverse SDK
|
|
196
|
+
*/
|
|
197
|
+
contentRootElement: HTMLElement | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* For storing, registering element React component from Weaverse or created by user/developer
|
|
200
|
+
*/
|
|
201
|
+
elementInstances: Map<string, WeaverseElement>;
|
|
202
|
+
/**
|
|
203
|
+
* list of element/items store to provide data, handle state update, state sharing, etc.
|
|
204
|
+
*/
|
|
205
|
+
itemInstances: Map<string | number, WeaverseItemStore>;
|
|
206
|
+
/**
|
|
207
|
+
* Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
208
|
+
* @type {string}
|
|
209
|
+
*/
|
|
210
|
+
appUrl: string;
|
|
211
|
+
/**
|
|
212
|
+
* Weaverse project key to access project data via API
|
|
213
|
+
* @type {string}
|
|
214
|
+
*/
|
|
215
|
+
projectKey: string;
|
|
216
|
+
/**
|
|
217
|
+
* Weaverse project data, by default, user can provide project data via React Component:
|
|
218
|
+
* <WeaverseRoot defaultData={projectData} /> it will be used to server-side rendering
|
|
219
|
+
*/
|
|
220
|
+
projectData: ProjectDataType;
|
|
221
|
+
/**
|
|
222
|
+
* storing subscribe callback function for any component that want to listen to the change of WeaverseRoot
|
|
223
|
+
* @type {Map<string, (data: any) => void>}
|
|
224
|
+
*/
|
|
225
|
+
listeners: Set<any>;
|
|
226
|
+
/**
|
|
227
|
+
* check whether the sdk is isDesignMode or not
|
|
228
|
+
* if isDesignMode is true, it means the sdk is isDesignMode mode, render the editor UI
|
|
229
|
+
* else render the preview UI, plain HTML + CSS + React hydrate
|
|
230
|
+
* @type {boolean}
|
|
231
|
+
*/
|
|
232
|
+
isDesignMode: boolean;
|
|
233
|
+
/**
|
|
234
|
+
* Use in element to optionally render special HTML for hydration
|
|
235
|
+
* @type {boolean}
|
|
236
|
+
*/
|
|
237
|
+
ssrMode: boolean;
|
|
238
|
+
/**
|
|
239
|
+
* stitches instance for handling CSS stylesheet, media, theme for Weaverse project
|
|
240
|
+
*/
|
|
241
|
+
stitchesInstance: Stitches$1 | any;
|
|
242
|
+
studioBridge?: any;
|
|
243
|
+
static WeaverseItemStore: typeof WeaverseItemStore;
|
|
244
|
+
mediaBreakPoints: {
|
|
245
|
+
desktop: string;
|
|
246
|
+
mobile: string;
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* constructor
|
|
250
|
+
* @param appUrl {string} Weaverse base URL that can provide by user/developer. for local development, use localhost:3000
|
|
251
|
+
* @param projectKey {string} Weaverse project key to access project data via API
|
|
252
|
+
* @param projectData {ProjectDataType} Weaverse project data, by default, user can provide project data via React Component.
|
|
253
|
+
* @param mediaBreakPoints {object} Pass down custom media query breakpoints or just use the default.
|
|
254
|
+
* @param isDesignMode {boolean} check whether the sdk is isDesignMode or not
|
|
255
|
+
* @param ssrMode {boolean} Use in element to optionally render special HTML for hydration
|
|
256
|
+
*/
|
|
257
|
+
constructor({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode }?: WeaverseType);
|
|
258
|
+
init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode }?: WeaverseType): void;
|
|
259
|
+
initialized: boolean;
|
|
260
|
+
initializeData: (data: InitializeData) => void;
|
|
261
|
+
loadStudio(): void;
|
|
262
|
+
/**
|
|
263
|
+
* register the custom React Component to Weaverse, store it into Weaverse.elementInstances
|
|
264
|
+
* @param element {WeaverseElement} custom React Component
|
|
265
|
+
*/
|
|
266
|
+
registerElement(element: WeaverseElement): void;
|
|
267
|
+
initStitches: () => void;
|
|
268
|
+
subscribe(fn: any): void;
|
|
269
|
+
unsubscribe(fn: any): void;
|
|
270
|
+
triggerUpdate(): void;
|
|
271
|
+
/**
|
|
272
|
+
* fetch data from Weaverse API (https://weaverse.io/api/v1/projects/:projectKey)
|
|
273
|
+
* @param fetch {fetch} custom fetch function, pass in custom fetch function if you want to use your own fetch function
|
|
274
|
+
* @param appUrl
|
|
275
|
+
* @param projectKey
|
|
276
|
+
*/
|
|
277
|
+
static fetchProjectData({ fetch, appUrl, projectKey, }: {
|
|
278
|
+
fetch?: any;
|
|
279
|
+
appUrl?: string;
|
|
280
|
+
projectKey?: string;
|
|
281
|
+
}): any;
|
|
282
|
+
setProjectData(projectData: ProjectDataType): void;
|
|
283
|
+
/**
|
|
284
|
+
* fetch and update the project data, then trigger update to re-render the WeaverseRoot
|
|
285
|
+
*/
|
|
286
|
+
initProjectItemData(): void;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
declare const isReactNative: boolean;
|
|
290
|
+
declare const isBrowser: boolean;
|
|
291
|
+
declare const isIframe: boolean;
|
|
292
|
+
|
|
293
|
+
export { AdvancedGroup, AdvancedInput, BasicGroup, BasicInput, CatalogGroup, ChildElement, ChildElementCSS, ElementCSS, ElementCatalog, ElementData, ElementFlags, ElementInspector, ElementSchema, ElementsMap, FlagType, GridSize, InitializeData, InputType, ParentType, ProjectDataType, TODO, ToolbarAction, Weaverse, WeaverseCSSProperties, WeaverseElement, WeaverseItemStore, WeaverseType, isBrowser, isIframe, isReactNative };
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
8
|
var __export = (target, all) => {
|
|
25
9
|
for (var name in all)
|
|
26
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -33,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
33
17
|
}
|
|
34
18
|
return to;
|
|
35
19
|
};
|
|
36
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
37
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
25
|
|
|
39
26
|
// src/index.ts
|
|
@@ -124,7 +111,7 @@ var WeaverseItemStore = class {
|
|
|
124
111
|
this.weaverse = weaverse;
|
|
125
112
|
if (id && type) {
|
|
126
113
|
weaverse.itemInstances.set(id, this);
|
|
127
|
-
this.data =
|
|
114
|
+
this.data = { ...itemData };
|
|
128
115
|
}
|
|
129
116
|
}
|
|
130
117
|
get _id() {
|
|
@@ -137,11 +124,11 @@ var WeaverseItemStore = class {
|
|
|
137
124
|
return this.weaverse.elementInstances.get(this._data.type);
|
|
138
125
|
}
|
|
139
126
|
set data(data) {
|
|
140
|
-
this._data =
|
|
127
|
+
this._data = { ...this.data, ...data };
|
|
141
128
|
}
|
|
142
129
|
get data() {
|
|
143
130
|
var _a, _b;
|
|
144
|
-
return
|
|
131
|
+
return { ...(_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps, ...this._data };
|
|
145
132
|
}
|
|
146
133
|
};
|
|
147
134
|
var Weaverse = class {
|
|
@@ -167,7 +154,7 @@ var Weaverse = class {
|
|
|
167
154
|
if (!this.initialized) {
|
|
168
155
|
let { data: pageData, published, id, projectKey } = data;
|
|
169
156
|
this.projectKey = projectKey || this.projectKey;
|
|
170
|
-
this.projectData =
|
|
157
|
+
this.projectData = { ...pageData, pageId: id };
|
|
171
158
|
this.isDesignMode = !published;
|
|
172
159
|
this.initProjectItemData();
|
|
173
160
|
if (this.isDesignMode) {
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// src/core.ts
|
|
2
|
+
import * as stitches from "@stitches/core";
|
|
3
|
+
|
|
4
|
+
// src/utils/index.ts
|
|
5
|
+
var isReactNative = typeof navigator === "object" && navigator.product === "ReactNative";
|
|
6
|
+
var isBrowser = typeof window !== "undefined" && !isReactNative;
|
|
7
|
+
var isIframe = isBrowser && window.top !== window.self;
|
|
8
|
+
|
|
9
|
+
// src/utils/styles.ts
|
|
10
|
+
var stichesUtils = {
|
|
11
|
+
m: (value) => ({
|
|
12
|
+
margin: value
|
|
13
|
+
}),
|
|
14
|
+
mt: (value) => ({
|
|
15
|
+
marginTop: value
|
|
16
|
+
}),
|
|
17
|
+
mr: (value) => ({
|
|
18
|
+
marginRight: value
|
|
19
|
+
}),
|
|
20
|
+
mb: (value) => ({
|
|
21
|
+
marginBottom: value
|
|
22
|
+
}),
|
|
23
|
+
ml: (value) => ({
|
|
24
|
+
marginLeft: value
|
|
25
|
+
}),
|
|
26
|
+
mx: (value) => ({
|
|
27
|
+
marginLeft: value,
|
|
28
|
+
marginRight: value
|
|
29
|
+
}),
|
|
30
|
+
my: (value) => ({
|
|
31
|
+
marginTop: value,
|
|
32
|
+
marginBottom: value
|
|
33
|
+
}),
|
|
34
|
+
size: (value) => ({
|
|
35
|
+
width: value,
|
|
36
|
+
height: value
|
|
37
|
+
}),
|
|
38
|
+
px: (value) => ({
|
|
39
|
+
paddingLeft: value,
|
|
40
|
+
paddingRight: value
|
|
41
|
+
}),
|
|
42
|
+
py: (value) => ({
|
|
43
|
+
paddingTop: value,
|
|
44
|
+
paddingBottom: value
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/core.ts
|
|
49
|
+
var WeaverseItemStore = class {
|
|
50
|
+
constructor(itemData, weaverse) {
|
|
51
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
52
|
+
this.ref = {
|
|
53
|
+
current: null
|
|
54
|
+
};
|
|
55
|
+
this.stitchesClass = "";
|
|
56
|
+
this._data = { id: "", type: "" };
|
|
57
|
+
this._flags = {};
|
|
58
|
+
this.setData = (data) => {
|
|
59
|
+
this.data = Object.assign(this.data, data);
|
|
60
|
+
this.triggerUpdate();
|
|
61
|
+
return this.data;
|
|
62
|
+
};
|
|
63
|
+
this.subscribe = (fn) => {
|
|
64
|
+
this.listeners.add(fn);
|
|
65
|
+
};
|
|
66
|
+
this.unsubscribe = (fn) => {
|
|
67
|
+
this.listeners.delete(fn);
|
|
68
|
+
};
|
|
69
|
+
this.triggerUpdate = () => {
|
|
70
|
+
this.listeners.forEach((fn) => {
|
|
71
|
+
return fn(this.data);
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
let { type, id } = itemData;
|
|
75
|
+
this.weaverse = weaverse;
|
|
76
|
+
if (id && type) {
|
|
77
|
+
weaverse.itemInstances.set(id, this);
|
|
78
|
+
this.data = { ...itemData };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
get _id() {
|
|
82
|
+
return this._data.id;
|
|
83
|
+
}
|
|
84
|
+
get _element() {
|
|
85
|
+
return this.ref.current;
|
|
86
|
+
}
|
|
87
|
+
get Element() {
|
|
88
|
+
return this.weaverse.elementInstances.get(this._data.type);
|
|
89
|
+
}
|
|
90
|
+
set data(data) {
|
|
91
|
+
this._data = { ...this.data, ...data };
|
|
92
|
+
}
|
|
93
|
+
get data() {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
return { ...(_b = (_a = this.Element) == null ? void 0 : _a.Component) == null ? void 0 : _b.defaultProps, ...this._data };
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var Weaverse = class {
|
|
99
|
+
constructor({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
|
|
100
|
+
this.elementInstances = /* @__PURE__ */ new Map();
|
|
101
|
+
this.itemInstances = /* @__PURE__ */ new Map();
|
|
102
|
+
this.appUrl = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://weaverse.io";
|
|
103
|
+
this.projectKey = "";
|
|
104
|
+
this.projectData = {
|
|
105
|
+
rootId: "",
|
|
106
|
+
items: [],
|
|
107
|
+
script: { css: "", js: "" }
|
|
108
|
+
};
|
|
109
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
110
|
+
this.isDesignMode = false;
|
|
111
|
+
this.ssrMode = false;
|
|
112
|
+
this.mediaBreakPoints = {
|
|
113
|
+
desktop: "all",
|
|
114
|
+
mobile: "(max-width: 767.98px)"
|
|
115
|
+
};
|
|
116
|
+
this.initialized = false;
|
|
117
|
+
this.initializeData = (data) => {
|
|
118
|
+
if (!this.initialized) {
|
|
119
|
+
let { data: pageData, published, id, projectKey } = data;
|
|
120
|
+
this.projectKey = projectKey || this.projectKey;
|
|
121
|
+
this.projectData = { ...pageData, pageId: id };
|
|
122
|
+
this.isDesignMode = !published;
|
|
123
|
+
this.initProjectItemData();
|
|
124
|
+
if (this.isDesignMode) {
|
|
125
|
+
this.triggerUpdate();
|
|
126
|
+
this.loadStudio();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
this.initialized = true;
|
|
130
|
+
};
|
|
131
|
+
this.initStitches = () => {
|
|
132
|
+
this.stitchesInstance = this.stitchesInstance || stitches.createStitches({
|
|
133
|
+
prefix: "weaverse",
|
|
134
|
+
media: this.mediaBreakPoints,
|
|
135
|
+
utils: stichesUtils
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
this.init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode });
|
|
139
|
+
}
|
|
140
|
+
init({ appUrl, projectKey, projectData, mediaBreakPoints, isDesignMode, ssrMode } = {}) {
|
|
141
|
+
this.appUrl = appUrl || this.appUrl;
|
|
142
|
+
this.projectKey = projectKey || this.projectKey;
|
|
143
|
+
this.mediaBreakPoints = mediaBreakPoints || this.mediaBreakPoints;
|
|
144
|
+
this.isDesignMode = isDesignMode || this.isDesignMode;
|
|
145
|
+
this.ssrMode = ssrMode || this.ssrMode;
|
|
146
|
+
this.projectData = projectData || this.projectData;
|
|
147
|
+
this.initStitches();
|
|
148
|
+
this.initProjectItemData();
|
|
149
|
+
}
|
|
150
|
+
loadStudio() {
|
|
151
|
+
setTimeout(() => {
|
|
152
|
+
if (isIframe && this.isDesignMode && !this.studioBridge) {
|
|
153
|
+
const initStudio = () => {
|
|
154
|
+
this.studioBridge = new window.WeaverseStudioBridge(this);
|
|
155
|
+
this.triggerUpdate();
|
|
156
|
+
};
|
|
157
|
+
if (!window.WeaverseStudioBridge) {
|
|
158
|
+
const studioBridgeScript = document.createElement("script");
|
|
159
|
+
studioBridgeScript.src = `${this.appUrl}/assets/studio/studio-bridge.js`;
|
|
160
|
+
studioBridgeScript.type = "module";
|
|
161
|
+
studioBridgeScript.onload = initStudio;
|
|
162
|
+
document.body.appendChild(studioBridgeScript);
|
|
163
|
+
} else {
|
|
164
|
+
initStudio();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}, 2e3);
|
|
168
|
+
}
|
|
169
|
+
registerElement(element) {
|
|
170
|
+
if (element == null ? void 0 : element.type) {
|
|
171
|
+
if (this.elementInstances.has(element.type)) {
|
|
172
|
+
throw new Error(`Weaverse: Element '${element.type}' already registered`);
|
|
173
|
+
}
|
|
174
|
+
this.elementInstances.set(element == null ? void 0 : element.type, element);
|
|
175
|
+
} else {
|
|
176
|
+
throw new Error("Weaverse: registerElement: `type` is required");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
subscribe(fn) {
|
|
180
|
+
this.listeners.add(fn);
|
|
181
|
+
}
|
|
182
|
+
unsubscribe(fn) {
|
|
183
|
+
this.listeners.delete(fn);
|
|
184
|
+
}
|
|
185
|
+
triggerUpdate() {
|
|
186
|
+
this.listeners.forEach((fn) => fn());
|
|
187
|
+
}
|
|
188
|
+
static fetchProjectData({
|
|
189
|
+
fetch = globalThis.fetch,
|
|
190
|
+
appUrl,
|
|
191
|
+
projectKey
|
|
192
|
+
}) {
|
|
193
|
+
return fetch(appUrl + `/api/public/${projectKey}`).then((r) => r.json()).catch(console.error);
|
|
194
|
+
}
|
|
195
|
+
setProjectData(projectData) {
|
|
196
|
+
this.projectData = projectData;
|
|
197
|
+
this.initProjectItemData();
|
|
198
|
+
this.triggerUpdate();
|
|
199
|
+
}
|
|
200
|
+
initProjectItemData() {
|
|
201
|
+
const data = this.projectData;
|
|
202
|
+
if (data == null ? void 0 : data.items) {
|
|
203
|
+
data.items.forEach((item) => {
|
|
204
|
+
if (!this.itemInstances.get(item.id)) {
|
|
205
|
+
new WeaverseItemStore(item, this);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
Weaverse.WeaverseItemStore = WeaverseItemStore;
|
|
212
|
+
export {
|
|
213
|
+
Weaverse,
|
|
214
|
+
WeaverseItemStore,
|
|
215
|
+
isBrowser,
|
|
216
|
+
isIframe,
|
|
217
|
+
isReactNative
|
|
218
|
+
};
|
package/package.json
CHANGED