@ecomcx/ecomweb-editor 1.30.0 → 1.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/package.json +3 -3
  2. package/src/locales/en/attrs.ts +10 -0
  3. package/src/locales/en/background.ts +17 -0
  4. package/src/locales/en/block-desc.ts +1 -0
  5. package/src/locales/en/block-style.ts +7 -4
  6. package/src/locales/en/block-titles.ts +1 -0
  7. package/src/locales/en/blocks.ts +10 -2
  8. package/src/locales/en/columns.ts +12 -0
  9. package/src/locales/en/controls.ts +21 -0
  10. package/src/locales/en/hints.ts +215 -1
  11. package/src/locales/en/index.ts +6 -2
  12. package/src/locales/en/layout.ts +32 -0
  13. package/src/locales/en/legacy.ts +1 -1
  14. package/src/locales/en/library.ts +38 -0
  15. package/src/locales/en/parts.ts +12 -0
  16. package/src/locales/en/placeholders.ts +104 -0
  17. package/src/locales/en/presets.ts +6 -0
  18. package/src/locales/en/v2.ts +21 -0
  19. package/src/locales/vi/attrs.ts +10 -0
  20. package/src/locales/vi/background.ts +17 -0
  21. package/src/locales/vi/block-desc.ts +1 -0
  22. package/src/locales/vi/block-style.ts +7 -4
  23. package/src/locales/vi/block-titles.ts +1 -0
  24. package/src/locales/vi/blocks.ts +10 -2
  25. package/src/locales/vi/columns.ts +12 -0
  26. package/src/locales/vi/controls.ts +21 -0
  27. package/src/locales/vi/hints.ts +216 -2
  28. package/src/locales/vi/index.ts +6 -2
  29. package/src/locales/vi/layout.ts +32 -0
  30. package/src/locales/vi/legacy.ts +1 -1
  31. package/src/locales/vi/library.ts +38 -0
  32. package/src/locales/vi/parts.ts +12 -0
  33. package/src/locales/vi/placeholders.ts +103 -0
  34. package/src/locales/vi/presets.ts +6 -0
  35. package/src/locales/vi/v2.ts +21 -0
  36. package/src/shared/panel-store.ts +3 -3
  37. package/src/shared/patterns.ts +3 -52
  38. package/src/shared/style-panel.tsx +2 -2
  39. package/src/shared/types.ts +2 -0
  40. package/src/v2/blocks/columns-inspector.tsx +210 -0
  41. package/src/v2/blocks/compat.tsx +91 -5
  42. package/src/v2/blocks/form.tsx +7 -4
  43. package/src/v2/blocks/index.ts +10 -4
  44. package/src/v2/blocks/inline-style.ts +1 -1
  45. package/src/v2/blocks/layout.tsx +305 -83
  46. package/src/v2/blocks/media.tsx +91 -81
  47. package/src/v2/blocks/resize.tsx +172 -4
  48. package/src/v2/blocks/text.tsx +60 -22
  49. package/src/v2/blocks/theme.tsx +83 -17
  50. package/src/v2/blocks/tools.tsx +50 -5
  51. package/src/v2/blocks/widgets.tsx +85 -20
  52. package/src/v2/canvas/block-list.tsx +21 -6
  53. package/src/v2/canvas/canvas-css.ts +139 -7
  54. package/src/v2/canvas/canvas.tsx +20 -3
  55. package/src/v2/canvas/dnd.ts +34 -3
  56. package/src/v2/canvas/move-animation.ts +94 -0
  57. package/src/v2/canvas/server-block.tsx +7 -4
  58. package/src/v2/canvas/spacing-viz.ts +51 -0
  59. package/src/v2/core/block-presets.ts +68 -0
  60. package/src/v2/core/host.ts +14 -0
  61. package/src/v2/core/registry.ts +3 -1
  62. package/src/v2/core/structure.ts +4 -2
  63. package/src/v2/editor.css +244 -11
  64. package/src/v2/editor.tsx +8 -2
  65. package/src/v2/index.ts +3 -0
  66. package/src/v2/ui/ai.tsx +9 -5
  67. package/src/v2/ui/attr-form.tsx +42 -13
  68. package/src/v2/ui/attr-hints.ts +104 -6
  69. package/src/v2/ui/background-panel.tsx +65 -21
  70. package/src/v2/ui/block-icons.tsx +152 -0
  71. package/src/v2/ui/block-menu.ts +22 -0
  72. package/src/v2/ui/block-style-panel.tsx +14 -3
  73. package/src/v2/ui/color-field.tsx +74 -9
  74. package/src/v2/ui/divider-panel.tsx +58 -0
  75. package/src/v2/ui/group-width.tsx +38 -0
  76. package/src/v2/ui/icon-picker.tsx +51 -0
  77. package/src/v2/ui/inserter.tsx +51 -7
  78. package/src/v2/ui/inspector.tsx +48 -16
  79. package/src/v2/ui/layout-panel.tsx +5 -47
  80. package/src/v2/ui/list-view.tsx +81 -7
  81. package/src/v2/{blocks → ui}/media-actions.tsx +4 -3
  82. package/src/v2/ui/motion-panel.tsx +2 -1
  83. package/src/v2/ui/parts-panel.tsx +164 -0
  84. package/src/v2/ui/pattern-library.tsx +228 -0
  85. package/src/v2/ui/placeholders.tsx +205 -0
  86. package/src/v2/ui/preset-bar.tsx +83 -0
  87. package/src/v2/ui/style-controls.tsx +65 -15
  88. package/src/v2/ui/style-supports.ts +1 -1
  89. package/src/v2/ui/style-tab.tsx +127 -41
  90. package/src/v2/ui/toolbar.tsx +59 -16
  91. package/src/v2/ui/unit-controls.tsx +88 -39
  92. package/src/locales/en/patterns.ts +0 -50
  93. package/src/locales/vi/patterns.ts +0 -50
  94. package/src/shared/patterns-kit.ts +0 -7
  95. package/src/shared/patterns-library.ts +0 -172
@@ -0,0 +1,205 @@
1
+ import { embedInfo, isSafeUrl } from "@ecomcx/ecomweb-content";
2
+ import { File as FileIcon, Image as ImageIcon, Images, Link2, Music, Plus, Video } from "lucide-react";
3
+ import { useId, useState, type ComponentType, type CSSProperties, type MouseEvent, type ReactNode } from "react";
4
+ import { useEditor } from "../context";
5
+ import { useMsgs } from "../i18n";
6
+ import { MediaActions, useFileDrop } from "./media-actions";
7
+ import type { BlockProps } from "../core/registry";
8
+ import type { MediaItem, MediaKind } from "../../shared/types";
9
+
10
+ /**
11
+ * Ô giữ chỗ dùng chung (trình soạn v2) — khối lõi và khối plugin cùng một kiểu: ô thả media, ô gõ có gợi ý, ô dán link,
12
+ * ô chọn từ danh sách, nút "+ Thêm khối". Chỉ có trong khung soạn; website không vẽ gì khi ô còn trống.
13
+ */
14
+ const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
15
+ type Icon = ComponentType<{ className?: string }>;
16
+ const KIND_ICON: Record<MediaKind, Icon> = { image: ImageIcon, gallery: Images, video: Video, audio: Music, file: FileIcon };
17
+ /** Bấm vào ô nhập / nút trong khối: không chọn khối, không để khung soạn bắt phím. */
18
+ const stop = { contentEditable: false, onMouseDown: (e: MouseEvent) => e.stopPropagation() } as const;
19
+
20
+ export interface MediaPlaceholderProps {
21
+ kind: MediaKind;
22
+ onSelect(items: MediaItem[]): void;
23
+ /** Có → hiện nút "Chèn từ URL". */
24
+ onUrl?(url: string): void;
25
+ /** Câu chính (mặc định "Kéo thả ảnh vào đây"…). */
26
+ title?: ReactNode;
27
+ icon?: Icon;
28
+ /** Kiểu tệp nhận khi tải lên (vd "application/pdf"); mặc định theo `kind`. */
29
+ accept?: string;
30
+ /** Nút / nội dung thêm dưới hàng nút (vd chọn màu nền của ảnh bìa). */
31
+ extra?: ReactNode;
32
+ className?: string;
33
+ style?: CSSProperties;
34
+ /** Ô là phần tử gốc của khối (khối Ảnh, Video…): gắn props của khối. */
35
+ blockProps?: BlockProps;
36
+ compact?: boolean;
37
+ }
38
+
39
+ /** Vùng kéo thả media: viền nét đứt luôn thấy; rê chuột → màu chính; kéo tệp vào → nền xanh, viền liền, "Thả để tải lên". */
40
+ export function MediaPlaceholder({ kind, onSelect, onUrl, title, icon, accept, extra, className, style, blockProps, compact }: MediaPlaceholderProps) {
41
+ const t = useMsgs("placeholders");
42
+ const drop = useFileDrop(kind, onSelect);
43
+ const I = icon ?? KIND_ICON[kind] ?? ImageIcon;
44
+ return (
45
+ <div {...(blockProps ?? {})} {...drop.props} data-ecx-placeholder="media"
46
+ className={cx(blockProps?.className, "ecx-media-placeholder ecx-ph-drop", compact && "is-compact", drop.over && "is-drop-target", className)}
47
+ style={{ ...(blockProps?.style ?? {}), ...style }}>
48
+ <I className="ecx-icon ecx-ph-icon" />
49
+ <p className="ecx-ph-title">{drop.over ? t.dropNow : title ?? t.drop[kind] ?? t.drop.file}</p>
50
+ {!drop.over && <p className="ecx-placeholder-hint">{t.dropSub}</p>}
51
+ {!drop.over && <MediaActions kind={kind} accept={accept} onSelect={onSelect} onUrl={onUrl} extra={extra} />}
52
+ </div>
53
+ );
54
+ }
55
+
56
+ export interface TextPlaceholderProps {
57
+ value: string;
58
+ onChange(value: string): void;
59
+ /** Chữ gợi ý (vd nhãn của thuộc tính). */
60
+ placeholder: string;
61
+ /** Nhiều dòng (textarea). */
62
+ multiline?: boolean;
63
+ /** Nhãn cho trình đọc màn hình (mặc định = gợi ý). */
64
+ label?: string;
65
+ required?: boolean;
66
+ id?: string;
67
+ className?: string;
68
+ style?: CSSProperties;
69
+ }
70
+
71
+ /** Ô gõ ngay trên khối: chữ gợi ý luôn hiện khi trống, cỡ chữ = cỡ chữ thật của chỗ đặt ô (kế thừa font). */
72
+ export function TextPlaceholder({ value, onChange, placeholder, multiline, label, required, id, className, style }: TextPlaceholderProps) {
73
+ const props = {
74
+ ...stop, id, className: cx("ecx-ph-text", className), style, value, placeholder, required, "aria-label": label ?? placeholder, "aria-required": required || undefined,
75
+ "data-ecx-placeholder": "text", onChange: (e: { target: { value: string } }) => onChange(e.target.value),
76
+ };
77
+ return multiline ? <textarea {...props} rows={2} /> : <input {...props} type="text" />;
78
+ }
79
+
80
+ export interface LinkPlaceholderProps {
81
+ /** Đường dẫn hợp lệ được gửi (đã trim). */
82
+ onSubmit(url: string): void;
83
+ title?: ReactNode;
84
+ hint?: ReactNode;
85
+ /** Kiểm link nhúng được (embedInfo) thay vì chỉ link an toàn. */
86
+ embed?: boolean;
87
+ /** Kiểm thêm (vd link YouTube); trả về false → báo lỗi. */
88
+ validate?(url: string): boolean;
89
+ buttonText?: string;
90
+ label?: string;
91
+ icon?: Icon;
92
+ defaultValue?: string;
93
+ className?: string;
94
+ blockProps?: BlockProps;
95
+ }
96
+
97
+ /** Ô dán đường dẫn ngay trong khối (nhúng, YouTube, link mua…): kiểm hợp lệ trước khi nhận. */
98
+ export function LinkPlaceholder({ onSubmit, title, hint, embed, validate, buttonText, label, icon: I = Link2, defaultValue = "", className, blockProps }: LinkPlaceholderProps) {
99
+ const t = useMsgs("placeholders");
100
+ const id = useId();
101
+ const [draft, setDraft] = useState(defaultValue);
102
+ const [error, setError] = useState<string | null>(null);
103
+ const submit = () => {
104
+ const url = draft.trim();
105
+ if (!isSafeUrl(url) || (validate && !validate(url))) return setError(t.linkInvalid);
106
+ if (embed && !embedInfo(url)) return setError(t.embedUnsupported);
107
+ setError(null);
108
+ onSubmit(url);
109
+ };
110
+ return (
111
+ <div {...(blockProps ?? {})} data-ecx-placeholder="link" className={cx(blockProps?.className, "ecx-media-placeholder ecx-ph-link", className)}>
112
+ {title && <p className="ecx-ph-title"><I className="ecx-icon" />{title}</p>}
113
+ <form className="ecx-ph-row" {...stop} onSubmit={(e) => { e.preventDefault(); submit(); }}>
114
+ <label htmlFor={id} className="ecx-ph-label">{label ?? t.link}</label>
115
+ <input id={id} className="ecx-input" type="url" inputMode="url" placeholder="https://…" value={draft} aria-invalid={!!error || undefined}
116
+ onChange={(e) => { setDraft(e.target.value); setError(null); }} />
117
+ <button type="submit" className="ecx-btn ecx-btn-primary">{buttonText ?? t.embed}</button>
118
+ </form>
119
+ {error ? <p className="ecx-media-error" role="alert">{error}</p> : hint ? <p className="ecx-placeholder-hint">{hint}</p> : null}
120
+ </div>
121
+ );
122
+ }
123
+
124
+ export interface PickPlaceholderProps {
125
+ title?: ReactNode;
126
+ label: string;
127
+ options: Array<{ value: string; label: string }>;
128
+ value?: string | null;
129
+ onChange(value: string): void;
130
+ /** Nút tạo mới (vd "Tạo form"). */
131
+ createLabel?: string;
132
+ onCreate?(): void;
133
+ hint?: ReactNode;
134
+ icon?: Icon;
135
+ className?: string;
136
+ blockProps?: BlockProps;
137
+ }
138
+
139
+ /** Chọn từ danh sách ngay trong khối (form, menu…) — không bắt nhớ mã số. */
140
+ export function PickPlaceholder({ title, label, options, value, onChange, createLabel, onCreate, hint, icon: I, className, blockProps }: PickPlaceholderProps) {
141
+ const t = useMsgs("placeholders");
142
+ const id = useId();
143
+ return (
144
+ <div {...(blockProps ?? {})} data-ecx-placeholder="pick" className={cx(blockProps?.className, "ecx-media-placeholder ecx-ph-pick", className)}>
145
+ {title && <p className="ecx-ph-title">{I && <I className="ecx-icon" />}{title}</p>}
146
+ <div className="ecx-ph-row" {...stop}>
147
+ <label htmlFor={id} className="ecx-ph-label">{label}</label>
148
+ <select id={id} className="ecx-input" value={value ?? ""} onChange={(e) => e.target.value && onChange(e.target.value)}>
149
+ <option value="">{t.choose}</option>
150
+ {options.map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
151
+ </select>
152
+ {onCreate && <button type="button" className="ecx-btn" onClick={onCreate}><Plus className="ecx-icon ecx-icon-sm" />{createLabel}</button>}
153
+ </div>
154
+ {hint && <p className="ecx-placeholder-hint">{hint}</p>}
155
+ </div>
156
+ );
157
+ }
158
+
159
+ export interface AddBlockButtonProps {
160
+ /** Khối chứa nhận khối mới; không truyền → khối chứa gần nhất của nút trên khung soạn. */
161
+ parentId?: string;
162
+ docId?: string;
163
+ /** Vị trí chèn; mặc định cuối danh sách. */
164
+ index?: number;
165
+ label?: string;
166
+ className?: string;
167
+ }
168
+
169
+ /** Nút "+ Thêm khối" lớn giữa khối chứa trống: mở trình chèn ngay tại chỗ. */
170
+ export function AddBlockButton({ parentId, docId, index, label, className }: AddBlockButtonProps) {
171
+ const { ui, store } = useEditor();
172
+ const t = useMsgs("placeholders");
173
+ const text = label ?? t.addBlock;
174
+ return (
175
+ <button type="button" className={cx("ecx-appender ecx-add-block", className)} aria-label={text} contentEditable={false} data-ecx-placeholder="add"
176
+ onMouseDown={(e) => { e.preventDefault(); e.stopPropagation(); }}
177
+ onClick={(e) => {
178
+ const el = e.currentTarget as HTMLElement;
179
+ const pid = parentId ?? el.closest<HTMLElement>("[data-ecx-block]")?.dataset.ecxBlock;
180
+ const loc = pid ? store.locate(pid) : null;
181
+ if (!pid || !loc) return;
182
+ const r = el.getBoundingClientRect();
183
+ const f = ui.canvasFrame?.getBoundingClientRect();
184
+ ui.openInserter({ mode: "appender", docId: docId ?? loc.docId, parentId: pid, index: index ?? loc.block.content?.length ?? 0, rect: { left: (f?.left ?? 0) + r.left, top: (f?.top ?? 0) + r.top, bottom: (f?.top ?? 0) + r.bottom } });
185
+ }}>
186
+ <Plus className="ecx-icon" aria-hidden />{text}
187
+ </button>
188
+ );
189
+ }
190
+
191
+ /** Nút "+ Thêm …" nhỏ ở cuối danh sách (tab, mục accordion, slide, ảnh gallery). */
192
+ export function AddItemButton({ label, onClick, className }: { label: string; onClick(): void; className?: string }) {
193
+ return (
194
+ <button type="button" className={cx("ecx-add-item", className)} {...stop} data-ecx-placeholder="add-item" onClick={onClick}>
195
+ <Plus className="ecx-icon ecx-icon-sm" aria-hidden />{label}
196
+ </button>
197
+ );
198
+ }
199
+
200
+ /** Ô bắt buộc còn trống: viền cam + "Còn thiếu: …". */
201
+ export function MissingNote({ fields }: { fields: string[] }) {
202
+ const t = useMsgs("placeholders");
203
+ if (!fields.length) return null;
204
+ return <p className="ecx-ph-missing" role="status">{t.missing.replace("{fields}", fields.join(", "))}</p>;
205
+ }
@@ -0,0 +1,83 @@
1
+ import { Check, ChevronDown, RotateCcw, Save, Star, Trash2 } from "lucide-react";
2
+ import { useEffect, useRef, useState, useSyncExternalStore } from "react";
3
+ import { blockControls } from "../../shared/panel-store";
4
+ import { applyStyle, ensurePresets, onPresetsChange, pickStyle, presetsSnapshot, sameStyle, setPresets, styleKeys, type BlockPresetMap } from "../core/block-presets";
5
+ import type { BNode } from "../core/model";
6
+ import { useEditor } from "../context";
7
+ import { useMsgs } from "../i18n";
8
+ import { cx } from "./style-controls";
9
+
10
+ /**
11
+ * Hàng "Kiểu" dưới tên khối: chọn kiểu đặt sẵn của loại khối (toàn website), báo "(đã chỉnh)" khi kiểu đang dùng đã bị sửa,
12
+ * lưu kiểu hiện tại thành kiểu mới, đặt làm mặc định khi chèn khối mới, về mặc định. Chỉ đụng thuộc tính kiểu — nội dung giữ nguyên.
13
+ */
14
+ export function PresetBar({ block, setAttrs }: { block: BNode; setAttrs(a: Record<string, unknown>): void }) {
15
+ const { host } = useEditor();
16
+ const t = useMsgs("presets");
17
+ const map = useSyncExternalStore(onPresetsChange, presetsSnapshot, presetsSnapshot);
18
+ useEffect(() => ensurePresets(host.blockPresets?.load), [host]);
19
+ const [open, setOpen] = useState(false);
20
+ const [naming, setNaming] = useState<string | null>(null);
21
+ const [busy, setBusy] = useState(false);
22
+ const ref = useRef<HTMLDivElement>(null);
23
+ useEffect(() => {
24
+ if (!open) return;
25
+ const out = (e: MouseEvent) => { if (!ref.current?.contains(e.target as Node)) { setOpen(false); setNaming(null); } };
26
+ document.addEventListener("mousedown", out, true);
27
+ return () => document.removeEventListener("mousedown", out, true);
28
+ }, [open]);
29
+ if (!host.blockPresets) return null;
30
+ const keys = styleKeys(block.type, blockControls());
31
+ const cur = pickStyle(block.attrs, keys);
32
+ const entry = map?.[block.type] ?? {};
33
+ const presets = entry.presets ?? [];
34
+ const def = entry.default ?? {};
35
+ const match = presets.find((p) => sameStyle(cur, pickStyle(p.attrs, keys)));
36
+ const isDefault = !match && sameStyle(cur, pickStyle(def, keys));
37
+ const label = match ? match.name : t.default;
38
+ const edited = !match && !isDefault;
39
+ const canSave = !!host.blockPresets.save;
40
+
41
+ const save = async (next: BlockPresetMap) => {
42
+ setBusy(true);
43
+ try { setPresets((await host.blockPresets!.save!(next as Record<string, unknown>)) as BlockPresetMap); }
44
+ finally { setBusy(false); }
45
+ };
46
+ const withEntry = (patch: Partial<typeof entry>) => ({ ...(map ?? {}), [block.type]: { ...entry, ...patch } });
47
+ const apply = (style: Record<string, unknown>) => { setAttrs(applyStyle(keys, pickStyle(style, keys))); setOpen(false); };
48
+
49
+ return (
50
+ <div className="ecx-preset" ref={ref}>
51
+ <span className="ecx-field-label">{t.label}</span>
52
+ <div className="ecx-preset-row">
53
+ <button type="button" className={cx("ecx-preset-btn", open && "is-open")} aria-haspopup="menu" aria-expanded={open} onClick={() => setOpen(!open)}>
54
+ <span>{label}{edited && <em className="ecx-preset-edited"> {t.edited}</em>}</span><ChevronDown className="ecx-icon" />
55
+ </button>
56
+ <button type="button" className="ecx-preset-reset" title={t.toDefault} aria-label={t.toDefault} disabled={isDefault} onClick={() => apply(def)}><RotateCcw className="ecx-icon" /></button>
57
+ </div>
58
+ {open && (
59
+ <div className="ecx-preset-menu" role="menu">
60
+ <div className="ecx-pop-title">{t.presets}</div>
61
+ <button type="button" role="menuitemradio" aria-checked={isDefault} className="ecx-preset-item" onClick={() => apply(def)}>{t.default}{isDefault && <Check className="ecx-icon" />}</button>
62
+ {presets.map((p) => (
63
+ <div key={p.name} className="ecx-preset-line">
64
+ <button type="button" role="menuitemradio" aria-checked={match === p} className="ecx-preset-item" onClick={() => apply(p.attrs)}>{p.name}{match === p && <Check className="ecx-icon" />}</button>
65
+ {canSave && <button type="button" className="ecx-preset-del" title={t.remove} aria-label={`${t.remove}: ${p.name}`} disabled={busy} onClick={() => save(withEntry({ presets: presets.filter((x) => x !== p) }))}><Trash2 className="ecx-icon" /></button>}
66
+ </div>
67
+ ))}
68
+ {canSave && <>
69
+ <hr />
70
+ {naming === null
71
+ ? <button type="button" className="ecx-preset-item" disabled={busy} onClick={() => setNaming("")}><Save className="ecx-icon" />{t.saveNew}</button>
72
+ : <form className="ecx-preset-name" onSubmit={(e) => { e.preventDefault(); const name = naming.trim().slice(0, 60); if (!name) return; save(withEntry({ presets: [...presets.filter((x) => x.name !== name), { name, attrs: cur }] })).then(() => { setNaming(null); setOpen(false); }); }}>
73
+ <input className="ecx-input" autoFocus value={naming} maxLength={60} placeholder={t.namePlaceholder} onChange={(e) => setNaming(e.target.value)} />
74
+ <button type="submit" className="ecx-btn ecx-btn-primary" disabled={busy || !naming.trim()}>{t.save}</button>
75
+ </form>}
76
+ <button type="button" className="ecx-preset-item" disabled={busy} onClick={() => save(withEntry({ default: Object.keys(cur).length ? cur : undefined })).then(() => setOpen(false))}><Star className="ecx-icon" />{t.makeDefault}</button>
77
+ <p className="ecx-pop-hint">{t.defaultHint}</p>
78
+ </>}
79
+ </div>
80
+ )}
81
+ </div>
82
+ );
83
+ }
@@ -1,10 +1,10 @@
1
1
  import type { Device } from "@ecomcx/ecomweb-content";
2
- import { ChevronRight } from "lucide-react";
3
- import { useState, type ComponentType, type ReactNode } from "react";
2
+ import { ChevronRight, Monitor, RotateCcw, Smartphone, Tablet } from "lucide-react";
3
+ import { createContext, useContext, useState, type ComponentType, type ReactNode } from "react";
4
4
  import { useEditor } from "../context";
5
5
  import { useMsgs } from "../i18n";
6
6
 
7
- /** 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
+ /** Thành phần chung của bảng cài đặt (cùng một giao diện cho mọi mục). */
8
8
  export const cx = (...c: unknown[]) => c.filter((x): x is string => typeof x === "string" && x !== "").join(" ");
9
9
  export type Icon = ComponentType<{ className?: string }>;
10
10
 
@@ -18,33 +18,84 @@ export function Seg<T extends string>({ value, options, onChange, label }: { val
18
18
  </div>
19
19
  );
20
20
  }
21
- export function Row({ label, children, device }: { label: string; children: ReactNode; device?: Device }) {
21
+
22
+ /** Cụm 3 icon khổ màn hình cạnh nhãn: ô này chỉnh riêng theo khổ; bấm = đổi khổ của cả trình soạn (khung soạn co lại theo). */
23
+ export function DeviceSwitch() {
24
+ const { ui } = useEditor();
25
+ const t = useMsgs("controls");
26
+ const items: Array<["desktop" | "tablet" | "mobile", Icon, string]> = [["desktop", Monitor, t.desktop], ["tablet", Tablet, t.tablet], ["mobile", Smartphone, t.mobile]];
27
+ return (
28
+ <span className="ecx-row-devices" role="group" aria-label={t.device}>
29
+ {items.map(([d, I, title]) => (
30
+ <button key={d} type="button" title={title} aria-label={title} aria-pressed={ui.device === d} className={cx(ui.device === d && "is-active")} onClick={() => ui.setDevice(d)}><I className="ecx-icon" /></button>
31
+ ))}
32
+ </span>
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Một ô của bảng: nhãn bên trái; bên phải là cụm khổ màn hình (ô chỉnh theo khổ — có `device`) và nút ↶ về mặc định (khi đã đổi — có `onReset`).
38
+ * `extra`: nút nhỏ thêm cạnh nhãn (vd xích 4 cạnh).
39
+ */
40
+ export function Row({ label, children, device, onReset, extra }: { label: ReactNode; children: ReactNode; device?: Device; onReset?(): void; extra?: ReactNode }) {
22
41
  const t = useMsgs("controls");
23
42
  return (
24
43
  <div className="ecx-style-row">
25
- <span className="ecx-field-label">{label}{device && device !== "base" && <em className="ecx-device-tag">{device === "tablet" ? t.tablet : t.mobile}</em>}</span>
44
+ <span className="ecx-row-head">
45
+ <span className="ecx-field-label">{label}</span>
46
+ {onReset && <button type="button" className="ecx-row-reset" title={t.reset} aria-label={`${t.reset}: ${typeof label === "string" ? label : ""}`} onClick={onReset}><RotateCcw className="ecx-icon" /></button>}
47
+ <span className="ecx-row-tools">{extra}{device !== undefined && <DeviceSwitch />}</span>
48
+ </span>
26
49
  {children}
27
50
  </div>
28
51
  );
29
52
  }
53
+
54
+ /** Công tắc trạng thái ngay trong một mục (Thường / Rê chuột), như bảng của các bộ khối lớn. */
55
+ export function StateTabs({ value, onChange }: { value: "normal" | "hover"; onChange(v: "normal" | "hover"): void }) {
56
+ const t = useMsgs("controls");
57
+ return (
58
+ <div role="tablist" className="ecx-seg is-text ecx-state-tabs">
59
+ {(["normal", "hover"] as const).map((v) => (
60
+ <button key={v} type="button" role="tab" aria-selected={value === v} className={cx("ecx-seg-btn", value === v && "is-active")} onClick={() => onChange(v)}>{v === "normal" ? t.stateNormal : t.stateHover}</button>
61
+ ))}
62
+ </div>
63
+ );
64
+ }
65
+
66
+ /** Đếm thứ tự mục trong một bảng: mục đầu mở sẵn, còn lại gập (như bảng của các bộ khối lớn: thấy hết tên mục trong một màn hình). */
67
+ const SectionIndex = createContext<{ n: number } | null>(null);
68
+ export function SectionGroup({ children }: { children: ReactNode }) {
69
+ return <SectionIndex.Provider value={{ n: 0 }}>{children}</SectionIndex.Provider>;
70
+ }
71
+ // Mục người dùng đã mở / gập: nhớ theo tên trong phiên soạn (đổi sang khối khác vẫn giữ)
72
+ const remembered = new Map<string, boolean>();
73
+
30
74
  /**
31
- * 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,
32
- * phần "Thêm tuỳ chọn" cho thiết lập ít dùng (tự mở khi đã có giá trị).
75
+ * Một mục: tiêu đề bấm để thu / mở, chấm khi mục có giá trị, nút đặt lại, phần "Thêm tuỳ chọn" cho thiết lập ít dùng.
76
+ * Mặc định: mục đầu của bảng (trong SectionGroup) và mục đã có giá trị thì mở; còn lại gập. `summary`: một dòng hiện khi gập.
33
77
  */
34
- export function Section({ title, children, more, defaultOpen = true, modified, onReset, moreOpen }: {
35
- title: string; children?: ReactNode; more?: ReactNode; defaultOpen?: boolean; modified?: boolean; onReset?(): void; moreOpen?: boolean;
78
+ export function Section({ title, children, more, defaultOpen, modified, onReset, moreOpen, summary, head }: {
79
+ title: string; children?: ReactNode; more?: ReactNode; defaultOpen?: boolean; modified?: boolean; onReset?(): void; moreOpen?: boolean; summary?: ReactNode;
80
+ /** Phần tử đặt trước tiêu đề (vd công tắc "Hiện" của một phần khối). */
81
+ head?: ReactNode;
36
82
  }) {
37
- const { labels } = useEditor();
38
- const [open, setOpen] = useState(defaultOpen);
83
+ const group = useContext(SectionIndex);
84
+ const [index] = useState(() => (group ? group.n++ : 0));
85
+ const [open, setOpenState] = useState(() => remembered.get(title) ?? defaultOpen ?? (!group || index === 0 || !!modified));
86
+ const setOpen = (v: boolean) => { remembered.set(title, v); setOpenState(v); };
39
87
  const [showMore, setShowMore] = useState(!!moreOpen);
40
88
  const t = useMsgs("controls");
41
89
  return (
42
- <section className="ecx-style-section">
90
+ <section className={cx("ecx-style-section", open && "is-open")}>
43
91
  <div className="ecx-style-section-head">
92
+ {head}
44
93
  <button type="button" className="ecx-style-section-toggle" aria-expanded={open} onClick={() => setOpen(!open)}>
45
- <ChevronRight className={cx("ecx-icon ecx-icon-sm ecx-chevron", open && "is-open")} aria-hidden />{title}{modified && <span className="ecx-dot" aria-label={t.modified} />}
94
+ <span className="ecx-style-section-title">{title}{modified && <span className="ecx-dot" aria-label={t.modified} />}</span>
95
+ {!open && summary && <span className="ecx-style-section-summary">{summary}</span>}
96
+ <ChevronRight className={cx("ecx-icon ecx-icon-sm ecx-chevron", open && "is-open")} aria-hidden />
46
97
  </button>
47
- {modified && onReset && <button type="button" className="ecx-link-btn" onClick={onReset}>{t.reset}</button>}
98
+ {open && modified && onReset && <button type="button" className="ecx-link-btn" onClick={onReset}>{t.reset}</button>}
48
99
  </div>
49
100
  {open && (
50
101
  <div className="ecx-style-section-body">
@@ -55,4 +106,3 @@ export function Section({ title, children, more, defaultOpen = true, modified, o
55
106
  </section>
56
107
  );
57
108
  }
58
-
@@ -13,7 +13,7 @@ const CORE: Record<string, StyleGroup[]> = {
13
13
  spacer: MINIMAL, pageBreak: ["visibility"], customHtml: MINIMAL, shortcode: MINIMAL, audio: MINIMAL,
14
14
  horizontalRule: ["color", "spacing", "border", "visibility"],
15
15
  image: MEDIA, gallery: MEDIA, video: MEDIA, youtube: MEDIA, embed: MEDIA, featuredImage: MEDIA, siteLogo: MEDIA, avatar: MEDIA,
16
- icon: ["color", "spacing", "motion", "visibility"],
16
+ icon: ["color", "spacing", "hover", "motion", "visibility"],
17
17
  socialLinks: ["color", "spacing", "motion", "visibility"],
18
18
  // Màu nút (Thường / Rê chuột) ở mục Nút riêng (core-controls) — không lặp nhóm Màu chung (dữ liệu cũ có màu vẫn hiện để sửa)
19
19
  button: NO_BACKGROUND.filter((g) => g !== "color"),