@ecomcx/ecomweb-editor 1.18.0 → 1.20.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 +25 -7
- package/src/index.ts +26 -25
- package/src/{ask.tsx → legacy/ask.tsx} +2 -2
- package/src/{blockMeta.ts → legacy/block-meta.ts} +1 -1
- package/src/{blocks.tsx → legacy/blocks.tsx} +8 -32
- package/src/{content.ts → legacy/content.ts} +1 -1
- package/src/{handle.tsx → legacy/drag-handle.tsx} +3 -3
- package/src/{dynamic.tsx → legacy/dynamic.tsx} +7 -3
- package/src/{Editor.tsx → legacy/editor.tsx} +9 -9
- package/src/{extensions.ts → legacy/extensions.ts} +1 -1
- package/src/legacy/insert-pattern.ts +13 -0
- package/src/{inserter.tsx → legacy/inserter.tsx} +6 -5
- package/src/{inspector.tsx → legacy/inspector.tsx} +7 -7
- package/src/{layoutUi.tsx → legacy/layout-ui.tsx} +6 -5
- package/src/{layout.tsx → legacy/layout.tsx} +2 -2
- package/src/{media.ts → legacy/media.ts} +1 -1
- package/src/{nodes.tsx → legacy/nodes.tsx} +2 -2
- package/src/{outline.tsx → legacy/outline.tsx} +4 -4
- package/src/legacy/panels.ts +22 -0
- package/src/{rank.ts → legacy/rank.ts} +2 -2
- package/src/{registry.ts → legacy/registry.ts} +1 -1
- package/src/{handles.tsx → legacy/resize-handles.tsx} +1 -1
- package/src/{styleattr.ts → legacy/style-attr.ts} +15 -1
- package/src/{synced.tsx → legacy/synced.tsx} +2 -2
- package/src/{toolbar.tsx → legacy/toolbar.tsx} +7 -7
- package/src/{unknown.tsx → legacy/unknown.tsx} +1 -1
- package/src/shared/block-defs.ts +38 -0
- package/src/shared/color-swatch.tsx +19 -0
- package/src/{panels.ts → shared/panel-store.ts} +4 -21
- package/src/{patterns.ts → shared/patterns.ts} +0 -12
- package/src/{stylepanel.tsx → shared/style-panel.tsx} +2 -2
- package/src/{types.ts → shared/types.ts} +2 -0
- package/src/v2/blocks/compat.tsx +51 -0
- package/src/v2/blocks/index.ts +42 -0
- package/src/v2/blocks/layout.tsx +281 -0
- package/src/v2/blocks/media-actions.tsx +76 -0
- package/src/v2/blocks/media.tsx +234 -0
- package/src/v2/blocks/resize.tsx +111 -0
- package/src/v2/blocks/text.tsx +150 -0
- package/src/v2/blocks/theme.tsx +293 -0
- package/src/v2/blocks/tools.tsx +72 -0
- package/src/v2/canvas/block-list.tsx +216 -0
- package/src/v2/canvas/canvas-css.ts +131 -0
- package/src/v2/canvas/canvas.tsx +379 -0
- package/src/v2/canvas/dnd.ts +192 -0
- package/src/v2/context.tsx +146 -0
- package/src/v2/core/bindings.ts +64 -0
- package/src/v2/core/clipboard.ts +238 -0
- package/src/v2/core/host.ts +80 -0
- package/src/v2/core/model.ts +157 -0
- package/src/v2/core/ops.ts +151 -0
- package/src/v2/core/patterns.ts +10 -0
- package/src/v2/core/registry.ts +135 -0
- package/src/v2/core/store.ts +804 -0
- package/src/v2/core/structure.ts +106 -0
- package/src/v2/core/tables.ts +194 -0
- package/src/v2/editor.css +324 -0
- package/src/v2/editor.tsx +288 -0
- package/src/v2/index.ts +28 -0
- package/src/v2/labels.ts +349 -0
- package/src/v2/prefs.ts +69 -0
- package/src/v2/text/paste.ts +75 -0
- package/src/v2/text/rich-schema.ts +96 -0
- package/src/v2/text/rich-text.tsx +512 -0
- package/src/v2/ui/attr-form.tsx +182 -0
- package/src/v2/ui/attr-hints.ts +129 -0
- package/src/v2/ui/bindings.tsx +91 -0
- package/src/v2/ui/block-descriptions.ts +34 -0
- package/src/v2/ui/color-field.tsx +17 -0
- package/src/v2/ui/doc-title.tsx +45 -0
- package/src/v2/ui/inserter.tsx +300 -0
- package/src/v2/ui/inspector.tsx +85 -0
- package/src/v2/ui/layout-panel.tsx +159 -0
- package/src/v2/ui/list-view.tsx +65 -0
- package/src/v2/ui/options.tsx +242 -0
- package/src/v2/ui/style-controls.tsx +57 -0
- package/src/v2/ui/style-tab.tsx +158 -0
- package/src/v2/ui/toolbar.tsx +402 -0
- package/src/v2/ui/unit-controls.tsx +99 -0
- /package/src/{basic.tsx → legacy/basic.tsx} +0 -0
- /package/src/{blockops.ts → legacy/block-ops.ts} +0 -0
- /package/src/{boundary.tsx → legacy/boundary.tsx} +0 -0
- /package/src/{editor.css → legacy/editor.css} +0 -0
- /package/src/{insert.ts → legacy/insert.ts} +0 -0
- /package/src/{labels.ts → shared/labels.ts} +0 -0
- /package/src/{paste.ts → shared/paste.ts} +0 -0
- /package/src/{util.ts → shared/util.ts} +0 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { StylePresets } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import type { MediaProvider } from "../../shared/types";
|
|
3
|
+
import type { BNode } from "./model";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Mọi thứ trình soạn cần từ ứng dụng nhúng — trình soạn không gọi API của CMS nào.
|
|
7
|
+
* Dự án khác chỉ cần viết lại lớp này (thư viện ảnh, chỗ lưu tài liệu lồng nhau…).
|
|
8
|
+
*/
|
|
9
|
+
export interface EditorHost {
|
|
10
|
+
/** Thư viện media (không có → nhập URL). */
|
|
11
|
+
media?: MediaProvider;
|
|
12
|
+
/**
|
|
13
|
+
* Tài liệu lồng trong tài liệu đang sửa: phần dùng chung (header/footer), mẫu đồng bộ, nội dung bài trong template.
|
|
14
|
+
* `kind` + `ref` lấy từ thuộc tính khối (templatePart.slug, syncedPattern.ref…).
|
|
15
|
+
*/
|
|
16
|
+
documents?: DocumentHost;
|
|
17
|
+
/** Dữ liệu thật cho khối động (tên website, menu, bài mẫu…) — không có thì khối động hiện tên khối. */
|
|
18
|
+
dynamic?: DynamicData;
|
|
19
|
+
/** Khung soạn: CSS của website (đường dẫn hoặc nội dung CSS) để soạn trông giống hệt trang thật. */
|
|
20
|
+
canvasStyles?: Array<string | { css: string }>;
|
|
21
|
+
/** Preset của theme (biến --ecx-*). */
|
|
22
|
+
presets?: StylePresets;
|
|
23
|
+
/** Chữ "%khoá%" của plugin → chữ theo ngôn ngữ. */
|
|
24
|
+
translate?(text: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* "Tạo mẫu" từ các khối đang chọn: ứng dụng hỏi tên / nhóm / đồng bộ rồi lưu. Trả về mẫu đã tạo (đồng bộ → các khối được
|
|
27
|
+
* thay bằng tham chiếu tới mẫu), null = huỷ. Không có → ẩn lệnh.
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Lựa chọn cho ô cài đặt của khối (vị trí menu, loại bài, phân loại, chuyên mục / thẻ, phần dùng chung, cỡ ảnh) — dữ liệu
|
|
31
|
+
* thật của website. Không có → ô gõ tay.
|
|
32
|
+
*/
|
|
33
|
+
choices?(kind: ChoiceKind, args?: Record<string, string>): Promise<Choice[]> | Choice[];
|
|
34
|
+
/** Bài thật cho "Danh sách bài" theo thiết lập của khối (soạn thấy đúng bài sẽ hiện). Không có → dynamic.posts. */
|
|
35
|
+
queryPosts?(query: QueryArgs): Promise<DynamicPost[]>;
|
|
36
|
+
createPattern?(content: SavedContent): Promise<{ ref: number; synced: boolean } | null>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Nội dung lưu (ContentDoc của @ecomcx/ecomweb-content). */
|
|
40
|
+
export interface SavedContent { version: number; blocks: Array<{ type: string; data: unknown; id?: string }> }
|
|
41
|
+
|
|
42
|
+
export interface NestedDocRef {
|
|
43
|
+
kind: string;
|
|
44
|
+
ref: unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface LoadedDoc {
|
|
47
|
+
title?: string;
|
|
48
|
+
/** Cây `doc` của định dạng lưu (RichNode) hoặc ContentDoc. */
|
|
49
|
+
content: unknown;
|
|
50
|
+
/** Người dùng không có quyền sửa → chỉ xem. */
|
|
51
|
+
readOnly?: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface DocumentHost {
|
|
54
|
+
/** Định danh tài liệu trong store (mặc định `${kind}:${ref}`). */
|
|
55
|
+
idOf?(ref: NestedDocRef): string;
|
|
56
|
+
load(ref: NestedDocRef): Promise<LoadedDoc | null>;
|
|
57
|
+
/** Lưu một tài liệu lồng (trình soạn gọi cho từng tài liệu có thay đổi khi bấm Lưu). `content` là ContentDoc chuẩn. */
|
|
58
|
+
save?(ref: NestedDocRef, blocks: BNode[], content: SavedContent): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type DynamicPost = { title?: string; date?: string; author?: string; excerpt?: string; image?: string | null; terms?: string[]; comments?: number; link?: string };
|
|
62
|
+
|
|
63
|
+
/** Thiết lập của khối "Danh sách bài" (attrs của query). */
|
|
64
|
+
export interface QueryArgs { inherit: boolean; postType: string; perPage: number; category?: string; tag?: string; orderBy: string; order: "asc" | "desc" }
|
|
65
|
+
|
|
66
|
+
export type ChoiceKind = "menuLocation" | "postType" | "taxonomy" | "term" | "templatePart" | "imageSize";
|
|
67
|
+
export interface Choice { value: string; label: string }
|
|
68
|
+
|
|
69
|
+
export interface DynamicData {
|
|
70
|
+
site?: { title?: string; tagline?: string; logo?: string | null };
|
|
71
|
+
menus?: Record<string, Array<{ title: string; url?: string }>>;
|
|
72
|
+
post?: DynamicPost;
|
|
73
|
+
/** Trang lưu trữ mẫu (tiêu đề, mô tả chuyên mục) cho khối Tiêu đề trang lưu trữ / Mô tả chuyên mục. */
|
|
74
|
+
archive?: { title?: string; description?: string };
|
|
75
|
+
/** Bài mẫu cho "Danh sách bài" khi không có queryPosts. */
|
|
76
|
+
posts?: DynamicPost[];
|
|
77
|
+
languages?: string[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const docIdOf = (host: EditorHost, ref: NestedDocRef) => host.documents?.idOf?.(ref) ?? `${ref.kind}:${String(ref.ref)}`;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { RichMark, RichNode } from "@ecomcx/ecomweb-content";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mô hình của trình soạn v2: cây khối (không phải một tài liệu văn bản liền như ProseMirror).
|
|
5
|
+
* - Khối "text" (đoạn, tiêu đề, mã…): `inline` là chữ có định dạng — mỗi khối một ProseMirror nhỏ để gõ.
|
|
6
|
+
* - Khối "container" (nhóm, cột, danh sách, ô bảng…): `content` là các khối con.
|
|
7
|
+
* - Khối "atom" (ảnh, đường kẻ, khối động…): chỉ có thuộc tính; `raw` giữ nguyên nội dung con không hiểu được.
|
|
8
|
+
* Đọc/ghi đúng định dạng lưu trữ RichNode của @ecomcx/ecomweb-content → website, máy chủ không đổi gì.
|
|
9
|
+
*/
|
|
10
|
+
export type BlockKind = "text" | "container" | "atom";
|
|
11
|
+
|
|
12
|
+
export interface BNode {
|
|
13
|
+
/** Định danh trong phiên soạn (không lưu xuống dữ liệu). */
|
|
14
|
+
id: string;
|
|
15
|
+
type: string;
|
|
16
|
+
attrs: Record<string, unknown>;
|
|
17
|
+
/** Khối con (container). */
|
|
18
|
+
content?: BNode[];
|
|
19
|
+
/** Chữ có định dạng (text): text, hardBreak, nút inline khác — đúng dạng RichNode. */
|
|
20
|
+
inline?: RichNode[];
|
|
21
|
+
/** Nội dung con giữ nguyên (atom có content mà trình soạn không sửa). */
|
|
22
|
+
raw?: RichNode[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let seq = 0;
|
|
26
|
+
const prefix = Math.random().toString(36).slice(2, 6);
|
|
27
|
+
export const newId = () => `b${prefix}${(++seq).toString(36)}`;
|
|
28
|
+
|
|
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
|
+
const CORE_KINDS: Record<string, BlockKind> = {
|
|
31
|
+
paragraph: "text", heading: "text", codeBlock: "text", preformatted: "text",
|
|
32
|
+
bulletList: "container", orderedList: "container", listItem: "container", taskList: "container", taskItem: "container",
|
|
33
|
+
blockquote: "container", columns: "container", column: "container", group: "container", callout: "container",
|
|
34
|
+
buttons: "container", cover: "container", mediaText: "container", pullquote: "text", details: "container",
|
|
35
|
+
table: "container", tableRow: "container", tableCell: "container", tableHeader: "container",
|
|
36
|
+
query: "container", postTemplate: "container", queryEmpty: "container", stack: "container",
|
|
37
|
+
horizontalRule: "atom", image: "atom", gallery: "atom", video: "atom", audio: "atom", file: "atom", youtube: "atom",
|
|
38
|
+
button: "atom", embed: "atom", spacer: "atom", customHtml: "atom", socialLinks: "atom", tableOfContents: "atom",
|
|
39
|
+
};
|
|
40
|
+
const kindOverrides = new Map<string, BlockKind>();
|
|
41
|
+
export function setBlockKind(type: string, kind: BlockKind) { kindOverrides.set(type, kind); }
|
|
42
|
+
|
|
43
|
+
const INLINE_TYPES = new Set(["text", "hardBreak"]);
|
|
44
|
+
const inlineOverrides = new Set<string>();
|
|
45
|
+
/** Đăng ký loại nút inline (plugin): luôn nằm trong chữ, không thành khối. */
|
|
46
|
+
export function setInlineType(type: string) { inlineOverrides.add(type); }
|
|
47
|
+
export const isInlineType = (type: string) => INLINE_TYPES.has(type) || inlineOverrides.has(type);
|
|
48
|
+
|
|
49
|
+
export function kindOf(type: string, sample?: RichNode): BlockKind {
|
|
50
|
+
const k = kindOverrides.get(type) ?? CORE_KINDS[type];
|
|
51
|
+
if (k) return k;
|
|
52
|
+
if (!sample?.content?.length) return "atom";
|
|
53
|
+
if (sample.content.every((c) => isInlineType(c.type))) return "text";
|
|
54
|
+
if (sample.content.every((c) => !isInlineType(c.type))) return "container";
|
|
55
|
+
return "atom";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const clean = (attrs: Record<string, unknown> | undefined) => {
|
|
59
|
+
const out: Record<string, unknown> = {};
|
|
60
|
+
for (const [k, v] of Object.entries(attrs ?? {})) if (v !== null && v !== undefined) out[k] = v;
|
|
61
|
+
return out;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** RichNode (một khối) → BNode. */
|
|
65
|
+
export function fromRich(node: RichNode): BNode {
|
|
66
|
+
const kind = kindOf(node.type, node);
|
|
67
|
+
const b: BNode = { id: newId(), type: node.type, attrs: clean(node.attrs) };
|
|
68
|
+
if (kind === "text") b.inline = (node.content ?? []).map(cloneRich);
|
|
69
|
+
else if (kind === "container") b.content = (node.content ?? []).filter((c) => !isInlineType(c.type)).map(fromRich);
|
|
70
|
+
else if (node.content?.length) b.raw = node.content.map(cloneRich);
|
|
71
|
+
return b;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** BNode → RichNode (đúng định dạng lưu). */
|
|
75
|
+
export function toRich(b: BNode): RichNode {
|
|
76
|
+
const n: RichNode = { type: b.type };
|
|
77
|
+
const attrs = clean(b.attrs);
|
|
78
|
+
if (Object.keys(attrs).length) n.attrs = attrs;
|
|
79
|
+
const kids = b.inline ? b.inline.map(cloneRich) : b.content ? b.content.map(toRich) : b.raw ? b.raw.map(cloneRich) : undefined;
|
|
80
|
+
if (kids?.length) n.content = kids;
|
|
81
|
+
return n;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Cây `doc` của định dạng lưu → danh sách khối cấp cao nhất. */
|
|
85
|
+
export function blocksFromDoc(doc: RichNode | null | undefined): BNode[] {
|
|
86
|
+
const list = (doc?.content ?? []).filter((c) => !isInlineType(c.type)).map(fromRich);
|
|
87
|
+
return list;
|
|
88
|
+
}
|
|
89
|
+
export function docFromBlocks(blocks: BNode[]): RichNode {
|
|
90
|
+
return { type: "doc", content: blocks.map(toRich) };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function cloneRich(n: RichNode): RichNode {
|
|
94
|
+
const c: RichNode = { type: n.type };
|
|
95
|
+
if (n.attrs) c.attrs = { ...n.attrs };
|
|
96
|
+
if (n.text !== undefined) c.text = n.text;
|
|
97
|
+
if (n.marks) c.marks = n.marks.map((m: RichMark) => (m.attrs ? { type: m.type, attrs: { ...m.attrs } } : { type: m.type }));
|
|
98
|
+
if (n.content) c.content = n.content.map(cloneRich);
|
|
99
|
+
return c;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Sao chép sâu kèm định danh mới (nhân bản, dán). */
|
|
103
|
+
export function cloneBlock(b: BNode): BNode {
|
|
104
|
+
const c: BNode = { id: newId(), type: b.type, attrs: structuredClone(b.attrs) };
|
|
105
|
+
if (b.content) c.content = b.content.map(cloneBlock);
|
|
106
|
+
if (b.inline) c.inline = b.inline.map(cloneRich);
|
|
107
|
+
if (b.raw) c.raw = b.raw.map(cloneRich);
|
|
108
|
+
return c;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Tạo khối mới đúng loại. */
|
|
112
|
+
export function makeBlock(type: string, attrs: Record<string, unknown> = {}, children?: BNode[] | RichNode[] | string): BNode {
|
|
113
|
+
const kind = kindOf(type);
|
|
114
|
+
const b: BNode = { id: newId(), type, attrs: clean(attrs) };
|
|
115
|
+
if (kind === "text") b.inline = typeof children === "string" ? (children ? [{ type: "text", text: children }] : []) : ((children as RichNode[]) ?? []);
|
|
116
|
+
else if (kind === "container") b.content = (children as BNode[]) ?? [];
|
|
117
|
+
return b;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Độ dài chữ (hardBreak và nút inline khác tính 1) — dùng cho vị trí con trỏ. */
|
|
121
|
+
export function inlineLength(inline: RichNode[] | undefined): number {
|
|
122
|
+
let n = 0;
|
|
123
|
+
for (const c of inline ?? []) n += c.type === "text" ? (c.text ?? "").length : 1;
|
|
124
|
+
return n;
|
|
125
|
+
}
|
|
126
|
+
export const inlineText = (inline: RichNode[] | undefined) => (inline ?? []).map((c) => (c.type === "text" ? c.text ?? "" : c.type === "hardBreak" ? "\n" : "")).join("");
|
|
127
|
+
|
|
128
|
+
/** Cắt chữ tại vị trí `at` → [trước, sau], giữ định dạng. */
|
|
129
|
+
export function splitInline(inline: RichNode[], at: number): [RichNode[], RichNode[]] {
|
|
130
|
+
const before: RichNode[] = [];
|
|
131
|
+
const after: RichNode[] = [];
|
|
132
|
+
let pos = 0;
|
|
133
|
+
for (const c of inline) {
|
|
134
|
+
const len = c.type === "text" ? (c.text ?? "").length : 1;
|
|
135
|
+
if (pos + len <= at) before.push(cloneRich(c));
|
|
136
|
+
else if (pos >= at) after.push(cloneRich(c));
|
|
137
|
+
else {
|
|
138
|
+
const k = at - pos;
|
|
139
|
+
before.push({ ...cloneRich(c), text: c.text!.slice(0, k) });
|
|
140
|
+
after.push({ ...cloneRich(c), text: c.text!.slice(k) });
|
|
141
|
+
}
|
|
142
|
+
pos += len;
|
|
143
|
+
}
|
|
144
|
+
return [before, after];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const sameMarks = (a?: RichMark[], b?: RichMark[]) => JSON.stringify(a ?? []) === JSON.stringify(b ?? []);
|
|
148
|
+
/** Nối hai đoạn chữ, gộp text cùng định dạng ở chỗ nối. */
|
|
149
|
+
export function joinInline(a: RichNode[], b: RichNode[]): RichNode[] {
|
|
150
|
+
const out = a.map(cloneRich);
|
|
151
|
+
for (const c of b) {
|
|
152
|
+
const last = out[out.length - 1];
|
|
153
|
+
if (last && last.type === "text" && c.type === "text" && sameMarks(last.marks, c.marks)) last.text = (last.text ?? "") + (c.text ?? "");
|
|
154
|
+
else out.push(cloneRich(c));
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { BNode } from "./model";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Thao tác thuần trên cây khối: không sửa cây cũ, chỉ chép những nhánh bị đổi (chia sẻ phần còn lại) —
|
|
5
|
+
* nên lịch sử hoàn tác giữ được nhiều bản chụp rẻ, React so sánh tham chiếu để vẽ lại đúng khối đổi.
|
|
6
|
+
*/
|
|
7
|
+
export interface Location {
|
|
8
|
+
block: BNode;
|
|
9
|
+
/** null = cấp cao nhất của tài liệu. */
|
|
10
|
+
parent: BNode | null;
|
|
11
|
+
index: number;
|
|
12
|
+
/** Định danh các khối tổ tiên, từ ngoài vào trong. */
|
|
13
|
+
ancestors: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function findBlock(blocks: readonly BNode[], id: string, parent: BNode | null = null, ancestors: string[] = []): Location | null {
|
|
17
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
18
|
+
const b = blocks[i];
|
|
19
|
+
if (b.id === id) return { block: b, parent, index: i, ancestors };
|
|
20
|
+
if (b.content) {
|
|
21
|
+
const hit = findBlock(b.content, id, b, [...ancestors, b.id]);
|
|
22
|
+
if (hit) return hit;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const childrenOf = (blocks: BNode[], parentId: string | null): BNode[] | null =>
|
|
29
|
+
parentId == null ? blocks : findBlock(blocks, parentId)?.block.content ?? null;
|
|
30
|
+
|
|
31
|
+
/** Thay một khối (fn trả về khối mới, mảng khối, hoặc null = xoá). */
|
|
32
|
+
export function mapBlock(blocks: BNode[], id: string, fn: (b: BNode) => BNode | BNode[] | null): BNode[] {
|
|
33
|
+
let changed = false;
|
|
34
|
+
const walk = (list: BNode[]): BNode[] => {
|
|
35
|
+
const out: BNode[] = [];
|
|
36
|
+
for (const b of list) {
|
|
37
|
+
if (!changed && b.id === id) {
|
|
38
|
+
changed = true;
|
|
39
|
+
const r = fn(b);
|
|
40
|
+
if (Array.isArray(r)) out.push(...r);
|
|
41
|
+
else if (r) out.push(r);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!changed && b.content) {
|
|
45
|
+
const kids = walk(b.content);
|
|
46
|
+
out.push(kids === b.content ? b : { ...b, content: kids });
|
|
47
|
+
} else out.push(b);
|
|
48
|
+
}
|
|
49
|
+
return changed ? out : list;
|
|
50
|
+
};
|
|
51
|
+
const res = walk(blocks);
|
|
52
|
+
return changed ? res : blocks;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Thay danh sách con của `parentId` (null = cấp cao nhất). */
|
|
56
|
+
export function mapChildren(blocks: BNode[], parentId: string | null, fn: (kids: BNode[]) => BNode[]): BNode[] {
|
|
57
|
+
if (parentId == null) return fn(blocks);
|
|
58
|
+
return mapBlock(blocks, parentId, (p) => ({ ...p, content: fn(p.content ?? []) }));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function updateBlock(blocks: BNode[], id: string, patch: Partial<Omit<BNode, "id">>): BNode[] {
|
|
62
|
+
return mapBlock(blocks, id, (b) => ({ ...b, ...patch }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function setAttrs(blocks: BNode[], id: string, attrs: Record<string, unknown>): BNode[] {
|
|
66
|
+
return mapBlock(blocks, id, (b) => {
|
|
67
|
+
const next = { ...b.attrs };
|
|
68
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
69
|
+
if (v === undefined || v === null || v === "") delete next[k];
|
|
70
|
+
else next[k] = v;
|
|
71
|
+
}
|
|
72
|
+
return { ...b, attrs: next };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function insertBlocks(blocks: BNode[], parentId: string | null, index: number, nodes: BNode[]): BNode[] {
|
|
77
|
+
return mapChildren(blocks, parentId, (kids) => {
|
|
78
|
+
const i = Math.max(0, Math.min(index, kids.length));
|
|
79
|
+
return [...kids.slice(0, i), ...nodes, ...kids.slice(i)];
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Xoá nhiều khối (xoá cha thì con đi theo). */
|
|
84
|
+
export function removeBlocks(blocks: BNode[], ids: readonly string[]): BNode[] {
|
|
85
|
+
const set = new Set(ids);
|
|
86
|
+
const walk = (list: BNode[]): BNode[] => {
|
|
87
|
+
let changed = false;
|
|
88
|
+
const out: BNode[] = [];
|
|
89
|
+
for (const b of list) {
|
|
90
|
+
if (set.has(b.id)) { changed = true; continue; }
|
|
91
|
+
if (b.content) {
|
|
92
|
+
const kids = walk(b.content);
|
|
93
|
+
if (kids !== b.content) { changed = true; out.push({ ...b, content: kids }); continue; }
|
|
94
|
+
}
|
|
95
|
+
out.push(b);
|
|
96
|
+
}
|
|
97
|
+
return changed ? out : list;
|
|
98
|
+
};
|
|
99
|
+
return walk(blocks);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Thứ tự xuất hiện trong tài liệu (duyệt sâu trước). */
|
|
103
|
+
export function flatten(blocks: readonly BNode[], out: BNode[] = []): BNode[] {
|
|
104
|
+
for (const b of blocks) {
|
|
105
|
+
out.push(b);
|
|
106
|
+
if (b.content) flatten(b.content, out);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Sắp xếp định danh theo thứ tự tài liệu, bỏ khối con khi đã có cha trong danh sách. */
|
|
112
|
+
export function topLevelOrdered(blocks: BNode[], ids: readonly string[]): string[] {
|
|
113
|
+
const set = new Set(ids);
|
|
114
|
+
const out: string[] = [];
|
|
115
|
+
const walk = (list: BNode[]) => {
|
|
116
|
+
for (const b of list) {
|
|
117
|
+
if (set.has(b.id)) out.push(b.id);
|
|
118
|
+
else if (b.content) walk(b.content);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
walk(blocks);
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Di chuyển các khối tới (parentId, index) — index tính trên danh sách con trước khi bỏ các khối đang di chuyển.
|
|
127
|
+
* Không cho thả một khối vào chính nó / con cháu của nó.
|
|
128
|
+
*/
|
|
129
|
+
export function moveBlocks(blocks: BNode[], ids: readonly string[], parentId: string | null, index: number): BNode[] {
|
|
130
|
+
const ordered = topLevelOrdered(blocks, ids);
|
|
131
|
+
if (!ordered.length) return blocks;
|
|
132
|
+
if (parentId != null) {
|
|
133
|
+
const target = findBlock(blocks, parentId);
|
|
134
|
+
if (!target) return blocks;
|
|
135
|
+
if (ordered.includes(parentId) || target.ancestors.some((a) => ordered.includes(a))) return blocks;
|
|
136
|
+
}
|
|
137
|
+
const nodes = ordered.map((id) => findBlock(blocks, id)!.block);
|
|
138
|
+
// Vị trí đích sau khi bỏ: trừ các khối đang di chuyển đứng trước index trong cùng danh sách cha
|
|
139
|
+
const siblings = childrenOf(blocks, parentId) ?? [];
|
|
140
|
+
const shift = siblings.slice(0, index).filter((s) => ordered.includes(s.id)).length;
|
|
141
|
+
const without = removeBlocks(blocks, ordered);
|
|
142
|
+
return insertBlocks(without, parentId, index - shift, nodes);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Khối kề trước / sau trong cùng cha. */
|
|
146
|
+
export function sibling(blocks: BNode[], id: string, dir: -1 | 1): BNode | null {
|
|
147
|
+
const loc = findBlock(blocks, id);
|
|
148
|
+
if (!loc) return null;
|
|
149
|
+
const list = loc.parent ? loc.parent.content! : blocks;
|
|
150
|
+
return list[loc.index + dir] ?? null;
|
|
151
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { onPatternsChange, patternNodes, registeredPatterns, type Pattern } from "../../shared/patterns";
|
|
2
|
+
import { fromRich, makeBlock, type BNode } from "./model";
|
|
3
|
+
|
|
4
|
+
/** Khối của một mẫu: mẫu đồng bộ → tham chiếu; mẫu thường → chép khối. */
|
|
5
|
+
export function patternBlocks(p: Pattern): BNode[] {
|
|
6
|
+
if (p.synced && p.ref) return [makeBlock("syncedPattern", { ref: p.ref })];
|
|
7
|
+
return patternNodes(p).map((n) => fromRich(n as never));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { registeredPatterns, onPatternsChange, type Pattern };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { BlockAttrSpec, BlockDefinition } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { blockDefaults } from "@ecomcx/ecomweb-content";
|
|
3
|
+
import type { ComponentType, HTMLAttributes, ReactNode, Ref } from "react";
|
|
4
|
+
import { kindOf, makeBlock, setBlockKind, type BlockKind, type BNode } from "./model";
|
|
5
|
+
import { setStructureRule, type StructureRule } from "./structure";
|
|
6
|
+
import type { V2Labels } from "../labels";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Định nghĩa khối của trình soạn v2. Phần dữ liệu (tên, thuộc tính) trùng với defineBlock của ecomweb-content;
|
|
10
|
+
* phần giao diện (edit) vẽ khối ngay trên khung soạn với đúng markup của website.
|
|
11
|
+
*/
|
|
12
|
+
export interface BlockEditProps {
|
|
13
|
+
block: BNode;
|
|
14
|
+
attrs: Record<string, any>;
|
|
15
|
+
setAttrs(attrs: Record<string, unknown>): void;
|
|
16
|
+
/** Props phải gắn vào phần tử gốc của khối (chọn, viền, kéo thả, class kiểu dáng). Không bọc thêm thẻ. */
|
|
17
|
+
blockProps: BlockProps;
|
|
18
|
+
selected: boolean;
|
|
19
|
+
/** Chế độ sửa của tài liệu chứa khối. */
|
|
20
|
+
editable: boolean;
|
|
21
|
+
labels: V2Labels;
|
|
22
|
+
}
|
|
23
|
+
|
|
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 };
|
|
26
|
+
|
|
27
|
+
export type BlockCategory = "text" | "media" | "design" | "widgets" | "theme" | "embed" | (string & {});
|
|
28
|
+
|
|
29
|
+
export interface BlockVariation {
|
|
30
|
+
name: string;
|
|
31
|
+
title: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
icon?: ComponentType<{ className?: string }>;
|
|
34
|
+
keywords?: string;
|
|
35
|
+
attrs?: Record<string, unknown>;
|
|
36
|
+
/** Tạo khối của biến thể (mặc định create của khối với attrs). */
|
|
37
|
+
create?(): BNode;
|
|
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
|
+
standalone?: boolean;
|
|
40
|
+
/** 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
|
+
isActive?(attrs: Record<string, any>): boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface BlockTransform {
|
|
45
|
+
/** Loại khối đích (hiện trong menu "Đổi loại khối"). */
|
|
46
|
+
to: string;
|
|
47
|
+
title?: string;
|
|
48
|
+
/** Nhận các khối đang chọn (cùng loại), trả về khối thay thế; null = không đổi được. */
|
|
49
|
+
convert(blocks: BNode[]): BNode[] | null;
|
|
50
|
+
/** Áp dụng được cho nhiều khối cùng lúc. */
|
|
51
|
+
multiple?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface BlockType {
|
|
55
|
+
name: string;
|
|
56
|
+
title: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
icon?: ComponentType<{ className?: string }>;
|
|
59
|
+
category?: BlockCategory;
|
|
60
|
+
/** Từ khoá tìm (không dấu). */
|
|
61
|
+
keywords?: string;
|
|
62
|
+
kind: BlockKind;
|
|
63
|
+
/** Thuộc tính gắn dữ liệu được (attrs.bindings) — khối lõi: CORE_BINDABLE. */
|
|
64
|
+
bindable?: string[];
|
|
65
|
+
attributes?: Record<string, BlockAttrSpec>;
|
|
66
|
+
/** Luật cấu trúc (chứa gì, nằm trong gì). */
|
|
67
|
+
structure?: StructureRule;
|
|
68
|
+
/** Ẩn khỏi trình chèn (khối con như column, listItem). */
|
|
69
|
+
hidden?: boolean;
|
|
70
|
+
/** Tạo khối mới khi chèn (mặc định: thuộc tính mặc định + nội dung tối thiểu). */
|
|
71
|
+
create?(attrs?: Record<string, unknown>): BNode;
|
|
72
|
+
/** Vẽ trên khung soạn. Không có → khung chung (tên khối + tóm tắt thuộc tính). */
|
|
73
|
+
edit?: ComponentType<BlockEditProps>;
|
|
74
|
+
/** Thanh công cụ riêng của khối (chèn vào thanh công cụ nổi). */
|
|
75
|
+
toolbar?: ComponentType<BlockEditProps>;
|
|
76
|
+
/**
|
|
77
|
+
* Công cụ hiện trên thanh công cụ khi đang thao tác BÊN TRONG khối này (gõ trong ô bảng → công cụ bảng,
|
|
78
|
+
* trong cột → thêm cột). `ancestor` là khối này, `currentId` là khối đang thao tác.
|
|
79
|
+
*/
|
|
80
|
+
contextToolbar?: ComponentType<{ ancestor: BNode; currentId: string }>;
|
|
81
|
+
/** Bảng cài đặt riêng (ngoài form tự sinh). */
|
|
82
|
+
inspector?: ComponentType<BlockEditProps>;
|
|
83
|
+
variations?: BlockVariation[];
|
|
84
|
+
transforms?: BlockTransform[];
|
|
85
|
+
/** Khối chữ: Enter xuống dòng trong khối thay vì tách khối (mã nguồn). */
|
|
86
|
+
multiline?: boolean;
|
|
87
|
+
/** Khối chữ: không cho định dạng chữ. */
|
|
88
|
+
plain?: boolean;
|
|
89
|
+
/** Khối chữ: chữ gợi ý khi trống. */
|
|
90
|
+
placeholder?: string | ((labels: V2Labels) => string);
|
|
91
|
+
supports?: { style?: boolean; anchor?: boolean; className?: boolean; blockStyle?: boolean; align?: boolean };
|
|
92
|
+
/** Tóm tắt một dòng (cây khối, khung chung). */
|
|
93
|
+
summary?(attrs: Record<string, any>): string | undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class BlockTypeRegistry {
|
|
97
|
+
private types = new Map<string, BlockType>();
|
|
98
|
+
private listeners = new Set<() => void>();
|
|
99
|
+
private version = 0;
|
|
100
|
+
|
|
101
|
+
register(type: BlockType) {
|
|
102
|
+
this.types.set(type.name, type);
|
|
103
|
+
if (kindOf(type.name) !== type.kind) setBlockKind(type.name, type.kind);
|
|
104
|
+
if (type.structure) setStructureRule(type.name, type.structure);
|
|
105
|
+
this.changed();
|
|
106
|
+
}
|
|
107
|
+
unregister(name: string) { if (this.types.delete(name)) this.changed(); }
|
|
108
|
+
get(name: string): BlockType | undefined { return this.types.get(name); }
|
|
109
|
+
all(): BlockType[] { return [...this.types.values()]; }
|
|
110
|
+
subscribe = (l: () => void) => { this.listeners.add(l); return () => { this.listeners.delete(l); }; };
|
|
111
|
+
getVersion = () => this.version;
|
|
112
|
+
private changed() { this.version++; this.listeners.forEach((l) => l()); }
|
|
113
|
+
|
|
114
|
+
/** Tạo khối mới của loại `name` (có nội dung tối thiểu). */
|
|
115
|
+
create(name: string, attrs?: Record<string, unknown>): BNode {
|
|
116
|
+
const t = this.types.get(name);
|
|
117
|
+
if (t?.create) return t.create(attrs);
|
|
118
|
+
const defaults = t?.attributes ? Object.fromEntries(Object.entries(t.attributes).filter(([, s]) => s.default !== undefined).map(([k, s]) => [k, s.default])) : {};
|
|
119
|
+
const kind = t?.kind ?? kindOf(name);
|
|
120
|
+
return makeBlock(name, { ...defaults, ...attrs }, kind === "container" ? [makeBlock("paragraph")] : undefined);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Khối của plugin khai báo bằng defineBlock (ecomweb-content) → khối v2 với khung chung + form tự sinh. */
|
|
124
|
+
registerDefinition(def: BlockDefinition, extra: Partial<BlockType> = {}) {
|
|
125
|
+
const kind: BlockKind = def.content === "inline" ? "text" : def.content === "blocks" ? "container" : "atom";
|
|
126
|
+
this.register({
|
|
127
|
+
name: def.name, title: def.title, description: def.description, category: def.category ?? "widgets", keywords: def.keywords,
|
|
128
|
+
kind, attributes: def.attributes,
|
|
129
|
+
create: (attrs) => makeBlock(def.name, { ...blockDefaults(def), ...attrs }, kind === "container" ? [makeBlock("paragraph")] : undefined),
|
|
130
|
+
...extra,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type RenderChildren = (props?: { className?: string; orientation?: "vertical" | "horizontal" }) => ReactNode;
|