@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,242 @@
|
|
|
1
|
+
import { normalizeContent } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { Check, ChevronRight, Code2, Copy, Keyboard, MoreVertical, SlidersHorizontal, X } from "lucide-react";
|
|
3
|
+
import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
|
|
4
|
+
import { serializeBlocks } from "../core/clipboard";
|
|
5
|
+
import { useEditor, useEditorState } from "../context";
|
|
6
|
+
import { blocksFromDoc, type BNode } from "../core/model";
|
|
7
|
+
import { usePrefs, type EditorPrefs } from "../prefs";
|
|
8
|
+
import { blockLabel } from "./list-view";
|
|
9
|
+
import { Dropdown, MenuItem } from "./toolbar";
|
|
10
|
+
|
|
11
|
+
const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
|
|
12
|
+
const isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform);
|
|
13
|
+
const MOD = isMac ? "⌘" : "Ctrl";
|
|
14
|
+
|
|
15
|
+
type BoolPref = { [K in keyof EditorPrefs]: EditorPrefs[K] extends boolean ? K : never }[keyof EditorPrefs];
|
|
16
|
+
|
|
17
|
+
/** Mục bật/tắt trong menu (dấu ✓ khi bật). */
|
|
18
|
+
function PrefItem({ name, label, hint, close }: { name: BoolPref; label: string; hint?: string; close(): void }) {
|
|
19
|
+
const { prefs } = useEditor();
|
|
20
|
+
const on = usePrefs(prefs)[name];
|
|
21
|
+
return (
|
|
22
|
+
<button type="button" role="menuitemcheckbox" aria-checked={on} className="ecx-menu-item ecx-menu-pref" title={hint}
|
|
23
|
+
onMouseDown={(e) => e.preventDefault()} onClick={() => { prefs.toggle(name); close(); }}>
|
|
24
|
+
<span className="ecx-icon">{on && <Check className="ecx-icon" />}</span>
|
|
25
|
+
<span className="ecx-menu-text"><span>{label}</span>{hint && <small>{hint}</small>}</span>
|
|
26
|
+
</button>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Menu ⋮ "Tuỳ chọn": cách xem, chế độ soạn, công cụ, hộp tuỳ chọn. `extra` = mục riêng của ứng dụng (lịch sử, thùng rác…). */
|
|
31
|
+
export function MoreMenu({ extra }: { extra?: (close: () => void) => ReactNode }) {
|
|
32
|
+
const { prefs, labels, store, ui } = useEditor();
|
|
33
|
+
const p = usePrefs(prefs);
|
|
34
|
+
const [modal, setModal] = useState<"prefs" | "shortcuts" | null>(null);
|
|
35
|
+
const copyAll = async () => {
|
|
36
|
+
const root = store.getState().docs[store.getState().rootId].blocks;
|
|
37
|
+
const s = serializeBlocks(root);
|
|
38
|
+
try { await navigator.clipboard.write([new ClipboardItem({ "text/plain": new Blob([s.text], { type: "text/plain" }), "text/html": new Blob([s.html], { type: "text/html" }) })]); }
|
|
39
|
+
catch { try { await navigator.clipboard.writeText(s.text); } catch { /* trình duyệt chặn */ } }
|
|
40
|
+
ui.announce(labels.copiedAll);
|
|
41
|
+
};
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<Dropdown label={labels.options} className="ecx-more-menu" trigger={<MoreVertical className="ecx-icon" />}>
|
|
45
|
+
{(close) => (
|
|
46
|
+
<div className="ecx-menu-wide">
|
|
47
|
+
<div className="ecx-menu-group-label">{labels.view}</div>
|
|
48
|
+
<PrefItem name="fixedToolbar" label={labels.fixedToolbar} hint={labels.fixedToolbarHint} close={close} />
|
|
49
|
+
<PrefItem name="distractionFree" label={labels.distractionFree} hint={labels.distractionFreeHint} close={close} />
|
|
50
|
+
<PrefItem name="spotlight" label={labels.spotlight} hint={labels.spotlightHint} close={close} />
|
|
51
|
+
<PrefItem name="fullscreen" label={labels.fullscreen} hint={labels.fullscreenHint} close={close} />
|
|
52
|
+
<div className="ecx-menu-sep" />
|
|
53
|
+
<div className="ecx-menu-group-label">{labels.editorMode}</div>
|
|
54
|
+
{(["visual", "code"] as const).map((m) => (
|
|
55
|
+
<button key={m} type="button" role="menuitemradio" aria-checked={p.mode === m} className="ecx-menu-item" onMouseDown={(e) => e.preventDefault()}
|
|
56
|
+
onClick={() => { prefs.set({ mode: m }); close(); }}>
|
|
57
|
+
<span className="ecx-icon">{p.mode === m && <Check className="ecx-icon" />}</span><span>{m === "visual" ? labels.visualEditor : labels.codeEditor}</span><kbd>{m === "code" ? `${MOD}⇧⌥M` : ""}</kbd>
|
|
58
|
+
</button>
|
|
59
|
+
))}
|
|
60
|
+
<div className="ecx-menu-sep" />
|
|
61
|
+
<div className="ecx-menu-group-label">{labels.tools}</div>
|
|
62
|
+
<MenuItem icon={Keyboard} label={labels.shortcuts} shortcut={isMac ? "⌃⌥H" : "Shift+Alt+H"} onClick={() => { close(); setModal("shortcuts"); }} />
|
|
63
|
+
<MenuItem icon={Copy} label={labels.copyAll} onClick={() => { close(); void copyAll(); }} />
|
|
64
|
+
{extra?.(close)}
|
|
65
|
+
<div className="ecx-menu-sep" />
|
|
66
|
+
<MenuItem icon={SlidersHorizontal} label={`${labels.preferences}…`} onClick={() => { close(); setModal("prefs"); }} />
|
|
67
|
+
</div>
|
|
68
|
+
)}
|
|
69
|
+
</Dropdown>
|
|
70
|
+
{modal === "prefs" && <PreferencesModal onClose={() => setModal(null)} />}
|
|
71
|
+
{modal === "shortcuts" && <ShortcutsModal onClose={() => setModal(null)} />}
|
|
72
|
+
</>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Hộp thoại dùng chung (Esc / bấm nền để đóng, giữ focus trong hộp). */
|
|
77
|
+
function Modal({ title, onClose, children, wide }: { title: string; onClose(): void; children: ReactNode; wide?: boolean }) {
|
|
78
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
79
|
+
const { labels } = useEditor();
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
const prev = document.activeElement as HTMLElement | null;
|
|
82
|
+
ref.current?.querySelector<HTMLElement>("button, input, [tabindex]")?.focus();
|
|
83
|
+
const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); onClose(); } };
|
|
84
|
+
document.addEventListener("keydown", onKey, true);
|
|
85
|
+
return () => { document.removeEventListener("keydown", onKey, true); prev?.focus?.(); };
|
|
86
|
+
}, [onClose]);
|
|
87
|
+
return (
|
|
88
|
+
<div className="ecx-modal-backdrop" onMouseDown={(e) => { if (e.target === e.currentTarget) onClose(); }}>
|
|
89
|
+
<div ref={ref} className={cx("ecx-modal", wide && "is-wide")} role="dialog" aria-modal="true" aria-label={title}>
|
|
90
|
+
<header className="ecx-modal-header"><h2>{title}</h2><button type="button" className="ecx-tool" aria-label={labels.close} onClick={onClose}><X className="ecx-icon" /></button></header>
|
|
91
|
+
{children}
|
|
92
|
+
</div>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function Toggle({ name, label, hint }: { name: BoolPref; label: string; hint?: string }) {
|
|
98
|
+
const { prefs } = useEditor();
|
|
99
|
+
const on = usePrefs(prefs)[name];
|
|
100
|
+
return (
|
|
101
|
+
<label className="ecx-pref-row">
|
|
102
|
+
<span><strong>{label}</strong>{hint && <small>{hint}</small>}</span>
|
|
103
|
+
<input type="checkbox" role="switch" className="ecx-switch" checked={on} onChange={() => prefs.toggle(name)} />
|
|
104
|
+
</label>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Hộp "Tuỳ chọn trình soạn": Chung, Giao diện, Trợ năng, Khối (ẩn bớt loại khối khỏi thư viện). */
|
|
109
|
+
export function PreferencesModal({ onClose }: { onClose(): void }) {
|
|
110
|
+
const { labels, registry, prefs } = useEditor();
|
|
111
|
+
const p = usePrefs(prefs);
|
|
112
|
+
const [tab, setTab] = useState<"general" | "appearance" | "accessibility" | "blocks">("general");
|
|
113
|
+
const tabs = [["general", labels.general], ["appearance", labels.appearance], ["accessibility", labels.accessibility], ["blocks", labels.blocks]] as const;
|
|
114
|
+
const types = registry.all().filter((t) => !t.hidden);
|
|
115
|
+
const cats = [...new Set(types.map((t) => t.category ?? "other"))];
|
|
116
|
+
const catLabel: Record<string, string> = { text: labels.catText, media: labels.catMedia, design: labels.catDesign, widgets: labels.catWidgets, theme: labels.catTheme, embed: labels.catEmbed };
|
|
117
|
+
const hidden = new Set(p.hiddenBlockTypes);
|
|
118
|
+
const setHidden = (names: string[], hide: boolean) => {
|
|
119
|
+
const next = new Set(hidden);
|
|
120
|
+
for (const n of names) if (hide) next.add(n); else next.delete(n);
|
|
121
|
+
prefs.set({ hiddenBlockTypes: [...next] });
|
|
122
|
+
};
|
|
123
|
+
return (
|
|
124
|
+
<Modal title={labels.preferences} onClose={onClose} wide>
|
|
125
|
+
<div className="ecx-prefs">
|
|
126
|
+
<nav className="ecx-prefs-tabs" role="tablist" aria-orientation="vertical">
|
|
127
|
+
{tabs.map(([k, l]) => <button key={k} type="button" role="tab" aria-selected={tab === k} className={cx(tab === k && "is-active")} onClick={() => setTab(k)}>{l}<ChevronRight className="ecx-icon-sm" /></button>)}
|
|
128
|
+
</nav>
|
|
129
|
+
<section className="ecx-prefs-body" role="tabpanel">
|
|
130
|
+
{tab === "general" && <>
|
|
131
|
+
<Toggle name="showListView" label={labels.showListView} hint={labels.showListViewHint} />
|
|
132
|
+
<Toggle name="showBreadcrumbs" label={labels.showBreadcrumbs} hint={labels.showBreadcrumbsHint} />
|
|
133
|
+
</>}
|
|
134
|
+
{tab === "appearance" && <>
|
|
135
|
+
<Toggle name="fixedToolbar" label={labels.fixedToolbar} hint={labels.fixedToolbarHint} />
|
|
136
|
+
<Toggle name="distractionFree" label={labels.distractionFree} hint={labels.distractionFreeHint} />
|
|
137
|
+
<Toggle name="spotlight" label={labels.spotlight} hint={labels.spotlightHint} />
|
|
138
|
+
<Toggle name="fullscreen" label={labels.fullscreen} hint={labels.fullscreenHint} />
|
|
139
|
+
</>}
|
|
140
|
+
{tab === "accessibility" && <>
|
|
141
|
+
<Toggle name="keepCaretInsideBlock" label={labels.keepCaret} hint={labels.keepCaretHint} />
|
|
142
|
+
<Toggle name="showIconLabels" label={labels.iconLabels} hint={labels.iconLabelsHint} />
|
|
143
|
+
</>}
|
|
144
|
+
{tab === "blocks" && <>
|
|
145
|
+
<Toggle name="mostUsedBlocks" label={labels.mostUsed} hint={labels.mostUsedHint} />
|
|
146
|
+
<h3 className="ecx-panel-heading">{labels.blockVisibility}</h3>
|
|
147
|
+
<p className="ecx-muted">{labels.blockVisibilityHint}</p>
|
|
148
|
+
{cats.map((c) => {
|
|
149
|
+
const list = types.filter((t) => (t.category ?? "other") === c);
|
|
150
|
+
const allOn = list.every((t) => !hidden.has(t.name));
|
|
151
|
+
return (
|
|
152
|
+
<fieldset key={c} className="ecx-block-visibility">
|
|
153
|
+
<legend><label className="ecx-check"><input type="checkbox" checked={allOn} onChange={() => setHidden(list.map((t) => t.name), allOn)} /> <strong>{catLabel[c] ?? c}</strong></label></legend>
|
|
154
|
+
{list.map((t) => (
|
|
155
|
+
<label key={t.name} className="ecx-check" data-block-type={t.name}>
|
|
156
|
+
<input type="checkbox" checked={!hidden.has(t.name)} onChange={(e) => setHidden([t.name], !e.target.checked)} /> {t.icon && <t.icon className="ecx-icon" />} {t.title}
|
|
157
|
+
</label>
|
|
158
|
+
))}
|
|
159
|
+
</fieldset>
|
|
160
|
+
);
|
|
161
|
+
})}
|
|
162
|
+
</>}
|
|
163
|
+
</section>
|
|
164
|
+
</div>
|
|
165
|
+
</Modal>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Bảng phím tắt. */
|
|
170
|
+
export function ShortcutsModal({ onClose }: { onClose(): void }) {
|
|
171
|
+
const { labels } = useEditor();
|
|
172
|
+
const vi = labels.lang === "vi";
|
|
173
|
+
const rows: Array<[string, string]> = vi ? [
|
|
174
|
+
[`${MOD}+Z / ${MOD}+⇧+Z`, "Hoàn tác / làm lại (xuyên header, footer)"], [`${MOD}+S`, "Lưu mọi phần đã đổi"], ["/", "Chèn khối (gõ ở đoạn trống)"],
|
|
175
|
+
["Enter / ⇧+Enter", "Tách khối / xuống dòng trong khối"], ["Tab / ⇧+Tab", "Lồng / đưa mục danh sách ra"], ["Esc", "Chọn cả khối"],
|
|
176
|
+
[`${MOD}+A ×2`, "Chọn mọi khối"], [`${MOD}+⇧+D`, "Nhân bản khối"], ["⇧+⌥+Z", "Xoá khối"], [`${MOD}+⌥+T / Y`, "Thêm khối phía trên / dưới"],
|
|
177
|
+
[`${MOD}+⇧+⌥+T / Y`, "Đưa khối lên / xuống"], ["Alt+F10", "Lên thanh công cụ của khối"], [`${MOD}+B / I / U`, "Đậm / nghiêng / gạch chân"],
|
|
178
|
+
[`${MOD}+K`, "Liên kết"], [`${MOD}+E`, "Mã trong dòng"], ["## · - · 1. · > · ``` · ---", "Gõ tắt: tiêu đề, danh sách, trích dẫn, mã, đường kẻ"],
|
|
179
|
+
] : [
|
|
180
|
+
[`${MOD}+Z / ${MOD}+⇧+Z`, "Undo / redo (across header and footer)"], [`${MOD}+S`, "Save every changed part"], ["/", "Insert a block (in an empty paragraph)"],
|
|
181
|
+
["Enter / ⇧+Enter", "Split block / line break"], ["Tab / ⇧+Tab", "Indent / outdent list item"], ["Esc", "Select the whole block"],
|
|
182
|
+
[`${MOD}+A ×2`, "Select all blocks"], [`${MOD}+⇧+D`, "Duplicate"], ["⇧+⌥+Z", "Delete block"], [`${MOD}+⌥+T / Y`, "Insert before / after"],
|
|
183
|
+
[`${MOD}+⇧+⌥+T / Y`, "Move up / down"], ["Alt+F10", "Go to the block toolbar"], [`${MOD}+B / I / U`, "Bold / italic / underline"],
|
|
184
|
+
[`${MOD}+K`, "Link"], [`${MOD}+E`, "Inline code"], ["## · - · 1. · > · ``` · ---", "Shortcuts: heading, list, quote, code, separator"],
|
|
185
|
+
];
|
|
186
|
+
return (
|
|
187
|
+
<Modal title={labels.shortcuts} onClose={onClose}>
|
|
188
|
+
<dl className="ecx-shortcuts">{rows.map(([k, d]) => <div key={k}><dt>{d}</dt><dd><kbd>{k}</kbd></dd></div>)}</dl>
|
|
189
|
+
</Modal>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Soạn mã: nội dung dạng JSON (định dạng lưu) — áp dụng khi rời ô / bấm nút; sai cấu trúc thì báo, không ghi. */
|
|
194
|
+
export function CodeEditor() {
|
|
195
|
+
const { store, labels, prefs } = useEditor();
|
|
196
|
+
const rootId = useEditorState((s) => s.rootId);
|
|
197
|
+
const blocks = useEditorState((s) => s.docs[s.rootId].blocks);
|
|
198
|
+
const text = useMemo(() => JSON.stringify(JSON.parse(serializeBlocks(blocks).json), null, 2), [blocks]);
|
|
199
|
+
const [draft, setDraft] = useState(text);
|
|
200
|
+
const [error, setError] = useState<string | null>(null);
|
|
201
|
+
useEffect(() => { setDraft(text); setError(null); }, [text]);
|
|
202
|
+
const apply = () => {
|
|
203
|
+
if (draft === text) return true;
|
|
204
|
+
try {
|
|
205
|
+
const parsed = JSON.parse(draft);
|
|
206
|
+
const { doc } = normalizeContent(Array.isArray(parsed) ? { type: "doc", content: parsed } : parsed);
|
|
207
|
+
const rich = doc.blocks.find((b) => b.type === "richtext")?.data as Parameters<typeof blocksFromDoc>[0];
|
|
208
|
+
store.commit(rootId, blocksFromDoc(rich));
|
|
209
|
+
setError(null);
|
|
210
|
+
return true;
|
|
211
|
+
} catch (e) { setError(`${labels.invalidCode}: ${(e as Error).message}`); return false; }
|
|
212
|
+
};
|
|
213
|
+
return (
|
|
214
|
+
<div className="ecx-code-editor">
|
|
215
|
+
<div className="ecx-code-bar"><Code2 className="ecx-icon" /><span>{labels.codeEditorHint}</span><span className="ecx-flex" />
|
|
216
|
+
<button type="button" className="ecx-btn" onClick={() => { if (apply()) prefs.set({ mode: "visual" }); }}>{labels.exitCode}</button></div>
|
|
217
|
+
{error && <div className="ecx-notice is-error" role="alert">{error}</div>}
|
|
218
|
+
<textarea className="ecx-code-input" aria-label={labels.codeEditor} spellCheck={false} value={draft} onChange={(e) => setDraft(e.target.value)} onBlur={apply} />
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Đường dẫn khối ở đáy: Tài liệu › Nhóm › Cột › Đoạn văn — bấm để chọn khối cha. */
|
|
224
|
+
export function Breadcrumbs() {
|
|
225
|
+
const { store, registry, labels } = useEditor();
|
|
226
|
+
const sel = useEditorState((s) => s.selection);
|
|
227
|
+
useEditorState((s) => s.revision);
|
|
228
|
+
const id = sel.ids[0] ?? sel.editing;
|
|
229
|
+
const loc = id ? store.locate(id) : null;
|
|
230
|
+
const chain: BNode[] = loc ? [...loc.ancestors.map((a) => store.block(a)!).filter(Boolean), loc.block] : [];
|
|
231
|
+
const doc = loc ? store.doc(loc.docId) : null;
|
|
232
|
+
return (
|
|
233
|
+
<nav className="ecx-breadcrumbs" aria-label={labels.showBreadcrumbs}>
|
|
234
|
+
<button type="button" onClick={() => store.clearSelection()}>{doc?.title ?? labels.documentRoot}</button>
|
|
235
|
+
{chain.map((b, i) => (
|
|
236
|
+
<span key={b.id} className="ecx-crumb"><ChevronRight className="ecx-icon-sm" />
|
|
237
|
+
<button type="button" aria-current={i === chain.length - 1 ? "true" : undefined} onClick={() => store.select([b.id], { editing: null })}>{blockLabel(b, registry.get(b.type)).split(" · ")[0]}</button>
|
|
238
|
+
</span>
|
|
239
|
+
))}
|
|
240
|
+
</nav>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Device } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { ChevronRight } from "lucide-react";
|
|
3
|
+
import { useState, type ComponentType, type ReactNode } from "react";
|
|
4
|
+
import { useEditor } from "../context";
|
|
5
|
+
|
|
6
|
+
/** Thành phần chung của tab Kiểu dáng (cùng một giao diện cho mọi mục). */
|
|
7
|
+
export const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
|
|
8
|
+
export type Icon = ComponentType<{ className?: string }>;
|
|
9
|
+
|
|
10
|
+
export function Seg<T extends string>({ value, options, onChange, label }: { value: T | undefined; options: Array<[T, Icon | string, string]>; onChange(v: T | undefined): void; label: string }) {
|
|
11
|
+
return (
|
|
12
|
+
<div role="radiogroup" aria-label={label} className="ecx-seg">
|
|
13
|
+
{options.map(([v, I, title]) => (
|
|
14
|
+
<button key={v} type="button" role="radio" aria-checked={value === v} title={title} aria-label={title} className={cx("ecx-seg-btn", value === v && "is-active")}
|
|
15
|
+
onClick={() => onChange(value === v ? undefined : v)}>{typeof I === "string" ? I : <I className="ecx-icon" />}</button>
|
|
16
|
+
))}
|
|
17
|
+
</div>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
export function Row({ label, children, device }: { label: string; children: ReactNode; device?: Device }) {
|
|
21
|
+
const { labels } = useEditor();
|
|
22
|
+
return (
|
|
23
|
+
<div className="ecx-style-row">
|
|
24
|
+
<span className="ecx-field-label">{label}{device && device !== "base" && <em className="ecx-device-tag">{device === "tablet" ? labels.lang === "vi" ? "Máy tính bảng" : "Tablet" : labels.lang === "vi" ? "Điện thoại" : "Mobile"}</em>}</span>
|
|
25
|
+
{children}
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Một mục của tab Kiểu dáng: tiêu đề bấm để thu / mở (mũi tên), dấu chấm khi mục có giá trị, nút đặt lại,
|
|
31
|
+
* phần "Thêm tuỳ chọn" cho thiết lập ít dùng (tự mở khi đã có giá trị).
|
|
32
|
+
*/
|
|
33
|
+
export function Section({ title, children, more, defaultOpen = true, modified, onReset, moreOpen }: {
|
|
34
|
+
title: string; children?: ReactNode; more?: ReactNode; defaultOpen?: boolean; modified?: boolean; onReset?(): void; moreOpen?: boolean;
|
|
35
|
+
}) {
|
|
36
|
+
const { labels } = useEditor();
|
|
37
|
+
const [open, setOpen] = useState(defaultOpen);
|
|
38
|
+
const [showMore, setShowMore] = useState(!!moreOpen);
|
|
39
|
+
const vi = labels.lang === "vi";
|
|
40
|
+
return (
|
|
41
|
+
<section className="ecx-style-section">
|
|
42
|
+
<div className="ecx-style-section-head">
|
|
43
|
+
<button type="button" className="ecx-style-section-toggle" aria-expanded={open} onClick={() => setOpen(!open)}>
|
|
44
|
+
<ChevronRight className={cx("ecx-icon ecx-icon-sm ecx-chevron", open && "is-open")} aria-hidden />{title}{modified && <span className="ecx-dot" aria-label={vi ? "đã chỉnh" : "modified"} />}
|
|
45
|
+
</button>
|
|
46
|
+
{modified && onReset && <button type="button" className="ecx-link-btn" onClick={onReset}>{vi ? "Đặt lại" : "Reset"}</button>}
|
|
47
|
+
</div>
|
|
48
|
+
{open && (
|
|
49
|
+
<div className="ecx-style-section-body">
|
|
50
|
+
{children}
|
|
51
|
+
{more && (showMore || moreOpen ? more : <button type="button" className="ecx-link-btn ecx-more-btn" onClick={() => setShowMore(true)}>{vi ? "+ Thêm tuỳ chọn" : "+ More options"}</button>)}
|
|
52
|
+
</div>
|
|
53
|
+
)}
|
|
54
|
+
</section>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { normalizeStyle, type BlockStyle, type Device } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { AlignCenter, AlignJustify, AlignLeft, AlignRight, Monitor, Smartphone, Tablet } from "lucide-react";
|
|
3
|
+
import { useEffect, useState, type ReactNode } from "react";
|
|
4
|
+
import { enLabels, viLabels, type EditorLabels } from "../../shared/labels";
|
|
5
|
+
import { useEditor } from "../context";
|
|
6
|
+
import { ColorInput } from "./color-field";
|
|
7
|
+
import { LayoutEffectsPanel } from "./layout-panel";
|
|
8
|
+
import { Section, Seg, cx } from "./style-controls";
|
|
9
|
+
import { SpacingControl, UnitInput } from "./unit-controls";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Tab Kiểu dáng của khối (attrs.style, @ecomcx/ecomweb-content): một giao diện thống nhất cho chữ, màu, khoảng cách,
|
|
13
|
+
* viền, bố cục, nền / hiệu ứng, hover và hiển thị. Thuộc tính theo khổ màn hình sửa theo khổ đang chọn ở đầu tab;
|
|
14
|
+
* chưa đặt cho khổ nhỏ thì hiện giá trị kế thừa từ khổ lớn hơn.
|
|
15
|
+
*/
|
|
16
|
+
type Style = BlockStyle & Record<string, any>;
|
|
17
|
+
const RESPONSIVE = new Set(["padding", "margin", "gap", "width", "maxWidth", "minHeight", "fontSize", "lineHeight", "letterSpacing", "textAlign"]);
|
|
18
|
+
const SIZES = ["sm", "md", "lg", "xl", "2xl"];
|
|
19
|
+
const SPACES = ["0", "xs", "sm", "md", "lg", "xl", "2xl"];
|
|
20
|
+
const WEIGHTS = ["300", "400", "500", "600", "700", "800", "900"];
|
|
21
|
+
const TYPO = ["fontFamily", "fontSize", "lineHeight", "letterSpacing", "fontWeight", "fontStyle", "textDecoration", "textTransform", "textAlign"];
|
|
22
|
+
const SPACING = ["padding", "margin", "gap", "width", "maxWidth", "minHeight"];
|
|
23
|
+
|
|
24
|
+
/** Ô giá trị (preset như `lg` hoặc `16px`, `1.5rem`…): lưu khi rời ô / Enter; sai thì viền đỏ, không lưu. */
|
|
25
|
+
function ValueInput({ value, onCommit, check, placeholder, label, inherited }: { value?: string | number; onCommit(v: string | null): void; check(v: string): boolean; placeholder?: string; label: string; inherited?: string }) {
|
|
26
|
+
const [text, setText] = useState(value == null ? "" : String(value));
|
|
27
|
+
const [bad, setBad] = useState(false);
|
|
28
|
+
useEffect(() => { setText(value == null ? "" : String(value)); setBad(false); }, [value]);
|
|
29
|
+
const commit = () => {
|
|
30
|
+
const v = text.trim();
|
|
31
|
+
if (v === "") { setBad(false); if (value != null) onCommit(null); return; }
|
|
32
|
+
if (!check(v)) { setBad(true); return; }
|
|
33
|
+
setBad(false);
|
|
34
|
+
if (v !== String(value ?? "")) onCommit(v);
|
|
35
|
+
};
|
|
36
|
+
return <input type="text" className="ecx-input" value={text} placeholder={inherited ?? placeholder} aria-label={label} aria-invalid={bad || undefined}
|
|
37
|
+
onChange={(e) => setText(e.target.value)} onBlur={commit} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); commit(); } }} />;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function StyleTab({ type, style: raw, onChange, device, onDeviceChange, variants, variant, onVariantChange }: {
|
|
41
|
+
type: string; style: unknown; onChange(style: BlockStyle | null): void; device: Device; onDeviceChange(d: Device): void;
|
|
42
|
+
variants?: Array<{ name: string; label: string }>; variant?: string | null; onVariantChange?(v: string | null): void;
|
|
43
|
+
}) {
|
|
44
|
+
const { host, labels } = useEditor();
|
|
45
|
+
const t = labels.lang === "vi" ? viLabels : enLabels;
|
|
46
|
+
const presets = host.presets;
|
|
47
|
+
const sizes = presets?.fontSizes ? Object.keys(presets.fontSizes) : SIZES;
|
|
48
|
+
const spaces = presets?.spacing ? ["0", ...Object.keys(presets.spacing)] : SPACES;
|
|
49
|
+
const fonts = presets?.fonts ? Object.keys(presets.fonts) : ["body", "heading", "mono"];
|
|
50
|
+
const style: Style = (normalizeStyle(raw) ?? {}) as Style;
|
|
51
|
+
|
|
52
|
+
const commit = (next: Style) => onChange(normalizeStyle(next) ?? null);
|
|
53
|
+
const get = (k: string): any => (RESPONSIVE.has(k) ? style[k]?.[device] : style[k]);
|
|
54
|
+
const inherited = (k: string): string | undefined => {
|
|
55
|
+
if (!RESPONSIVE.has(k) || device === "base" || get(k) !== undefined) return undefined;
|
|
56
|
+
const v = device === "mobile" ? style[k]?.tablet ?? style[k]?.base : style[k]?.base;
|
|
57
|
+
return v === undefined ? undefined : `${t.inherited}: ${typeof v === "object" ? "…" : v}`;
|
|
58
|
+
};
|
|
59
|
+
const set = (k: string, v: unknown) => {
|
|
60
|
+
const next: Style = { ...style };
|
|
61
|
+
if (RESPONSIVE.has(k)) {
|
|
62
|
+
const r = { ...(style[k] ?? {}) };
|
|
63
|
+
if (v === undefined || v === null || v === "") delete r[device]; else r[device] = v;
|
|
64
|
+
next[k] = Object.keys(r).length ? r : undefined;
|
|
65
|
+
} else next[k] = v === "" ? undefined : v;
|
|
66
|
+
commit(next);
|
|
67
|
+
};
|
|
68
|
+
const setGroup = (k: "color" | "border", sub: string, v: unknown) => set(k, { ...(style[k] ?? {}), [sub]: v });
|
|
69
|
+
const reset = (keys: string[]) => { const next: Style = { ...style }; for (const k of keys) delete next[k]; commit(next); };
|
|
70
|
+
const has = (keys: string[]) => keys.some((k) => style[k] !== undefined);
|
|
71
|
+
const ok = (k: string) => (v: string) => !!normalizeStyle({ [k]: v });
|
|
72
|
+
const tag = (k: string) => RESPONSIVE.has(k) && device !== "base" ? <em className="ecx-device-tag">{t[`device_${device}` as keyof EditorLabels]}</em> : null;
|
|
73
|
+
const row = (k: string, label: string, control: ReactNode) => (
|
|
74
|
+
<div className="ecx-style-row" key={k}><span className="ecx-field-label">{label}{tag(k)}</span>{control}</div>
|
|
75
|
+
);
|
|
76
|
+
const appearance = `${style.fontWeight ?? ""}${style.fontStyle === "italic" ? "i" : ""}`;
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div className="ecx-style-tab">
|
|
80
|
+
<div className="ecx-style-device">
|
|
81
|
+
<span className="ecx-field-label">{t.device}</span>
|
|
82
|
+
<Seg label={t.device} value={device} onChange={(d) => onDeviceChange(d ?? "base")}
|
|
83
|
+
options={[["base", Monitor, t.device_base], ["tablet", Tablet, t.device_tablet], ["mobile", Smartphone, t.device_mobile]]} />
|
|
84
|
+
</div>
|
|
85
|
+
|
|
86
|
+
{variants && variants.length > 0 && (
|
|
87
|
+
<div className="ecx-style-row">
|
|
88
|
+
<span className="ecx-field-label">{t.blockVariant}</span>
|
|
89
|
+
<div role="radiogroup" aria-label={t.blockVariant} className="ecx-seg is-text">
|
|
90
|
+
{[{ name: "", label: t.variant_default }, ...variants].map((v) => (
|
|
91
|
+
<button key={v.name || "default"} type="button" role="radio" aria-checked={(variant ?? "") === v.name} className={cx("ecx-seg-btn", (variant ?? "") === v.name && "is-active")}
|
|
92
|
+
onClick={() => onVariantChange?.(v.name || null)}>{v.label}</button>
|
|
93
|
+
))}
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
)}
|
|
97
|
+
|
|
98
|
+
<Section title={t.style_typography} modified={has(TYPO)} onReset={() => reset(TYPO)} moreOpen={has(["fontFamily", "letterSpacing", "textDecoration", "textTransform"])}
|
|
99
|
+
more={<>
|
|
100
|
+
{row("fontFamily", t.style_fontFamily,
|
|
101
|
+
<select className="ecx-input" value={style.fontFamily ?? ""} aria-label={t.style_fontFamily} onChange={(e) => set("fontFamily", e.target.value || undefined)}>
|
|
102
|
+
<option value="">{t.default}</option>
|
|
103
|
+
{fonts.map((f) => <option key={f} value={f}>{(t as unknown as Record<string, string>)[`font_${f}`] ?? f}</option>)}
|
|
104
|
+
</select>)}
|
|
105
|
+
{row("letterSpacing", t.style_letterSpacing, <ValueInput label={t.style_letterSpacing} value={get("letterSpacing")} inherited={inherited("letterSpacing")} check={ok("letterSpacing")} placeholder="1px, 0.05em" onCommit={(v) => set("letterSpacing", v)} />)}
|
|
106
|
+
{row("decoration", t.style_decoration, <Seg label={t.style_decoration} value={style.textDecoration} onChange={(v) => set("textDecoration", v)}
|
|
107
|
+
options={[["none", "—", t.none], ["underline", "U̲", t.underlineText], ["line-through", "S̶", t.strikeText]]} />)}
|
|
108
|
+
{row("transform", t.style_transform, <Seg label={t.style_transform} value={style.textTransform} onChange={(v) => set("textTransform", v)}
|
|
109
|
+
options={[["none", "—", t.none], ["uppercase", "AB", t.upper], ["lowercase", "ab", t.lower], ["capitalize", "Ab", t.capitalize]]} />)}
|
|
110
|
+
</>}>
|
|
111
|
+
{row("fontSize", t.style_fontSize, <>
|
|
112
|
+
<Seg label={t.style_fontSize} value={get("fontSize")} onChange={(v) => set("fontSize", v)} options={sizes.map((v) => [v, v.toUpperCase(), v] as [string, string, string])} />
|
|
113
|
+
<ValueInput label={`${t.style_fontSize}: ${t.custom}`} value={sizes.includes(get("fontSize")) ? undefined : get("fontSize")} inherited={inherited("fontSize")} check={ok("fontSize")} placeholder="18px, 1.25rem" onCommit={(v) => set("fontSize", v)} />
|
|
114
|
+
</>)}
|
|
115
|
+
{row("appearance", t.style_appearance,
|
|
116
|
+
<select className="ecx-input" value={appearance} aria-label={t.style_appearance} onChange={(e) => {
|
|
117
|
+
const v = e.target.value;
|
|
118
|
+
commit({ ...style, fontWeight: v.replace("i", "") || undefined, fontStyle: v.endsWith("i") ? "italic" : undefined });
|
|
119
|
+
}}>
|
|
120
|
+
<option value="">{t.default}</option>
|
|
121
|
+
{WEIGHTS.flatMap((w) => [<option key={w} value={w}>{t[`weight_${w}` as keyof EditorLabels]}</option>, <option key={`${w}i`} value={`${w}i`}>{t[`weight_${w}` as keyof EditorLabels]} {t.italicSuffix}</option>])}
|
|
122
|
+
</select>)}
|
|
123
|
+
{row("lineHeight", t.style_lineHeight, <ValueInput label={t.style_lineHeight} value={get("lineHeight")} inherited={inherited("lineHeight")} check={ok("lineHeight")} placeholder="1.5" onCommit={(v) => set("lineHeight", v == null ? undefined : Number(v))} />)}
|
|
124
|
+
{row("textAlign", t.textAlign, <Seg label={t.textAlign} value={get("textAlign")} onChange={(v) => set("textAlign", v)}
|
|
125
|
+
options={[["left", AlignLeft, t.alignLeft], ["center", AlignCenter, t.alignCenter], ["right", AlignRight, t.alignRight], ["justify", AlignJustify, t.alignJustify]]} />)}
|
|
126
|
+
</Section>
|
|
127
|
+
|
|
128
|
+
<Section title={t.style_color} modified={has(["color"])} onReset={() => reset(["color"])}>
|
|
129
|
+
{row("text", t.style_text, <ColorInput label={t.style_text} value={style.color?.text} onChange={(v) => setGroup("color", "text", v)} />)}
|
|
130
|
+
{row("background", t.style_background, <ColorInput label={t.style_background} value={style.color?.background} onChange={(v) => setGroup("color", "background", v)} />)}
|
|
131
|
+
</Section>
|
|
132
|
+
|
|
133
|
+
<Section title={t.style_spacing} modified={has(SPACING)} onReset={() => reset(SPACING)} moreOpen={has(["gap", "width", "maxWidth", "minHeight"])}
|
|
134
|
+
more={<>{(["gap", "width", "maxWidth", "minHeight"] as const).map((k) =>
|
|
135
|
+
row(k, t[`style_${k}`], <ValueInput label={t[`style_${k}`]} value={get(k)} inherited={inherited(k)} check={ok(k)} placeholder={k === "gap" ? "md, 24px" : "100%, 640px, 60vh"} onCommit={(v) => set(k, v)} />))}</>}>
|
|
136
|
+
<SpacingControl label={t.style_padding} steps={spaces} value={get("padding")} inherited={inherited("padding")} onChange={(v) => set("padding", v)} labels={{ vertical: labels.lang === "vi" ? "dọc" : "vertical", horizontal: labels.lang === "vi" ? "ngang" : "horizontal", link: t.linkSides, unlink: t.unlinkSides }} />
|
|
137
|
+
<SpacingControl label={t.style_margin} steps={spaces} value={get("margin")} inherited={inherited("margin")} onChange={(v) => set("margin", v)} labels={{ vertical: labels.lang === "vi" ? "dọc" : "vertical", horizontal: labels.lang === "vi" ? "ngang" : "horizontal", link: t.linkSides, unlink: t.unlinkSides }} />
|
|
138
|
+
</Section>
|
|
139
|
+
|
|
140
|
+
<Section title={t.style_border} modified={has(["border", "radius"])} onReset={() => reset(["border", "radius"])} defaultOpen={has(["border", "radius"])}>
|
|
141
|
+
{row("borderWidth", t.style_borderWidth, <UnitInput label={t.style_borderWidth} value={style.border?.width} max={20} units={["px", "em", "rem"]} onChange={(v) => setGroup("border", "width", v)} />)}
|
|
142
|
+
{row("borderStyle", t.style_borderStyle, <Seg label={t.style_borderStyle} value={style.border?.style} onChange={(v) => setGroup("border", "style", v)}
|
|
143
|
+
options={[["solid", "—", t.border_solid], ["dashed", "- -", t.border_dashed], ["dotted", "···", t.border_dotted]]} />)}
|
|
144
|
+
{row("borderColor", t.style_borderColor, <ColorInput label={t.style_borderColor} value={style.border?.color} onChange={(v) => setGroup("border", "color", v)} />)}
|
|
145
|
+
{row("radius", t.style_radius, <UnitInput label={t.style_radius} value={typeof style.radius === "string" ? style.radius : undefined} max={60} units={["px", "%", "em", "rem"]} onChange={(v) => set("radius", v)} />)}
|
|
146
|
+
</Section>
|
|
147
|
+
|
|
148
|
+
<LayoutEffectsPanel type={type} style={raw} onChange={onChange} device={device} />
|
|
149
|
+
|
|
150
|
+
<Section title={t.style_visibility} modified={has(["hide"])} onReset={() => reset(["hide"])} defaultOpen={has(["hide"])}>
|
|
151
|
+
{(["desktop", "tablet", "mobile"] as const).map((d) => (
|
|
152
|
+
<label key={d} className="ecx-check"><input type="checkbox" checked={!!style.hide?.includes(d)}
|
|
153
|
+
onChange={(e) => set("hide", e.target.checked ? [...(style.hide ?? []), d] : (style.hide ?? []).filter((x) => x !== d))} /> {t[`hide_${d}`]}</label>
|
|
154
|
+
))}
|
|
155
|
+
</Section>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|