@evanschleret/formforgeclient 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.
- package/LICENSE +21 -0
- package/README.md +94 -0
- package/dist/module.cjs +112 -0
- package/dist/module.d.cts +20 -0
- package/dist/module.d.mts +20 -0
- package/dist/module.d.ts +20 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +109 -0
- package/dist/runtime/api/categories.d.ts +9 -0
- package/dist/runtime/api/categories.js +83 -0
- package/dist/runtime/api/client.d.ts +45 -0
- package/dist/runtime/api/client.js +148 -0
- package/dist/runtime/api/drafts.d.ts +6 -0
- package/dist/runtime/api/drafts.js +77 -0
- package/dist/runtime/api/http.d.ts +3 -0
- package/dist/runtime/api/http.js +138 -0
- package/dist/runtime/api/index.d.ts +9 -0
- package/dist/runtime/api/index.js +11 -0
- package/dist/runtime/api/management.d.ts +19 -0
- package/dist/runtime/api/management.js +180 -0
- package/dist/runtime/api/request.d.ts +8 -0
- package/dist/runtime/api/request.js +52 -0
- package/dist/runtime/api/responses.d.ts +6 -0
- package/dist/runtime/api/responses.js +61 -0
- package/dist/runtime/api/schema.d.ts +7 -0
- package/dist/runtime/api/schema.js +56 -0
- package/dist/runtime/api/submission.d.ts +11 -0
- package/dist/runtime/api/submission.js +47 -0
- package/dist/runtime/api/upload.d.ts +8 -0
- package/dist/runtime/api/upload.js +37 -0
- package/dist/runtime/composables/index.d.ts +31 -0
- package/dist/runtime/composables/index.js +16 -0
- package/dist/runtime/composables/useFormForgeApi.d.ts +3 -0
- package/dist/runtime/composables/useFormForgeApi.js +4 -0
- package/dist/runtime/composables/useFormForgeBuilder.d.ts +57 -0
- package/dist/runtime/composables/useFormForgeBuilder.js +515 -0
- package/dist/runtime/composables/useFormForgeCategory.d.ts +61 -0
- package/dist/runtime/composables/useFormForgeCategory.js +248 -0
- package/dist/runtime/composables/useFormForgeClient.d.ts +3 -0
- package/dist/runtime/composables/useFormForgeClient.js +200 -0
- package/dist/runtime/composables/useFormForgeDrafts.d.ts +20 -0
- package/dist/runtime/composables/useFormForgeDrafts.js +78 -0
- package/dist/runtime/composables/useFormForgeForm.d.ts +26 -0
- package/dist/runtime/composables/useFormForgeForm.js +114 -0
- package/dist/runtime/composables/useFormForgeGetForm.d.ts +22 -0
- package/dist/runtime/composables/useFormForgeGetForm.js +36 -0
- package/dist/runtime/composables/useFormForgeI18n.d.ts +250 -0
- package/dist/runtime/composables/useFormForgeI18n.js +324 -0
- package/dist/runtime/composables/useFormForgeManagement.d.ts +40 -0
- package/dist/runtime/composables/useFormForgeManagement.js +153 -0
- package/dist/runtime/composables/useFormForgeResolver.d.ts +28 -0
- package/dist/runtime/composables/useFormForgeResolver.js +88 -0
- package/dist/runtime/composables/useFormForgeResponses.d.ts +45 -0
- package/dist/runtime/composables/useFormForgeResponses.js +206 -0
- package/dist/runtime/composables/useFormForgeSchema.d.ts +24 -0
- package/dist/runtime/composables/useFormForgeSchema.js +69 -0
- package/dist/runtime/composables/useFormForgeSubmission.d.ts +12 -0
- package/dist/runtime/composables/useFormForgeSubmission.js +4 -0
- package/dist/runtime/composables/useFormForgeSubmit.d.ts +29 -0
- package/dist/runtime/composables/useFormForgeSubmit.js +291 -0
- package/dist/runtime/composables/useFormForgeUploads.d.ts +21 -0
- package/dist/runtime/composables/useFormForgeUploads.js +37 -0
- package/dist/runtime/composables/useFormForgeWizard.d.ts +20 -0
- package/dist/runtime/composables/useFormForgeWizard.js +83 -0
- package/dist/runtime/index.d.ts +11 -0
- package/dist/runtime/index.js +14 -0
- package/dist/runtime/plugin.d.ts +3 -0
- package/dist/runtime/plugin.js +175 -0
- package/dist/runtime/renderers/default/FormForgeBuilder.d.vue.ts +40 -0
- package/dist/runtime/renderers/default/FormForgeBuilder.vue +1159 -0
- package/dist/runtime/renderers/default/FormForgeBuilder.vue.d.ts +40 -0
- package/dist/runtime/renderers/default/FormForgeCategoryCreateModal.d.vue.ts +16 -0
- package/dist/runtime/renderers/default/FormForgeCategoryCreateModal.vue +129 -0
- package/dist/runtime/renderers/default/FormForgeCategoryCreateModal.vue.d.ts +16 -0
- package/dist/runtime/renderers/default/FormForgeRenderer.d.vue.ts +72 -0
- package/dist/runtime/renderers/default/FormForgeRenderer.vue +1188 -0
- package/dist/runtime/renderers/default/FormForgeRenderer.vue.d.ts +72 -0
- package/dist/runtime/renderers/default/FormForgeResponse.d.vue.ts +18 -0
- package/dist/runtime/renderers/default/FormForgeResponse.vue +744 -0
- package/dist/runtime/renderers/default/FormForgeResponse.vue.d.ts +18 -0
- package/dist/runtime/renderers/default/index.d.ts +5 -0
- package/dist/runtime/renderers/default/index.js +4 -0
- package/dist/runtime/renderers/index.d.ts +2 -0
- package/dist/runtime/renderers/index.js +1 -0
- package/dist/runtime/types/api.d.ts +129 -0
- package/dist/runtime/types/api.js +0 -0
- package/dist/runtime/types/category.d.ts +42 -0
- package/dist/runtime/types/category.js +0 -0
- package/dist/runtime/types/errors.d.ts +16 -0
- package/dist/runtime/types/errors.js +0 -0
- package/dist/runtime/types/index.d.ts +8 -0
- package/dist/runtime/types/index.js +0 -0
- package/dist/runtime/types/json.d.ts +6 -0
- package/dist/runtime/types/json.js +0 -0
- package/dist/runtime/types/management.d.ts +46 -0
- package/dist/runtime/types/management.js +0 -0
- package/dist/runtime/types/nuxt.d.ts +13 -0
- package/dist/runtime/types/nuxt.js +1 -0
- package/dist/runtime/types/schema.d.ts +93 -0
- package/dist/runtime/types/schema.js +0 -0
- package/dist/runtime/utils/category.d.ts +5 -0
- package/dist/runtime/utils/category.js +101 -0
- package/dist/runtime/utils/form-data.d.ts +8 -0
- package/dist/runtime/utils/form-data.js +64 -0
- package/dist/runtime/utils/object.d.ts +8 -0
- package/dist/runtime/utils/object.js +43 -0
- package/dist/runtime/utils/schema.d.ts +3 -0
- package/dist/runtime/utils/schema.js +309 -0
- package/dist/runtime/utils/submission.d.ts +4 -0
- package/dist/runtime/utils/submission.js +45 -0
- package/dist/runtime/validation/errors.d.ts +5 -0
- package/dist/runtime/validation/errors.js +130 -0
- package/dist/runtime/validation/zod.d.ts +6 -0
- package/dist/runtime/validation/zod.js +203 -0
- package/dist/runtime.cjs +16 -0
- package/dist/runtime.d.cts +1 -0
- package/dist/runtime.d.mts +1 -0
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.mjs +1 -0
- package/dist/types.d.mts +3 -0
- package/package.json +60 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import { computed, ref, watch } from "#imports";
|
|
2
|
+
import { useFormForgeClient } from "./useFormForgeClient.js";
|
|
3
|
+
export const FORM_FORGE_BUILDER_FIELD_TYPES = [
|
|
4
|
+
"text",
|
|
5
|
+
"textarea",
|
|
6
|
+
"email",
|
|
7
|
+
"number",
|
|
8
|
+
"select",
|
|
9
|
+
"select_menu",
|
|
10
|
+
"radio",
|
|
11
|
+
"checkbox",
|
|
12
|
+
"checkbox_group",
|
|
13
|
+
"switch",
|
|
14
|
+
"date",
|
|
15
|
+
"time",
|
|
16
|
+
"datetime",
|
|
17
|
+
"date_range",
|
|
18
|
+
"datetime_range",
|
|
19
|
+
"file"
|
|
20
|
+
];
|
|
21
|
+
export const FORM_FORGE_BUILDER_CONDITION_TARGET_TYPES = ["page", "field"];
|
|
22
|
+
export const FORM_FORGE_BUILDER_CONDITION_ACTIONS = ["show", "hide", "skip", "require", "disable"];
|
|
23
|
+
export const FORM_FORGE_BUILDER_CONDITION_MATCHES = ["all", "any"];
|
|
24
|
+
export const FORM_FORGE_BUILDER_CONDITION_OPERATORS = [
|
|
25
|
+
"eq",
|
|
26
|
+
"neq",
|
|
27
|
+
"in",
|
|
28
|
+
"not_in",
|
|
29
|
+
"gt",
|
|
30
|
+
"gte",
|
|
31
|
+
"lt",
|
|
32
|
+
"lte",
|
|
33
|
+
"contains",
|
|
34
|
+
"not_contains",
|
|
35
|
+
"is_empty",
|
|
36
|
+
"not_empty"
|
|
37
|
+
];
|
|
38
|
+
function shortKey(prefix) {
|
|
39
|
+
return `${prefix}_${Math.random().toString(36).slice(2, 8)}`;
|
|
40
|
+
}
|
|
41
|
+
function fieldLabelFromType(type) {
|
|
42
|
+
if (type === "checkbox_group") {
|
|
43
|
+
return "Checkbox Group";
|
|
44
|
+
}
|
|
45
|
+
if (type === "select_menu") {
|
|
46
|
+
return "Select Menu";
|
|
47
|
+
}
|
|
48
|
+
if (type === "date_range") {
|
|
49
|
+
return "Date Range";
|
|
50
|
+
}
|
|
51
|
+
if (type === "datetime_range") {
|
|
52
|
+
return "Datetime Range";
|
|
53
|
+
}
|
|
54
|
+
return type.charAt(0).toUpperCase() + type.slice(1);
|
|
55
|
+
}
|
|
56
|
+
function createField(type, pageKey) {
|
|
57
|
+
const fieldKey = shortKey("fk");
|
|
58
|
+
const name = fieldKey.replace("fk_", "");
|
|
59
|
+
const baseField = {
|
|
60
|
+
field_key: fieldKey,
|
|
61
|
+
type,
|
|
62
|
+
name,
|
|
63
|
+
page_key: pageKey,
|
|
64
|
+
label: fieldLabelFromType(type),
|
|
65
|
+
required: false,
|
|
66
|
+
nullable: false,
|
|
67
|
+
disabled: false,
|
|
68
|
+
readonly: false,
|
|
69
|
+
default: null,
|
|
70
|
+
rules: [],
|
|
71
|
+
meta: {},
|
|
72
|
+
options: type === "select" || type === "select_menu" || type === "radio" || type === "checkbox_group" ? [] : void 0
|
|
73
|
+
};
|
|
74
|
+
if (type === "file") {
|
|
75
|
+
return {
|
|
76
|
+
...baseField,
|
|
77
|
+
type: "file",
|
|
78
|
+
multiple: false,
|
|
79
|
+
accept: []
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return baseField;
|
|
83
|
+
}
|
|
84
|
+
function createPage() {
|
|
85
|
+
const pageKey = shortKey("pg");
|
|
86
|
+
return {
|
|
87
|
+
page_key: pageKey,
|
|
88
|
+
title: "Page",
|
|
89
|
+
description: null,
|
|
90
|
+
meta: {},
|
|
91
|
+
fields: [createField("text", pageKey)]
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function createCondition() {
|
|
95
|
+
return {
|
|
96
|
+
condition_key: shortKey("cd"),
|
|
97
|
+
target_type: "field",
|
|
98
|
+
target_key: "",
|
|
99
|
+
action: "show",
|
|
100
|
+
match: "all",
|
|
101
|
+
when: [
|
|
102
|
+
{
|
|
103
|
+
field_key: "",
|
|
104
|
+
operator: "eq",
|
|
105
|
+
value: null
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function cloneField(field) {
|
|
111
|
+
return {
|
|
112
|
+
...field,
|
|
113
|
+
field_key: shortKey("fk"),
|
|
114
|
+
name: shortKey("field")
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function isUuidLike(value) {
|
|
118
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
|
|
119
|
+
}
|
|
120
|
+
function extractFormUuid(payload) {
|
|
121
|
+
if (typeof payload.uuid === "string" && payload.uuid !== "") {
|
|
122
|
+
return payload.uuid;
|
|
123
|
+
}
|
|
124
|
+
if (typeof payload.form_uuid === "string" && payload.form_uuid !== "") {
|
|
125
|
+
return payload.form_uuid;
|
|
126
|
+
}
|
|
127
|
+
if (typeof payload.id === "string" && payload.id !== "") {
|
|
128
|
+
return payload.id;
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
function extractFormKey(payload) {
|
|
133
|
+
if (typeof payload.key === "string" && payload.key !== "") {
|
|
134
|
+
return payload.key;
|
|
135
|
+
}
|
|
136
|
+
if (typeof payload.form_key === "string" && payload.form_key !== "") {
|
|
137
|
+
return payload.form_key;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function resolveMutationIdentifier(draft) {
|
|
142
|
+
if (typeof draft.uuid === "string" && draft.uuid !== "") {
|
|
143
|
+
return draft.uuid;
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
function sanitizeDraftShape(value) {
|
|
148
|
+
if (typeof value.uuid !== "string" || value.uuid === "") {
|
|
149
|
+
value.uuid = null;
|
|
150
|
+
}
|
|
151
|
+
if (typeof value.key !== "string" || value.key === "") {
|
|
152
|
+
value.key = null;
|
|
153
|
+
}
|
|
154
|
+
if (value.uuid === null && typeof value.key === "string" && isUuidLike(value.key)) {
|
|
155
|
+
value.uuid = value.key;
|
|
156
|
+
}
|
|
157
|
+
const pages = (Array.isArray(value.pages) ? value.pages : []).filter((page) => page !== void 0 && page !== null);
|
|
158
|
+
if (pages.length === 0) {
|
|
159
|
+
pages.push(createPage());
|
|
160
|
+
}
|
|
161
|
+
value.pages = pages;
|
|
162
|
+
for (const page of value.pages) {
|
|
163
|
+
if (typeof page.page_key !== "string" || page.page_key === "") {
|
|
164
|
+
page.page_key = shortKey("pg");
|
|
165
|
+
}
|
|
166
|
+
if (typeof page.title !== "string") {
|
|
167
|
+
page.title = "Page";
|
|
168
|
+
}
|
|
169
|
+
const fields = (Array.isArray(page.fields) ? page.fields : []).filter((field) => field !== void 0 && field !== null);
|
|
170
|
+
if (fields.length === 0) {
|
|
171
|
+
fields.push(createField("text", page.page_key));
|
|
172
|
+
}
|
|
173
|
+
page.fields = fields;
|
|
174
|
+
for (const field of page.fields) {
|
|
175
|
+
field.page_key = page.page_key;
|
|
176
|
+
field.nullable = false;
|
|
177
|
+
field.disabled = false;
|
|
178
|
+
field.readonly = false;
|
|
179
|
+
if (typeof field.field_key !== "string" || field.field_key === "") {
|
|
180
|
+
field.field_key = shortKey("fk");
|
|
181
|
+
}
|
|
182
|
+
if (typeof field.name !== "string" || field.name === "") {
|
|
183
|
+
field.name = shortKey("field");
|
|
184
|
+
}
|
|
185
|
+
if (typeof field.rules !== "object" || !Array.isArray(field.rules)) {
|
|
186
|
+
field.rules = [];
|
|
187
|
+
}
|
|
188
|
+
if (typeof field.meta !== "object" || field.meta === null || Array.isArray(field.meta)) {
|
|
189
|
+
field.meta = {};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export function useFormForgeBuilder(options = {}) {
|
|
195
|
+
const client = options.client ?? useFormForgeClient(options.clientConfig);
|
|
196
|
+
const autosaveEnabled = ref(options.autosave ?? true);
|
|
197
|
+
const autosaveDelay = ref(options.autosaveDelay ?? 5e3);
|
|
198
|
+
const loading = ref(false);
|
|
199
|
+
const saving = ref(false);
|
|
200
|
+
const publishing = ref(false);
|
|
201
|
+
const error = ref(null);
|
|
202
|
+
const lastSavedAt = ref(null);
|
|
203
|
+
const defaultPage = createPage();
|
|
204
|
+
const keyFromOptions = options.formKey ?? options.initial?.key ?? null;
|
|
205
|
+
const uuidFromOptions = options.formUuid ?? options.initial?.uuid ?? null;
|
|
206
|
+
const initialDraft = {
|
|
207
|
+
uuid: uuidFromOptions ?? (typeof keyFromOptions === "string" && isUuidLike(keyFromOptions) ? keyFromOptions : null),
|
|
208
|
+
key: typeof keyFromOptions === "string" && !isUuidLike(keyFromOptions) ? keyFromOptions : options.initial?.key ?? null,
|
|
209
|
+
title: options.initial?.title ?? "",
|
|
210
|
+
category: options.initial?.category ?? null,
|
|
211
|
+
pages: options.initial?.pages ?? [defaultPage],
|
|
212
|
+
conditions: options.initial?.conditions ?? [],
|
|
213
|
+
drafts: options.initial?.drafts ?? {
|
|
214
|
+
enabled: true
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
sanitizeDraftShape(initialDraft);
|
|
218
|
+
const draft = ref(initialDraft);
|
|
219
|
+
const publishable = computed(() => {
|
|
220
|
+
const pages = draft.value.pages;
|
|
221
|
+
if (draft.value.title.trim() === "") {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (pages.length === 0) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
return pages.some((page) => page.fields.length > 0);
|
|
228
|
+
});
|
|
229
|
+
function normalizeFieldLocations() {
|
|
230
|
+
const pages = draft.value.pages;
|
|
231
|
+
for (const page of pages) {
|
|
232
|
+
for (const field of page.fields) {
|
|
233
|
+
field.page_key = page.page_key;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function toManagementInput() {
|
|
238
|
+
normalizeFieldLocations();
|
|
239
|
+
const pages = draft.value.pages;
|
|
240
|
+
const fields = [];
|
|
241
|
+
for (const page of pages) {
|
|
242
|
+
for (const field of page.fields) {
|
|
243
|
+
fields.push(field);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const input = {
|
|
247
|
+
title: draft.value.title,
|
|
248
|
+
category: draft.value.category,
|
|
249
|
+
pages,
|
|
250
|
+
fields,
|
|
251
|
+
conditions: draft.value.conditions,
|
|
252
|
+
drafts: draft.value.drafts
|
|
253
|
+
};
|
|
254
|
+
return input;
|
|
255
|
+
}
|
|
256
|
+
async function save(idempotencyKey) {
|
|
257
|
+
if (draft.value.title.trim() === "") {
|
|
258
|
+
throw new Error("Title is required to save");
|
|
259
|
+
}
|
|
260
|
+
saving.value = true;
|
|
261
|
+
error.value = null;
|
|
262
|
+
try {
|
|
263
|
+
const input = toManagementInput();
|
|
264
|
+
const mutationIdentifier = resolveMutationIdentifier(draft.value);
|
|
265
|
+
if (mutationIdentifier === null) {
|
|
266
|
+
const hasExistingSlug = typeof draft.value.key === "string" && draft.value.key !== "";
|
|
267
|
+
if (hasExistingSlug) {
|
|
268
|
+
throw new Error("Form uuid is required for update");
|
|
269
|
+
}
|
|
270
|
+
const created = await client.createForm(input, {
|
|
271
|
+
idempotencyKey,
|
|
272
|
+
endpoint: options.endpoint,
|
|
273
|
+
scope: options.scope
|
|
274
|
+
});
|
|
275
|
+
const nextUuid = extractFormUuid(created);
|
|
276
|
+
const nextKey = extractFormKey(created);
|
|
277
|
+
if (nextUuid !== null) {
|
|
278
|
+
draft.value.uuid = nextUuid;
|
|
279
|
+
}
|
|
280
|
+
if (nextKey !== null) {
|
|
281
|
+
draft.value.key = nextKey;
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
const patchInput = input;
|
|
285
|
+
const patched = await client.patchForm(mutationIdentifier, patchInput, {
|
|
286
|
+
idempotencyKey,
|
|
287
|
+
endpoint: options.endpoint,
|
|
288
|
+
scope: options.scope
|
|
289
|
+
});
|
|
290
|
+
const nextUuid = extractFormUuid(patched);
|
|
291
|
+
const nextKey = extractFormKey(patched);
|
|
292
|
+
if (nextUuid !== null) {
|
|
293
|
+
draft.value.uuid = nextUuid;
|
|
294
|
+
}
|
|
295
|
+
if (nextKey !== null) {
|
|
296
|
+
draft.value.key = nextKey;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
lastSavedAt.value = (/* @__PURE__ */ new Date()).toISOString();
|
|
300
|
+
} catch (caughtError) {
|
|
301
|
+
error.value = caughtError instanceof Error ? caughtError.message : "Failed to save form";
|
|
302
|
+
throw caughtError;
|
|
303
|
+
} finally {
|
|
304
|
+
saving.value = false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async function publish(idempotencyKey) {
|
|
308
|
+
if (!publishable.value) {
|
|
309
|
+
throw new Error("Form is not publishable");
|
|
310
|
+
}
|
|
311
|
+
const mutationIdentifier = resolveMutationIdentifier(draft.value);
|
|
312
|
+
if (mutationIdentifier === null) {
|
|
313
|
+
throw new Error("Form uuid is missing");
|
|
314
|
+
}
|
|
315
|
+
publishing.value = true;
|
|
316
|
+
error.value = null;
|
|
317
|
+
try {
|
|
318
|
+
await client.publishForm(mutationIdentifier, {
|
|
319
|
+
idempotencyKey,
|
|
320
|
+
endpoint: options.endpoint,
|
|
321
|
+
scope: options.scope
|
|
322
|
+
});
|
|
323
|
+
} catch (caughtError) {
|
|
324
|
+
error.value = caughtError instanceof Error ? caughtError.message : "Failed to publish form";
|
|
325
|
+
throw caughtError;
|
|
326
|
+
} finally {
|
|
327
|
+
publishing.value = false;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async function unpublish(idempotencyKey) {
|
|
331
|
+
const mutationIdentifier = resolveMutationIdentifier(draft.value);
|
|
332
|
+
if (mutationIdentifier === null) {
|
|
333
|
+
throw new Error("Form uuid is missing");
|
|
334
|
+
}
|
|
335
|
+
publishing.value = true;
|
|
336
|
+
error.value = null;
|
|
337
|
+
try {
|
|
338
|
+
await client.unpublishForm(mutationIdentifier, {
|
|
339
|
+
idempotencyKey,
|
|
340
|
+
endpoint: options.endpoint,
|
|
341
|
+
scope: options.scope
|
|
342
|
+
});
|
|
343
|
+
} catch (caughtError) {
|
|
344
|
+
error.value = caughtError instanceof Error ? caughtError.message : "Failed to unpublish form";
|
|
345
|
+
throw caughtError;
|
|
346
|
+
} finally {
|
|
347
|
+
publishing.value = false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function addPage() {
|
|
351
|
+
const pages = draft.value.pages;
|
|
352
|
+
pages.push(createPage());
|
|
353
|
+
}
|
|
354
|
+
function removePage(pageKey) {
|
|
355
|
+
const pages = draft.value.pages;
|
|
356
|
+
if (pages.length <= 1) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const pageIndex = pages.findIndex((page) => page.page_key === pageKey);
|
|
360
|
+
if (pageIndex < 0) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
pages.splice(pageIndex, 1);
|
|
364
|
+
}
|
|
365
|
+
function mergePageWithPrevious(pageKey) {
|
|
366
|
+
const pages = draft.value.pages;
|
|
367
|
+
if (pages.length <= 1) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const pageIndex = pages.findIndex((page) => page.page_key === pageKey);
|
|
371
|
+
if (pageIndex <= 0) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const previousPage = pages[pageIndex - 1];
|
|
375
|
+
const currentPage = pages[pageIndex];
|
|
376
|
+
previousPage.fields.push(...currentPage.fields.map((field) => ({
|
|
377
|
+
...field,
|
|
378
|
+
page_key: previousPage.page_key
|
|
379
|
+
})));
|
|
380
|
+
pages.splice(pageIndex, 1);
|
|
381
|
+
}
|
|
382
|
+
function addField(pageKey, type) {
|
|
383
|
+
const pages = draft.value.pages;
|
|
384
|
+
const page = pages.find((item) => item.page_key === pageKey);
|
|
385
|
+
if (page === void 0) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
page.fields.push(createField(type, pageKey));
|
|
389
|
+
}
|
|
390
|
+
function duplicateField(pageKey, fieldKey) {
|
|
391
|
+
const pages = draft.value.pages;
|
|
392
|
+
const page = pages.find((item) => item.page_key === pageKey);
|
|
393
|
+
if (page === void 0) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const field = page.fields.find((item) => item.field_key === fieldKey);
|
|
397
|
+
if (field === void 0) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
const clonedField = cloneField(field);
|
|
401
|
+
clonedField.page_key = page.page_key;
|
|
402
|
+
page.fields.push(clonedField);
|
|
403
|
+
}
|
|
404
|
+
function removeField(pageKey, fieldKey) {
|
|
405
|
+
const pages = draft.value.pages;
|
|
406
|
+
const page = pages.find((item) => item.page_key === pageKey);
|
|
407
|
+
if (page === void 0) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (page.fields.length <= 1) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const fieldIndex = page.fields.findIndex((field) => field.field_key === fieldKey);
|
|
414
|
+
if (fieldIndex < 0) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
page.fields.splice(fieldIndex, 1);
|
|
418
|
+
}
|
|
419
|
+
function addCondition() {
|
|
420
|
+
const conditions = draft.value.conditions;
|
|
421
|
+
conditions.push(createCondition());
|
|
422
|
+
}
|
|
423
|
+
function removeCondition(conditionKey) {
|
|
424
|
+
const conditions = draft.value.conditions;
|
|
425
|
+
const conditionIndex = conditions.findIndex((condition) => condition.condition_key === conditionKey);
|
|
426
|
+
if (conditionIndex < 0) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
conditions.splice(conditionIndex, 1);
|
|
430
|
+
}
|
|
431
|
+
function addConditionClause(conditionKey) {
|
|
432
|
+
const conditions = draft.value.conditions;
|
|
433
|
+
const condition = conditions.find((item) => item.condition_key === conditionKey);
|
|
434
|
+
if (condition === void 0) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
condition.when.push({
|
|
438
|
+
field_key: "",
|
|
439
|
+
operator: "eq",
|
|
440
|
+
value: null
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
function removeConditionClause(conditionKey, index) {
|
|
444
|
+
const conditions = draft.value.conditions;
|
|
445
|
+
const condition = conditions.find((item) => item.condition_key === conditionKey);
|
|
446
|
+
if (condition === void 0) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (condition.when.length <= 1) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
condition.when.splice(index, 1);
|
|
453
|
+
}
|
|
454
|
+
let autosaveTimer;
|
|
455
|
+
let hasInitializedWatch = false;
|
|
456
|
+
function clearAutosave() {
|
|
457
|
+
if (autosaveTimer !== void 0) {
|
|
458
|
+
clearTimeout(autosaveTimer);
|
|
459
|
+
autosaveTimer = void 0;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
function scheduleAutosave() {
|
|
463
|
+
if (!autosaveEnabled.value) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
clearAutosave();
|
|
467
|
+
autosaveTimer = setTimeout(() => {
|
|
468
|
+
if (saving.value || publishing.value) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
if (draft.value.title.trim() === "") {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
save().catch(() => {
|
|
475
|
+
});
|
|
476
|
+
}, autosaveDelay.value);
|
|
477
|
+
}
|
|
478
|
+
watch(() => draft.value, () => {
|
|
479
|
+
if (!hasInitializedWatch) {
|
|
480
|
+
hasInitializedWatch = true;
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
scheduleAutosave();
|
|
484
|
+
}, {
|
|
485
|
+
deep: true
|
|
486
|
+
});
|
|
487
|
+
return {
|
|
488
|
+
client,
|
|
489
|
+
loading,
|
|
490
|
+
saving,
|
|
491
|
+
publishing,
|
|
492
|
+
error,
|
|
493
|
+
draft,
|
|
494
|
+
autosaveEnabled,
|
|
495
|
+
autosaveDelay,
|
|
496
|
+
lastSavedAt,
|
|
497
|
+
publishable,
|
|
498
|
+
addPage,
|
|
499
|
+
removePage,
|
|
500
|
+
mergePageWithPrevious,
|
|
501
|
+
addField,
|
|
502
|
+
duplicateField,
|
|
503
|
+
removeField,
|
|
504
|
+
addCondition,
|
|
505
|
+
removeCondition,
|
|
506
|
+
addConditionClause,
|
|
507
|
+
removeConditionClause,
|
|
508
|
+
normalizeFieldLocations,
|
|
509
|
+
save,
|
|
510
|
+
publish,
|
|
511
|
+
unpublish,
|
|
512
|
+
clearAutosave,
|
|
513
|
+
scheduleAutosave
|
|
514
|
+
};
|
|
515
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { FormForgeRequestOptions } from '../api/request.js';
|
|
2
|
+
import type { FormForgeCategory, FormForgeCategoryCreateInput, FormForgeCategoryListQuery, FormForgeCategoryListResponse, FormForgeCategorySelectOption, FormForgeCategoryUpdateInput, FormForgeClient, FormForgeClientConfig, FormForgeScope } from '../types/index.js';
|
|
3
|
+
import type { FormForgeMutationOptions } from '../api/management.js';
|
|
4
|
+
export interface UseFormForgeCategoryOptions {
|
|
5
|
+
immediate?: boolean;
|
|
6
|
+
initialQuery?: FormForgeCategoryListQuery;
|
|
7
|
+
endpoint?: string;
|
|
8
|
+
scope?: FormForgeScope;
|
|
9
|
+
client?: FormForgeClient;
|
|
10
|
+
clientConfig?: FormForgeClientConfig;
|
|
11
|
+
}
|
|
12
|
+
export type FormForgeCategoryRequestOptions = FormForgeRequestOptions;
|
|
13
|
+
export interface FormForgeCategoryRefreshOptions {
|
|
14
|
+
mode?: 'auto' | 'list' | 'resource';
|
|
15
|
+
query?: FormForgeCategoryListQuery;
|
|
16
|
+
categoryKey?: string;
|
|
17
|
+
endpoint?: string;
|
|
18
|
+
scope?: FormForgeScope;
|
|
19
|
+
}
|
|
20
|
+
export declare function useFormForgeCategory(options?: UseFormForgeCategoryOptions): {
|
|
21
|
+
client: FormForgeClient;
|
|
22
|
+
loading: any;
|
|
23
|
+
error: any;
|
|
24
|
+
clientError: any;
|
|
25
|
+
fieldErrors: any;
|
|
26
|
+
businessErrorCode: any;
|
|
27
|
+
isCategoryInUse: any;
|
|
28
|
+
list: any;
|
|
29
|
+
current: any;
|
|
30
|
+
lastMeta: any;
|
|
31
|
+
lastLinks: any;
|
|
32
|
+
lastListQuery: any;
|
|
33
|
+
lastListEndpoint: any;
|
|
34
|
+
lastListScope: any;
|
|
35
|
+
lastCategoryKey: any;
|
|
36
|
+
listCategories: (query?: FormForgeCategoryListQuery, requestOptions?: FormForgeCategoryRequestOptions) => Promise<FormForgeCategoryListResponse>;
|
|
37
|
+
getCategory: (categoryKey: string, requestOptions?: FormForgeCategoryRequestOptions) => Promise<FormForgeCategory>;
|
|
38
|
+
createCategory: (input: FormForgeCategoryCreateInput, options?: FormForgeMutationOptions) => Promise<FormForgeCategory>;
|
|
39
|
+
patchCategory: (categoryKey: string, input: FormForgeCategoryUpdateInput, options?: FormForgeMutationOptions) => Promise<FormForgeCategory>;
|
|
40
|
+
deleteCategory: (categoryKey: string, options?: FormForgeMutationOptions) => Promise<FormForgeCategory>;
|
|
41
|
+
refresh: (options?: FormForgeCategoryRefreshOptions) => Promise<FormForgeCategoryListResponse | FormForgeCategory>;
|
|
42
|
+
};
|
|
43
|
+
type FormForgeCategoryOptionsSource = FormForgeCategory[] | {
|
|
44
|
+
value: FormForgeCategory[] | null | undefined;
|
|
45
|
+
} | (() => FormForgeCategory[] | null | undefined) | Record<string, unknown> | null | undefined;
|
|
46
|
+
export interface UseFormForgeCategoryOptionsHelperInput {
|
|
47
|
+
source?: FormForgeCategoryOptionsSource;
|
|
48
|
+
categories?: FormForgeCategoryOptionsSource;
|
|
49
|
+
key?: string;
|
|
50
|
+
includeInactive?: boolean;
|
|
51
|
+
query?: FormForgeCategoryListQuery;
|
|
52
|
+
endpoint?: string;
|
|
53
|
+
scope?: FormForgeScope;
|
|
54
|
+
immediate?: boolean;
|
|
55
|
+
client?: FormForgeClient;
|
|
56
|
+
clientConfig?: FormForgeClientConfig;
|
|
57
|
+
}
|
|
58
|
+
export declare function normalizeFormForgeCategoryOptions(categories: FormForgeCategory[], includeInactive?: boolean): FormForgeCategorySelectOption[];
|
|
59
|
+
export declare function useFormForgeCategoryOptions(options?: UseFormForgeCategoryOptionsHelperInput): any;
|
|
60
|
+
export {};
|
|
61
|
+
//# sourceMappingURL=useFormForgeCategory.d.ts.map
|