@weaverse/core 0.6.33 → 0.7.2

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