@simple-builder/react 1.4.0 → 1.4.1

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,244 @@
1
+ import { cn, builder, insertAt } from './chunk-QSAIF4GO.mjs';
2
+ import * as React from 'react';
3
+ import { Slot } from '@radix-ui/react-slot';
4
+ import { cva } from 'class-variance-authority';
5
+
6
+ var BuilderContext = React.createContext(
7
+ null
8
+ );
9
+ var BuilderProvider = (props) => {
10
+ const url = props.url ?? "/api/simple-builder";
11
+ const [content, setContent] = React.useState(
12
+ props.content ?? []
13
+ );
14
+ const getItem = React.useCallback(
15
+ (id) => {
16
+ return findRecursive(content, (item) => item.id === id);
17
+ },
18
+ [content]
19
+ );
20
+ const patchItem = React.useCallback(
21
+ (id, patch) => {
22
+ setContent(
23
+ (content2) => mapRecursive(content2, (item) => {
24
+ if (item.id === id) {
25
+ return {
26
+ ...item,
27
+ ...patch
28
+ };
29
+ }
30
+ return item;
31
+ })
32
+ );
33
+ },
34
+ [setContent, mapRecursive]
35
+ );
36
+ const addContent = React.useCallback(
37
+ (name, container, parent, index = 0) => {
38
+ const component = builder.getComponent(name);
39
+ if (!component) {
40
+ throw new Error(`[simple-builder]: Component "${name}" not found`);
41
+ }
42
+ const entry = {
43
+ id: crypto.randomUUID(),
44
+ component: component.name,
45
+ ...parent && { parent },
46
+ ...component.inputs && {
47
+ props: component.inputs.reduce(
48
+ (acc, input) => ({
49
+ ...acc,
50
+ [input.name]: input.defaultValue
51
+ }),
52
+ {}
53
+ )
54
+ },
55
+ ...component.defaultStyles && {
56
+ styles: component.defaultStyles
57
+ }
58
+ };
59
+ if (!parent) {
60
+ setContent((content2) => insertAt(content2, index, entry));
61
+ return;
62
+ }
63
+ setContent(
64
+ (content2) => mapRecursive(content2, (item) => {
65
+ if (item.id === parent) {
66
+ return {
67
+ ...item,
68
+ content: {
69
+ ...item.content,
70
+ [container]: insertAt(item.content?.[container], index, entry)
71
+ }
72
+ };
73
+ }
74
+ return item;
75
+ })
76
+ );
77
+ },
78
+ [setContent, mapRecursive]
79
+ );
80
+ const deleteContent = React.useCallback(
81
+ (id) => setContent((prev) => filterRecursive(prev, (item) => item.id !== id)),
82
+ [setContent, filterRecursive]
83
+ );
84
+ const bringUp = React.useCallback(
85
+ (id) => {
86
+ setContent(
87
+ (content2) => mapRecursive(content2, (item, index, arr) => {
88
+ if (item.id === id && index > 0) {
89
+ return arr[index - 1];
90
+ }
91
+ if (arr[index + 1]?.id === id) {
92
+ return arr[index + 1];
93
+ }
94
+ return item;
95
+ })
96
+ );
97
+ },
98
+ [setContent, mapRecursive]
99
+ );
100
+ const bringDown = React.useCallback(
101
+ (id) => {
102
+ setContent(
103
+ (content2) => mapRecursive(content2, (item, index, arr) => {
104
+ if (item.id === id && index < arr.length - 1) {
105
+ return arr[index + 1];
106
+ }
107
+ if (arr[index - 1]?.id === id) {
108
+ return arr[index - 1];
109
+ }
110
+ return item;
111
+ })
112
+ );
113
+ },
114
+ [setContent, mapRecursive]
115
+ );
116
+ const save = React.useCallback(async () => {
117
+ const slug = window.location.pathname;
118
+ try {
119
+ await fetch(`${url}/page/update`, {
120
+ method: "POST",
121
+ body: JSON.stringify({
122
+ slug,
123
+ content
124
+ })
125
+ });
126
+ alert("Pagina opgeslagen!");
127
+ } catch (e) {
128
+ console.error("[simple-builder]: Failed to save content", e);
129
+ alert("Er is iets fout gegaan. Bekijk de console voor meer info.");
130
+ }
131
+ }, [content]);
132
+ return /* @__PURE__ */ React.createElement(
133
+ BuilderContext.Provider,
134
+ {
135
+ value: {
136
+ url,
137
+ content,
138
+ setContent,
139
+ getItem,
140
+ patchItem,
141
+ addContent,
142
+ bringUp,
143
+ bringDown,
144
+ deleteContent,
145
+ save
146
+ }
147
+ },
148
+ props.children
149
+ );
150
+ };
151
+ var useBuilder = () => {
152
+ const context = React.useContext(BuilderContext);
153
+ if (!context) {
154
+ throw new Error(
155
+ "[simple-builder]: useBuilder must be used within a BuilderProvider"
156
+ );
157
+ }
158
+ return context;
159
+ };
160
+ var filterRecursive = (items, predicate, key = "content") => {
161
+ return items.filter(predicate).map((item) => ({
162
+ ...item,
163
+ ...item[key] && {
164
+ [key]: Object.entries(item[key] ?? {}).reduce(
165
+ (acc, [name, content]) => ({
166
+ ...acc,
167
+ [name]: filterRecursive(content, predicate, key)
168
+ }),
169
+ {}
170
+ )
171
+ }
172
+ }));
173
+ };
174
+ var mapRecursive = (items, predicate, key = "content") => {
175
+ return items.map(predicate).map((item) => ({
176
+ ...item,
177
+ ...item[key] && {
178
+ [key]: Object.entries(item[key] ?? {}).reduce(
179
+ (acc, [name, content]) => ({
180
+ ...acc,
181
+ [name]: mapRecursive(content, predicate, key)
182
+ }),
183
+ {}
184
+ )
185
+ }
186
+ }));
187
+ };
188
+ var findRecursive = (items, predicate, key = "content") => {
189
+ for (const item of items) {
190
+ if (predicate(item)) {
191
+ return item;
192
+ }
193
+ if (item[key]) {
194
+ const keys = Object.keys(item[key] ?? {});
195
+ for (const x of keys) {
196
+ const found = findRecursive(item[key][x], predicate, key);
197
+ if (found) {
198
+ return found;
199
+ }
200
+ }
201
+ }
202
+ }
203
+ };
204
+ var buttonVariants = cva(
205
+ "sb:inline-flex sb:items-center sb:justify-center sb:gap-2 sb:whitespace-nowrap sb:rounded-md sb:text-sm sb:font-medium sb:transition-colors sb:focus-visible:outline-hidden sb:focus-visible:ring-1 sb:focus-visible:ring-ring sb:disabled:pointer-events-none sb:disabled:opacity-50 sb:[&_svg]:pointer-events-none sb:[&_svg]:size-4 sb:[&_svg]:shrink-0",
206
+ {
207
+ variants: {
208
+ variant: {
209
+ default: "sb:bg-primary sb:text-primary-foreground sb:shadow sb:hover:bg-primary/90",
210
+ destructive: "sb:bg-destructive sb:text-destructive-foreground sb:shadow-sm sb:hover:bg-destructive/90",
211
+ outline: "sb:border sb:border-input sb:bg-background sb:shadow-sm sb:hover:bg-accent sb:hover:text-accent-foreground",
212
+ secondary: "sb:bg-secondary sb:text-secondary-foreground sb:shadow-sm sb:hover:bg-secondary/80",
213
+ ghost: "sb:hover:bg-accent sb:hover:text-accent-foreground",
214
+ link: "sb:text-primary sb:underline-offset-4 sb:hover:underline"
215
+ },
216
+ size: {
217
+ default: "sb:h-9 sb:px-4 sb:py-2",
218
+ sm: "sb:h-8 sb:rounded-md sb:px-3 sb:text-xs",
219
+ lg: "sb:h-10 sb:rounded-md sb:px-8",
220
+ icon: "sb:h-9 sb:w-9"
221
+ }
222
+ },
223
+ defaultVariants: {
224
+ variant: "default",
225
+ size: "default"
226
+ }
227
+ }
228
+ );
229
+ var Button = React.forwardRef(
230
+ ({ className, variant, size, asChild = false, ...props }, ref) => {
231
+ const Comp = asChild ? Slot : "button";
232
+ return /* @__PURE__ */ React.createElement(
233
+ Comp,
234
+ {
235
+ className: cn(buttonVariants({ variant, size, className })),
236
+ ref,
237
+ ...props
238
+ }
239
+ );
240
+ }
241
+ );
242
+ Button.displayName = "Button";
243
+
244
+ export { BuilderProvider, Button, buttonVariants, useBuilder };
package/dist/index.js CHANGED
@@ -1,23 +1,23 @@
1
1
  'use strict';
2
2
 
3
- var chunk7I4RGF45_js = require('./chunk-7I4RGF45.js');
4
- var chunkYFRFNRKR_js = require('./chunk-YFRFNRKR.js');
3
+ var chunkJFWIO2AR_js = require('./chunk-JFWIO2AR.js');
4
+ var chunk63LNKQZD_js = require('./chunk-63LNKQZD.js');
5
5
 
6
6
 
7
7
 
8
8
  Object.defineProperty(exports, "BuilderContent", {
9
9
  enumerable: true,
10
- get: function () { return chunk7I4RGF45_js.BuilderContent; }
10
+ get: function () { return chunkJFWIO2AR_js.BuilderContent; }
11
11
  });
12
12
  Object.defineProperty(exports, "StaticContent", {
13
13
  enumerable: true,
14
- get: function () { return chunk7I4RGF45_js.StaticContent; }
14
+ get: function () { return chunkJFWIO2AR_js.StaticContent; }
15
15
  });
16
16
  Object.defineProperty(exports, "BuildContainer", {
17
17
  enumerable: true,
18
- get: function () { return chunkYFRFNRKR_js.BuildContainer; }
18
+ get: function () { return chunk63LNKQZD_js.BuildContainer; }
19
19
  });
20
20
  Object.defineProperty(exports, "builder", {
21
21
  enumerable: true,
22
- get: function () { return chunkYFRFNRKR_js.builder; }
22
+ get: function () { return chunk63LNKQZD_js.builder; }
23
23
  });
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { BuilderContent, StaticContent } from './chunk-UA2CNH3R.mjs';
2
- export { BuildContainer, builder } from './chunk-ZQBKKI2S.mjs';
1
+ export { BuilderContent, StaticContent } from './chunk-X5MZZBYC.mjs';
2
+ export { BuildContainer, builder } from './chunk-QSAIF4GO.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simple-builder/react",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "private": false,
5
5
  "description": "React package for the simple builder package.",
6
6
  "repository": "https://github.com/pieter-berkel/simple-builder.git",
@@ -59,7 +59,7 @@
59
59
  "tailwind-merge": "^2.2.0",
60
60
  "tailwind-variants": "^0.1.20",
61
61
  "zod": "^3.22.4",
62
- "@simple-builder/server": "1.4.0"
62
+ "@simple-builder/server": "1.4.1"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "react": ">=16.8.0",