@simple-builder/react 1.0.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
+ 'use strict';
2
+
3
+ var chunkVMHWPKU4_js = require('./chunk-VMHWPKU4.js');
4
+ var React = require('react');
5
+ var clsx = require('clsx');
6
+ var tailwindMerge = require('tailwind-merge');
7
+ var reactSlot = require('@radix-ui/react-slot');
8
+ var classVarianceAuthority = require('class-variance-authority');
9
+
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
29
+
30
+ function cn(...inputs) {
31
+ return tailwindMerge.twMerge(clsx.clsx(inputs));
32
+ }
33
+ var insertAt = (items, index, item) => {
34
+ return Array.isArray(items) ? [...items.slice(0, index), item, ...items.slice(index)] : [item];
35
+ };
36
+ var spacing = {
37
+ parse: (input) => {
38
+ const matches = input.match(/(\d+)/gm);
39
+ const [top = 0, right = 0, bottom = 0, left = 0] = matches?.map((match) => +match) || [];
40
+ switch (matches?.length) {
41
+ case 1:
42
+ return { top, right: top, bottom: top, left: top };
43
+ case 2:
44
+ return { top, bottom: top, left: right, right };
45
+ case 3:
46
+ return { top, right, left: right, bottom };
47
+ default:
48
+ return { top, right, bottom, left };
49
+ }
50
+ },
51
+ isIndipendent: (input) => {
52
+ const { top, right, bottom, left } = spacing.parse(input);
53
+ return top !== bottom || right !== left;
54
+ }
55
+ };
56
+
57
+ // src/components/context/builder-context.tsx
58
+ var BuilderContext = React__namespace.createContext(
59
+ null
60
+ );
61
+ var BuilderProvider = (props) => {
62
+ const url = props.url ?? "/api/simple-builder";
63
+ const [content, setContent] = React__namespace.useState(
64
+ props.content ?? []
65
+ );
66
+ const getItem = React__namespace.useCallback(
67
+ (id) => {
68
+ const res = findRecursive(content, (item) => item.id === id);
69
+ console.log("RES", res);
70
+ return res;
71
+ },
72
+ [content]
73
+ );
74
+ const patchItem = React__namespace.useCallback(
75
+ (id, patch) => {
76
+ setContent(
77
+ (content2) => mapRecursive(content2, (item) => {
78
+ if (item.id === id) {
79
+ return {
80
+ ...item,
81
+ ...patch
82
+ };
83
+ }
84
+ return item;
85
+ })
86
+ );
87
+ },
88
+ [setContent, mapRecursive]
89
+ );
90
+ const addContent = React__namespace.useCallback(
91
+ (name, container, parent, index = 0) => {
92
+ const component = chunkVMHWPKU4_js.builder.getComponent(name);
93
+ if (!component) {
94
+ throw new Error(`[simple-builder]: Component "${name}" not found`);
95
+ }
96
+ const entry = {
97
+ id: crypto.randomUUID(),
98
+ component: component.name,
99
+ ...parent && { parent },
100
+ ...component.inputs && {
101
+ props: component.inputs.reduce(
102
+ (acc, input) => ({
103
+ ...acc,
104
+ [input.name]: input.defaultValue
105
+ }),
106
+ {}
107
+ )
108
+ },
109
+ ...component.defaultStyles && {
110
+ styles: component.defaultStyles
111
+ }
112
+ };
113
+ if (!parent) {
114
+ setContent((content2) => insertAt(content2, index, entry));
115
+ return;
116
+ }
117
+ setContent(
118
+ (content2) => mapRecursive(content2, (item) => {
119
+ if (item.id === parent) {
120
+ return {
121
+ ...item,
122
+ content: {
123
+ ...item.content,
124
+ [container]: insertAt(item.content?.[container], index, entry)
125
+ }
126
+ };
127
+ }
128
+ return item;
129
+ })
130
+ );
131
+ },
132
+ [setContent, mapRecursive]
133
+ );
134
+ const deleteContent = React__namespace.useCallback(
135
+ (id) => setContent((prev) => filterRecursive(prev, (item) => item.id !== id)),
136
+ [setContent, filterRecursive]
137
+ );
138
+ const bringUp = React__namespace.useCallback(
139
+ (id) => {
140
+ setContent(
141
+ (content2) => mapRecursive(content2, (item, index, arr) => {
142
+ if (item.id === id && index > 0) {
143
+ return arr[index - 1];
144
+ }
145
+ if (arr[index + 1]?.id === id) {
146
+ return arr[index + 1];
147
+ }
148
+ return item;
149
+ })
150
+ );
151
+ },
152
+ [setContent, mapRecursive]
153
+ );
154
+ const bringDown = React__namespace.useCallback(
155
+ (id) => {
156
+ setContent(
157
+ (content2) => mapRecursive(content2, (item, index, arr) => {
158
+ if (item.id === id && index < arr.length - 1) {
159
+ return arr[index + 1];
160
+ }
161
+ if (arr[index - 1]?.id === id) {
162
+ return arr[index - 1];
163
+ }
164
+ return item;
165
+ })
166
+ );
167
+ },
168
+ [setContent, mapRecursive]
169
+ );
170
+ const save = React__namespace.useCallback(async () => {
171
+ const slug = window.location.pathname;
172
+ await fetch(`${url}/page/update`, {
173
+ method: "POST",
174
+ body: JSON.stringify({
175
+ slug,
176
+ content
177
+ })
178
+ });
179
+ }, [content]);
180
+ return /* @__PURE__ */ React__namespace.createElement(
181
+ BuilderContext.Provider,
182
+ {
183
+ value: {
184
+ url,
185
+ content,
186
+ setContent,
187
+ getItem,
188
+ patchItem,
189
+ addContent,
190
+ bringUp,
191
+ bringDown,
192
+ deleteContent,
193
+ save
194
+ }
195
+ },
196
+ props.children
197
+ );
198
+ };
199
+ var useBuilder = () => {
200
+ const context = React__namespace.useContext(BuilderContext);
201
+ if (!context) {
202
+ throw new Error(
203
+ "[simple-builder]: useBuilder must be used within a BuilderProvider"
204
+ );
205
+ }
206
+ return context;
207
+ };
208
+ var filterRecursive = (items, predicate, key = "content") => {
209
+ return items.filter(predicate).map((item) => ({
210
+ ...item,
211
+ ...item[key] && {
212
+ [key]: Object.entries(item[key] ?? {}).reduce(
213
+ (acc, [name, content]) => ({
214
+ ...acc,
215
+ [name]: filterRecursive(content, predicate, key)
216
+ }),
217
+ {}
218
+ )
219
+ }
220
+ }));
221
+ };
222
+ var mapRecursive = (items, predicate, key = "content") => {
223
+ return items.map(predicate).map((item) => ({
224
+ ...item,
225
+ ...item[key] && {
226
+ [key]: Object.entries(item[key] ?? {}).reduce(
227
+ (acc, [name, content]) => ({
228
+ ...acc,
229
+ [name]: mapRecursive(content, predicate, key)
230
+ }),
231
+ {}
232
+ )
233
+ }
234
+ }));
235
+ };
236
+ var findRecursive = (items, predicate, key = "content") => {
237
+ for (const item of items) {
238
+ if (predicate(item)) {
239
+ return item;
240
+ }
241
+ if (item[key]) {
242
+ const keys = Object.keys(item[key] ?? {});
243
+ for (const x of keys) {
244
+ const found = findRecursive(item[key][x], predicate, key);
245
+ if (found) {
246
+ return found;
247
+ }
248
+ }
249
+ }
250
+ }
251
+ };
252
+ var buttonVariants = classVarianceAuthority.cva(
253
+ "inline-flex uppercase items-center justify-center whitespace-nowrap rounded-md text-xs font-semibold ring-offset-sb-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sb-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
254
+ {
255
+ variants: {
256
+ variant: {
257
+ primary: "bg-sb-primary text-sb-primary-foreground hover:bg-sb-primary/90",
258
+ destructive: "bg-sb-destructive text-sb-destructive-foreground hover:bg-sb-destructive/90",
259
+ outline: "border border-sb-input bg-sb-background hover:bg-sb-accent hover:text-sb-accent-foreground",
260
+ ghost: "hover:bg-sb-accent hover:text-sb-accent-foreground"
261
+ },
262
+ size: {
263
+ default: "h-10 px-4 py-2",
264
+ sm: "h-8 px-3 py-1"
265
+ }
266
+ },
267
+ defaultVariants: {
268
+ variant: "primary",
269
+ size: "default"
270
+ }
271
+ }
272
+ );
273
+ var Button = React__namespace.forwardRef(
274
+ ({ className, variant, size, asChild = false, ...props }, ref) => {
275
+ const Comp = asChild ? reactSlot.Slot : "button";
276
+ return /* @__PURE__ */ React__namespace.createElement(
277
+ Comp,
278
+ {
279
+ className: cn(buttonVariants({ variant, size, className })),
280
+ ref,
281
+ ...props
282
+ }
283
+ );
284
+ }
285
+ );
286
+ Button.displayName = "Button";
287
+
288
+ exports.BuilderProvider = BuilderProvider;
289
+ exports.Button = Button;
290
+ exports.buttonVariants = buttonVariants;
291
+ exports.cn = cn;
292
+ exports.spacing = spacing;
293
+ exports.useBuilder = useBuilder;
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ // src/lib/builder.ts
6
+ var builder = /* @__PURE__ */ (() => {
7
+ const components = [];
8
+ return {
9
+ register: (component, config) => {
10
+ if (components.find(({ name }) => name === config.name)) {
11
+ console.log(
12
+ `[simple-builder]: Component ${config.name} already registered.`
13
+ );
14
+ return;
15
+ }
16
+ components.push({
17
+ component,
18
+ ...config
19
+ });
20
+ },
21
+ getComponents: () => components,
22
+ getComponent: (name) => {
23
+ return components.find((c) => c.name === name);
24
+ },
25
+ bindComponent: ({ content, ...block }, edit) => {
26
+ const component = components.find(({ name }) => name === block.component);
27
+ if (!component) {
28
+ throw new Error(
29
+ `[simple-builder]: Component ${block.component} not found`
30
+ );
31
+ }
32
+ const Element = react.createElement(component.component, {
33
+ key: block.id,
34
+ builder: { id: block.id, content, edit },
35
+ ...block.props
36
+ });
37
+ return Element;
38
+ }
39
+ };
40
+ })();
41
+
42
+ exports.builder = builder;