@weaverse/core 0.6.33 → 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.
@@ -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.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
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.6.33",
2
+ "version": "0.7.0",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "access": "public"
9
9
  },
10
10
  "files": [
11
- "dist/"
11
+ "dist/*"
12
12
  ],
13
13
  "engines": {
14
14
  "node": ">=10"