@ecomcx/ecomweb-editor 1.30.0 → 1.32.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 +10 -0
- package/src/locales/en/background.ts +17 -0
- package/src/locales/en/block-desc.ts +1 -0
- package/src/locales/en/block-style.ts +7 -4
- package/src/locales/en/block-titles.ts +1 -0
- package/src/locales/en/blocks.ts +10 -2
- package/src/locales/en/columns.ts +12 -0
- package/src/locales/en/controls.ts +21 -0
- package/src/locales/en/hints.ts +215 -1
- package/src/locales/en/index.ts +6 -2
- package/src/locales/en/layout.ts +32 -0
- package/src/locales/en/legacy.ts +1 -1
- package/src/locales/en/library.ts +38 -0
- package/src/locales/en/parts.ts +12 -0
- package/src/locales/en/placeholders.ts +104 -0
- package/src/locales/en/presets.ts +6 -0
- package/src/locales/en/v2.ts +21 -0
- package/src/locales/vi/attrs.ts +10 -0
- package/src/locales/vi/background.ts +17 -0
- package/src/locales/vi/block-desc.ts +1 -0
- package/src/locales/vi/block-style.ts +7 -4
- package/src/locales/vi/block-titles.ts +1 -0
- package/src/locales/vi/blocks.ts +10 -2
- package/src/locales/vi/columns.ts +12 -0
- package/src/locales/vi/controls.ts +21 -0
- package/src/locales/vi/hints.ts +216 -2
- package/src/locales/vi/index.ts +6 -2
- package/src/locales/vi/layout.ts +32 -0
- package/src/locales/vi/legacy.ts +1 -1
- package/src/locales/vi/library.ts +38 -0
- package/src/locales/vi/parts.ts +12 -0
- package/src/locales/vi/placeholders.ts +103 -0
- package/src/locales/vi/presets.ts +6 -0
- package/src/locales/vi/v2.ts +21 -0
- package/src/shared/panel-store.ts +3 -3
- package/src/shared/patterns.ts +3 -52
- package/src/shared/style-panel.tsx +2 -2
- package/src/shared/types.ts +2 -0
- package/src/v2/blocks/columns-inspector.tsx +210 -0
- package/src/v2/blocks/compat.tsx +91 -5
- package/src/v2/blocks/form.tsx +7 -4
- package/src/v2/blocks/index.ts +10 -4
- package/src/v2/blocks/inline-style.ts +1 -1
- package/src/v2/blocks/layout.tsx +305 -83
- package/src/v2/blocks/media.tsx +91 -81
- package/src/v2/blocks/resize.tsx +172 -4
- package/src/v2/blocks/text.tsx +60 -22
- package/src/v2/blocks/theme.tsx +83 -17
- package/src/v2/blocks/tools.tsx +50 -5
- package/src/v2/blocks/widgets.tsx +85 -20
- package/src/v2/canvas/block-list.tsx +21 -6
- package/src/v2/canvas/canvas-css.ts +139 -7
- package/src/v2/canvas/canvas.tsx +20 -3
- package/src/v2/canvas/dnd.ts +34 -3
- package/src/v2/canvas/move-animation.ts +94 -0
- package/src/v2/canvas/server-block.tsx +7 -4
- package/src/v2/canvas/spacing-viz.ts +51 -0
- package/src/v2/core/block-presets.ts +68 -0
- package/src/v2/core/host.ts +14 -0
- package/src/v2/core/registry.ts +3 -1
- package/src/v2/core/structure.ts +4 -2
- package/src/v2/editor.css +244 -11
- package/src/v2/editor.tsx +8 -2
- package/src/v2/index.ts +3 -0
- package/src/v2/ui/ai.tsx +9 -5
- package/src/v2/ui/attr-form.tsx +42 -13
- package/src/v2/ui/attr-hints.ts +104 -6
- package/src/v2/ui/background-panel.tsx +65 -21
- package/src/v2/ui/block-icons.tsx +152 -0
- package/src/v2/ui/block-menu.ts +22 -0
- package/src/v2/ui/block-style-panel.tsx +14 -3
- package/src/v2/ui/color-field.tsx +74 -9
- package/src/v2/ui/divider-panel.tsx +58 -0
- package/src/v2/ui/group-width.tsx +38 -0
- package/src/v2/ui/icon-picker.tsx +51 -0
- package/src/v2/ui/inserter.tsx +51 -7
- package/src/v2/ui/inspector.tsx +48 -16
- package/src/v2/ui/layout-panel.tsx +5 -47
- package/src/v2/ui/list-view.tsx +81 -7
- package/src/v2/{blocks → ui}/media-actions.tsx +4 -3
- package/src/v2/ui/motion-panel.tsx +2 -1
- package/src/v2/ui/parts-panel.tsx +164 -0
- package/src/v2/ui/pattern-library.tsx +228 -0
- package/src/v2/ui/placeholders.tsx +205 -0
- package/src/v2/ui/preset-bar.tsx +83 -0
- package/src/v2/ui/style-controls.tsx +65 -15
- package/src/v2/ui/style-supports.ts +1 -1
- package/src/v2/ui/style-tab.tsx +127 -41
- package/src/v2/ui/toolbar.tsx +59 -16
- package/src/v2/ui/unit-controls.tsx +88 -39
- package/src/locales/en/patterns.ts +0 -50
- package/src/locales/vi/patterns.ts +0 -50
- package/src/shared/patterns-kit.ts +0 -7
- package/src/shared/patterns-library.ts +0 -172
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { useEditor } from "../context";
|
|
3
|
+
import type { BNode } from "../core/model";
|
|
4
|
+
import type { EditorState } from "../core/store";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Khối trượt về chỗ mới khi thứ tự đổi (lên / xuống, kéo thả, chèn, xoá, hoàn tác) thay vì nhảy (kiểu FLIP):
|
|
8
|
+
* thứ tự trong cây đổi → đo vị trí cũ ngay lúc store báo (DOM chưa vẽ lại) → khung hình kế tiếp đo vị trí mới →
|
|
9
|
+
* đặt khối về chỗ cũ rồi thả về 0. Gõ chữ không đổi thứ tự → không đo, không chạy.
|
|
10
|
+
*/
|
|
11
|
+
export const MOVE_MS = 340;
|
|
12
|
+
// Dừng êm, không nảy (như lò xo tắt dần): quãng đường còn lại giảm theo hàm mũ, hằng số thời gian 75ms → ~35% sau 40ms, ~70% sau 100ms
|
|
13
|
+
const TAU = 75;
|
|
14
|
+
export const MOVE_EASE = `linear(${Array.from({ length: 21 }, (_, i) => { const t = (i / 20) * MOVE_MS; return ((1 - Math.exp(-t / TAU)) / (1 - Math.exp(-MOVE_MS / TAU))).toFixed(3) + ` ${i * 5}%`; }).join(", ")})`;
|
|
15
|
+
// Trình duyệt chưa hiểu linear(): đường cong gần giống
|
|
16
|
+
const FALLBACK_EASE = "cubic-bezier(.25, .7, .3, 1)";
|
|
17
|
+
|
|
18
|
+
/** Vị trí của mọi khối trong cây: id → "cha:thứ tự". */
|
|
19
|
+
export function orderMap(docs: EditorState["docs"]): Map<string, string> {
|
|
20
|
+
const out = new Map<string, string>();
|
|
21
|
+
const walk = (list: readonly BNode[], parent: string) => list.forEach((b, i) => { out.set(b.id, `${parent}:${i}`); if (b.content) walk(b.content, b.id); });
|
|
22
|
+
for (const d of Object.values(docs)) walk(d.blocks, d.id);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Khối cần trượt: đã có trước, đổi chỗ trong cây, và vị trí trên màn hình đổi; bỏ khối con khi khối cha cùng trượt một quãng. */
|
|
27
|
+
export function movesToAnimate(before: Map<string, string>, after: Map<string, string>, oldRects: Map<string, DOMRect>, newRect: (id: string) => DOMRect | null, parentOf: (id: string) => string | null) {
|
|
28
|
+
const moves = new Map<string, { dx: number; dy: number }>();
|
|
29
|
+
for (const id of after.keys()) {
|
|
30
|
+
const o = oldRects.get(id);
|
|
31
|
+
if (!before.has(id) || !o) continue; // khối mới chèn: hiện ngay
|
|
32
|
+
const n = newRect(id);
|
|
33
|
+
if (!n) continue;
|
|
34
|
+
const dx = o.left - n.left, dy = o.top - n.top;
|
|
35
|
+
if (Math.abs(dx) < 1 && Math.abs(dy) < 1) continue;
|
|
36
|
+
moves.set(id, { dx, dy });
|
|
37
|
+
}
|
|
38
|
+
// Khối cha đã trượt → khối con đi theo; chỉ trượt riêng phần chênh lệch
|
|
39
|
+
const abs = new Map(moves);
|
|
40
|
+
for (const [id, m] of abs) {
|
|
41
|
+
for (let p = parentOf(id); p; p = parentOf(p)) {
|
|
42
|
+
const pm = abs.get(p);
|
|
43
|
+
if (!pm) continue;
|
|
44
|
+
const rx = m.dx - pm.dx, ry = m.dy - pm.dy;
|
|
45
|
+
if (Math.abs(rx) < 1 && Math.abs(ry) < 1) moves.delete(id); else moves.set(id, { dx: rx, dy: ry });
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return moves;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function useMoveAnimation(doc: Document) {
|
|
53
|
+
const { store } = useEditor();
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const win = doc.defaultView;
|
|
56
|
+
if (!win) return;
|
|
57
|
+
let docs = store.getState().docs;
|
|
58
|
+
let order = orderMap(docs);
|
|
59
|
+
let raf = 0;
|
|
60
|
+
const els = () => doc.querySelectorAll<HTMLElement>("[data-ecx-block]");
|
|
61
|
+
const off = store.subscribe(() => {
|
|
62
|
+
const next = store.getState().docs;
|
|
63
|
+
if (next === docs) return;
|
|
64
|
+
const nextOrder = orderMap(next);
|
|
65
|
+
docs = next;
|
|
66
|
+
let changed = nextOrder.size !== order.size;
|
|
67
|
+
if (!changed) for (const [id, p] of nextOrder) if (order.get(id) !== p) { changed = true; break; }
|
|
68
|
+
const before = order;
|
|
69
|
+
order = nextOrder;
|
|
70
|
+
if (!changed || win.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
|
|
71
|
+
// DOM vẫn là bản cũ: đo vị trí cũ (bỏ phần đang trượt dở)
|
|
72
|
+
const old = new Map<string, DOMRect>();
|
|
73
|
+
for (const el of els()) {
|
|
74
|
+
for (const a of el.getAnimations?.() ?? []) if (a.id === "ecx-move") a.finish();
|
|
75
|
+
old.set(el.dataset.ecxBlock!, el.getBoundingClientRect());
|
|
76
|
+
}
|
|
77
|
+
win.cancelAnimationFrame(raf);
|
|
78
|
+
raf = win.requestAnimationFrame(() => {
|
|
79
|
+
const byId = new Map<string, HTMLElement>();
|
|
80
|
+
for (const el of els()) byId.set(el.dataset.ecxBlock!, el);
|
|
81
|
+
const parentOf = (id: string) => byId.get(id)?.parentElement?.closest<HTMLElement>("[data-ecx-block]")?.dataset.ecxBlock ?? null;
|
|
82
|
+
const moves = movesToAnimate(before, nextOrder, old, (id) => byId.get(id)?.getBoundingClientRect() ?? null, parentOf);
|
|
83
|
+
for (const [id, { dx, dy }] of moves) {
|
|
84
|
+
const el = byId.get(id);
|
|
85
|
+
if (!el?.animate) continue;
|
|
86
|
+
const frames = [{ transform: `translate(${dx}px, ${dy}px)` }, { transform: "translate(0, 0)" }];
|
|
87
|
+
try { el.animate(frames, { duration: MOVE_MS, easing: MOVE_EASE, id: "ecx-move" }); }
|
|
88
|
+
catch { el.animate(frames, { duration: MOVE_MS, easing: FALLBACK_EASE, id: "ecx-move" }); }
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return () => { off(); win.cancelAnimationFrame(raf); };
|
|
93
|
+
}, [doc, store]);
|
|
94
|
+
}
|
|
@@ -3,6 +3,7 @@ import { useEditor } from "../context";
|
|
|
3
3
|
import { docFromBlocks, type BNode } from "../core/model";
|
|
4
4
|
import type { BlockEditProps } from "../core/registry";
|
|
5
5
|
import { sanitizeServerHtml } from "./server-html";
|
|
6
|
+
import { useMsgs } from "../i18n";
|
|
6
7
|
|
|
7
8
|
const MEDIA = "img, svg, video, canvas, picture, iframe, input, select, textarea, button, hr";
|
|
8
9
|
/** Có chữ hoặc ảnh / hình / ô nhập nào thấy được không (khung rỗng thì không). */
|
|
@@ -20,8 +21,9 @@ const CACHE_LIMIT = 300;
|
|
|
20
21
|
* Khối động / khối của theme-plugin do **website** vẽ bằng chính code + CSS của theme (host.renderBlock) — khung soạn thấy
|
|
21
22
|
* đúng menu riêng, breadcrumb thật… như trang thật. Đang tải hoặc website không vẽ được → giao diện của trình soạn (`fallback`).
|
|
22
23
|
*/
|
|
23
|
-
export function ServerBlock({ node, blockProps, fallback }: { node: BNode; blockProps: BlockEditProps["blockProps"]; fallback: ReactNode }) {
|
|
24
|
+
export function ServerBlock({ node, blockProps, fallback, live = true }: { node: BNode; blockProps: BlockEditProps["blockProps"]; fallback: ReactNode; live?: boolean }) {
|
|
24
25
|
const { host } = useEditor();
|
|
26
|
+
const tp = useMsgs("placeholders");
|
|
25
27
|
const json = docFromBlocks([node]).content?.[0];
|
|
26
28
|
const key = JSON.stringify(json ?? null);
|
|
27
29
|
const [html, setHtml] = useState<string | null | undefined>(() => cache.get(key));
|
|
@@ -38,10 +40,11 @@ export function ServerBlock({ node, blockProps, fallback }: { node: BNode; block
|
|
|
38
40
|
}, 250);
|
|
39
41
|
return () => { alive = false; clearTimeout(timer); };
|
|
40
42
|
}, [key, host]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
41
|
-
|
|
43
|
+
// fallback luôn là con đầu của cùng một Fragment ở mọi nhánh dưới → đổi nhánh không dựng lại ô nhập đang gõ
|
|
44
|
+
if (!live || !html) return <>{fallback}</>;
|
|
42
45
|
const clean = sanitizeServerHtml(html, document);
|
|
43
|
-
// Website vẽ ra rỗng (thiếu dữ liệu
|
|
44
|
-
if (!hasVisibleContent(clean)) return <
|
|
46
|
+
// Website vẽ ra rỗng (thiếu dữ liệu, theme chưa hỗ trợ khối…) → ô giữ chỗ của trình soạn (nhập được ngay) + nói rõ website chưa vẽ gì
|
|
47
|
+
if (!hasVisibleContent(clean)) return <>{fallback}<p className="ecx-ph-missing is-note" contentEditable={false}>{tp.serverEmpty}</p></>;
|
|
45
48
|
return (
|
|
46
49
|
<div {...blockProps} className={[blockProps.className, "ecx-server-block"].filter(Boolean).join(" ")} contentEditable={false}>
|
|
47
50
|
{/* pointer-events: none → bấm vào là chọn khối, không mở liên kết / gửi form của trang thật */}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tô vùng đệm (padding — bên trong khối) hoặc lề (margin — bên ngoài khối) của một khối trên khung soạn, kèm giá trị:
|
|
3
|
+
* rê chuột lên ô cạnh ở bảng bên phải → thấy ngay vùng đó trên khối. Lớp phủ riêng (position: fixed trong iframe), không đổi HTML của khối.
|
|
4
|
+
*/
|
|
5
|
+
export type SpacingKind = "padding" | "margin";
|
|
6
|
+
export type Side = "top" | "right" | "bottom" | "left";
|
|
7
|
+
const SIDES: Side[] = ["top", "right", "bottom", "left"];
|
|
8
|
+
const CLS = "ecx-spacing-viz";
|
|
9
|
+
|
|
10
|
+
export function hideSpacingViz(doc: Document | null | undefined) {
|
|
11
|
+
doc?.querySelectorAll(`.${CLS}`).forEach((n) => n.remove());
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Hình chữ nhật của từng cạnh (toạ độ khung nhìn của iframe). */
|
|
15
|
+
export function spacingRects(r: { left: number; top: number; right: number; bottom: number; width: number; height: number }, px: Record<Side, number>, kind: SpacingKind) {
|
|
16
|
+
const out: Record<Side, { left: number; top: number; width: number; height: number }> = {} as never;
|
|
17
|
+
if (kind === "padding") {
|
|
18
|
+
out.top = { left: r.left, top: r.top, width: r.width, height: px.top };
|
|
19
|
+
out.bottom = { left: r.left, top: r.bottom - px.bottom, width: r.width, height: px.bottom };
|
|
20
|
+
out.left = { left: r.left, top: r.top + px.top, width: px.left, height: Math.max(0, r.height - px.top - px.bottom) };
|
|
21
|
+
out.right = { left: r.right - px.right, top: r.top + px.top, width: px.right, height: Math.max(0, r.height - px.top - px.bottom) };
|
|
22
|
+
} else {
|
|
23
|
+
out.top = { left: r.left - px.left, top: r.top - px.top, width: r.width + px.left + px.right, height: px.top };
|
|
24
|
+
out.bottom = { left: r.left - px.left, top: r.bottom, width: r.width + px.left + px.right, height: px.bottom };
|
|
25
|
+
out.left = { left: r.left - px.left, top: r.top, width: px.left, height: r.height };
|
|
26
|
+
out.right = { left: r.right, top: r.top, width: px.right, height: r.height };
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Vẽ lớp tô cho các cạnh `sides` (rỗng = cả 4). `names`: tên bậc hiện cạnh giá trị px (vd "MD · 20px"). */
|
|
32
|
+
export function showSpacingViz(el: HTMLElement | null, kind: SpacingKind, sides: Side[] = SIDES, names: Partial<Record<Side, string>> = {}) {
|
|
33
|
+
if (!el) return;
|
|
34
|
+
const doc = el.ownerDocument;
|
|
35
|
+
hideSpacingViz(doc);
|
|
36
|
+
const cs = doc.defaultView!.getComputedStyle(el);
|
|
37
|
+
const px = Object.fromEntries(SIDES.map((s) => [s, parseFloat(cs.getPropertyValue(`${kind}-${s}`)) || 0])) as Record<Side, number>;
|
|
38
|
+
const rects = spacingRects(el.getBoundingClientRect(), px, kind);
|
|
39
|
+
for (const s of sides.length ? sides : SIDES) {
|
|
40
|
+
const r = rects[s];
|
|
41
|
+
const box = doc.createElement("div");
|
|
42
|
+
box.className = `${CLS} is-${kind}`;
|
|
43
|
+
Object.assign(box.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${Math.max(0, r.width)}px`, height: `${Math.max(0, r.height)}px` });
|
|
44
|
+
if (px[s] >= 1) {
|
|
45
|
+
const tag = doc.createElement("span");
|
|
46
|
+
tag.textContent = names[s] ? `${names[s]} · ${Math.round(px[s])}px` : `${Math.round(px[s])}px`;
|
|
47
|
+
box.appendChild(tag);
|
|
48
|
+
}
|
|
49
|
+
doc.body.appendChild(box);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { BlockControls } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import type { BNode } from "./model";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Kiểu đặt sẵn + mặc định khi chèn của từng loại khối (toàn website, host.blockPresets):
|
|
6
|
+
* { "<loại>": { default?: attrs, presets?: [{ name, attrs }] } }. Chỉ chứa thuộc tính KIỂU (style, parts, biến thể, ô kiểu dáng của khối),
|
|
7
|
+
* không chứa nội dung (chữ, ảnh, liên kết) — "Mặc định của khối" không đổi khối đã có, không lưu nội dung.
|
|
8
|
+
*/
|
|
9
|
+
export interface BlockPreset { name: string; attrs: Record<string, unknown> }
|
|
10
|
+
export interface BlockPresetEntry { default?: Record<string, unknown>; presets?: BlockPreset[] }
|
|
11
|
+
export type BlockPresetMap = Record<string, BlockPresetEntry>;
|
|
12
|
+
|
|
13
|
+
/** Thuộc tính kiểu chung của mọi khối. */
|
|
14
|
+
const COMMON = ["style", "parts", "blockStyle"];
|
|
15
|
+
|
|
16
|
+
/** Tên thuộc tính kiểu của một loại khối: chung + ô điều khiển khai CSS (lõi + plugin). */
|
|
17
|
+
export function styleKeys(type: string, controls: readonly BlockControls[]): string[] {
|
|
18
|
+
const own = controls.filter((c) => c.blocks === "*" || c.blocks.includes(type)).flatMap((c) => Object.keys(c.attributes));
|
|
19
|
+
return [...new Set([...COMMON, ...own])];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Lấy phần kiểu của attrs (bỏ ô trống). */
|
|
23
|
+
export function pickStyle(attrs: Record<string, unknown>, keys: string[]): Record<string, unknown> {
|
|
24
|
+
const out: Record<string, unknown> = {};
|
|
25
|
+
for (const k of keys) if (attrs[k] !== undefined && attrs[k] !== null && attrs[k] !== "") out[k] = attrs[k];
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Áp một bộ kiểu: xoá mọi thuộc tính kiểu đang có rồi đặt theo bộ kiểu (nội dung giữ nguyên). */
|
|
30
|
+
export function applyStyle(keys: string[], style: Record<string, unknown>): Record<string, unknown> {
|
|
31
|
+
const patch: Record<string, unknown> = {};
|
|
32
|
+
for (const k of keys) patch[k] = style[k];
|
|
33
|
+
return patch;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** So sánh hai bộ kiểu (thứ tự khoá không quan trọng). */
|
|
37
|
+
export const sameStyle = (a: Record<string, unknown>, b: Record<string, unknown>) => stable(a) === stable(b);
|
|
38
|
+
function stable(v: unknown): string {
|
|
39
|
+
if (v && typeof v === "object" && !Array.isArray(v)) return `{${Object.keys(v).sort().map((k) => `${JSON.stringify(k)}:${stable((v as Record<string, unknown>)[k])}`).join(",")}}`;
|
|
40
|
+
if (Array.isArray(v)) return `[${v.map(stable).join(",")}]`;
|
|
41
|
+
return JSON.stringify(v);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Khối mới chèn: thêm kiểu mặc định của loại khối (chỉ thuộc tính chưa có — mẫu / biến thể đã đặt thì giữ). */
|
|
45
|
+
export function withDefaults(node: BNode, map: BlockPresetMap | null): BNode {
|
|
46
|
+
const def = map?.[node.type]?.default;
|
|
47
|
+
if (!def) return node;
|
|
48
|
+
const attrs = { ...node.attrs };
|
|
49
|
+
for (const [k, v] of Object.entries(def)) if (attrs[k] === undefined) attrs[k] = v;
|
|
50
|
+
return { ...node, attrs };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Kho trong bộ nhớ (một lần nạp mỗi phiên soạn); UI đăng ký nghe thay đổi
|
|
54
|
+
let current: BlockPresetMap | null = null;
|
|
55
|
+
let loading: Promise<void> | null = null;
|
|
56
|
+
const listeners = new Set<() => void>();
|
|
57
|
+
export const presetsSnapshot = () => current;
|
|
58
|
+
export const onPresetsChange = (l: () => void) => { listeners.add(l); return () => { listeners.delete(l); }; };
|
|
59
|
+
export function setPresets(m: BlockPresetMap | null) { current = m; listeners.forEach((l) => l()); }
|
|
60
|
+
export function ensurePresets(load?: () => Promise<Record<string, unknown>>) {
|
|
61
|
+
if (current || loading || !load) return;
|
|
62
|
+
loading = load().then((m) => setPresets((m ?? {}) as BlockPresetMap), () => setPresets({})).finally(() => { loading = null; });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Như withDefaults cho kết quả tạo khối (một khối hoặc mẫu nhiều khối). */
|
|
66
|
+
export function freshWithDefaults<T extends BNode | BNode[]>(made: T): T {
|
|
67
|
+
return (Array.isArray(made) ? made.map((n) => withDefaults(n, current)) : withDefaults(made, current)) as T;
|
|
68
|
+
}
|
package/src/v2/core/host.ts
CHANGED
|
@@ -29,6 +29,13 @@ export interface EditorHost {
|
|
|
29
29
|
canvasContentClass?: string;
|
|
30
30
|
/** Mở nơi sửa menu của một vị trí (khối Menu đang chọn hiện nút "Sửa menu"). Không có → không hiện nút. */
|
|
31
31
|
editMenu?(location: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Mở trang quản trị để nhập dữ liệu còn thiếu của khối giao diện (ô giữ chỗ cam): "menus" (tạo / chọn menu), "customize" (logo, tên
|
|
34
|
+
* website), "newPost" (viết bài đầu tiên; args.postType). Không có → không hiện nút.
|
|
35
|
+
*/
|
|
36
|
+
openAdmin?(target: "menus" | "customize" | "settings" | "newPost", args?: Record<string, string>): void;
|
|
37
|
+
/** Đặt logo cho cả website từ ô thả logo của khối Logo (trả về khi đã lưu). Không có → ô thả logo chỉ có nút mở Tuỳ biến. */
|
|
38
|
+
setSiteLogo?(item: import("../../shared/types").MediaItem): Promise<void>;
|
|
32
39
|
/**
|
|
33
40
|
* Website vẽ giúp một khối (nút nội dung dạng JSON như khi lưu) bằng code + CSS của theme → HTML. null = không vẽ được
|
|
34
41
|
* (trình soạn dùng giao diện của mình). Dùng cho khối có `serverRender` (khối động, khối plugin/theme không có `edit`).
|
|
@@ -38,6 +45,11 @@ export interface EditorHost {
|
|
|
38
45
|
scope?: "template" | "content";
|
|
39
46
|
/** Preset của theme (biến --ecx-*). */
|
|
40
47
|
presets?: StylePresets;
|
|
48
|
+
/**
|
|
49
|
+
* v3: kiểu đặt sẵn + mặc định khi chèn của từng loại khối (toàn website). `save` không có = người dùng không có quyền lưu (chỉ chọn kiểu).
|
|
50
|
+
* Dạng dữ liệu: core/block-presets BlockPresetMap.
|
|
51
|
+
*/
|
|
52
|
+
blockPresets?: { load(): Promise<Record<string, unknown>>; save?(map: Record<string, unknown>): Promise<Record<string, unknown>> };
|
|
41
53
|
/** Chữ "%khoá%" của plugin → chữ theo ngôn ngữ. */
|
|
42
54
|
translate?(text: string): string;
|
|
43
55
|
/**
|
|
@@ -58,6 +70,8 @@ export interface EditorHost {
|
|
|
58
70
|
*/
|
|
59
71
|
ai?: {
|
|
60
72
|
run(task: string, text: string, opts?: { language?: string; instruction?: string }): Promise<string>;
|
|
73
|
+
/** Viết nội dung mới theo yêu cầu; `nodes` là khối dạng lưu (tiêu đề, danh sách…) — có thì chèn đúng khối, không thì tách `text` thành đoạn văn. */
|
|
74
|
+
write?(prompt: string): Promise<{ text: string; nodes?: unknown[] }>;
|
|
61
75
|
/** Mô tả (alt) cho ảnh trong thư viện media; không có = khối Ảnh không hiện nút AI. */
|
|
62
76
|
altText?(mediaId: number, language?: string): Promise<string>;
|
|
63
77
|
};
|
package/src/v2/core/registry.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface BlockEditProps {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
-
export type BlockProps = HTMLAttributes<HTMLElement> & { ref: Ref<any>; "data-ecx-block": string; "data-type": string
|
|
25
|
+
export type BlockProps = HTMLAttributes<HTMLElement> & { ref: Ref<any>; "data-ecx-block": string; "data-type": string };
|
|
26
26
|
|
|
27
27
|
export type BlockCategory = "text" | "media" | "design" | "widgets" | "theme" | "embed" | (string & {});
|
|
28
28
|
|
|
@@ -79,6 +79,8 @@ export interface BlockType {
|
|
|
79
79
|
* ServerSideRender chỉ cho khối động của plugin). `edit` chỉ dùng khi đang tải / website không vẽ được / khối nằm trong vòng lặp bài.
|
|
80
80
|
*/
|
|
81
81
|
serverRender?: boolean;
|
|
82
|
+
/** Khối `serverRender`: đang chọn thì vẽ `edit` (ô giữ chỗ để nhập), bỏ chọn mới hiện bản website vẽ. */
|
|
83
|
+
editWhenSelected?: boolean;
|
|
82
84
|
/** Thanh công cụ riêng của khối (chèn vào thanh công cụ nổi). */
|
|
83
85
|
toolbar?: ComponentType<BlockEditProps>;
|
|
84
86
|
/**
|
package/src/v2/core/structure.ts
CHANGED
|
@@ -28,11 +28,13 @@ const RULES: Record<string, StructureRule> = {
|
|
|
28
28
|
listItem: { parents: ["bulletList", "orderedList"], wrapSelfWith: "bulletList", required: p, firstChild: "paragraph" },
|
|
29
29
|
taskList: { allowedChildren: ["taskItem"], wrapChildrenWith: "taskItem", removeWhenEmpty: true },
|
|
30
30
|
taskItem: { parents: ["taskList"], wrapSelfWith: "taskList", required: p, firstChild: "paragraph" },
|
|
31
|
-
|
|
31
|
+
// Bộ cột rỗng (vừa chèn, chưa chọn bố cục) giữ lại như bảng rỗng: trình soạn hiện lưới chọn bố cục cột
|
|
32
|
+
columns: { allowedChildren: ["column"], wrapChildrenWith: "column" },
|
|
32
33
|
column: { parents: ["columns"], wrapSelfWith: "columns" },
|
|
33
34
|
buttons: { allowedChildren: ["button"], removeWhenEmpty: true },
|
|
34
35
|
button: { parents: ["buttons"], wrapSelfWith: "buttons" },
|
|
35
|
-
|
|
36
|
+
// Bảng rỗng (vừa chèn, chưa chọn kích thước) giữ lại: trình soạn hỏi số cột / hàng, website không vẽ bảng không có hàng
|
|
37
|
+
table: { allowedChildren: ["tableRow"], wrapChildrenWith: "tableRow" },
|
|
36
38
|
tableRow: { parents: ["table"], allowedChildren: ["tableCell", "tableHeader"], wrapChildrenWith: "tableCell", removeWhenEmpty: true },
|
|
37
39
|
tableCell: { parents: ["tableRow"], required: p },
|
|
38
40
|
tableHeader: { parents: ["tableRow"], required: p },
|