@ecomcx/ecomweb-editor 1.26.2 → 1.27.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/package.json +3 -3
- package/src/locales/en/attrs.ts +6 -0
- package/src/locales/en/block-desc.ts +2 -0
- package/src/locales/en/block-titles.ts +2 -0
- package/src/locales/en/blocks.ts +17 -0
- package/src/locales/en/hints.ts +17 -0
- package/src/locales/en/pattern-lib.ts +15 -1
- package/src/locales/en/patterns.ts +6 -1
- package/src/locales/vi/attrs.ts +6 -0
- package/src/locales/vi/block-desc.ts +2 -0
- package/src/locales/vi/block-titles.ts +2 -0
- package/src/locales/vi/blocks.ts +17 -0
- package/src/locales/vi/hints.ts +17 -0
- package/src/locales/vi/pattern-lib.ts +15 -1
- package/src/locales/vi/patterns.ts +6 -1
- package/src/shared/patterns-library.ts +22 -2
- package/src/shared/patterns.ts +5 -2
- package/src/v2/blocks/form.tsx +129 -0
- package/src/v2/blocks/index.ts +2 -1
- package/src/v2/ui/attr-hints.ts +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecomcx/ecomweb-editor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.0",
|
|
4
4
|
"description": "Trình soạn nội dung dạng khối của CMS — dùng độc lập trong mọi dự án React (1.x: TipTap; ./v2: cây khối, khung soạn iframe, sửa header/footer tại chỗ).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"*.css"
|
|
30
30
|
],
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@ecomcx/ecomweb-content": "^1.
|
|
32
|
+
"@ecomcx/ecomweb-content": "^1.21.0",
|
|
33
33
|
"react": ">=18",
|
|
34
34
|
"react-dom": ">=18"
|
|
35
35
|
},
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"prosemirror-view": "1.42.7"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@ecomcx/ecomweb-content": "^1.
|
|
48
|
+
"@ecomcx/ecomweb-content": "^1.21.0",
|
|
49
49
|
"@playwright/test": "1.53.0",
|
|
50
50
|
"@types/react": "19.2.2",
|
|
51
51
|
"@types/react-dom": "19.2.2",
|
package/src/locales/en/attrs.ts
CHANGED
|
@@ -70,4 +70,10 @@ export const attrs = {
|
|
|
70
70
|
showTitle: "Show post titles",
|
|
71
71
|
anchor: "Anchor",
|
|
72
72
|
className: "CSS class",
|
|
73
|
+
kind: "Type",
|
|
74
|
+
help: "Help text",
|
|
75
|
+
required: "Required",
|
|
76
|
+
submitText: "Submit button text",
|
|
77
|
+
successMessage: "Thank-you message",
|
|
78
|
+
notifyEmail: "Notification email",
|
|
73
79
|
};
|
|
@@ -50,4 +50,6 @@ export const blockDesc = {
|
|
|
50
50
|
postNavigation: "Links to the previous and next posts.",
|
|
51
51
|
query: "A list of posts by criteria.",
|
|
52
52
|
templatePart: "A shared part (header, footer…) — edit once, update everywhere.",
|
|
53
|
+
form: "Basic contact / sign-up form: submissions are kept in the admin and emailed. Advanced forms: plugins.",
|
|
54
|
+
formField: "One field of a form: text, email, phone, message, choice, consent…",
|
|
53
55
|
};
|
package/src/locales/en/blocks.ts
CHANGED
|
@@ -41,4 +41,21 @@ export const blocks = {
|
|
|
41
41
|
clickToEditThe: "Click to edit the template on this post",
|
|
42
42
|
network: "Network {n}",
|
|
43
43
|
urlN: "URL {n}",
|
|
44
|
+
formSubmit: "Send",
|
|
45
|
+
fieldLabel: "Field label",
|
|
46
|
+
fieldOptions: "Options (one per line)",
|
|
47
|
+
fieldNameInvalid: "Field names use lowercase letters, digits and underscores — fixed automatically when you click away",
|
|
48
|
+
fieldText: "Text",
|
|
49
|
+
fieldEmail: "Email",
|
|
50
|
+
fieldPhone: "Phone number",
|
|
51
|
+
fieldMessage: "Message",
|
|
52
|
+
fieldSelect: "Dropdown",
|
|
53
|
+
fieldRadio: "Choose one",
|
|
54
|
+
fieldCheckbox: "Checkbox",
|
|
55
|
+
fieldConsent: "I agree to be contacted",
|
|
56
|
+
fieldNumber: "Number",
|
|
57
|
+
fieldDate: "Date",
|
|
58
|
+
fieldName: "Full name",
|
|
59
|
+
option1: "Option 1",
|
|
60
|
+
option2: "Option 2",
|
|
44
61
|
};
|
package/src/locales/en/hints.ts
CHANGED
|
@@ -184,4 +184,21 @@ export const hints = {
|
|
|
184
184
|
"icon.name.opt.info": "Info",
|
|
185
185
|
"icon.name.opt.alert": "Alert",
|
|
186
186
|
"icon.name.opt.externalLink": "External link",
|
|
187
|
+
"formField.name.label": "Field name (data key)",
|
|
188
|
+
"formField.name.help": "Lowercase letters, digits, underscores; used in submissions and CSV export.",
|
|
189
|
+
"formField.label.label": "Label",
|
|
190
|
+
"formField.label.help": "Consent field: add links with [text](path), e.g. I agree to the [terms](/terms) and the [privacy policy](/privacy).",
|
|
191
|
+
"formField.width.opt.full": "Full row",
|
|
192
|
+
"formField.width.opt.half": "Half row",
|
|
193
|
+
"form.notifyEmail.help": "Empty: sent to the site's admin email.",
|
|
194
|
+
"formField.kind.opt.text": "Text",
|
|
195
|
+
"formField.kind.opt.email": "Email",
|
|
196
|
+
"formField.kind.opt.tel": "Phone",
|
|
197
|
+
"formField.kind.opt.textarea": "Long text",
|
|
198
|
+
"formField.kind.opt.select": "Dropdown",
|
|
199
|
+
"formField.kind.opt.radio": "Choose one",
|
|
200
|
+
"formField.kind.opt.checkbox": "Checkbox",
|
|
201
|
+
"formField.kind.opt.consent": "Consent",
|
|
202
|
+
"formField.kind.opt.number": "Number",
|
|
203
|
+
"formField.kind.opt.date": "Date",
|
|
187
204
|
};
|
|
@@ -137,7 +137,7 @@ export const patternLib = {
|
|
|
137
137
|
logos: "Logo strip",
|
|
138
138
|
logosD: "A row of customer / partner logos",
|
|
139
139
|
contact_split: "Split contact",
|
|
140
|
-
contact_splitD: "Details on the left,
|
|
140
|
+
contact_splitD: "Details and social links on the left, form on the right",
|
|
141
141
|
contact_cards: "3 contact cards",
|
|
142
142
|
contact_cardsD: "Address, phone, email",
|
|
143
143
|
text_cols: "Two text columns",
|
|
@@ -148,4 +148,18 @@ export const patternLib = {
|
|
|
148
148
|
footer_simpleD: "Copyright and social links",
|
|
149
149
|
footer_cols: "3-column footer",
|
|
150
150
|
footer_colsD: "3 link groups + copyright",
|
|
151
|
+
fSend: "Send message",
|
|
152
|
+
fThanks: "Thank you! We will get back to you soon.",
|
|
153
|
+
fName: "Full name",
|
|
154
|
+
fPhone: "Phone number",
|
|
155
|
+
fEmail: "Email",
|
|
156
|
+
fTopic: "What do you need?",
|
|
157
|
+
topic1: "Advice",
|
|
158
|
+
topic2: "A quote",
|
|
159
|
+
topic3: "Other",
|
|
160
|
+
fMessage: "Message",
|
|
161
|
+
fMessageP: "Briefly describe what you need…",
|
|
162
|
+
fConsent: "I agree to be contacted by phone / email.",
|
|
163
|
+
contact_form: "Contact with form",
|
|
164
|
+
contact_formD: "Contact details on the left, message form on the right",
|
|
151
165
|
};
|
|
@@ -39,7 +39,12 @@ export const patterns = {
|
|
|
39
39
|
introTitle: "About us",
|
|
40
40
|
introText: "A short introduction to the company, its mission and values.",
|
|
41
41
|
contact: "Contact",
|
|
42
|
-
contactD: "
|
|
42
|
+
contactD: "Heading, short intro and a contact form",
|
|
43
43
|
contactTitle: "Get in touch",
|
|
44
44
|
contactText: "Address · Phone · Email",
|
|
45
|
+
send: "Send",
|
|
46
|
+
thanks: "Thank you! We will get back to you soon.",
|
|
47
|
+
name: "Full name",
|
|
48
|
+
email: "Email",
|
|
49
|
+
message: "Message",
|
|
45
50
|
};
|
package/src/locales/vi/attrs.ts
CHANGED
|
@@ -70,4 +70,10 @@ export const attrs = {
|
|
|
70
70
|
showTitle: "Hiện tiêu đề bài",
|
|
71
71
|
anchor: "Neo (id)",
|
|
72
72
|
className: "Class CSS",
|
|
73
|
+
kind: "Loại",
|
|
74
|
+
help: "Ghi chú dưới ô",
|
|
75
|
+
required: "Bắt buộc",
|
|
76
|
+
submitText: "Chữ trên nút gửi",
|
|
77
|
+
successMessage: "Lời cảm ơn sau khi gửi",
|
|
78
|
+
notifyEmail: "Email nhận thông báo",
|
|
73
79
|
};
|
|
@@ -50,4 +50,6 @@ export const blockDesc = {
|
|
|
50
50
|
postNavigation: "Liên kết tới bài trước và bài sau.",
|
|
51
51
|
query: "Danh sách bài theo điều kiện (chuyên mục, số bài, sắp xếp).",
|
|
52
52
|
templatePart: "Phần dùng chung (header, footer…) — sửa một nơi, đổi mọi trang.",
|
|
53
|
+
form: "Form liên hệ / đăng ký cơ bản: bài gửi lưu trong admin và báo qua email. Form nâng cao: plugin.",
|
|
54
|
+
formField: "Một ô của form: chữ, email, điện thoại, nội dung, chọn, đồng ý…",
|
|
53
55
|
};
|
package/src/locales/vi/blocks.ts
CHANGED
|
@@ -41,4 +41,21 @@ export const blocks = {
|
|
|
41
41
|
clickToEditThe: "Bấm để sửa khuôn trên bài này",
|
|
42
42
|
network: "Mạng {n}",
|
|
43
43
|
urlN: "Địa chỉ {n}",
|
|
44
|
+
formSubmit: "Gửi",
|
|
45
|
+
fieldLabel: "Nhãn ô",
|
|
46
|
+
fieldOptions: "Các lựa chọn (mỗi dòng một)",
|
|
47
|
+
fieldNameInvalid: "Tên ô chỉ gồm chữ thường không dấu, số, gạch dưới — tự sửa khi bạn bấm ra ngoài",
|
|
48
|
+
fieldText: "Ô chữ",
|
|
49
|
+
fieldEmail: "Email",
|
|
50
|
+
fieldPhone: "Số điện thoại",
|
|
51
|
+
fieldMessage: "Nội dung",
|
|
52
|
+
fieldSelect: "Chọn trong danh sách",
|
|
53
|
+
fieldRadio: "Chọn một",
|
|
54
|
+
fieldCheckbox: "Ô đánh dấu",
|
|
55
|
+
fieldConsent: "Tôi đồng ý để được liên hệ lại",
|
|
56
|
+
fieldNumber: "Số",
|
|
57
|
+
fieldDate: "Ngày",
|
|
58
|
+
fieldName: "Họ và tên",
|
|
59
|
+
option1: "Lựa chọn 1",
|
|
60
|
+
option2: "Lựa chọn 2",
|
|
44
61
|
};
|
package/src/locales/vi/hints.ts
CHANGED
|
@@ -184,4 +184,21 @@ export const hints = {
|
|
|
184
184
|
"icon.name.opt.info": "Thông tin",
|
|
185
185
|
"icon.name.opt.alert": "Cảnh báo",
|
|
186
186
|
"icon.name.opt.externalLink": "Liên kết ngoài",
|
|
187
|
+
"formField.name.label": "Tên ô (khoá dữ liệu)",
|
|
188
|
+
"formField.name.help": "Chữ thường không dấu, số, gạch dưới; dùng trong bài gửi và xuất CSV.",
|
|
189
|
+
"formField.label.label": "Nhãn",
|
|
190
|
+
"formField.label.help": "Ô \"Đồng ý\": gắn link bằng [chữ](đường dẫn), vd Tôi đồng ý với [điều khoản](/dieu-khoan) và [chính sách bảo mật](/chinh-sach-bao-mat).",
|
|
191
|
+
"formField.width.opt.full": "Cả hàng",
|
|
192
|
+
"formField.width.opt.half": "Nửa hàng",
|
|
193
|
+
"form.notifyEmail.help": "Để trống: gửi về email quản trị của website.",
|
|
194
|
+
"formField.kind.opt.text": "Ô chữ",
|
|
195
|
+
"formField.kind.opt.email": "Email",
|
|
196
|
+
"formField.kind.opt.tel": "Điện thoại",
|
|
197
|
+
"formField.kind.opt.textarea": "Nội dung dài",
|
|
198
|
+
"formField.kind.opt.select": "Chọn trong danh sách",
|
|
199
|
+
"formField.kind.opt.radio": "Chọn một",
|
|
200
|
+
"formField.kind.opt.checkbox": "Ô đánh dấu",
|
|
201
|
+
"formField.kind.opt.consent": "Đồng ý (điều khoản)",
|
|
202
|
+
"formField.kind.opt.number": "Số",
|
|
203
|
+
"formField.kind.opt.date": "Ngày",
|
|
187
204
|
};
|
|
@@ -137,7 +137,7 @@ export const patternLib = {
|
|
|
137
137
|
logos: "Dải đối tác",
|
|
138
138
|
logosD: "Hàng logo khách hàng / đối tác",
|
|
139
139
|
contact_split: "Liên hệ chia đôi",
|
|
140
|
-
contact_splitD: "Thông tin
|
|
140
|
+
contact_splitD: "Thông tin và mạng xã hội bên trái, form bên phải",
|
|
141
141
|
contact_cards: "3 thẻ liên hệ",
|
|
142
142
|
contact_cardsD: "Địa chỉ, điện thoại, email",
|
|
143
143
|
text_cols: "Hai cột chữ",
|
|
@@ -148,4 +148,18 @@ export const patternLib = {
|
|
|
148
148
|
footer_simpleD: "Bản quyền và mạng xã hội",
|
|
149
149
|
footer_cols: "Chân trang 3 cột",
|
|
150
150
|
footer_colsD: "3 nhóm liên kết + bản quyền",
|
|
151
|
+
fSend: "Gửi liên hệ",
|
|
152
|
+
fThanks: "Cảm ơn bạn! Chúng tôi sẽ liên hệ lại sớm.",
|
|
153
|
+
fName: "Họ và tên",
|
|
154
|
+
fPhone: "Số điện thoại",
|
|
155
|
+
fEmail: "Email",
|
|
156
|
+
fTopic: "Bạn cần gì?",
|
|
157
|
+
topic1: "Tư vấn",
|
|
158
|
+
topic2: "Báo giá",
|
|
159
|
+
topic3: "Khác",
|
|
160
|
+
fMessage: "Nội dung",
|
|
161
|
+
fMessageP: "Mô tả ngắn nhu cầu của bạn…",
|
|
162
|
+
fConsent: "Tôi đồng ý để được liên hệ lại qua điện thoại / email.",
|
|
163
|
+
contact_form: "Liên hệ có form",
|
|
164
|
+
contact_formD: "Thông tin liên hệ bên trái, form gửi tin nhắn bên phải",
|
|
151
165
|
};
|
|
@@ -39,7 +39,12 @@ export const patterns = {
|
|
|
39
39
|
introTitle: "Về chúng tôi",
|
|
40
40
|
introText: "Giới thiệu ngắn về công ty, sứ mệnh và giá trị.",
|
|
41
41
|
contact: "Liên hệ",
|
|
42
|
-
contactD: "
|
|
42
|
+
contactD: "Tiêu đề, giới thiệu ngắn và form liên hệ",
|
|
43
43
|
contactTitle: "Liên hệ với chúng tôi",
|
|
44
44
|
contactText: "Địa chỉ · Số điện thoại · Email",
|
|
45
|
+
send: "Gửi",
|
|
46
|
+
thanks: "Cảm ơn bạn! Chúng tôi sẽ liên hệ lại sớm.",
|
|
47
|
+
name: "Họ và tên",
|
|
48
|
+
email: "Email",
|
|
49
|
+
message: "Nội dung",
|
|
45
50
|
};
|
|
@@ -42,6 +42,18 @@ function library(t: L): Pattern[] {
|
|
|
42
42
|
const member = (): Node => card([img(t.member), h(3, t.member), muted(t.role), p(t.bio)], { padding: { base: "lg" }, textAlign: { base: "center" } });
|
|
43
43
|
const info = (label: string, value: string): Node => card([h(3, label), p(value)], { padding: { base: "lg" } });
|
|
44
44
|
const links = (title: string, items: string[]): Node => ({ type: "column", content: [h(4, title), { type: "bulletList", content: items.map((s) => ({ type: "listItem", content: [{ type: "paragraph", content: [link(s)] }] })) }] });
|
|
45
|
+
// Form cơ bản của lõi (khối form): formId để trống — trình soạn tạo mã khi mẫu được chèn
|
|
46
|
+
const fld = (kind: string, name: string, label: string, extra: Record<string, unknown> = {}): Node => ({ type: "formField", attrs: { kind, name, label, ...extra } });
|
|
47
|
+
const contactForm = (topics = false): Node => ({ type: "form", attrs: { submitText: t.fSend, successMessage: t.fThanks }, content: [
|
|
48
|
+
fld("text", "name", t.fName, { required: true, width: "half" }), fld("tel", "phone", t.fPhone, { required: true, width: "half" }),
|
|
49
|
+
fld("email", "email", t.fEmail),
|
|
50
|
+
...(topics ? [fld("select", "topic", t.fTopic, { options: [t.topic1, t.topic2, t.topic3] })] : []),
|
|
51
|
+
fld("textarea", "message", t.fMessage, { required: true, placeholder: t.fMessageP }),
|
|
52
|
+
fld("consent", "consent", t.fConsent, { required: true }),
|
|
53
|
+
] });
|
|
54
|
+
const infoRow = (icon: string, label: string, value: string): Node => ({ type: "group", attrs: { style: { layout: { type: "flex", direction: { base: "row" }, align: { base: "start" }, wrap: false }, gap: "md" } }, content: [
|
|
55
|
+
{ type: "icon", attrs: { name: icon, size: 22, style: { color: { text: "primary" } } } },
|
|
56
|
+
{ type: "group", content: [p(label, { style: { fontWeight: "600", margin: { base: "0" } } }), muted(value, { margin: { base: "0" } })] }] });
|
|
45
57
|
const m = (id: string, category: string, keywords: string, content: Node[]): Pattern => ({ name: `core/${id}`, category, title: t[id.replace(/-/g, "_")], description: t[`${id.replace(/-/g, "_")}D`], keywords, content });
|
|
46
58
|
|
|
47
59
|
return [
|
|
@@ -127,10 +139,18 @@ function library(t: L): Pattern[] {
|
|
|
127
139
|
section([muted(t.trusted, { ...center, textTransform: "uppercase", letterSpacing: "0.08em", fontSize: { base: "sm" } }), grid(5, [1, 2, 3, 4, 5].map(() => img(t.logo)), { motion: { in: "fade", stagger: true } })])]),
|
|
128
140
|
|
|
129
141
|
// ---------------- contact ----------------
|
|
142
|
+
m("contact-form", "contact", "lien he form gui tin nhan contact form", [
|
|
143
|
+
section([{ type: "columns", attrs: { verticalAlign: "top" }, content: [
|
|
144
|
+
{ type: "column", attrs: { width: 40 }, content: [eyebrow(t.l_contact), h(2, t.contactTitle), muted(t.contactSub, { fontSize: { base: "lg" } }), { type: "spacer", attrs: { height: "sm" } },
|
|
145
|
+
{ type: "group", attrs: { style: { layout: { type: "flex", direction: { base: "column" } }, gap: "lg" } }, content: [
|
|
146
|
+
infoRow("phone", t.phone, t.phoneV), infoRow("mail", t.email, t.emailV), infoRow("mapPin", t.address, t.addressV), infoRow("clock", t.hours, t.hoursV)] }] },
|
|
147
|
+
{ type: "column", attrs: { width: 60 }, content: [card([contactForm(true)], { padding: { base: "xl", mobile: "lg" }, shadow: "md", motion: undefined })] }] }],
|
|
148
|
+
{ motion: { in: "fade" } })]),
|
|
130
149
|
m("contact-split", "contact", "lien he chia doi contact split", [
|
|
131
150
|
section([{ type: "columns", content: [
|
|
132
|
-
{ type: "column", content: [h(2, t.contactTitle), muted(t.contactSub), p(`${t.address}: ${t.addressV}`), p(`${t.phone}: ${t.phoneV}`), p(`${t.email}: ${t.emailV}`)
|
|
133
|
-
|
|
151
|
+
{ type: "column", content: [h(2, t.contactTitle), muted(t.contactSub), p(`${t.address}: ${t.addressV}`), p(`${t.phone}: ${t.phoneV}`), p(`${t.email}: ${t.emailV}`),
|
|
152
|
+
{ type: "socialLinks", attrs: { links: [{ service: "facebook", url: "https://facebook.com" }, { service: "zalo", url: "https://zalo.me" }, { service: "email", url: "mailto:hello@example.com" }] } }] },
|
|
153
|
+
{ type: "column", content: [contactForm()] }] }])]),
|
|
134
154
|
m("contact-cards", "contact", "the lien he dia chi dien thoai email contact cards", [
|
|
135
155
|
section([h(2, t.contactTitle, center), muted(t.contactSub, center), { type: "spacer", attrs: { height: "md" } }, grid(3, [info(t.address, t.addressV), info(t.phone, t.phoneV), info(t.email, t.emailV)], slideUp())])]),
|
|
136
156
|
|
package/src/shared/patterns.ts
CHANGED
|
@@ -73,9 +73,12 @@ function corePatterns(t: L): Pattern[] {
|
|
|
73
73
|
{ type: "pullquote", attrs: { citation: t.who }, content: [text(t.quote)] }] },
|
|
74
74
|
{ name: "core/intro", category: "text", title: t.intro, description: t.introD, keywords: "about gioi thieu media", content: [
|
|
75
75
|
{ type: "mediaText", attrs: { position: "left", width: 50, verticalAlign: "center" }, content: [h(2, t.introTitle), p(t.introText), buttons("left", btn(t.learn, "outline"))] }] },
|
|
76
|
-
{ name: "core/contact", category: "contact", title: t.contact, description: t.contactD, keywords: "contact lien he", content: [
|
|
76
|
+
{ name: "core/contact", category: "contact", title: t.contact, description: t.contactD, keywords: "contact lien he form", content: [
|
|
77
77
|
h(2, t.contactTitle), p(t.contactText),
|
|
78
|
-
{ type: "
|
|
78
|
+
{ type: "form", attrs: { submitText: t.send, successMessage: t.thanks }, content: [
|
|
79
|
+
{ type: "formField", attrs: { kind: "text", name: "name", label: t.name, required: true, width: "half" } },
|
|
80
|
+
{ type: "formField", attrs: { kind: "email", name: "email", label: t.email, required: true, width: "half" } },
|
|
81
|
+
{ type: "formField", attrs: { kind: "textarea", name: "message", label: t.message, required: true } }] }] },
|
|
79
82
|
];
|
|
80
83
|
}
|
|
81
84
|
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { CORE_NODES, FORM_FIELD_NAME, blockShell } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { ClipboardList, TextCursorInput } from "lucide-react";
|
|
3
|
+
import { useEffect, type CSSProperties } from "react";
|
|
4
|
+
import { InnerBlocks } from "../canvas/block-list";
|
|
5
|
+
import { makeBlock } from "../core/model";
|
|
6
|
+
import type { BlockEditProps, BlockType } from "../core/registry";
|
|
7
|
+
import { useMsgs } from "../i18n";
|
|
8
|
+
import { messages } from "../../i18n";
|
|
9
|
+
import { RichText } from "../text/rich-text";
|
|
10
|
+
import { Field } from "../ui/attr-form";
|
|
11
|
+
import { useEditor } from "../context";
|
|
12
|
+
|
|
13
|
+
const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
|
|
14
|
+
const merge = (bp: BlockEditProps["blockProps"], className: string, style?: CSSProperties) => ({ ...bp, className: cx(bp.className, className), style: { ...(bp.style ?? {}), ...style } });
|
|
15
|
+
/** Mã form: máy chủ tìm lại định nghĩa form (các ô) theo mã này trong trang / template chứa nó. */
|
|
16
|
+
export const newFormId = () => `f_${Math.random().toString(36).slice(2, 10)}`;
|
|
17
|
+
const optionsOf = (a: Record<string, unknown>): string[] => (Array.isArray(a.options) ? a.options.filter((x): x is string => typeof x === "string") : []);
|
|
18
|
+
|
|
19
|
+
/** Tên ô hợp lệ (khoá trong bài gửi) từ nhãn: bỏ dấu, chữ thường, gạch dưới. */
|
|
20
|
+
export function fieldNameFrom(label: string, taken: string[] = []): string {
|
|
21
|
+
const base = label.normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/đ/gi, "d").toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").replace(/^(\d)/, "f_$1").slice(0, 32) || "field";
|
|
22
|
+
let name = FORM_FIELD_NAME.test(base) ? base : "field", n = 2;
|
|
23
|
+
while (taken.includes(name)) name = `${base}_${n++}`;
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function FormEdit({ block, blockProps, setAttrs, editable }: BlockEditProps) {
|
|
28
|
+
const t = useMsgs("blocks");
|
|
29
|
+
const a = block.attrs;
|
|
30
|
+
// Mẫu (pattern) chèn vào chưa có mã: tạo một lần để máy chủ nhận ra form này
|
|
31
|
+
useEffect(() => { if (editable && !a.formId) setAttrs({ formId: newFormId() }); }, [editable, a.formId, setAttrs]);
|
|
32
|
+
return (
|
|
33
|
+
<div {...merge(blockProps, "ecx-form-blocks")} data-form-id={String(a.formId ?? "")}>
|
|
34
|
+
<InnerBlocks parent={block} />
|
|
35
|
+
<div className="ecx-form-actions">
|
|
36
|
+
<span className={blockShell("button", { variant: "primary" })?.className} style={blockShell("button", { variant: "primary" })?.style as CSSProperties} role="presentation">
|
|
37
|
+
<RichText blockId={block.id} field="submitText" tagName="span" value={String(a.submitText ?? "")} onChange={(v) => setAttrs({ submitText: v })} placeholder={t.formSubmit} placeholderAlways editable={editable} />
|
|
38
|
+
</span>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Ô nhập trên khung soạn: đúng markup website (nhãn + ô), ô bị khoá — sửa nhãn ngay trên ô, phần còn lại ở bảng cài đặt. */
|
|
45
|
+
function FieldEdit({ block, blockProps, setAttrs, editable }: BlockEditProps) {
|
|
46
|
+
const t = useMsgs("blocks");
|
|
47
|
+
const { store } = useEditor();
|
|
48
|
+
const a = block.attrs;
|
|
49
|
+
// Tên ô = khoá trong bài gửi: phải hợp lệ và không trùng ô đứng trước trong cùng form (chèn hai ô cùng loại, chép / dán ô)
|
|
50
|
+
const siblings = (store.locate(block.id)?.parent?.content ?? []).filter((b) => b.type === "formField");
|
|
51
|
+
const sibKey = siblings.map((b) => `${b.id}:${String(b.attrs.name ?? "")}`).join("|");
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!editable) return;
|
|
54
|
+
const name = String(a.name ?? "");
|
|
55
|
+
const before = siblings.slice(0, siblings.findIndex((b) => b.id === block.id)).map((b) => String(b.attrs.name ?? ""));
|
|
56
|
+
if (!FORM_FIELD_NAME.test(name) || before.includes(name)) setAttrs({ name: fieldNameFrom(name || String(a.label ?? ""), siblings.filter((b) => b.id !== block.id).map((b) => String(b.attrs.name ?? ""))) });
|
|
57
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
58
|
+
}, [editable, a.name, sibKey]);
|
|
59
|
+
const kind = String(a.kind ?? "text");
|
|
60
|
+
const label = <RichText blockId={block.id} field="label" tagName="span" value={String(a.label ?? "")} onChange={(v) => setAttrs({ label: v })} placeholder={t.fieldLabel} placeholderAlways editable={editable} />;
|
|
61
|
+
const star = a.required ? <span className="ecx-field-required" aria-hidden> *</span> : null;
|
|
62
|
+
const opts = optionsOf(a);
|
|
63
|
+
const ph = String(a.placeholder ?? "");
|
|
64
|
+
const check = kind === "checkbox" || kind === "consent";
|
|
65
|
+
return (
|
|
66
|
+
<div {...merge(blockProps, cx("ecx-field", `is-${kind}`, a.width === "half" && "is-half", check && "is-check"))}>
|
|
67
|
+
{check ? <label><input type="checkbox" disabled tabIndex={-1} /> <span>{label}{star}</span></label> : <label>{label}{star}</label>}
|
|
68
|
+
{kind === "textarea" ? <textarea disabled tabIndex={-1} placeholder={ph} />
|
|
69
|
+
: kind === "select" ? <select disabled tabIndex={-1}><option>{opts[0] ?? ph ?? "—"}</option></select>
|
|
70
|
+
: kind === "radio" ? <div className="ecx-field-options">{(opts.length ? opts : ["…"]).map((o, i) => <label key={i}><input type="radio" disabled tabIndex={-1} /> {o}</label>)}</div>
|
|
71
|
+
: check ? null
|
|
72
|
+
: <input disabled tabIndex={-1} placeholder={ph} type={kind === "number" ? "number" : kind === "date" ? "date" : "text"} />}
|
|
73
|
+
{a.help ? <span className="ecx-field-help">{String(a.help)}</span> : null}
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Bảng cài đặt thêm của ô: danh sách lựa chọn (mỗi dòng một lựa chọn) cho ô chọn / chọn một, và tên ô (khoá trong bài gửi). */
|
|
79
|
+
function FieldInspector({ attrs, setAttrs }: BlockEditProps) {
|
|
80
|
+
const t = useMsgs("blocks");
|
|
81
|
+
const kind = String(attrs.kind ?? "text");
|
|
82
|
+
const name = String(attrs.name ?? "");
|
|
83
|
+
return (
|
|
84
|
+
<>
|
|
85
|
+
{(kind === "select" || kind === "radio") && (
|
|
86
|
+
<Field label={t.fieldOptions}>
|
|
87
|
+
<textarea className="ecx-input" rows={4} value={optionsOf(attrs).join("\n")} data-field-options
|
|
88
|
+
onChange={(e) => setAttrs({ options: e.target.value.split("\n").map((s) => s.trim()).filter(Boolean).slice(0, 30) })} />
|
|
89
|
+
</Field>
|
|
90
|
+
)}
|
|
91
|
+
{name && !FORM_FIELD_NAME.test(name) && <p className="ecx-muted" role="alert">{t.fieldNameInvalid}</p>}
|
|
92
|
+
</>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const spec = (name: string) => CORE_NODES[name]?.attrs;
|
|
97
|
+
|
|
98
|
+
/** Khối form cơ bản; `lang` = ngôn ngữ của trình soạn (nhãn mặc định của ô mới). */
|
|
99
|
+
export function formBlocks(lang?: string): BlockType[] {
|
|
100
|
+
const m = messages("blocks", lang) as Record<string, string>;
|
|
101
|
+
const field = (kind: string, name: string, label: string, extra: Record<string, unknown> = {}) => makeBlock("formField", { kind, name, label, ...extra });
|
|
102
|
+
const KINDS: Array<[string, string, string, Record<string, unknown>?]> = [
|
|
103
|
+
["text", "text", m.fieldText], ["email", "email", m.fieldEmail], ["tel", "phone", m.fieldPhone], ["textarea", "message", m.fieldMessage],
|
|
104
|
+
["select", "topic", m.fieldSelect, { options: [m.option1, m.option2] }], ["radio", "choice", m.fieldRadio, { options: [m.option1, m.option2] }],
|
|
105
|
+
["checkbox", "subscribe", m.fieldCheckbox], ["consent", "consent", m.fieldConsent, { required: true }], ["number", "quantity", m.fieldNumber], ["date", "date", m.fieldDate],
|
|
106
|
+
];
|
|
107
|
+
return [
|
|
108
|
+
{
|
|
109
|
+
name: "form", title: "Form", icon: ClipboardList, category: "widgets", keywords: "form lien he contact dang ky gui bieu mau", kind: "container", edit: FormEdit,
|
|
110
|
+
attributes: { ...spec("form"), formId: { type: "string" } },
|
|
111
|
+
structure: { allowedChildren: ["formField", "paragraph", "heading"] },
|
|
112
|
+
create: () => makeBlock("form", { formId: newFormId() }, [
|
|
113
|
+
field("text", "name", m.fieldName, { required: true, width: "half" }),
|
|
114
|
+
field("tel", "phone", m.fieldPhone, { required: true, width: "half" }),
|
|
115
|
+
field("email", "email", m.fieldEmail),
|
|
116
|
+
field("textarea", "message", m.fieldMessage, { required: true }),
|
|
117
|
+
]),
|
|
118
|
+
supports: { style: true, anchor: true, className: true },
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "formField", title: "Ô nhập", icon: TextCursorInput, category: "widgets", keywords: "o nhap truong field input", kind: "atom", edit: FieldEdit, inspector: FieldInspector,
|
|
122
|
+
// Chỉ hiện trong trình chèn khi đang ở trong một form (structure.parents)
|
|
123
|
+
attributes: spec("formField"), structure: { parents: ["form"] },
|
|
124
|
+
create: (attrs) => field("text", "text", m.fieldText, attrs ?? {}),
|
|
125
|
+
variations: KINDS.map(([kind, name, title, extra]) => ({ name: kind, title, attrs: { kind }, create: () => field(kind, name, title, extra), isActive: (a) => (a.kind ?? "text") === kind })),
|
|
126
|
+
supports: { style: true, className: true }, summary: (a) => a.label,
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
}
|
package/src/v2/blocks/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { layoutBlocks } from "./layout";
|
|
|
5
5
|
import { mediaBlocks } from "./media";
|
|
6
6
|
import { textBlocks } from "./text";
|
|
7
7
|
import { themeBlocks } from "./theme";
|
|
8
|
+
import { formBlocks } from "./form";
|
|
8
9
|
|
|
9
10
|
/** Tên khối theo ngôn ngữ giao diện (khối có nhãn trong bộ chữ). */
|
|
10
11
|
const TITLE_KEYS: Partial<Record<string, keyof V2Labels>> = {
|
|
@@ -16,7 +17,7 @@ const TITLE_KEYS: Partial<Record<string, keyof V2Labels>> = {
|
|
|
16
17
|
|
|
17
18
|
export function coreBlockTypes(labels: V2Labels): BlockType[] {
|
|
18
19
|
const names = messages("blockTitles", labels.lang);
|
|
19
|
-
return [...textBlocks(), ...layoutBlocks(), ...mediaBlocks(), ...themeBlocks()].map((t) => {
|
|
20
|
+
return [...textBlocks(), ...layoutBlocks(), ...mediaBlocks(), ...formBlocks(labels.lang), ...themeBlocks()].map((t) => {
|
|
20
21
|
const key = TITLE_KEYS[t.name];
|
|
21
22
|
const str = (v: unknown) => (typeof v === "string" ? v : undefined);
|
|
22
23
|
const title = key ? String(labels[key]) : str(names[t.name]) ?? t.title;
|
package/src/v2/ui/attr-hints.ts
CHANGED
|
@@ -95,6 +95,13 @@ export const ATTR_HINTS: Record<string, AttrHint> = {
|
|
|
95
95
|
"gallery.imageCrop": { label: true },
|
|
96
96
|
"gallery.lightbox": { label: true },
|
|
97
97
|
"countdown.expiredText": { label: true },
|
|
98
|
+
// 1.27: form cơ bản
|
|
99
|
+
"form.formId": { control: "hidden" },
|
|
100
|
+
"form.notifyEmail": { help: true },
|
|
101
|
+
"formField.kind": { control: "select", options: ["text", "email", "tel", "textarea", "select", "radio", "checkbox", "consent", "number", "date"] },
|
|
102
|
+
"formField.name": { label: true, help: true },
|
|
103
|
+
"formField.label": { label: true, help: true },
|
|
104
|
+
"formField.width": { control: "segmented", options: ["full", "half"] },
|
|
98
105
|
"search.showLabel": { label: true },
|
|
99
106
|
"search.buttonPosition": { control: "segmented", label: true, options: ["right", "left", "below"] },
|
|
100
107
|
"postAuthor.showName": { label: true },
|