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