@ecomcx/ecomweb-theme-sdk 1.38.5 → 1.39.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/dist/client.d.ts +22 -1
- package/dist/client.js +54 -0
- package/dist/i18n.js +2 -2
- package/dist/rich.d.ts +2 -0
- package/dist/rich.js +50 -1
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Component } from "react";
|
|
2
2
|
import type { Comment } from "./types.js";
|
|
3
|
-
import type { AnchorHTMLAttributes, ReactNode } from "react";
|
|
3
|
+
import type { AnchorHTMLAttributes, CSSProperties, ReactNode } from "react";
|
|
4
4
|
export { pluginClient, PluginApiError, emit, on, useEvent, TranslationsProvider, useT, type PluginClient, type PluginRequestOptions } from "./browser.js";
|
|
5
5
|
/**
|
|
6
6
|
* Bảng tên sự kiện phía trình duyệt → dữ liệu (emit / on / useEvent, core ≥ 1.20.0). Theme / plugin khai báo kiểu cho sự kiện của mình:
|
|
@@ -142,3 +142,24 @@ export declare function LightboxClient({ src, alt, children }: {
|
|
|
142
142
|
alt: string;
|
|
143
143
|
children: ReactNode;
|
|
144
144
|
}): import("react").JSX.Element;
|
|
145
|
+
/**
|
|
146
|
+
* Khối form cơ bản của lõi (1.39): các ô đã vẽ sẵn ở máy chủ (children), phần này gửi bài qua /api/cms/forms, báo lỗi đúng ô (422),
|
|
147
|
+
* hiện lời cảm ơn. Chống spam: ô bẫy ẩn (_hp) + thời gian điền (máy chủ kiểm), máy chủ còn giới hạn theo IP.
|
|
148
|
+
*/
|
|
149
|
+
export declare function FormBlockClient({ formId, source, successMessage, labels, button, className, children }: {
|
|
150
|
+
formId: string;
|
|
151
|
+
source: string;
|
|
152
|
+
successMessage?: string;
|
|
153
|
+
labels: {
|
|
154
|
+
submit: string;
|
|
155
|
+
sending: string;
|
|
156
|
+
error: string;
|
|
157
|
+
};
|
|
158
|
+
/** Vỏ nút gửi = nút "primary" của nội dung (cùng màu / bo góc với nút của theme). */
|
|
159
|
+
button?: {
|
|
160
|
+
className?: string;
|
|
161
|
+
style?: CSSProperties;
|
|
162
|
+
};
|
|
163
|
+
className?: string;
|
|
164
|
+
children?: ReactNode;
|
|
165
|
+
}): import("react").JSX.Element;
|
package/dist/client.js
CHANGED
|
@@ -247,3 +247,57 @@ export function LightboxClient({ src, alt, children }) {
|
|
|
247
247
|
}, [open]);
|
|
248
248
|
return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "ecx-lightbox-trigger", "aria-haspopup": "dialog", onClick: () => setOpen(true), style: { all: "unset", display: "block", cursor: "zoom-in" }, children: children }), open && (_jsx("div", { role: "dialog", "aria-modal": "true", "aria-label": alt || "Image", className: "ecx-lightbox", onClick: () => setOpen(false), style: { position: "fixed", inset: 0, zIndex: 9999, background: "rgba(0,0,0,.85)", display: "flex", alignItems: "center", justifyContent: "center", cursor: "zoom-out" }, children: _jsx("img", { src: src, alt: alt, style: { maxWidth: "95vw", maxHeight: "95vh", objectFit: "contain" } }) }))] }));
|
|
249
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Khối form cơ bản của lõi (1.39): các ô đã vẽ sẵn ở máy chủ (children), phần này gửi bài qua /api/cms/forms, báo lỗi đúng ô (422),
|
|
252
|
+
* hiện lời cảm ơn. Chống spam: ô bẫy ẩn (_hp) + thời gian điền (máy chủ kiểm), máy chủ còn giới hạn theo IP.
|
|
253
|
+
*/
|
|
254
|
+
export function FormBlockClient({ formId, source, successMessage, labels, button, className, children }) {
|
|
255
|
+
const started = useRef(Date.now());
|
|
256
|
+
const [state, setState] = useState({ status: "idle" });
|
|
257
|
+
function showErrors(form, errors) {
|
|
258
|
+
form.querySelectorAll("[data-field]").forEach((el) => {
|
|
259
|
+
const msg = errors[el.dataset.field ?? ""]?.join(" ") ?? "";
|
|
260
|
+
const out = el.querySelector(".ecx-field-error");
|
|
261
|
+
if (out)
|
|
262
|
+
out.textContent = msg;
|
|
263
|
+
el.querySelectorAll("input, textarea, select").forEach((i) => { if (msg)
|
|
264
|
+
i.setAttribute("aria-invalid", "true");
|
|
265
|
+
else
|
|
266
|
+
i.removeAttribute("aria-invalid"); });
|
|
267
|
+
});
|
|
268
|
+
const first = Object.keys(errors)[0];
|
|
269
|
+
if (first)
|
|
270
|
+
form.querySelector(`[data-field="${CSS.escape(first)}"] input, [data-field="${CSS.escape(first)}"] textarea, [data-field="${CSS.escape(first)}"] select`)?.focus();
|
|
271
|
+
}
|
|
272
|
+
async function submit(e) {
|
|
273
|
+
e.preventDefault();
|
|
274
|
+
const form = e.currentTarget;
|
|
275
|
+
const data = new FormData(form);
|
|
276
|
+
const fields = {};
|
|
277
|
+
form.querySelectorAll("[data-field]").forEach((el) => {
|
|
278
|
+
const name = el.dataset.field;
|
|
279
|
+
const box = el.querySelector("input[type=checkbox]");
|
|
280
|
+
fields[name] = box ? box.checked : String(data.get(name) ?? "");
|
|
281
|
+
});
|
|
282
|
+
setState({ status: "sending" });
|
|
283
|
+
showErrors(form, {});
|
|
284
|
+
try {
|
|
285
|
+
const res = await fetch("/api/cms/forms", { method: "POST", headers: { "content-type": "application/json" },
|
|
286
|
+
body: JSON.stringify({ formId, source, fields, hp: String(data.get("_hp") ?? ""), ms: Date.now() - started.current, page: location.pathname }) });
|
|
287
|
+
const json = await res.json().catch(() => ({}));
|
|
288
|
+
if (!res.ok) {
|
|
289
|
+
showErrors(form, json.errors ?? {});
|
|
290
|
+
setState({ status: "error", message: json.title ?? labels.error });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
form.reset();
|
|
294
|
+
setState({ status: "done", message: json.message || successMessage || "" });
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
setState({ status: "error", message: labels.error });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (state.status === "done")
|
|
301
|
+
return _jsx("p", { className: "ecx-form-status is-success", role: "status", "data-form-done": formId, children: state.message });
|
|
302
|
+
return (_jsxs("form", { className: cx("ecx-form-blocks", className), onSubmit: submit, noValidate: true, "data-form": formId, children: [children, _jsx("p", { className: "ecx-form-hp", "aria-hidden": "true", children: _jsxs("label", { children: ["Website", _jsx("input", { type: "text", name: "_hp", tabIndex: -1, autoComplete: "off" })] }) }), state.status === "error" && _jsx("p", { className: "ecx-form-status is-error", role: "alert", children: state.message }), _jsx("div", { className: "ecx-form-actions", children: _jsx("button", { type: "submit", className: button?.className ?? "ecx-button", style: { border: 0, cursor: "pointer", font: "inherit", ...button?.style }, disabled: state.status === "sending", children: state.status === "sending" ? labels.sending : labels.submit }) })] }));
|
|
303
|
+
}
|
package/dist/i18n.js
CHANGED
|
@@ -23,7 +23,7 @@ export const BUILTIN = {
|
|
|
23
23
|
"auth.noAccount": "Chưa có tài khoản?", "auth.register": "Đăng ký", "auth.forgot": "Quên mật khẩu?", "auth.haveAccount": "Đã có tài khoản?",
|
|
24
24
|
"auth.login2": "Đăng nhập", "auth.registerClosed": "Website hiện không mở đăng ký thành viên.",
|
|
25
25
|
"auth.missingToken": "Liên kết không hợp lệ — hãy yêu cầu đặt lại mật khẩu lần nữa.", "auth.signedInAs": "Bạn đang đăng nhập với tên {name}.",
|
|
26
|
-
"file.download": "Tải tệp", "form.submit": "Gửi", "form.sending": "Đang gửi…", "form.error": "Không gửi được, vui lòng thử lại",
|
|
26
|
+
"file.download": "Tải tệp", "form.submit": "Gửi", "form.choose": "Chọn…", "form.sending": "Đang gửi…", "form.error": "Không gửi được, vui lòng thử lại",
|
|
27
27
|
"auth.bio": "Giới thiệu", "auth.currentPassword": "Mật khẩu hiện tại", "auth.currentPasswordForEmail": "Mật khẩu hiện tại (cần khi đổi email)",
|
|
28
28
|
"auth.submitProfile": "Lưu hồ sơ", "auth.submitPassword": "Đổi mật khẩu", "auth.notSignedIn": "Bạn cần đăng nhập để xem trang này.",
|
|
29
29
|
"auth.deleteWarning": "Xoá tài khoản là vĩnh viễn: bạn không đăng nhập được nữa, bài viết của bạn không còn tác giả.",
|
|
@@ -49,7 +49,7 @@ export const BUILTIN = {
|
|
|
49
49
|
"auth.noAccount": "No account yet?", "auth.register": "Register", "auth.forgot": "Forgot password?", "auth.haveAccount": "Already have an account?",
|
|
50
50
|
"auth.login2": "Log in", "auth.registerClosed": "Registration is currently closed.",
|
|
51
51
|
"auth.missingToken": "Invalid link — please request a new password reset.", "auth.signedInAs": "You are logged in as {name}.",
|
|
52
|
-
"file.download": "Download", "form.submit": "Send", "form.sending": "Sending…", "form.error": "Could not send, please try again",
|
|
52
|
+
"file.download": "Download", "form.submit": "Send", "form.choose": "Choose…", "form.sending": "Sending…", "form.error": "Could not send, please try again",
|
|
53
53
|
"auth.bio": "Bio", "auth.currentPassword": "Current password", "auth.currentPasswordForEmail": "Current password (needed to change your email)",
|
|
54
54
|
"auth.submitProfile": "Save profile", "auth.submitPassword": "Change password", "auth.notSignedIn": "Please log in to see this page.",
|
|
55
55
|
"auth.deleteWarning": "Deleting your account is permanent: you can no longer log in and your posts will have no author.",
|
package/dist/rich.d.ts
CHANGED
|
@@ -71,6 +71,8 @@ export interface RenderContext {
|
|
|
71
71
|
source?: string;
|
|
72
72
|
/** Khoá của khối richtext đang vẽ — đường dẫn khối = phần khoá sau nó (do renderBlock đặt). */
|
|
73
73
|
pathRoot?: string;
|
|
74
|
+
/** Mã của khối form đang vẽ (ô formField bên trong dùng làm tiền tố id). */
|
|
75
|
+
form?: string;
|
|
74
76
|
}
|
|
75
77
|
type Node = Omit<RichNode, "attrs" | "content" | "marks"> & {
|
|
76
78
|
attrs?: Record<string, any>;
|
package/dist/rich.js
CHANGED
|
@@ -4,7 +4,7 @@ import { ICON_PATHS } from "./icons.js";
|
|
|
4
4
|
import { DYNAMIC_NODES, widthClass, blockCitation, blockShell, buttonWrapShell, coverParts, linkRel, embedInfo, motionClasses, normalizeContent, safeUrl, styleClass, styleCss } from "@ecomcx/ecomweb-content";
|
|
5
5
|
import { cx } from "./utils.js";
|
|
6
6
|
import { SOCIAL_ICONS } from "./social-icons.js";
|
|
7
|
-
import { CountdownClient, LightboxClient, TabsClient } from "./client.js";
|
|
7
|
+
import { CountdownClient, FormBlockClient, LightboxClient, TabsClient } from "./client.js";
|
|
8
8
|
import { getT } from "./i18n.js";
|
|
9
9
|
const DYNAMIC = new Set(DYNAMIC_NODES);
|
|
10
10
|
const SHORTCODE = /^\s*\[([a-z0-9_-]+)((?:\s+[a-z0-9_-]+=(?:"[^"]*"|'[^']*'|[^\s\]]+))*)\s*\]\s*$/i;
|
|
@@ -383,6 +383,37 @@ async function renderNodeInner(node, key, ctx) {
|
|
|
383
383
|
const units = { d: t("countdown.d"), h: t("countdown.h"), m: t("countdown.m"), s: t("countdown.s") }; // theo ngôn ngữ của trang (vi: ngày/giờ/phút/giây)
|
|
384
384
|
return _jsx(CountdownClient, { target: ms, serverNow: Date.now(), units: units, label: a.label ? String(a.label) : undefined, expiredText: a.expiredText ? String(a.expiredText) : undefined }, key);
|
|
385
385
|
}
|
|
386
|
+
case "form": {
|
|
387
|
+
// Form cơ bản của lõi: ô vẽ sẵn ở máy chủ, phần gửi chạy trên trình duyệt; máy chủ tìm lại định nghĩa form theo (source, formId)
|
|
388
|
+
const formId = typeof a.formId === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(a.formId) ? a.formId : "";
|
|
389
|
+
if (!formId || !ctx.source)
|
|
390
|
+
return null;
|
|
391
|
+
const t = await getT();
|
|
392
|
+
const inner = await Promise.all((node.content ?? []).map((c, i) => renderNode(c, `${key}-${i}`, { ...ctx, form: formId })));
|
|
393
|
+
return (_jsx(FormBlockClient, { formId: formId, source: ctx.source, successMessage: a.successMessage ? String(a.successMessage) : undefined, button: { className: blockShell("button", { variant: "primary" })?.className, style: blockShell("button", { variant: "primary" })?.style }, labels: { submit: a.submitText ? String(a.submitText) : t("form.submit"), sending: t("form.sending"), error: t("form.error") }, children: inner }, key));
|
|
394
|
+
}
|
|
395
|
+
case "formField": {
|
|
396
|
+
const name = typeof a.name === "string" && /^[a-z][a-z0-9_]{0,39}$/.test(a.name) ? a.name : "";
|
|
397
|
+
if (!name || !ctx.form)
|
|
398
|
+
return null;
|
|
399
|
+
const kind = String(a.kind ?? "text");
|
|
400
|
+
const id = `${ctx.form}-${name}`;
|
|
401
|
+
const label = String(a.label ?? name);
|
|
402
|
+
const required = a.required === true;
|
|
403
|
+
const options = Array.isArray(a.options) ? a.options.filter((o) => typeof o === "string").slice(0, 30) : [];
|
|
404
|
+
const describedBy = cx(a.help && `${id}-help`, `${id}-error`);
|
|
405
|
+
const star = required ? _jsx("span", { className: "ecx-field-required", "aria-hidden": "true", children: " *" }) : null;
|
|
406
|
+
const common = { id, name, required, "aria-describedby": describedBy, placeholder: a.placeholder ? String(a.placeholder) : undefined };
|
|
407
|
+
const check = kind === "checkbox" || kind === "consent";
|
|
408
|
+
const types = { email: "email", tel: "tel", number: "number", date: "date" };
|
|
409
|
+
const choose = kind === "select" && !a.placeholder ? (await getT())("form.choose") : String(a.placeholder ?? "");
|
|
410
|
+
return (_jsxs("div", { className: cx("ecx-field", `is-${kind}`, a.width === "half" && "is-half", check && "is-check"), "data-field": name, children: [check ? _jsxs("label", { htmlFor: id, children: [_jsx("input", { type: "checkbox", id: id, name: name, value: "1", required: required, "aria-describedby": describedBy }), " ", _jsxs("span", { children: [kind === "consent" ? linkedLabel(label) : label, star] })] })
|
|
411
|
+
: kind === "radio" ? (_jsxs("fieldset", { "aria-describedby": describedBy, children: [_jsxs("legend", { children: [label, star] }), _jsx("div", { className: "ecx-field-options", children: options.map((o, i) => _jsxs("label", { children: [_jsx("input", { type: "radio", name: name, value: o, required: required }), " ", o] }, i)) })] }))
|
|
412
|
+
: _jsxs("label", { htmlFor: id, children: [label, star] }), kind === "textarea" ? _jsx("textarea", { rows: 5, maxLength: 5000, ...common })
|
|
413
|
+
: kind === "select" ? _jsxs("select", { ...common, defaultValue: "", children: [_jsx("option", { value: "", disabled: required, children: choose }), options.map((o, i) => _jsx("option", { value: o, children: o }, i))] })
|
|
414
|
+
: check || kind === "radio" ? null
|
|
415
|
+
: _jsx("input", { type: types[kind] ?? "text", maxLength: kind === "number" || kind === "date" ? undefined : 500, autoComplete: { name: "name", email: "email", phone: "tel" }[name] ?? (kind === "email" ? "email" : kind === "tel" ? "tel" : undefined), ...common }), a.help ? _jsx("span", { className: "ecx-field-help", id: `${id}-help`, children: String(a.help) }) : null, _jsx("span", { className: "ecx-field-error", id: `${id}-error`, "aria-live": "polite" })] }, key));
|
|
416
|
+
}
|
|
386
417
|
case "rating": {
|
|
387
418
|
const max = Math.min(10, Math.max(1, Math.round(Number(a.max) || 5)));
|
|
388
419
|
const v = Math.min(max, Math.max(0, Number(a.value) || 0));
|
|
@@ -505,3 +536,21 @@ export async function renderContent(content, ctx, transform) {
|
|
|
505
536
|
out.unshift(_jsx("style", { "data-ecx-styles": "", children: [...ctx.styles.values()].join("") }, "ecx-styles"));
|
|
506
537
|
return out;
|
|
507
538
|
}
|
|
539
|
+
/**
|
|
540
|
+
* Nhãn ô đồng ý có link: "[chữ](đường-dẫn)" → <a> mở tab mới (khách không mất nội dung đang nhập).
|
|
541
|
+
* Chỉ nhận đường dẫn trong website ("/…") hoặc http(s); còn lại giữ chữ, bỏ link.
|
|
542
|
+
*/
|
|
543
|
+
function linkedLabel(text) {
|
|
544
|
+
const out = [];
|
|
545
|
+
let last = 0, i = 0;
|
|
546
|
+
for (const m of text.matchAll(/\[([^\]\n]{1,120})\]\(([^)\s]{1,500})\)/g)) {
|
|
547
|
+
if (m.index > last)
|
|
548
|
+
out.push(text.slice(last, m.index));
|
|
549
|
+
const href = m[2].startsWith("/") && !m[2].startsWith("//") || /^https?:\/\//i.test(m[2]) ? m[2] : null;
|
|
550
|
+
out.push(href ? _jsx("a", { href: href, target: "_blank", rel: "noopener noreferrer", children: m[1] }, i++) : m[1]);
|
|
551
|
+
last = m.index + m[0].length;
|
|
552
|
+
}
|
|
553
|
+
if (last < text.length)
|
|
554
|
+
out.push(text.slice(last));
|
|
555
|
+
return out;
|
|
556
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecomcx/ecomweb-theme-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.1",
|
|
4
4
|
"description": "SDK để viết theme và phần frontend của plugin cho Ecomweb",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"README.md"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ecomcx/ecomweb-content": "^1.
|
|
36
|
+
"@ecomcx/ecomweb-content": "^1.21.0",
|
|
37
37
|
"intl-messageformat": "12.1.3"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|