@weaverse/core 0.6.26 → 0.6.27

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