@ecomcx/ecomweb-editor 1.33.0 → 1.34.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 +2 -1
- package/src/locales/en/background.ts +5 -0
- package/src/locales/en/bindings.ts +4 -0
- package/src/locales/en/block-desc.ts +4 -11
- package/src/locales/en/block-style.ts +17 -6
- package/src/locales/en/block-titles.ts +38 -16
- package/src/locales/en/blocks.ts +36 -6
- package/src/locales/en/columns.ts +1 -0
- package/src/locales/en/controls.ts +1 -1
- package/src/locales/en/hints.ts +241 -12
- package/src/locales/en/index.ts +3 -1
- package/src/locales/en/layout.ts +13 -0
- package/src/locales/en/motion.ts +16 -0
- package/src/locales/en/palette.ts +8 -0
- package/src/locales/en/parts.ts +2 -1
- package/src/locales/en/pattern-overrides.ts +14 -0
- package/src/locales/en/placeholders.ts +6 -3
- package/src/locales/en/v2.ts +2 -0
- package/src/locales/vi/attrs.ts +3 -2
- package/src/locales/vi/background.ts +5 -0
- package/src/locales/vi/bindings.ts +4 -0
- package/src/locales/vi/block-desc.ts +7 -14
- package/src/locales/vi/block-style.ts +18 -7
- package/src/locales/vi/block-titles.ts +48 -26
- package/src/locales/vi/blocks.ts +36 -6
- package/src/locales/vi/columns.ts +1 -0
- package/src/locales/vi/controls.ts +1 -1
- package/src/locales/vi/hints.ts +241 -12
- package/src/locales/vi/index.ts +3 -1
- package/src/locales/vi/layout.ts +13 -0
- package/src/locales/vi/motion.ts +16 -0
- package/src/locales/vi/palette.ts +8 -0
- package/src/locales/vi/parts.ts +2 -1
- package/src/locales/vi/pattern-overrides.ts +14 -0
- package/src/locales/vi/placeholders.ts +7 -4
- package/src/locales/vi/v2.ts +2 -0
- package/src/v2/blocks/columns-inspector.tsx +11 -2
- package/src/v2/blocks/index.ts +9 -6
- package/src/v2/blocks/layout-options.tsx +80 -0
- package/src/v2/blocks/layout.tsx +331 -124
- package/src/v2/blocks/media.tsx +51 -21
- package/src/v2/blocks/resize.tsx +50 -19
- package/src/v2/blocks/social-icons.ts +22 -0
- package/src/v2/blocks/text.tsx +12 -50
- package/src/v2/blocks/theme.tsx +13 -19
- package/src/v2/blocks/tools.tsx +24 -2
- package/src/v2/blocks/widgets.tsx +84 -28
- package/src/v2/canvas/block-list.tsx +30 -4
- package/src/v2/canvas/canvas-css.ts +81 -9
- package/src/v2/canvas/canvas.tsx +63 -7
- package/src/v2/canvas/dnd.ts +7 -0
- package/src/v2/canvas/variation-picker.tsx +63 -0
- package/src/v2/context.tsx +48 -0
- package/src/v2/core/bindings.ts +6 -1
- package/src/v2/core/block-presets.ts +10 -0
- package/src/v2/core/clipboard.ts +5 -5
- package/src/v2/core/host.ts +2 -0
- package/src/v2/core/model.ts +6 -6
- package/src/v2/core/registry.ts +15 -0
- package/src/v2/core/store.ts +9 -9
- package/src/v2/core/structure.ts +1 -3
- package/src/v2/editor.css +74 -0
- package/src/v2/editor.tsx +22 -7
- package/src/v2/text/rich-text.tsx +20 -5
- package/src/v2/ui/ai-markdown.ts +52 -0
- package/src/v2/ui/ai.tsx +81 -14
- package/src/v2/ui/attr-form.tsx +26 -6
- package/src/v2/ui/attr-hints.ts +115 -16
- package/src/v2/ui/background-panel.tsx +22 -0
- package/src/v2/ui/bg-video-preview.tsx +7 -0
- package/src/v2/ui/bindings.tsx +7 -2
- package/src/v2/ui/block-icons.tsx +3 -10
- package/src/v2/ui/block-live-preview.tsx +21 -0
- package/src/v2/ui/block-style-panel.tsx +3 -1
- package/src/v2/ui/command-palette.tsx +125 -0
- package/src/v2/ui/icon-picker.tsx +88 -21
- package/src/v2/ui/icon-svg.tsx +21 -0
- package/src/v2/ui/inserter.tsx +62 -12
- package/src/v2/ui/inspector.tsx +12 -6
- package/src/v2/ui/layout-panel.tsx +1 -1
- package/src/v2/ui/motion-panel.tsx +22 -13
- package/src/v2/ui/option-thumbs.tsx +81 -0
- package/src/v2/ui/pattern-library.tsx +2 -1
- package/src/v2/ui/pattern-overrides.tsx +67 -0
- package/src/v2/ui/placeholders.tsx +21 -2
- package/src/v2/ui/preset-bar.tsx +5 -5
- package/src/v2/ui/style-supports.ts +2 -3
- package/src/v2/ui/style-tab.tsx +83 -20
- package/src/v2/ui/toolbar.tsx +40 -6
- package/src/v2/ui/transform-panel.tsx +77 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect, useRef, type KeyboardEvent, type ReactNode } from "react";
|
|
2
|
+
import type { BlockType, BlockLayout } from "../core/registry";
|
|
3
|
+
import { useMsgs } from "../i18n";
|
|
4
|
+
|
|
5
|
+
const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
|
|
6
|
+
|
|
7
|
+
/** Bản vẽ thu nhỏ của bố cục: nền xám nhạt cho chữ / khung, màu nhấn của trình soạn cho phần đang nổi bật. */
|
|
8
|
+
export function Thumb({ children }: { children: ReactNode }) {
|
|
9
|
+
return <svg className="ecx-vthumb" viewBox="0 0 96 64" aria-hidden="true" focusable="false">{children}</svg>;
|
|
10
|
+
}
|
|
11
|
+
export const bar = (x: number, y: number, w: number, h = 4, accent = false) => <rect key={`${x},${y},${w}`} x={x} y={y} width={w} height={h} rx={1.5} className={accent ? "is-accent" : undefined} />;
|
|
12
|
+
export const box = (x: number, y: number, w: number, h: number, accent = false) => <rect key={`b${x},${y},${w},${h}`} x={x} y={y} width={w} height={h} rx={3} className={cx("is-box", accent && "is-accent")} />;
|
|
13
|
+
|
|
14
|
+
/** Giá trị đang dùng (hoặc mặc định của thuộc tính) có khớp hết thuộc tính của bố cục không. */
|
|
15
|
+
export function variationMatches(type: BlockType | undefined, attrs: Record<string, unknown>, v: BlockLayout): boolean {
|
|
16
|
+
return Object.entries(v.attrs).every(([k, want]) => (attrs[k] ?? type?.attributes?.[k]?.default) === want);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Lưới thẻ bố cục: bấm một thẻ → áp thuộc tính của bố cục lên khối. Dùng cả ở bảng phải (đổi sau) lẫn ô chọn lúc vừa chèn. */
|
|
20
|
+
export function VariationGrid({ type, attrs, onPick, className }: { type?: BlockType; attrs: Record<string, unknown>; onPick(v: BlockLayout): void; className?: string }) {
|
|
21
|
+
const vs = type?.layouts ?? [];
|
|
22
|
+
if (!vs.length) return null;
|
|
23
|
+
/** Mũi tên di chuyển giữa các thẻ (như nhóm nút chọn chuẩn): thẻ đang được tick giữ tabindex 0, các thẻ còn lại -1. */
|
|
24
|
+
const arrows = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
25
|
+
const keys = ["ArrowRight", "ArrowDown", "ArrowLeft", "ArrowUp", "Home", "End"];
|
|
26
|
+
if (!keys.includes(e.key)) return;
|
|
27
|
+
const btns = [...e.currentTarget.querySelectorAll<HTMLButtonElement>("button[role=radio]")];
|
|
28
|
+
const i = btns.indexOf(e.target as HTMLButtonElement);
|
|
29
|
+
if (i < 0) return;
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
const next = e.key === "Home" ? 0 : e.key === "End" ? btns.length - 1 : (i + (e.key === "ArrowRight" || e.key === "ArrowDown" ? 1 : -1) + btns.length) % btns.length;
|
|
32
|
+
btns[next]?.focus();
|
|
33
|
+
};
|
|
34
|
+
const onIdx = vs.findIndex((v) => variationMatches(type, attrs, v));
|
|
35
|
+
return (
|
|
36
|
+
<div className={cx("ecx-variations", className)} role="radiogroup" onKeyDown={arrows}>
|
|
37
|
+
{vs.map((v, i) => {
|
|
38
|
+
const on = variationMatches(type, attrs, v);
|
|
39
|
+
return (
|
|
40
|
+
<button key={v.id} type="button" role="radio" aria-checked={on} tabIndex={i === Math.max(0, onIdx) ? 0 : -1} className={cx("ecx-variation", on && "is-active")} onMouseDown={(e) => e.preventDefault()} onClick={() => onPick(v)}>
|
|
41
|
+
{v.thumb}<span>{v.label}</span>
|
|
42
|
+
</button>
|
|
43
|
+
);
|
|
44
|
+
})}
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Ô chọn bố cục đứng thế chỗ khối ngay sau khi chèn (như Kadence): chọn một thẻ hoặc "Bỏ qua" để dùng bố cục mặc định. */
|
|
50
|
+
export function VariationChooser({ type, attrs, onPick, onSkip, blockProps }: { type: BlockType; attrs: Record<string, unknown>; onPick(v: BlockLayout): void; onSkip(): void; blockProps: Record<string, unknown> }) {
|
|
51
|
+
const t = useMsgs("blocks") as Record<string, string>;
|
|
52
|
+
const box = useRef<HTMLDivElement | null>(null);
|
|
53
|
+
// Bàn phím: thẻ đầu nhận focus ngay khi ô chọn hiện (Enter / Space chọn, Esc bỏ qua)
|
|
54
|
+
useEffect(() => { box.current?.querySelector<HTMLButtonElement>("button[role=radio]")?.focus({ preventScroll: true }); }, []);
|
|
55
|
+
const ref = (el: HTMLDivElement | null) => { box.current = el; const r = (blockProps as { ref?: unknown }).ref; if (typeof r === "function") (r as (n: HTMLDivElement | null) => void)(el); else if (r && typeof r === "object") (r as { current: unknown }).current = el; };
|
|
56
|
+
return (
|
|
57
|
+
<div {...blockProps} ref={ref} onKeyDown={(e) => { if (e.key === "Escape") { e.preventDefault(); onSkip(); } }} className={cx((blockProps as { className?: string }).className, "ecx-variation-chooser")} contentEditable={false}>
|
|
58
|
+
<strong className="ecx-variation-title">{t.chooseLayout}</strong>
|
|
59
|
+
<VariationGrid type={type} attrs={attrs} onPick={onPick} />
|
|
60
|
+
<button type="button" className="ecx-variation-skip" onMouseDown={(e) => e.preventDefault()} onClick={onSkip}>{t.skipLayout}</button>
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
}
|
package/src/v2/context.tsx
CHANGED
|
@@ -32,6 +32,9 @@ export class EditorUi {
|
|
|
32
32
|
typing = false;
|
|
33
33
|
/** Thu nhỏ: cả trang trong tầm nhìn, chỉ chọn / sắp xếp các phần (khối cấp cao nhất). */
|
|
34
34
|
zoomOut = false;
|
|
35
|
+
/** Khối đang "xem trước khi rê chuột": khung soạn vẽ mọi quy tắc :hover của khối đó như đang rê (Kiểu dáng › Xem trước khi rê chuột). */
|
|
36
|
+
previewHoverId: string | null = null;
|
|
37
|
+
setPreviewHover(id: string | null) { if (this.previewHoverId !== id) { this.previewHoverId = id; this.bump(); } }
|
|
35
38
|
/** Khổ màn hình đang soạn: khung soạn vẽ theo bề rộng này, tab Kiểu dáng sửa giá trị của khổ này (dùng chung, đổi một nơi thì nơi kia theo). */
|
|
36
39
|
device: "desktop" | "tablet" | "mobile" = "desktop";
|
|
37
40
|
setDevice(d: "desktop" | "tablet" | "mobile") { if (this.device !== d) { this.device = d; this.bump(); } }
|
|
@@ -66,6 +69,51 @@ export class EditorUi {
|
|
|
66
69
|
return new DOMRect(f.left + r.left * scale, f.top + r.top * scale, r.width * scale, r.height * scale);
|
|
67
70
|
}
|
|
68
71
|
openInserter(req: InserterRequest | null) { this.inserter = req; this.bump(); }
|
|
72
|
+
/** Vẽ nội dung bằng khung soạn chỉ xem (tầng editor gán; ui không import editor): xem trước thật ở bảng chèn và thư viện mẫu. */
|
|
73
|
+
renderPreview: ((value: { version: number; blocks: unknown[] }) => ReactNode) | null = null;
|
|
74
|
+
/** Mục con đang chọn trong khối dạng danh sách (Icon List, Mạng xã hội…): khung soạn tô mục, bảng phải hiện cài đặt riêng của mục. */
|
|
75
|
+
activeItem = new Map<string, number>();
|
|
76
|
+
setActiveItem(blockId: string, i: number) { if (this.activeItem.get(blockId) !== i) { this.activeItem.set(blockId, i); this.bump(); } }
|
|
77
|
+
/** Khối vừa chèn còn chữ mẫu chưa sửa: bấm vào ô chữ lần đầu thì chọn hết để gõ là thay (không chèn thêm vào chữ mẫu). Bỏ cờ khi khối có thay đổi đầu tiên. */
|
|
78
|
+
fresh = new Set<string>();
|
|
79
|
+
/**
|
|
80
|
+
* Khối vừa chèn từ bảng chèn + vùng chọn "tự đến" sau đó (con trỏ vào ô chữ đầu của khối). Lần chèn kế tiếp, nếu người dùng chưa bấm chỗ khác,
|
|
81
|
+
* đặt khối mới NGAY SAU khối vừa chèn cùng cấp — thay vì lồng vào trong khối vừa chèn (vì con trỏ đang nằm sâu trong nó).
|
|
82
|
+
*/
|
|
83
|
+
insertAnchor: { id: string; sel: string } | null = null;
|
|
84
|
+
/** Cầu nối tới vùng chọn hiện tại của store (EditorProvider gắn vào). */
|
|
85
|
+
currentIds: () => readonly string[] = () => [];
|
|
86
|
+
private anchorTimer: ReturnType<typeof setTimeout> | null = null;
|
|
87
|
+
markInsertAnchor(id: string, delay = 500) {
|
|
88
|
+
this.insertAnchor = null;
|
|
89
|
+
if (this.anchorTimer) clearTimeout(this.anchorTimer); // chèn nhanh nhiều khối: chỉ lần chèn cuối được đặt mốc
|
|
90
|
+
this.anchorTimer = setTimeout(() => { this.anchorTimer = null; this.insertAnchor = { id, sel: this.currentIds().join(",") }; }, delay);
|
|
91
|
+
}
|
|
92
|
+
/** Khối vừa chèn đang chờ chọn bố cục (không lưu vào nội dung). */
|
|
93
|
+
pickIds = new Set<string>();
|
|
94
|
+
requestPick(id: string) { this.pickIds.add(id); this.bump(); }
|
|
95
|
+
// Chọn / bỏ qua bố cục xong khối vẫn được chọn (bảng phải hiện lưới bố cục vừa chọn) — không đẩy con trỏ vào chữ
|
|
96
|
+
// `focus`: "Bỏ qua" = dùng bố cục mặc định và gõ luôn → con trỏ vào ô chữ đầu; chọn một thẻ thì giữ khối được chọn để bảng phải hiện thẻ đó
|
|
97
|
+
resolvePick(id: string, focus = false) { if (this.pickIds.delete(id)) { this.bump(); if (focus) { this.focusFirstField(id); this.markInsertAnchor(id, 450); } } }
|
|
98
|
+
/**
|
|
99
|
+
* Khối widget vừa chèn (Icon Box, Đếm số, Đánh giá…) có ô gõ ngay trên khối (ô chữ, không phải khối chữ) nên không tự nhận con trỏ như Đoạn văn:
|
|
100
|
+
* gõ ngay sau khi chèn không vào đâu. Đưa con trỏ vào ô chữ đầu tiên của khối (chữ mẫu đã chọn sẵn nên gõ là thay). Khối đang chờ chọn bố cục thì để ô chọn giữ focus.
|
|
101
|
+
*/
|
|
102
|
+
focusFirstField(id: string) {
|
|
103
|
+
let tries = 0;
|
|
104
|
+
const attempt = () => {
|
|
105
|
+
if (this.pickIds.has(id)) return;
|
|
106
|
+
const el = this.blockEl(id);
|
|
107
|
+
if (!el) { if (++tries < 8) setTimeout(attempt, 40); return; }
|
|
108
|
+
const doc = el.ownerDocument, active = doc.activeElement as HTMLElement | null;
|
|
109
|
+
if (active && active !== doc.body && el.contains(active) && !active.closest(".ecx-variation-chooser")) return;
|
|
110
|
+
const field = el.querySelector<HTMLElement>('input[data-ecx-placeholder="text"], textarea[data-ecx-placeholder="text"], [contenteditable="true"]');
|
|
111
|
+
if (field && !field.closest("[data-ecx-inert]")) { field.focus({ preventScroll: true }); return; }
|
|
112
|
+
// Khối không có ô gõ (Ảnh, Khoảng trống, Cột…): đưa focus về khung soạn — nếu để ở nút của bảng chèn thì ⌘Z, Delete, mũi tên không tới trình soạn
|
|
113
|
+
(doc.querySelector<HTMLElement>(".ecx-canvas-root"))?.focus({ preventScroll: true });
|
|
114
|
+
};
|
|
115
|
+
setTimeout(attempt, 60);
|
|
116
|
+
}
|
|
69
117
|
/** Thông báo cho trình đọc màn hình (vùng aria-live). */
|
|
70
118
|
announcement = "";
|
|
71
119
|
announce(text: string) { this.announcement = ""; this.bump(); setTimeout(() => { this.announcement = text; this.bump(); }, 30); }
|
package/src/v2/core/bindings.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CORE_BINDABLE as CONTENT_BINDABLE } from "@ecomcx/ecomweb-content";
|
|
1
2
|
import { msgs } from "../../i18n";
|
|
2
3
|
import type { DynamicData } from "./host";
|
|
3
4
|
import type { BNode } from "./model";
|
|
@@ -24,7 +25,7 @@ export interface EditorBindingSource {
|
|
|
24
25
|
export interface Binding { source: string; args?: Record<string, string | number | boolean> }
|
|
25
26
|
|
|
26
27
|
/** Thuộc tính gắn được của khối lõi; khối plugin khai báo bằng BlockType.bindable. */
|
|
27
|
-
export const CORE_BINDABLE: Record<string, string[]> =
|
|
28
|
+
export const CORE_BINDABLE: Record<string, readonly string[]> = CONTENT_BINDABLE;
|
|
28
29
|
|
|
29
30
|
const sources = new Map<string, EditorBindingSource>();
|
|
30
31
|
const listeners = new Set<() => void>();
|
|
@@ -44,12 +45,16 @@ export function registerCoreBindingSources(lang: string) {
|
|
|
44
45
|
fields: [
|
|
45
46
|
{ value: "title", label: t.fTitle }, { value: "excerpt", label: t.fExcerpt }, { value: "date", label: t.fDate },
|
|
46
47
|
{ value: "author", label: t.fAuthor }, { value: "link", label: t.fLink }, { value: "featuredImage", label: t.fFeaturedImage },
|
|
48
|
+
{ value: "featuredImageAlt", label: t.fFeaturedImageAlt }, { value: "category", label: t.fCategory }, { value: "modified", label: t.fModified },
|
|
49
|
+
{ value: "comments", label: t.fComments }, { value: "authorAvatar", label: t.fAuthorAvatar }, { value: "authorBio", label: t.fAuthorBio }, { value: "slug", label: t.fSlug }, { value: "id", label: "ID" },
|
|
47
50
|
],
|
|
48
51
|
preview: (args, { dynamic }) => {
|
|
49
52
|
const p = dynamic?.post;
|
|
50
53
|
switch (args.field) {
|
|
51
54
|
case "title": return p?.title; case "excerpt": return p?.excerpt; case "date": return p?.date; case "author": return p?.author;
|
|
52
55
|
case "featuredImage": return p?.image ?? undefined; case "link": return "/…";
|
|
56
|
+
case "category": return p?.category; case "comments": return p?.comments === undefined ? undefined : String(p.comments);
|
|
57
|
+
case "modified": return p?.date; case "slug": return "slug-bai-viet"; case "id": return "123";
|
|
53
58
|
default: return undefined;
|
|
54
59
|
}
|
|
55
60
|
},
|
|
@@ -50,6 +50,16 @@ export function withDefaults(node: BNode, map: BlockPresetMap | null): BNode {
|
|
|
50
50
|
return { ...node, attrs };
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Kho kiểu đặt sẵn mặc định khi ứng dụng chủ không cung cấp `host.blockPresets`: lưu trong trình duyệt (mỗi máy một bộ),
|
|
55
|
+
* để "Kiểu / Đặt làm mặc định" luôn dùng được. Ứng dụng có máy chủ nên cung cấp kho riêng để theo cả website.
|
|
56
|
+
*/
|
|
57
|
+
const LOCAL_KEY = "ecx-block-presets";
|
|
58
|
+
export const localBlockPresets = {
|
|
59
|
+
async load(): Promise<Record<string, unknown>> { try { return JSON.parse(localStorage.getItem(LOCAL_KEY) ?? "{}") ?? {}; } catch { return {}; } },
|
|
60
|
+
async save(map: Record<string, unknown>): Promise<Record<string, unknown>> { try { localStorage.setItem(LOCAL_KEY, JSON.stringify(map)); } catch { /* trình duyệt chặn bộ nhớ: giữ trong phiên */ } return map; },
|
|
61
|
+
};
|
|
62
|
+
|
|
53
63
|
// Kho trong bộ nhớ (một lần nạp mỗi phiên soạn); UI đăng ký nghe thay đổi
|
|
54
64
|
let current: BlockPresetMap | null = null;
|
|
55
65
|
let loading: Promise<void> | null = null;
|
package/src/v2/core/clipboard.ts
CHANGED
|
@@ -104,7 +104,7 @@ function blockOf(e: HTMLElement): RichNode | RichNode[] | null {
|
|
|
104
104
|
const code = (e.textContent ?? "").replace(/\n$/, "");
|
|
105
105
|
return { type: "codeBlock", ...(code ? { content: [{ type: "text", text: code }] } : {}) };
|
|
106
106
|
}
|
|
107
|
-
case "HR": return { type: "
|
|
107
|
+
case "HR": return { type: "spacer", attrs: { height: "md", line: true } };
|
|
108
108
|
case "IMG": {
|
|
109
109
|
const src = e.getAttribute("src") ?? "";
|
|
110
110
|
return src && isSafeUrl(src) ? { type: "image", attrs: { src, alt: e.getAttribute("alt") ?? undefined } } : null;
|
|
@@ -205,12 +205,12 @@ export function richToHtml(nodes: RichNode[]): string {
|
|
|
205
205
|
switch (n.type) {
|
|
206
206
|
case "paragraph": return `<p>${inl(n.content)}</p>`;
|
|
207
207
|
case "heading": return `<h${Number(n.attrs?.level) || 2}>${inl(n.content)}</h${Number(n.attrs?.level) || 2}>`;
|
|
208
|
-
case "bulletList":
|
|
208
|
+
case "bulletList": return `<ul>${kids()}</ul>`;
|
|
209
209
|
case "orderedList": return `<ol>${kids()}</ol>`;
|
|
210
|
-
case "listItem":
|
|
210
|
+
case "listItem": return `<li>${kids()}</li>`;
|
|
211
211
|
case "blockquote": return `<blockquote>${kids()}</blockquote>`;
|
|
212
|
-
case "codeBlock":
|
|
213
|
-
case "
|
|
212
|
+
case "codeBlock": return `<pre>${esc((n.content ?? []).map((c) => c.text ?? "").join(""))}</pre>`;
|
|
213
|
+
case "spacer": return n.attrs?.line === true ? "<hr>" : "";
|
|
214
214
|
case "image": return typeof n.attrs?.src === "string" && isSafeUrl(n.attrs.src) ? `<img src="${esc(n.attrs.src)}" alt="${esc(String(n.attrs.alt ?? ""))}">` : "";
|
|
215
215
|
case "table": return `<table>${kids()}</table>`;
|
|
216
216
|
case "tableRow": return `<tr>${kids()}</tr>`;
|
package/src/v2/core/host.ts
CHANGED
|
@@ -43,6 +43,8 @@ export interface EditorHost {
|
|
|
43
43
|
renderBlock?(node: { type: string; attrs?: Record<string, unknown>; content?: unknown[] }): Promise<string | null>;
|
|
44
44
|
/** Nơi đang soạn: "template" = template / phần dùng chung của theme (chèn được khối riêng của theme); mặc định = nội dung bài. */
|
|
45
45
|
scope?: "template" | "content";
|
|
46
|
+
/** Chỉ cho thử nghiệm / playground: hiện cả khối cũ đã gộp (người dùng thật không bao giờ thấy). */
|
|
47
|
+
showAllBlocks?: boolean;
|
|
46
48
|
/** Preset của theme (biến --ecx-*). */
|
|
47
49
|
presets?: StylePresets;
|
|
48
50
|
/**
|
package/src/v2/core/model.ts
CHANGED
|
@@ -28,14 +28,14 @@ export const newId = () => `b${prefix}${(++seq).toString(36)}`;
|
|
|
28
28
|
|
|
29
29
|
/** Các loại khối của lõi theo cách chứa nội dung. Khối lạ (plugin) suy từ nội dung. */
|
|
30
30
|
const CORE_KINDS: Record<string, BlockKind> = {
|
|
31
|
-
paragraph: "text", heading: "text", codeBlock: "text",
|
|
32
|
-
bulletList: "container", orderedList: "container", listItem: "container",
|
|
33
|
-
blockquote: "container", columns: "container", column: "container", group: "container",
|
|
34
|
-
buttons: "container", cover: "container",
|
|
31
|
+
paragraph: "text", heading: "text", codeBlock: "text",
|
|
32
|
+
bulletList: "container", orderedList: "container", listItem: "container",
|
|
33
|
+
blockquote: "container", columns: "container", column: "container", group: "container",
|
|
34
|
+
buttons: "container", cover: "container", details: "container", testimonials: "container", popup: "container", accordion: "container", tabs: "container", tab: "container", carousel: "container",
|
|
35
35
|
table: "container", tableRow: "container", tableCell: "container", tableHeader: "container",
|
|
36
36
|
query: "container", postTemplate: "container", queryEmpty: "container", stack: "container",
|
|
37
|
-
|
|
38
|
-
button: "atom", embed: "atom", spacer: "atom", customHtml: "atom", socialLinks: "atom", tableOfContents: "atom", countdown: "atom",
|
|
37
|
+
image: "atom", gallery: "atom", video: "atom", audio: "atom", file: "atom",
|
|
38
|
+
button: "atom", embed: "atom", spacer: "atom", customHtml: "atom", socialLinks: "atom", tableOfContents: "atom", countdown: "atom", shortcode: "atom",
|
|
39
39
|
};
|
|
40
40
|
const kindOverrides = new Map<string, BlockKind>();
|
|
41
41
|
export function setBlockKind(type: string, kind: BlockKind) { kindOverrides.set(type, kind); }
|
package/src/v2/core/registry.ts
CHANGED
|
@@ -37,6 +37,8 @@ export interface BlockVariation {
|
|
|
37
37
|
create?(): BNode;
|
|
38
38
|
/** Hiện trong thư viện khối bằng tên riêng (vd "Hàng ngang") thay vì "tên khối + tên biến thể". */
|
|
39
39
|
standalone?: boolean;
|
|
40
|
+
/** Ô cũ đã gộp vào khối cha (ô chọn bố cục / kiểu): ẩn khỏi bảng chèn, chỉ hiện khi host.showAllBlocks. */
|
|
41
|
+
legacy?: boolean;
|
|
40
42
|
/** Biến thể đang áp dụng cho khối này (hiện tên/biểu tượng đúng biến thể). */
|
|
41
43
|
isActive?(attrs: Record<string, any>): boolean;
|
|
42
44
|
}
|
|
@@ -59,7 +61,11 @@ export interface BlockType {
|
|
|
59
61
|
category?: BlockCategory;
|
|
60
62
|
/** Từ khoá tìm (không dấu). */
|
|
61
63
|
keywords?: string;
|
|
64
|
+
/** Tên khác (vd tên tiếng Việt cũ) — tìm bằng tên này vẫn ra khối ở hạng như tên chính. */
|
|
65
|
+
aliases?: string[];
|
|
62
66
|
kind: BlockKind;
|
|
67
|
+
/** Tên hiện ở bảng bên phải / đường dẫn theo thuộc tính (vd Nhóm rộng hết trang có khung nội dung là "Section"); undefined → dùng `title`. */
|
|
68
|
+
titleOf?(attrs: Record<string, unknown>): string | undefined;
|
|
63
69
|
/** Thuộc tính gắn dữ liệu được (attrs.bindings) — khối lõi: CORE_BINDABLE. */
|
|
64
70
|
bindable?: string[];
|
|
65
71
|
attributes?: Record<string, BlockAttrSpec>;
|
|
@@ -102,8 +108,17 @@ export interface BlockType {
|
|
|
102
108
|
supports?: { style?: boolean | string[]; anchor?: boolean; className?: boolean; blockStyle?: boolean; align?: boolean };
|
|
103
109
|
/** Tóm tắt một dòng (cây khối, khung chung). */
|
|
104
110
|
summary?(attrs: Record<string, any>): string | undefined;
|
|
111
|
+
/** Bố cục chọn sẵn (thẻ có hình thu nhỏ): hiện ngay sau khi chèn từ bảng chèn và ở đầu bảng phải. */
|
|
112
|
+
layouts?: BlockLayout[];
|
|
113
|
+
/** false = chỉ hiện lưới bố cục ở bảng phải, không bật ô chọn ngay sau khi chèn (khối nhỏ, bố cục đổi nhẹ). Mặc định: bật. */
|
|
114
|
+
layoutsOnInsert?: boolean;
|
|
115
|
+
/** Thuộc tính chỉ chỉnh bằng thẻ bố cục (mọi giá trị đều có thẻ): ẩn ô chọn trùng ở dưới để một việc chỉ có một chỗ chỉnh. */
|
|
116
|
+
layoutOwns?: string[];
|
|
105
117
|
}
|
|
106
118
|
|
|
119
|
+
/** Một bố cục chọn sẵn của khối: áp `attrs` lên khối; `thumb` là bản vẽ thu nhỏ (xem ui/variation-picker). */
|
|
120
|
+
export interface BlockLayout { id: string; label: string; attrs: Record<string, unknown>; thumb: ReactNode }
|
|
121
|
+
|
|
107
122
|
export class BlockTypeRegistry {
|
|
108
123
|
private types = new Map<string, BlockType>();
|
|
109
124
|
private listeners = new Set<() => void>();
|
package/src/v2/core/store.ts
CHANGED
|
@@ -593,19 +593,19 @@ export class EditorStore {
|
|
|
593
593
|
const b = loc.block;
|
|
594
594
|
const docId = loc.docId;
|
|
595
595
|
// Khối chứa bị khoá thêm khối: Enter không tạo khối mới
|
|
596
|
-
const newParent = loc.parent && (loc.parent.type === "listItem"
|
|
596
|
+
const newParent = loc.parent && (loc.parent.type === "listItem") && loc.index === 0 ? this.locate(loc.parent.id)?.parent?.id ?? null : loc.parent?.id ?? null;
|
|
597
597
|
if (!this.canInsertInto(newParent)) return;
|
|
598
598
|
let inline = b.inline!;
|
|
599
599
|
if (selectionEnd > offset) inline = joinInline(splitInline(inline, offset)[0], splitInline(inline, selectionEnd)[1]);
|
|
600
600
|
const len = inlineLength(inline);
|
|
601
|
-
const item = loc.parent && (loc.parent.type === "listItem"
|
|
601
|
+
const item = loc.parent && (loc.parent.type === "listItem") && loc.index === 0 ? this.locate(loc.parent.id) : null;
|
|
602
602
|
|
|
603
603
|
if (item) {
|
|
604
604
|
// Mục danh sách trống: thụt ra (lồng) hoặc thoát khỏi danh sách
|
|
605
605
|
if (!len && item.block.content!.length === 1) return this.outdentItem(item.block.id, true);
|
|
606
606
|
const [before, after] = splitInline(inline, offset);
|
|
607
607
|
const para = makeBlock("paragraph", {}, after);
|
|
608
|
-
const newItem = makeBlock(item.block.type,
|
|
608
|
+
const newItem = makeBlock(item.block.type, {}, [para]);
|
|
609
609
|
const blocks = this.state.docs[docId].blocks;
|
|
610
610
|
let next = mapBlock(blocks, id, (x) => ({ ...x, inline: before }));
|
|
611
611
|
next = insertBlocks(next, item.parent!.id, item.index + 1, [newItem]);
|
|
@@ -640,7 +640,7 @@ export class EditorStore {
|
|
|
640
640
|
const blocks = this.state.docs[docId].blocks;
|
|
641
641
|
const b = loc.block;
|
|
642
642
|
// Khối không xoá được (khoá): Backspace ở đầu chỉ đưa con trỏ về khối chữ trước
|
|
643
|
-
if (!this.canRemove(id) || (loc.parent && (loc.parent.type === "listItem"
|
|
643
|
+
if (!this.canRemove(id) || (loc.parent && (loc.parent.type === "listItem") && loc.index === 0 && !this.canRemove(loc.parent.id))) {
|
|
644
644
|
const order = flatten(blocks).filter((x) => x.inline);
|
|
645
645
|
const prev = order[order.findIndex((x) => x.id === id) - 1];
|
|
646
646
|
if (prev) this.focusText(prev.id, -1);
|
|
@@ -648,7 +648,7 @@ export class EditorStore {
|
|
|
648
648
|
}
|
|
649
649
|
|
|
650
650
|
// Đầu mục danh sách: nối vào mục trước, hoặc (mục đầu) thụt ra
|
|
651
|
-
if (loc.parent && (loc.parent.type === "listItem"
|
|
651
|
+
if (loc.parent && (loc.parent.type === "listItem") && loc.index === 0) {
|
|
652
652
|
const item = this.locate(loc.parent.id)!;
|
|
653
653
|
const prevItem = item.index > 0 ? item.parent!.content![item.index - 1] : null;
|
|
654
654
|
if (!prevItem) return this.outdentItem(item.block.id, true);
|
|
@@ -730,7 +730,7 @@ export class EditorStore {
|
|
|
730
730
|
blocks = mapBlock(blocks, prev.id, (pi) => {
|
|
731
731
|
const kids = pi.content ?? [];
|
|
732
732
|
const last = kids[kids.length - 1];
|
|
733
|
-
if (last && (last.type === "bulletList" || last.type === "orderedList"
|
|
733
|
+
if (last && (last.type === "bulletList" || last.type === "orderedList")) {
|
|
734
734
|
return { ...pi, content: [...kids.slice(0, -1), { ...last, content: [...last.content!, loc.block] }] };
|
|
735
735
|
}
|
|
736
736
|
return { ...pi, content: [...kids, makeBlock(listType, {}, [loc.block])] };
|
|
@@ -744,7 +744,7 @@ export class EditorStore {
|
|
|
744
744
|
const loc = this.locate(itemId);
|
|
745
745
|
if (!loc || !loc.parent || !this.canMove(itemId) || !this.canRemove(itemId)) return false;
|
|
746
746
|
const list = this.locate(loc.parent.id)!;
|
|
747
|
-
const outerItem = list.parent && (list.parent.type === "listItem"
|
|
747
|
+
const outerItem = list.parent && (list.parent.type === "listItem") ? this.locate(list.parent.id) : null;
|
|
748
748
|
const docId = loc.docId;
|
|
749
749
|
const firstId = loc.block.content![0].id;
|
|
750
750
|
const caret: Caret = this.lastCaret && this.lastCaret.id === firstId ? this.lastCaret : { id: firstId, offset: 0 };
|
|
@@ -765,9 +765,9 @@ export class EditorStore {
|
|
|
765
765
|
}
|
|
766
766
|
|
|
767
767
|
/** Khối chứa thoát ra được bằng Enter ở đoạn trống cuối / Backspace ở đầu. */
|
|
768
|
-
const ESCAPE_CONTAINERS = new Set(["blockquote", "
|
|
768
|
+
const ESCAPE_CONTAINERS = new Set(["blockquote", "details"]);
|
|
769
769
|
/** Enter ở cuối khối này tạo khối gì. */
|
|
770
|
-
const SPLIT_AS: Record<string, string> = { heading: "paragraph", codeBlock: "paragraph"
|
|
770
|
+
const SPLIT_AS: Record<string, string> = { heading: "paragraph", codeBlock: "paragraph" };
|
|
771
771
|
|
|
772
772
|
const firstText = (b: BNode): string | null => (b.inline ? b.id : b.content ? (b.content.map(firstText).find(Boolean) ?? null) : null);
|
|
773
773
|
const firstTextDeep = (b: BNode): BNode | null => {
|
package/src/v2/core/structure.ts
CHANGED
|
@@ -26,8 +26,6 @@ const RULES: Record<string, StructureRule> = {
|
|
|
26
26
|
bulletList: { allowedChildren: ["listItem"], wrapChildrenWith: "listItem", removeWhenEmpty: true },
|
|
27
27
|
orderedList: { allowedChildren: ["listItem"], wrapChildrenWith: "listItem", removeWhenEmpty: true },
|
|
28
28
|
listItem: { parents: ["bulletList", "orderedList"], wrapSelfWith: "bulletList", required: p, firstChild: "paragraph" },
|
|
29
|
-
taskList: { allowedChildren: ["taskItem"], wrapChildrenWith: "taskItem", removeWhenEmpty: true },
|
|
30
|
-
taskItem: { parents: ["taskList"], wrapSelfWith: "taskList", required: p, firstChild: "paragraph" },
|
|
31
29
|
// 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
30
|
columns: { allowedChildren: ["column"], wrapChildrenWith: "column" },
|
|
33
31
|
column: { parents: ["columns"], wrapSelfWith: "columns" },
|
|
@@ -41,7 +39,7 @@ const RULES: Record<string, StructureRule> = {
|
|
|
41
39
|
};
|
|
42
40
|
|
|
43
41
|
/** Mục danh sách / việc chỉ nhận chữ và danh sách lồng: khối lớn (bảng, ảnh, nhóm…) chèn mới thì phải ra ngoài danh sách. */
|
|
44
|
-
export const leavesItem = (parentType: string | null | undefined, child: string) => (parentType === "listItem"
|
|
42
|
+
export const leavesItem = (parentType: string | null | undefined, child: string) => (parentType === "listItem") && !/^(paragraph|bulletList|orderedList)$/.test(child);
|
|
45
43
|
|
|
46
44
|
const extra = new Map<string, StructureRule>();
|
|
47
45
|
export function setStructureRule(type: string, rule: StructureRule | null) {
|
package/src/v2/editor.css
CHANGED
|
@@ -372,6 +372,8 @@ body.is-dragging, body.is-dragging * { cursor: grabbing !important; user-select:
|
|
|
372
372
|
.ecx-state-tabs .ecx-seg-btn.is-active { background: var(--ecx-ed-panel); color: var(--ecx-ed-accent); box-shadow: 0 1px 2px rgb(14 40 58 / .12); }
|
|
373
373
|
.ecx-seg.is-text { display: flex; flex-wrap: wrap; width: 100%; }
|
|
374
374
|
.ecx-seg.is-text .ecx-seg-btn { flex: 1 1 auto; font: 500 12.5px var(--ecx-ed-font); white-space: nowrap; }
|
|
375
|
+
.ecx-css-input { font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
|
|
376
|
+
.ecx-field-warn { margin: 4px 0 0; padding-left: 16px; color: #b45309; font-size: 12px; }
|
|
375
377
|
.ecx-field-help { display: block; margin-top: 4px; font-size: 12px; line-height: 1.4; color: var(--ecx-ed-muted); }
|
|
376
378
|
.ecx-range-row { display: flex; align-items: center; gap: 10px; }
|
|
377
379
|
.ecx-range-row input[type=range] { flex: 1; accent-color: var(--ecx-ed-accent); }
|
|
@@ -529,6 +531,14 @@ body.is-dragging .ecx-section-adder { display: none; }
|
|
|
529
531
|
.ecx-icon-pop { position: absolute; z-index: 25; top: calc(100% + 6px); left: 0; right: 0; display: flex; flex-direction: column; gap: 8px; padding: 10px; border: 1px solid var(--ecx-ed-line); border-radius: 10px; background: var(--ecx-ed-panel); box-shadow: var(--ecx-ed-shadow); animation: ecx-pop .1s linear; }
|
|
530
532
|
.ecx-icon-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; max-height: 220px; overflow: auto; }
|
|
531
533
|
.ecx-icon-cell { display: grid; place-items: center; aspect-ratio: 1; border: 1px solid transparent; border-radius: 7px; background: none; color: var(--ecx-ed-fg); cursor: pointer; }
|
|
534
|
+
.ecx-icon-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--ecx-ed-line); }
|
|
535
|
+
.ecx-icon-tab { flex: 1; padding: 6px 8px; border: 0; border-bottom: 2px solid transparent; background: none; color: var(--ecx-ed-muted, inherit); font: 600 12px var(--ecx-ui-font); cursor: pointer; }
|
|
536
|
+
.ecx-icon-tab.is-active { color: var(--ecx-ed-accent); border-bottom-color: var(--ecx-ed-accent); }
|
|
537
|
+
.ecx-icon-cell-text { font-size: 10px; line-height: 1.1; padding: 2px; text-align: center; }
|
|
538
|
+
.ecx-icon-svg { display: flex; flex-direction: column; gap: 8px; }
|
|
539
|
+
.ecx-icon-svg textarea { font: 12px/1.4 ui-monospace, monospace; resize: vertical; }
|
|
540
|
+
.ecx-icon-svg-row { display: flex; align-items: center; gap: 12px; }
|
|
541
|
+
.ecx-icon-svg-preview { margin-left: auto; display: grid; place-items: center; width: 40px; height: 40px; border: 1px dashed var(--ecx-ed-line); border-radius: 8px; }
|
|
532
542
|
.ecx-icon-cell:hover { background: var(--ecx-ed-subtle); }
|
|
533
543
|
.ecx-icon-cell.is-active { border-color: var(--ecx-ed-accent); background: var(--ecx-ed-accent-soft); color: var(--ecx-ed-accent); }
|
|
534
544
|
|
|
@@ -576,6 +586,8 @@ body.is-dragging .ecx-section-adder { display: none; }
|
|
|
576
586
|
|
|
577
587
|
/* Hàng sửa một mục của danh sách (chữ; icon + liên kết + xoá) */
|
|
578
588
|
.ecx-item-row { display: flex; flex-direction: column; gap: 4px; padding: 8px; border: 1px solid var(--ecx-ed-line); border-radius: 8px; }
|
|
589
|
+
.ecx-item-colors { grid-column: 1 / -1; display: flex; gap: 6px; }
|
|
590
|
+
.ecx-item-colors > * { flex: 1; min-width: 0; }
|
|
579
591
|
.ecx-item-row-sub { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 28px; gap: 4px; align-items: center; }
|
|
580
592
|
|
|
581
593
|
.ecx-quick-colors { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px; max-width: 200px; }
|
|
@@ -592,3 +604,65 @@ body.is-dragging .ecx-section-adder { display: none; }
|
|
|
592
604
|
.ecx-textshadow { display: grid; gap: 10px; margin-top: 10px; }
|
|
593
605
|
.ecx-textshadow-row { display: grid; gap: 4px; font-size: 12px; color: var(--ecx-ed-muted, #5d6a75); }
|
|
594
606
|
.ecx-textshadow-row .ecx-range { width: 100%; }
|
|
607
|
+
|
|
608
|
+
/* Bố cục chọn sẵn: thẻ có hình thu nhỏ (bảng phải + ô chọn lúc vừa chèn) */
|
|
609
|
+
.ecx-layout-section { margin-bottom: 12px; }
|
|
610
|
+
.ecx-variations { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; margin-top: 6px; }
|
|
611
|
+
.ecx-variation { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 8px 6px 6px; border: 1px solid var(--ecx-ed-line); border-radius: 8px; background: var(--ecx-ed-panel); color: var(--ecx-ed-fg); font: 12px/1.25 var(--ecx-ed-font); cursor: pointer; transition: border-color .05s linear; }
|
|
612
|
+
.ecx-variation:hover { border-color: var(--ecx-ed-accent); }
|
|
613
|
+
.ecx-variation.is-active { border-color: var(--ecx-ed-accent); background: var(--ecx-ed-accent-soft); box-shadow: 0 0 0 1px var(--ecx-ed-accent); }
|
|
614
|
+
.ecx-vthumb { width: 100%; height: auto; display: block; }
|
|
615
|
+
.ecx-vthumb rect { fill: color-mix(in srgb, currentColor 18%, transparent); }
|
|
616
|
+
.ecx-vthumb rect.is-box, .ecx-vthumb polygon.is-box { fill: transparent; stroke: color-mix(in srgb, currentColor 32%, transparent); stroke-width: 1.2; }
|
|
617
|
+
.ecx-vthumb .is-accent { fill: var(--ecx-ed-accent); stroke: none; }
|
|
618
|
+
.ecx-vthumb rect.is-box.is-accent { fill: transparent; stroke: var(--ecx-ed-accent); }
|
|
619
|
+
.ecx-vthumb path.is-line { fill: none; stroke: var(--ecx-ed-accent); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
|
|
620
|
+
|
|
621
|
+
/* Mục con đang chọn (Icon List…): hàng được tô, chỉ hàng này mở icon / liên kết */
|
|
622
|
+
.ecx-item-row.is-active { border-color: var(--ecx-ed-accent); background: var(--ecx-ed-accent-soft); }
|
|
623
|
+
.ecx-item-row-head { display: flex; align-items: center; gap: 6px; }
|
|
624
|
+
.ecx-item-row-head .ecx-input { flex: 1; min-width: 0; }
|
|
625
|
+
.ecx-item-n { flex: none; width: 20px; height: 20px; border-radius: 50%; background: var(--ecx-ed-subtle); color: var(--ecx-ed-muted); font: 600 11px/20px var(--ecx-ed-font); text-align: center; }
|
|
626
|
+
.ecx-item-row.is-active .ecx-item-n { background: var(--ecx-ed-accent); color: #fff; }
|
|
627
|
+
|
|
628
|
+
/* Xem trước thật của khối ở bảng chèn */
|
|
629
|
+
.ecx-block-preview { width: 320px; }
|
|
630
|
+
.ecx-block-preview-live { grid-column: 1 / -1; position: relative; height: 190px; overflow: hidden; border: 1px solid var(--ecx-ed-line); border-radius: 8px; background: #fff; pointer-events: none; }
|
|
631
|
+
.ecx-block-preview-live-inner { transform-origin: 0 0; }
|
|
632
|
+
/* Chọn icon ngay trên thanh công cụ của khối: lưới icon nằm trong menu thả (Dropdown) */
|
|
633
|
+
.ecx-tool-icon-pop { display: grid; gap: 8px; width: 288px; padding: 8px; }
|
|
634
|
+
.ecx-tool-icon-pop .ecx-icon-grid { max-height: 220px; overflow: auto; }
|
|
635
|
+
|
|
636
|
+
/* Hộp Viết bằng AI: gợi ý nhanh, tuỳ chọn giọng văn / độ dài, kết quả đúng dạng sẽ chèn */
|
|
637
|
+
.ecx-ai-chips { display: flex; flex-wrap: wrap; gap: 6px; }
|
|
638
|
+
.ecx-ai-opts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
|
639
|
+
.ecx-ai-opt { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ecx-ed-muted, #667085); }
|
|
640
|
+
.ecx-ai-text h2, .ecx-ai-text h3, .ecx-ai-text h4 { margin: .9em 0 .35em; line-height: 1.3; } .ecx-ai-text h2 { font-size: 1.15em; } .ecx-ai-text h3, .ecx-ai-text h4 { font-size: 1.05em; }
|
|
641
|
+
.ecx-ai-text h2:first-child, .ecx-ai-text h3:first-child { margin-top: 0; }
|
|
642
|
+
.ecx-ai-text ul, .ecx-ai-text ol { margin: 0 0 .6em; padding-left: 1.3em; }
|
|
643
|
+
|
|
644
|
+
/* Ô chọn bằng hình (thẻ icon hai lớp): hiệu ứng chuyển động, bóng, khung icon… */
|
|
645
|
+
.ecx-pick-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; width: 100%; }
|
|
646
|
+
.ecx-pick { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 4px 5px; border: 1px solid var(--ecx-ed-line); border-radius: 8px; background: var(--ecx-ed-panel); color: var(--ecx-ed-fg); font: inherit; font-size: 11px; line-height: 1.2; cursor: pointer; transition: border-color .05s linear, background .05s linear; }
|
|
647
|
+
.ecx-pick:hover { border-color: var(--ecx-ed-accent); }
|
|
648
|
+
.ecx-pick.is-active { border-color: var(--ecx-ed-accent); background: var(--ecx-ed-accent-soft); box-shadow: inset 0 0 0 1px var(--ecx-ed-accent); }
|
|
649
|
+
.ecx-pick:focus-visible { outline: 2px solid var(--ecx-ed-accent); outline-offset: 1px; }
|
|
650
|
+
.ecx-pick-thumb { width: 100%; max-width: 72px; height: auto; }
|
|
651
|
+
.ecx-pick-thumb .is-solid { fill: var(--ecx-ed-accent); }
|
|
652
|
+
.ecx-pick-thumb .is-ghost { fill: color-mix(in srgb, var(--ecx-ed-accent) 14%, transparent); stroke: var(--ecx-ed-accent); stroke-width: 1.2; stroke-dasharray: 3 2.5; }
|
|
653
|
+
.ecx-pick-thumb .is-arrow { stroke: var(--ecx-ed-fg); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; opacity: .7; }
|
|
654
|
+
.ecx-pick-thumb .is-glyph { fill: #fff; }
|
|
655
|
+
.ecx-pick-label { max-width: 100%; overflow-wrap: anywhere; text-align: center; }
|
|
656
|
+
|
|
657
|
+
/* Bảng lệnh */
|
|
658
|
+
.ecx-palette-backdrop { position: fixed; inset: 0; z-index: 90; display: flex; justify-content: center; align-items: flex-start; padding-top: 12vh; background: rgb(15 23 42 / .35); }
|
|
659
|
+
.ecx-palette { width: min(640px, calc(100vw - 24px)); max-height: 70vh; display: flex; flex-direction: column; border: 1px solid var(--ecx-ed-line); border-radius: 14px; background: var(--ecx-ed-panel); box-shadow: 0 24px 60px rgb(15 23 42 / .28); overflow: hidden; font: 14px var(--ecx-ui-font); color: var(--ecx-ed-fg); }
|
|
660
|
+
.ecx-palette-input { display: flex; align-items: center; gap: 10px; padding: 0 14px; border-bottom: 1px solid var(--ecx-ed-line); }
|
|
661
|
+
.ecx-palette-input input { flex: 1; min-width: 0; height: 52px; border: 0; outline: 0; background: transparent; font: inherit; font-size: 15px; color: inherit; }
|
|
662
|
+
.ecx-palette-list { overflow: auto; padding: 6px; }
|
|
663
|
+
.ecx-palette-group { padding: 8px 10px 4px; font-size: 11px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ecx-ed-muted, #6b7785); }
|
|
664
|
+
.ecx-palette-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; padding: 9px 10px; border: 0; border-radius: 8px; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
|
|
665
|
+
.ecx-palette-item.is-active { background: var(--ecx-ed-accent-soft); color: var(--ecx-ed-accent); }
|
|
666
|
+
.ecx-palette-item kbd { font: 11px ui-monospace, monospace; padding: 2px 6px; border: 1px solid var(--ecx-ed-line); border-radius: 5px; color: var(--ecx-ed-muted, #6b7785); }
|
|
667
|
+
.ecx-palette-empty { margin: 0; padding: 20px; text-align: center; color: var(--ecx-ed-muted, #6b7785); }
|
|
668
|
+
.ecx-palette-foot { padding: 8px 14px; border-top: 1px solid var(--ecx-ed-line); font-size: 12px; color: var(--ecx-ed-muted, #6b7785); }
|
package/src/v2/editor.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ensurePresets } from "./core/block-presets";
|
|
1
|
+
import { ensurePresets, localBlockPresets } from "./core/block-presets";
|
|
2
2
|
import type { RichNode } from "@ecomcx/ecomweb-content";
|
|
3
|
-
import { normalizeContent } from "@ecomcx/ecomweb-content";
|
|
4
|
-
import { ListTree, Monitor, Plus, Redo2, Smartphone, Tablet, Undo2, X, PanelRight, ZoomOut } from "lucide-react";
|
|
3
|
+
import { normalizeContent, CONTENT_VERSION } from "@ecomcx/ecomweb-content";
|
|
4
|
+
import { Command as CommandIcon, ListTree, Monitor, Plus, Redo2, Smartphone, Tablet, Undo2, X, PanelRight, ZoomOut } from "lucide-react";
|
|
5
5
|
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode } from "react";
|
|
6
6
|
import { coreBlockTypes, registerCoreBlocks } from "./blocks";
|
|
7
7
|
import { registeredPatterns, syncPluginBlocks } from "./blocks/compat";
|
|
@@ -11,6 +11,7 @@ import { Canvas, type Device } from "./canvas/canvas";
|
|
|
11
11
|
import { EditorContext, EditorUi, useEditor, useEditorState, useUiTick, type EditorContextValue } from "./context";
|
|
12
12
|
import type { EditorHost } from "./core/host";
|
|
13
13
|
import { InserterPanel, InserterPopover } from "./ui/inserter";
|
|
14
|
+
import { CommandPalette } from "./ui/command-palette";
|
|
14
15
|
import { SectionAdder } from "./ui/section-adder";
|
|
15
16
|
import { AiWrite } from "./ui/ai";
|
|
16
17
|
import { labelsV2, type V2Labels } from "./labels";
|
|
@@ -43,7 +44,7 @@ export function splitContent(value: unknown): { blocks: BNode[]; extra: Array<{
|
|
|
43
44
|
}
|
|
44
45
|
/** Khối → nội dung lưu (ContentDoc chuẩn của CMS). */
|
|
45
46
|
export function contentFromBlocks(blocks: BNode[], extra: Array<{ type: string; data: unknown; id?: string }> = []) {
|
|
46
|
-
return { version:
|
|
47
|
+
return { version: CONTENT_VERSION, blocks: [{ type: "richtext", data: docFromBlocks(blocks) }, ...extra] };
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
export interface EditorProviderProps {
|
|
@@ -93,6 +94,7 @@ export function EditorProvider({ store: given, value, kind, title, host = {}, la
|
|
|
93
94
|
store.setFrame("frame");
|
|
94
95
|
}, [frame, store]);
|
|
95
96
|
const ui = useMemo(() => new EditorUi(), []);
|
|
97
|
+
ui.currentIds = () => store.currentIds;
|
|
96
98
|
const ownPrefs = useMemo(() => (givenPrefs ? null : new PrefsStore(prefsStorage)), [givenPrefs, prefsStorage]);
|
|
97
99
|
const prefs = givenPrefs ?? ownPrefs!;
|
|
98
100
|
// Đang mở trình soạn: focus không ở ô nhập nào (vd Tab ra khỏi khung soạn) thì Backspace / ⌘← / ⌘→ là lệnh quay lại /
|
|
@@ -109,7 +111,7 @@ export function EditorProvider({ store: given, value, kind, title, host = {}, la
|
|
|
109
111
|
// Khối của plugin (registerBlock) + mẫu có sẵn của lõi
|
|
110
112
|
useEffect(() => syncPluginBlocks(registry), [registry]);
|
|
111
113
|
// Kiểu đặt sẵn / mặc định khi chèn: nạp ngay khi mở trình soạn (khối chèn đầu tiên đã nhận mặc định)
|
|
112
|
-
useEffect(() => ensurePresets(host.blockPresets
|
|
114
|
+
useEffect(() => ensurePresets((host.blockPresets ?? localBlockPresets).load), [host]);
|
|
113
115
|
useEffect(() => { registerCoreBindingSources(allLabels.lang); }, [allLabels.lang]);
|
|
114
116
|
useEffect(() => { if (!registeredPatterns().some((p) => p.name.startsWith("core/"))) registerCorePatterns(allLabels.lang); }, [allLabels.lang]);
|
|
115
117
|
const ctx: EditorContextValue = useMemo(() => ({ store, registry, host, labels: allLabels, ui, prefs }), [store, registry, host, allLabels, ui, prefs]);
|
|
@@ -181,8 +183,12 @@ export function BlockEditor({ className, onSave, header, actions, menuExtra, can
|
|
|
181
183
|
|
|
182
184
|
function EditorShell({ className, onSave, header, actions, menuExtra, canvasBefore, defaultDevice, blockStyles, saveHandle, onDirtyChange }: Pick<BlockEditorProps, "saveHandle" | "onDirtyChange" | "className" | "onSave" | "header" | "actions" | "menuExtra" | "canvasBefore" | "defaultDevice" | "blockStyles">) {
|
|
183
185
|
const ctx = useEditor();
|
|
186
|
+
const previewRender = (value: { version: number; blocks: unknown[] }) => <EditorProvider value={value} kind="pattern-preview" registry={ctx.registry} labels={ctx.labels} host={{ ...ctx.host, documents: undefined, ai: undefined }}><Canvas preview /></EditorProvider>;
|
|
187
|
+
ctx.ui.renderPreview = previewRender;
|
|
184
188
|
const { store, labels } = ctx;
|
|
185
189
|
const p = usePrefs(ctx.prefs);
|
|
190
|
+
const [palette, setPalette] = useState(false);
|
|
191
|
+
const [inserterQuery, setInserterQuery] = useState<string | undefined>(undefined);
|
|
186
192
|
const [left, setLeft] = useState<"inserter" | "list" | null>(() => (ctx.prefs.get().showListView ? "list" : null));
|
|
187
193
|
useEffect(() => { ctx.ui.browseAll = () => setLeft("inserter"); return () => { ctx.ui.browseAll = null; }; }, [ctx.ui]);
|
|
188
194
|
// Điện thoại: bảng bên phải mở đè lên canvas nên mặc định đóng (mở bằng nút ở thanh trên)
|
|
@@ -218,6 +224,10 @@ function EditorShell({ className, onSave, header, actions, menuExtra, canvasBefo
|
|
|
218
224
|
const onKey = (e: KeyboardEvent) => {
|
|
219
225
|
const mod = e.metaKey || e.ctrlKey;
|
|
220
226
|
if (mod && e.key.toLowerCase() === "s") { e.preventDefault(); void saveRef.current(); }
|
|
227
|
+
// Bảng lệnh: ⌘⇧P luôn mở; ⌘K chỉ khi không đang gõ chữ (trong ô chữ ⌘K là chèn liên kết)
|
|
228
|
+
const t = e.target as HTMLElement | null;
|
|
229
|
+
const typing = !!t && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
|
|
230
|
+
if (mod && ((e.shiftKey && e.key.toLowerCase() === "p") || (!e.shiftKey && !e.altKey && e.key.toLowerCase() === "k" && !typing))) { e.preventDefault(); setPalette(true); }
|
|
221
231
|
// ⌘⇧⌥M: đổi soạn trực quan ↔ soạn mã
|
|
222
232
|
if (mod && e.shiftKey && e.altKey && e.code === "KeyM") { e.preventDefault(); ctx.prefs.set({ mode: ctx.prefs.get().mode === "code" ? "visual" : "code" }); }
|
|
223
233
|
};
|
|
@@ -235,10 +245,11 @@ function EditorShell({ className, onSave, header, actions, menuExtra, canvasBefo
|
|
|
235
245
|
<div className="ecx-topbar-start">
|
|
236
246
|
{header}
|
|
237
247
|
{!p.distractionFree && <ToolButton label={labels.addBlock} active={left === "inserter"} className="ecx-tool-primary" onClick={() => setLeft(left === "inserter" ? null : "inserter")}>{left === "inserter" ? <X className="ecx-icon" /> : <Plus className="ecx-icon" />}</ToolButton>}
|
|
248
|
+
{!p.distractionFree && <ToolButton label={`${labels.commandPalette} (${/Mac|iPhone|iPad/.test(typeof navigator === "undefined" ? "" : navigator.platform) ? "⌘⇧P" : "Ctrl+Shift+P"})`} onClick={() => setPalette(true)}><CommandIcon className="ecx-icon" /></ToolButton>}
|
|
238
249
|
<ToolButton label={labels.undo} disabled={!store.canUndo} onClick={() => store.undo()}><Undo2 className="ecx-icon" /></ToolButton>
|
|
239
250
|
<ToolButton label={labels.redo} disabled={!store.canRedo} onClick={() => store.redo()}><Redo2 className="ecx-icon" /></ToolButton>
|
|
240
251
|
{!p.distractionFree && <ToolButton label={labels.listView} active={left === "list"} onClick={() => setLeft(left === "list" ? null : "list")}><ListTree className="ecx-icon" /></ToolButton>}
|
|
241
|
-
{!p.distractionFree && p.mode === "visual" && <PatternLibraryButton renderPreview={
|
|
252
|
+
{!p.distractionFree && p.mode === "visual" && <PatternLibraryButton renderPreview={previewRender} />}
|
|
242
253
|
<FrameToggle />
|
|
243
254
|
{p.mode === "visual" && <ZoomToggle />}
|
|
244
255
|
</div>
|
|
@@ -254,10 +265,14 @@ function EditorShell({ className, onSave, header, actions, menuExtra, canvasBefo
|
|
|
254
265
|
)}
|
|
255
266
|
</div>
|
|
256
267
|
</div>
|
|
268
|
+
<CommandPalette open={palette} onClose={() => setPalette(false)} actions={{
|
|
269
|
+
save: () => void saveRef.current(), toggleList: () => setLeft((l) => (l === "list" ? null : "list")), toggleSettings: () => setRight((r) => !r),
|
|
270
|
+
openInserter: (q) => { setInserterQuery(q); setLeft("inserter"); },
|
|
271
|
+
}} />
|
|
257
272
|
{error && <div className="ecx-notice is-error" role="alert">{error}</div>}
|
|
258
273
|
{p.fixedToolbar && p.mode === "visual" && <div className="ecx-fixed-toolbar"><BlockToolbar placement="fixed" /></div>}
|
|
259
274
|
<div className="ecx-body">
|
|
260
|
-
{left && !p.distractionFree && <aside className="ecx-left">{left === "inserter" ? <InserterPanel onClose={() => setLeft(null)} onInserted={() => { /* Điện thoại: bảng đè lên canvas nên chèn xong thì đóng để tiếp tục soạn */ if (window.innerWidth <= 640) setLeft(null); }} /> : <ListView onClose={() => setLeft(null)} />}</aside>}
|
|
275
|
+
{left && !p.distractionFree && <aside className="ecx-left">{left === "inserter" ? <InserterPanel key={inserterQuery ?? ""} initialQuery={inserterQuery} onClose={() => setLeft(null)} onInserted={() => { /* Điện thoại: bảng đè lên canvas nên chèn xong thì đóng để tiếp tục soạn */ if (window.innerWidth <= 640) setLeft(null); }} /> : <ListView onClose={() => setLeft(null)} />}</aside>}
|
|
261
276
|
<main className="ecx-main">{p.mode === "code" ? <CodeEditor /> : <Canvas before={canvasBefore} />}</main>
|
|
262
277
|
{right && !p.distractionFree && <aside className="ecx-right"><Inspector blockStyles={blockStyles} /></aside>}
|
|
263
278
|
</div>
|