@tansy/forms 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/admin.d.ts +2 -0
- package/admin.js +36 -0
- package/form.d.ts +15 -0
- package/form.js +101 -0
- package/forms.css +35 -0
- package/index.d.ts +12 -0
- package/index.js +233 -0
- package/logic.d.ts +59 -0
- package/logic.js +173 -0
- package/messages.d.ts +78 -0
- package/messages.js +157 -0
- package/package.json +40 -4
- package/render.d.ts +4 -0
- package/render.js +48 -0
- package/schema.d.ts +208 -0
- package/schema.js +71 -0
- package/site-messages.d.ts +91 -0
- package/site-messages.js +94 -0
- package/status.d.ts +2 -0
- package/status.js +18 -0
package/logic.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { english } from "./site-messages";
|
|
2
|
+
const KINDS = { forms_text: "text", forms_email: "email", forms_textarea: "textarea", forms_select: "select", forms_checkbox: "checkbox", forms_number: "number" };
|
|
3
|
+
const NAME = /^[a-z][a-z0-9_]{0,39}$/;
|
|
4
|
+
/** The most fields one form takes, so a submission's size stays bounded. */
|
|
5
|
+
export const MAX_FIELDS = 50;
|
|
6
|
+
const str = (value) => (typeof value === "string" ? value : "");
|
|
7
|
+
const num = (value) => (typeof value === "number" && Number.isFinite(value) ? value : undefined);
|
|
8
|
+
/** One line of plain text: no control characters, so nothing can become a header or break a layout. */
|
|
9
|
+
export const oneLine = (value) => value.replace(/[\u0000-\u001f\u007f]+/g, " ").replace(/\s+/g, " ").trim();
|
|
10
|
+
/**
|
|
11
|
+
* A form's fields from its `fields` stream. Blocks of other types are skipped, and so is a
|
|
12
|
+
* second field with a name already used: the first one wins, as the owner was told when saving.
|
|
13
|
+
*/
|
|
14
|
+
export function fieldsOf(stream) {
|
|
15
|
+
if (!Array.isArray(stream))
|
|
16
|
+
return [];
|
|
17
|
+
const fields = [];
|
|
18
|
+
const seen = new Set();
|
|
19
|
+
for (const block of stream) {
|
|
20
|
+
const kind = KINDS[str(block?.type)];
|
|
21
|
+
const value = block?.value ?? {};
|
|
22
|
+
const name = str(value.name);
|
|
23
|
+
if (!kind || !NAME.test(name) || seen.has(name) || fields.length >= MAX_FIELDS)
|
|
24
|
+
continue;
|
|
25
|
+
seen.add(name);
|
|
26
|
+
const field = { kind, name, label: oneLine(str(value.label)) || name, required: value.required === true, help: oneLine(str(value.help)), placeholder: oneLine(str(value.placeholder)) };
|
|
27
|
+
if (kind === "text")
|
|
28
|
+
field.maxLength = Math.min(num(value.maxLength) ?? 200, 1000);
|
|
29
|
+
if (kind === "textarea")
|
|
30
|
+
field.maxLength = Math.min(num(value.maxLength) ?? 5000, 10000);
|
|
31
|
+
if (kind === "email")
|
|
32
|
+
field.maxLength = 254;
|
|
33
|
+
if (kind === "select")
|
|
34
|
+
field.options = [...new Set(str(value.options).split("\n").map(oneLine).filter(Boolean))].slice(0, 100);
|
|
35
|
+
if (kind === "number")
|
|
36
|
+
Object.assign(field, num(value.min) === undefined ? {} : { min: num(value.min) }, num(value.max) === undefined ? {} : { max: num(value.max) });
|
|
37
|
+
fields.push(field);
|
|
38
|
+
}
|
|
39
|
+
return fields;
|
|
40
|
+
}
|
|
41
|
+
/** Names used by more than one field, for the owner to fix before saving. */
|
|
42
|
+
export function duplicateNames(stream) {
|
|
43
|
+
if (!Array.isArray(stream))
|
|
44
|
+
return [];
|
|
45
|
+
const names = stream.filter((block) => KINDS[str(block?.type)]).map((block) => str(block.value?.name));
|
|
46
|
+
return [...new Set(names.filter((name, index) => name && names.indexOf(name) !== index))];
|
|
47
|
+
}
|
|
48
|
+
const ADDRESS = /^[^\s@<>()",;:\\[\]]+@[^\s@<>()",;:\\[\]]+\.[^\s@<>()",;:\\[\]]+$/;
|
|
49
|
+
export const isAddress = (value) => value.length <= 254 && ADDRESS.test(value);
|
|
50
|
+
/**
|
|
51
|
+
* Checks answers against the form's own fields, the way the browser should have. Problems are
|
|
52
|
+
* worded with `t`, in the visitor's language when the route knows it. Anything
|
|
53
|
+
* the form doesn't ask is dropped, so a submission can only hold what the owner asked for.
|
|
54
|
+
*/
|
|
55
|
+
export function check(fields, values, t = english) {
|
|
56
|
+
const data = {};
|
|
57
|
+
const errors = {};
|
|
58
|
+
let replyTo = null;
|
|
59
|
+
for (const field of fields) {
|
|
60
|
+
const raw = values[field.name];
|
|
61
|
+
if (field.kind === "checkbox") {
|
|
62
|
+
const ticked = raw === true || raw === "on" || raw === "true";
|
|
63
|
+
if (field.required && !ticked)
|
|
64
|
+
errors[field.name] = t("forms.errors.tick");
|
|
65
|
+
data[field.name] = ticked;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (field.kind === "number") {
|
|
69
|
+
const value = typeof raw === "number" ? raw : typeof raw === "string" && raw.trim() !== "" ? Number(raw) : undefined;
|
|
70
|
+
if (value === undefined) {
|
|
71
|
+
if (field.required)
|
|
72
|
+
errors[field.name] = t("forms.errors.fill");
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (!Number.isFinite(value))
|
|
76
|
+
errors[field.name] = t("forms.errors.number");
|
|
77
|
+
else if (field.min !== undefined && value < field.min)
|
|
78
|
+
errors[field.name] = t("forms.errors.min", { min: field.min });
|
|
79
|
+
else if (field.max !== undefined && value > field.max)
|
|
80
|
+
errors[field.name] = t("forms.errors.max", { max: field.max });
|
|
81
|
+
else
|
|
82
|
+
data[field.name] = value;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (raw !== undefined && raw !== null && typeof raw !== "string") {
|
|
86
|
+
errors[field.name] = t("forms.errors.text");
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
// Long answers keep their line breaks. Everything else is one line.
|
|
90
|
+
const text = field.kind === "textarea" ? str(raw).replace(/\r\n?/g, "\n").replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, "").trim() : oneLine(str(raw));
|
|
91
|
+
if (!text) {
|
|
92
|
+
if (field.required)
|
|
93
|
+
errors[field.name] = t("forms.errors.fill");
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (field.maxLength !== undefined && text.length > field.maxLength) {
|
|
97
|
+
errors[field.name] = t("forms.errors.tooLong", { limit: field.maxLength + 1 });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (field.kind === "email" && !isAddress(text)) {
|
|
101
|
+
errors[field.name] = t("forms.errors.email");
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (field.kind === "select" && !field.options?.includes(text)) {
|
|
105
|
+
errors[field.name] = t("forms.errors.option");
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
data[field.name] = text;
|
|
109
|
+
if (field.kind === "email" && replyTo === null)
|
|
110
|
+
replyTo = text;
|
|
111
|
+
}
|
|
112
|
+
return { data, errors, replyTo };
|
|
113
|
+
}
|
|
114
|
+
/** The addresses to notify: the form's own, else the settings'. At most ten, each checked. */
|
|
115
|
+
export function recipients(own, fallback) {
|
|
116
|
+
const parse = (value) => str(value).split(/[,;\s]+/).map((item) => item.trim()).filter((item) => isAddress(item));
|
|
117
|
+
const list = parse(own);
|
|
118
|
+
return [...new Set(list.length ? list : parse(fallback))].slice(0, 10);
|
|
119
|
+
}
|
|
120
|
+
const MAX_ANSWER = 2000;
|
|
121
|
+
const MAX_BODY = 18000;
|
|
122
|
+
/** The email the owner gets: each question and answer, plain text, with nothing that could become markup or a link. Worded with `t`, in the owner's language. */
|
|
123
|
+
export function notification(title, fields, data, page, t = english) {
|
|
124
|
+
const named = oneLine(title) || t("forms.mail.aForm");
|
|
125
|
+
const subject = t("forms.mail.subject", { title: named }).slice(0, 150);
|
|
126
|
+
const shown = (value) => (value === true ? t("forms.mail.yes") : value === false ? t("forms.mail.no") : String(value ?? ""));
|
|
127
|
+
const answers = fields.filter((field) => field.name in data).map((field) => {
|
|
128
|
+
const answer = shown(data[field.name]);
|
|
129
|
+
return `${field.label}\n${answer.length > MAX_ANSWER ? `${answer.slice(0, MAX_ANSWER)}…` : answer}`;
|
|
130
|
+
});
|
|
131
|
+
const intro = page ? t("forms.mail.introOn", { title: named, page: oneLine(page).slice(0, 300) }) : t("forms.mail.intro", { title: named });
|
|
132
|
+
let body = [intro, ...answers, t("forms.mail.outro")].join("\n\n");
|
|
133
|
+
if (body.length > MAX_BODY)
|
|
134
|
+
body = `${body.slice(0, MAX_BODY)}…\n\n${t("forms.mail.rest")}`;
|
|
135
|
+
return { subject, body };
|
|
136
|
+
}
|
|
137
|
+
/** Only a site path, such as /contact, so a submission can't carry a link to somewhere else. */
|
|
138
|
+
export const pageOf = (value) => {
|
|
139
|
+
const path = oneLine(str(value)).slice(0, 300);
|
|
140
|
+
return /^\/(?!\/)[^\s\\]*$/.test(path) ? path : "";
|
|
141
|
+
};
|
|
142
|
+
const encoder = new TextEncoder();
|
|
143
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
144
|
+
async function sign(key, message) {
|
|
145
|
+
const imported = await crypto.subtle.importKey("raw", encoder.encode(key), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
146
|
+
return hex(await crypto.subtle.sign("HMAC", imported, encoder.encode(message)));
|
|
147
|
+
}
|
|
148
|
+
/** When the form was handed out, signed, so a script can't claim it waited. */
|
|
149
|
+
export async function issueToken(key, formId, now = Date.now()) {
|
|
150
|
+
return `${now}.${await sign(key, `${formId}.${now}`)}`;
|
|
151
|
+
}
|
|
152
|
+
const DAY = 86_400_000;
|
|
153
|
+
/** "ok", or why the token doesn't do: not signed by us for this form, too fresh, or a day old. */
|
|
154
|
+
export async function tokenProblem(key, formId, token, minSeconds, now = Date.now()) {
|
|
155
|
+
const match = /^(\d{13})\.([0-9a-f]{64})$/.exec(str(token));
|
|
156
|
+
if (!match)
|
|
157
|
+
return "invalid";
|
|
158
|
+
const issued = Number(match[1]);
|
|
159
|
+
const expected = await sign(key, `${formId}.${issued}`);
|
|
160
|
+
// Both are hex of the same length, so this compares every character whatever matches.
|
|
161
|
+
let difference = 0;
|
|
162
|
+
for (let index = 0; index < expected.length; index++)
|
|
163
|
+
difference |= expected.charCodeAt(index) ^ match[2].charCodeAt(index);
|
|
164
|
+
if (difference !== 0 || issued > now + 60_000)
|
|
165
|
+
return "invalid";
|
|
166
|
+
if (now - issued > DAY)
|
|
167
|
+
return "expired";
|
|
168
|
+
if (now - issued < minSeconds * 1000)
|
|
169
|
+
return "early";
|
|
170
|
+
return "ok";
|
|
171
|
+
}
|
|
172
|
+
/** A new signing key: 32 random bytes. */
|
|
173
|
+
export const newKey = () => hex(crypto.getRandomValues(new Uint8Array(32)).buffer);
|
package/messages.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { FieldLabels } from "tansy/plugin";
|
|
2
|
+
/** The forms plugin's dashboard words, read as `t("ext.forms.<key>")`, and its collections' names. */
|
|
3
|
+
export declare const messages: {
|
|
4
|
+
en: {
|
|
5
|
+
section: string;
|
|
6
|
+
pages: {
|
|
7
|
+
forms: string;
|
|
8
|
+
};
|
|
9
|
+
categories: {
|
|
10
|
+
"Form fields": string;
|
|
11
|
+
};
|
|
12
|
+
settings: {
|
|
13
|
+
notify: {
|
|
14
|
+
label: string;
|
|
15
|
+
description: string;
|
|
16
|
+
};
|
|
17
|
+
minSeconds: {
|
|
18
|
+
label: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
keepDays: {
|
|
22
|
+
label: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
overview: {
|
|
27
|
+
description: string;
|
|
28
|
+
cantRead: string;
|
|
29
|
+
empty: string;
|
|
30
|
+
draft: string;
|
|
31
|
+
new: string;
|
|
32
|
+
};
|
|
33
|
+
status: {
|
|
34
|
+
noMail: string;
|
|
35
|
+
noCaptcha: string;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
ar: {
|
|
39
|
+
section: string;
|
|
40
|
+
pages: {
|
|
41
|
+
forms: string;
|
|
42
|
+
};
|
|
43
|
+
categories: {
|
|
44
|
+
"Form fields": string;
|
|
45
|
+
};
|
|
46
|
+
settings: {
|
|
47
|
+
notify: {
|
|
48
|
+
label: string;
|
|
49
|
+
description: string;
|
|
50
|
+
};
|
|
51
|
+
minSeconds: {
|
|
52
|
+
label: string;
|
|
53
|
+
description: string;
|
|
54
|
+
};
|
|
55
|
+
keepDays: {
|
|
56
|
+
label: string;
|
|
57
|
+
description: string;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
overview: {
|
|
61
|
+
description: string;
|
|
62
|
+
cantRead: string;
|
|
63
|
+
empty: string;
|
|
64
|
+
draft: string;
|
|
65
|
+
new: string;
|
|
66
|
+
};
|
|
67
|
+
status: {
|
|
68
|
+
noMail: string;
|
|
69
|
+
noCaptcha: string;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
/** The collections' names in other interface languages. Added by a migration, so names an owner set stay. */
|
|
74
|
+
export declare const COLLECTION_LABELS: Record<string, Record<string, string>>;
|
|
75
|
+
/** Fields' words in other interface languages, by collection. Added by a migration, so words an owner set stay. */
|
|
76
|
+
export declare const FIELD_LABELS: Record<string, Record<string, FieldLabels>>;
|
|
77
|
+
/** The same for the blocks the plugin's schema makes, by block. */
|
|
78
|
+
export declare const BLOCK_FIELD_LABELS: Record<string, Record<string, FieldLabels>>;
|
package/messages.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/** The forms plugin's dashboard words, read as `t("ext.forms.<key>")`, and its collections' names. */
|
|
2
|
+
export const messages = {
|
|
3
|
+
en: {
|
|
4
|
+
section: "Forms",
|
|
5
|
+
pages: { forms: "Overview" },
|
|
6
|
+
categories: { "Form fields": "Form fields" },
|
|
7
|
+
settings: {
|
|
8
|
+
notify: { label: "Email submissions to", description: "Addresses separated by commas, for forms that don't name their own. Empty sends no email: submissions are still kept." },
|
|
9
|
+
minSeconds: { label: "Fastest a person fills in a form (seconds)", description: "Anything sent sooner after the form loaded is refused as a script. 0 turns this off." },
|
|
10
|
+
keepDays: { label: "Delete submissions after (days)", description: "0 keeps them until someone deletes them." },
|
|
11
|
+
},
|
|
12
|
+
overview: {
|
|
13
|
+
description: "Forms visitors fill in, and what they sent. Put a form on a page with the Form block.",
|
|
14
|
+
cantRead: "You can't read forms. Ask an administrator for permissions on the Forms collection.",
|
|
15
|
+
empty: "No forms yet. Create one under Forms, add its fields, publish it, then put it on a page with the Form block.",
|
|
16
|
+
draft: "Draft, so visitors can't open it",
|
|
17
|
+
new: "{count} new",
|
|
18
|
+
},
|
|
19
|
+
status: {
|
|
20
|
+
noMail: "Email isn't set up, so nobody is told about new submissions. They're still kept here. Set up SMTP under Settings → Email, or an email plugin.",
|
|
21
|
+
noCaptcha: "No CAPTCHA is set up. Forms are still guarded by a hidden trap field, a minimum time to fill in and a rate limit. Add the CAPTCHA plugin for more.",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
ar: {
|
|
25
|
+
section: "النماذج",
|
|
26
|
+
pages: { forms: "نظرة عامة" },
|
|
27
|
+
categories: { "Form fields": "حقول النموذج" },
|
|
28
|
+
settings: {
|
|
29
|
+
notify: { label: "أرسل الإرسالات بالبريد إلى", description: "عناوين مفصولة بفواصل، للنماذج التي لا تحدد عناوينها. إن تُرك فارغًا فلا يُرسل بريد، وتبقى الإرسالات محفوظة." },
|
|
30
|
+
minSeconds: { label: "أسرع وقت يملأ فيه شخص نموذجًا (بالثواني)", description: "يُرفض كل ما يُرسل أسرع من ذلك بعد تحميل النموذج على أنه برنامج آلي. القيمة 0 تعطّل ذلك." },
|
|
31
|
+
keepDays: { label: "احذف الإرسالات بعد (أيام)", description: "القيمة 0 تبقيها حتى يحذفها أحد." },
|
|
32
|
+
},
|
|
33
|
+
overview: {
|
|
34
|
+
description: "النماذج التي يملؤها الزوار وما أرسلوه. ضع نموذجًا في صفحة بكتلة «نموذج».",
|
|
35
|
+
cantRead: "لا يمكنك قراءة النماذج. اطلب من المسؤول صلاحيات على مجموعة النماذج.",
|
|
36
|
+
empty: "لا نماذج بعد. أنشئ نموذجًا في «النماذج» وأضف حقوله وانشره، ثم ضعه في صفحة بكتلة «نموذج».",
|
|
37
|
+
draft: "مسودة، فلا يستطيع الزوار فتحه",
|
|
38
|
+
new: "{count} جديد",
|
|
39
|
+
},
|
|
40
|
+
status: {
|
|
41
|
+
noMail: "البريد غير مُعدّ، فلن يُبلَّغ أحد بالإرسالات الجديدة. تبقى محفوظة هنا. أعدّ SMTP من الإعدادات ← البريد، أو استخدم إضافة بريد.",
|
|
42
|
+
noCaptcha: "لا يوجد CAPTCHA مُعدّ. تبقى النماذج محمية بحقل فخ مخفي وحد أدنى لوقت الملء وحد للمعدّل. أضف إضافة CAPTCHA لحماية أكبر.",
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
/** The collections' names in other interface languages. Added by a migration, so names an owner set stay. */
|
|
47
|
+
export const COLLECTION_LABELS = {
|
|
48
|
+
"forms-forms": { ar: "النماذج" },
|
|
49
|
+
"forms-submissions": { ar: "الإرسالات" },
|
|
50
|
+
};
|
|
51
|
+
/** Fields' words in other interface languages, by collection. Added by a migration, so words an owner set stay. */
|
|
52
|
+
export const FIELD_LABELS = {
|
|
53
|
+
"forms-forms": {
|
|
54
|
+
title: {
|
|
55
|
+
labels: {
|
|
56
|
+
ar: "العنوان"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
fields: {
|
|
60
|
+
labels: {
|
|
61
|
+
ar: "الحقول"
|
|
62
|
+
},
|
|
63
|
+
descriptions: {
|
|
64
|
+
ar: "ما يسأله النموذج، بالترتيب."
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
submitLabel: {
|
|
68
|
+
labels: {
|
|
69
|
+
ar: "نص الزر"
|
|
70
|
+
},
|
|
71
|
+
descriptions: {
|
|
72
|
+
ar: "زر الإرسال. «إرسال» إن تُرك فارغًا."
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
successMessage: {
|
|
76
|
+
labels: {
|
|
77
|
+
ar: "رسالة النجاح"
|
|
78
|
+
},
|
|
79
|
+
descriptions: {
|
|
80
|
+
ar: "تظهر بعد الإرسال."
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
notify: {
|
|
84
|
+
labels: {
|
|
85
|
+
ar: "أرسل إشعارًا إلى"
|
|
86
|
+
},
|
|
87
|
+
descriptions: {
|
|
88
|
+
ar: "من يتلقى كل إرسال، مفصولين بفواصل. الفارغ يستخدم العناوين في النماذج ← الإعدادات."
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
captcha: {
|
|
92
|
+
labels: {
|
|
93
|
+
ar: "CAPTCHA"
|
|
94
|
+
},
|
|
95
|
+
descriptions: {
|
|
96
|
+
ar: "اطلب CAPTCHA الموقع، حين يُضبط في الإضافات ← CAPTCHA."
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"forms-submissions": {
|
|
101
|
+
form: {
|
|
102
|
+
labels: {
|
|
103
|
+
ar: "النموذج"
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
email: {
|
|
107
|
+
labels: {
|
|
108
|
+
ar: "البريد الإلكتروني"
|
|
109
|
+
},
|
|
110
|
+
descriptions: {
|
|
111
|
+
ar: "عنوان الزائر، من أول حقل بريد في النموذج. ترد الإشعارات عليه."
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
data: {
|
|
115
|
+
labels: {
|
|
116
|
+
ar: "الإجابات"
|
|
117
|
+
},
|
|
118
|
+
descriptions: {
|
|
119
|
+
ar: "الإجابات، حسب اسم الحقل."
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
page: {
|
|
123
|
+
labels: {
|
|
124
|
+
ar: "الصفحة"
|
|
125
|
+
},
|
|
126
|
+
descriptions: {
|
|
127
|
+
ar: "الصفحة التي أُرسل منها النموذج."
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
state: {
|
|
131
|
+
labels: {
|
|
132
|
+
ar: "الحالة"
|
|
133
|
+
},
|
|
134
|
+
options: {
|
|
135
|
+
new: {
|
|
136
|
+
ar: "جديد"
|
|
137
|
+
},
|
|
138
|
+
done: {
|
|
139
|
+
ar: "تم"
|
|
140
|
+
},
|
|
141
|
+
spam: {
|
|
142
|
+
ar: "مزعج"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
/** The same for the blocks the plugin's schema makes, by block. */
|
|
149
|
+
export const BLOCK_FIELD_LABELS = {
|
|
150
|
+
forms_embed: {
|
|
151
|
+
form: {
|
|
152
|
+
labels: {
|
|
153
|
+
ar: "النموذج"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/forms",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Contact forms and other forms for tansy: built from field blocks, kept as submissions, emailed to you, and guarded against spam.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-plugin",
|
|
8
|
+
"cms",
|
|
9
|
+
"nextjs"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
13
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
17
|
+
"directory": "plugins/forms"
|
|
18
|
+
},
|
|
19
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./index.d.ts",
|
|
23
|
+
"default": "./index.js"
|
|
24
|
+
},
|
|
25
|
+
"./admin": {
|
|
26
|
+
"types": "./admin.d.ts",
|
|
27
|
+
"default": "./admin.js"
|
|
28
|
+
},
|
|
29
|
+
"./render": {
|
|
30
|
+
"types": "./render.d.ts",
|
|
31
|
+
"default": "./render.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"next": "^16.4.6",
|
|
36
|
+
"react": "^19.3.0",
|
|
37
|
+
"tansy": "^0.1.1"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"zod": "^4.6.7"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/render.d.ts
ADDED
package/render.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { defineRenderers, pickWords, siteWords } from "tansy/react";
|
|
3
|
+
import { FormView } from "./form";
|
|
4
|
+
import { fieldsOf } from "./logic";
|
|
5
|
+
import { siteMessages } from "./site-messages";
|
|
6
|
+
import "./forms.css";
|
|
7
|
+
const forParts = (words) => (words ? pickWords(words, "forms") : undefined);
|
|
8
|
+
export { FormView } from "./form";
|
|
9
|
+
/** A reference arrives as an id, or as the record when expanded. */
|
|
10
|
+
const formId = (value) => {
|
|
11
|
+
const id = value && typeof value === "object" ? value.id : value;
|
|
12
|
+
return typeof id === "number" && Number.isSafeInteger(id) ? id : null;
|
|
13
|
+
};
|
|
14
|
+
/** The Form block, on any page. */
|
|
15
|
+
function FormEmbed({ value, words }) {
|
|
16
|
+
const id = formId(value.form);
|
|
17
|
+
return id === null ? null : _jsx(FormView, { form: id, words: forParts(words) });
|
|
18
|
+
}
|
|
19
|
+
/** How a field looks while the form is built: the real control, which can't be used there. */
|
|
20
|
+
function FieldPreview({ block, words }) {
|
|
21
|
+
const { t } = siteWords(words);
|
|
22
|
+
const [field] = fieldsOf([block]);
|
|
23
|
+
if (!field)
|
|
24
|
+
return _jsx("p", { className: "fm-help", children: t("forms.namePreview") });
|
|
25
|
+
const control = field.kind === "textarea" ? _jsx("textarea", { rows: 3, disabled: true, placeholder: field.placeholder })
|
|
26
|
+
: field.kind === "select" ? _jsxs("select", { disabled: true, children: [_jsx("option", { children: field.placeholder || t("forms.choose") }), field.options?.map((option) => _jsx("option", { children: option }, option))] })
|
|
27
|
+
: field.kind === "checkbox" ? null
|
|
28
|
+
: _jsx("input", { type: field.kind === "email" ? "email" : field.kind === "number" ? "number" : "text", disabled: true, placeholder: field.placeholder });
|
|
29
|
+
return (_jsx("div", { className: "fm-form fm-preview", children: _jsxs("div", { className: "fm-field", children: [field.kind === "checkbox" ? _jsxs("label", { className: "fm-check", children: [_jsx("input", { type: "checkbox", disabled: true }), _jsx("span", { children: field.label })] }) : _jsxs("label", { children: [field.label, field.required && _jsx("span", { "aria-hidden": true, children: " *" })] }), control, field.help && _jsx("p", { className: "fm-help", children: field.help })] }) }));
|
|
30
|
+
}
|
|
31
|
+
/** A form's own page, for an owner who gives the Forms collection an address. */
|
|
32
|
+
function FormPage({ record, words }) {
|
|
33
|
+
const id = formId(record?.id);
|
|
34
|
+
return (_jsxs("main", { className: "fm-page", children: [_jsx("h1", { children: typeof record?.title === "string" ? record.title : "" }), id !== null && _jsx(FormView, { form: id, words: forParts(words) })] }));
|
|
35
|
+
}
|
|
36
|
+
export default defineRenderers({
|
|
37
|
+
messages: siteMessages,
|
|
38
|
+
blocks: {
|
|
39
|
+
forms_embed: FormEmbed,
|
|
40
|
+
forms_text: FieldPreview,
|
|
41
|
+
forms_email: FieldPreview,
|
|
42
|
+
forms_textarea: FieldPreview,
|
|
43
|
+
forms_select: FieldPreview,
|
|
44
|
+
forms_checkbox: FieldPreview,
|
|
45
|
+
forms_number: FieldPreview,
|
|
46
|
+
},
|
|
47
|
+
templates: { "forms-forms": FormPage },
|
|
48
|
+
});
|