@ecomcx/ecomweb-editor 1.33.0 → 1.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/locales/en/attrs.ts +2 -1
- package/src/locales/en/background.ts +5 -0
- package/src/locales/en/bindings.ts +4 -0
- package/src/locales/en/block-desc.ts +4 -11
- package/src/locales/en/block-style.ts +17 -6
- package/src/locales/en/block-titles.ts +38 -16
- package/src/locales/en/blocks.ts +36 -6
- package/src/locales/en/columns.ts +1 -0
- package/src/locales/en/controls.ts +1 -1
- package/src/locales/en/hints.ts +241 -12
- package/src/locales/en/index.ts +3 -1
- package/src/locales/en/layout.ts +13 -0
- package/src/locales/en/motion.ts +16 -0
- package/src/locales/en/palette.ts +8 -0
- package/src/locales/en/parts.ts +2 -1
- package/src/locales/en/pattern-overrides.ts +14 -0
- package/src/locales/en/placeholders.ts +6 -3
- package/src/locales/en/v2.ts +2 -0
- package/src/locales/vi/attrs.ts +3 -2
- package/src/locales/vi/background.ts +5 -0
- package/src/locales/vi/bindings.ts +4 -0
- package/src/locales/vi/block-desc.ts +7 -14
- package/src/locales/vi/block-style.ts +18 -7
- package/src/locales/vi/block-titles.ts +48 -26
- package/src/locales/vi/blocks.ts +36 -6
- package/src/locales/vi/columns.ts +1 -0
- package/src/locales/vi/controls.ts +1 -1
- package/src/locales/vi/hints.ts +241 -12
- package/src/locales/vi/index.ts +3 -1
- package/src/locales/vi/layout.ts +13 -0
- package/src/locales/vi/motion.ts +16 -0
- package/src/locales/vi/palette.ts +8 -0
- package/src/locales/vi/parts.ts +2 -1
- package/src/locales/vi/pattern-overrides.ts +14 -0
- package/src/locales/vi/placeholders.ts +7 -4
- package/src/locales/vi/v2.ts +2 -0
- package/src/v2/blocks/columns-inspector.tsx +11 -2
- package/src/v2/blocks/index.ts +9 -6
- package/src/v2/blocks/layout-options.tsx +80 -0
- package/src/v2/blocks/layout.tsx +331 -124
- package/src/v2/blocks/media.tsx +51 -21
- package/src/v2/blocks/resize.tsx +50 -19
- package/src/v2/blocks/social-icons.ts +22 -0
- package/src/v2/blocks/text.tsx +12 -50
- package/src/v2/blocks/theme.tsx +13 -19
- package/src/v2/blocks/tools.tsx +24 -2
- package/src/v2/blocks/widgets.tsx +84 -28
- package/src/v2/canvas/block-list.tsx +30 -4
- package/src/v2/canvas/canvas-css.ts +81 -9
- package/src/v2/canvas/canvas.tsx +63 -7
- package/src/v2/canvas/dnd.ts +7 -0
- package/src/v2/canvas/variation-picker.tsx +63 -0
- package/src/v2/context.tsx +48 -0
- package/src/v2/core/bindings.ts +6 -1
- package/src/v2/core/block-presets.ts +10 -0
- package/src/v2/core/clipboard.ts +5 -5
- package/src/v2/core/host.ts +2 -0
- package/src/v2/core/model.ts +6 -6
- package/src/v2/core/registry.ts +15 -0
- package/src/v2/core/store.ts +9 -9
- package/src/v2/core/structure.ts +1 -3
- package/src/v2/editor.css +74 -0
- package/src/v2/editor.tsx +22 -7
- package/src/v2/text/rich-text.tsx +20 -5
- package/src/v2/ui/ai-markdown.ts +52 -0
- package/src/v2/ui/ai.tsx +81 -14
- package/src/v2/ui/attr-form.tsx +26 -6
- package/src/v2/ui/attr-hints.ts +115 -16
- package/src/v2/ui/background-panel.tsx +22 -0
- package/src/v2/ui/bg-video-preview.tsx +7 -0
- package/src/v2/ui/bindings.tsx +7 -2
- package/src/v2/ui/block-icons.tsx +3 -10
- package/src/v2/ui/block-live-preview.tsx +21 -0
- package/src/v2/ui/block-style-panel.tsx +3 -1
- package/src/v2/ui/command-palette.tsx +125 -0
- package/src/v2/ui/icon-picker.tsx +88 -21
- package/src/v2/ui/icon-svg.tsx +21 -0
- package/src/v2/ui/inserter.tsx +62 -12
- package/src/v2/ui/inspector.tsx +12 -6
- package/src/v2/ui/layout-panel.tsx +1 -1
- package/src/v2/ui/motion-panel.tsx +22 -13
- package/src/v2/ui/option-thumbs.tsx +81 -0
- package/src/v2/ui/pattern-library.tsx +2 -1
- package/src/v2/ui/pattern-overrides.tsx +67 -0
- package/src/v2/ui/placeholders.tsx +21 -2
- package/src/v2/ui/preset-bar.tsx +5 -5
- package/src/v2/ui/style-supports.ts +2 -3
- package/src/v2/ui/style-tab.tsx +83 -20
- package/src/v2/ui/toolbar.tsx +40 -6
- package/src/v2/ui/transform-panel.tsx +77 -0
|
@@ -163,6 +163,12 @@ export function BackgroundControl({ type, attrs, setAttrs, device }: { type: str
|
|
|
163
163
|
{inherited && !image && <p className="ecx-muted">{t.inherited}</p>}
|
|
164
164
|
</div>
|
|
165
165
|
</Row>
|
|
166
|
+
{!cover && (
|
|
167
|
+
<Row label={t.video}>
|
|
168
|
+
<input className="ecx-input" type="url" inputMode="url" aria-label={t.video} placeholder="/uploads/video.mp4" value={bg.video ?? ""} onChange={(e) => setBg({ video: e.target.value.trim() || undefined })} />
|
|
169
|
+
<p className="ecx-field-help">{t.videoHint}</p>
|
|
170
|
+
</Row>
|
|
171
|
+
)}
|
|
166
172
|
{hasImage && (
|
|
167
173
|
<>
|
|
168
174
|
{!cover && (
|
|
@@ -183,6 +189,22 @@ export function BackgroundControl({ type, attrs, setAttrs, device }: { type: str
|
|
|
183
189
|
</Row>
|
|
184
190
|
{!cover && <label className="ecx-check"><input type="checkbox" checked={!!bg.repeat} onChange={(e) => setBg({ repeat: e.target.checked || undefined })} /> {t.repeat}</label>}
|
|
185
191
|
<label className="ecx-check"><input type="checkbox" checked={cover ? !!attrs.fixed : !!bg.fixed} onChange={(e) => (cover ? setAttrs({ fixed: e.target.checked || undefined }) : setBg({ fixed: e.target.checked || undefined }))} /> {t.fixed}</label>
|
|
192
|
+
{!cover && (
|
|
193
|
+
<>
|
|
194
|
+
<Row label={`${t.imageOpacity}: ${bg.imageOpacity ?? 100}%`}>
|
|
195
|
+
<input type="range" min={0} max={100} step={5} value={bg.imageOpacity ?? 100} aria-label={t.imageOpacity} onChange={(e) => setBg({ imageOpacity: Number(e.target.value) >= 100 ? undefined : Number(e.target.value) })} />
|
|
196
|
+
</Row>
|
|
197
|
+
<Row label={`${t.imageBlur}: ${bg.imageBlur ?? 0}px`}>
|
|
198
|
+
<input type="range" min={0} max={40} step={1} value={bg.imageBlur ?? 0} aria-label={t.imageBlur} onChange={(e) => setBg({ imageBlur: Number(e.target.value) || undefined })} />
|
|
199
|
+
</Row>
|
|
200
|
+
<Row label={t.imageBlend}>
|
|
201
|
+
<select className="ecx-input" aria-label={t.imageBlend} value={bg.imageBlend ?? ""} onChange={(e) => setBg({ imageBlend: e.target.value || undefined })}>
|
|
202
|
+
<option value="">{t.blendNormal}</option>
|
|
203
|
+
{BLENDS.map((x) => <option key={x} value={x}>{(t as Record<string, string>)[`blend_${x.replace(/-/g, "_")}`] ?? x}</option>)}
|
|
204
|
+
</select>
|
|
205
|
+
</Row>
|
|
206
|
+
</>
|
|
207
|
+
)}
|
|
186
208
|
</>
|
|
187
209
|
)}
|
|
188
210
|
{cover && overlayFields}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { blockBgVideo } from "@ecomcx/ecomweb-content";
|
|
2
|
+
|
|
3
|
+
/** Xem trước video nền (style.background.video) của Section / Row / Column ngay trong khung soạn — cùng thẻ và class với website. */
|
|
4
|
+
export function BgVideoPreview({ style }: { style: unknown }) {
|
|
5
|
+
const src = blockBgVideo(style);
|
|
6
|
+
return src ? <video className="ecx-bg-video" src={src} autoPlay muted loop playsInline preload="metadata" aria-hidden="true" tabIndex={-1} contentEditable={false} data-ecx-ui /> : null;
|
|
7
|
+
}
|
package/src/v2/ui/bindings.tsx
CHANGED
|
@@ -33,7 +33,12 @@ export function BindingsPanel({ block, bindable, onChange }: { block: BNode; bin
|
|
|
33
33
|
const all = useSyncExternalStore(subscribeBindingSources, bindingSources, bindingSources);
|
|
34
34
|
const current = bindingsOf(block) ?? {};
|
|
35
35
|
const t = useMsgs("bindings");
|
|
36
|
-
const ATTR: Record<string, string> = {
|
|
36
|
+
const ATTR: Record<string, string> = {
|
|
37
|
+
content: t.attrContent, src: t.attrSrc, alt: t.attrAlt, caption: t.attrCaption, text: t.attrText, href: t.attrHref,
|
|
38
|
+
title: t.attrTitle, subtitle: t.attrSubtitle, quote: t.attrQuote, name: t.attrName, role: t.attrRole, poster: t.attrPoster, address: t.attrAddress,
|
|
39
|
+
placeholder: t.attrPlaceholder, buttonText: t.attrButtonText, summary: t.attrSummary, citation: t.attrCitation, label: t.attrLabel, tooltip: t.attrTooltip,
|
|
40
|
+
prefix: t.attrPrefix, suffix: t.attrSuffix, target: t.attrTarget,
|
|
41
|
+
};
|
|
37
42
|
const set = (attr: string, b: Binding | undefined) => {
|
|
38
43
|
const next = { ...current };
|
|
39
44
|
if (b) next[attr] = b; else delete next[attr];
|
|
@@ -66,7 +71,7 @@ export function BindingsPanel({ block, bindable, onChange }: { block: BNode; bin
|
|
|
66
71
|
</select>
|
|
67
72
|
: <input className="ecx-input" aria-label={`${ATTR[attr] ?? attr}: ${argKey}`} placeholder={argKey} value={String(b.args?.[argKey] ?? "")}
|
|
68
73
|
onChange={(e) => set(attr, { ...b, args: { ...b.args, [argKey]: e.target.value.replace(/[^\w.-]/g, "").slice(0, 200) } })} />)}
|
|
69
|
-
{b &&
|
|
74
|
+
{b && !["src", "href", "poster"].includes(attr) && (
|
|
70
75
|
<label className="ecx-check"><input type="checkbox" checked={b.args?.hideIfEmpty === true} onChange={(e) => { const { hideIfEmpty: _drop, ...rest } = b.args ?? {}; set(attr, { ...b, args: e.target.checked ? { ...rest, hideIfEmpty: true } : rest }); }} /> {t.hideIfEmpty}</label>
|
|
71
76
|
)}
|
|
72
77
|
</div>
|
|
@@ -38,25 +38,20 @@ const I = {
|
|
|
38
38
|
heading: draw([["r", 2, 2, 20, 20, 4, 0], ["p", H]]),
|
|
39
39
|
bulletList: draw([["c", 5, 6, 2], ["c", 5, 12, 2], ["c", 5, 18, 2], ["r", 9, 5, 13, 2.2, 1.1, 0], ["r", 9, 11, 13, 2.2, 1.1, 0], ["r", 9, 17, 10, 2.2, 1.1, 0]]),
|
|
40
40
|
orderedList: draw([["p", "M4 4h2v5H4.5V5.5H4zM3.5 13.5c0-1 .8-1.6 1.7-1.6s1.6.6 1.6 1.4c0 .9-.7 1.4-1.6 2.2h1.7V17H3.5v-1c1.6-1.3 2-1.8 2-2.3 0-.3-.2-.5-.4-.5s-.4.2-.4.5z"], ["r", 9, 5, 13, 2.2, 1.1, 0], ["r", 9, 11, 13, 2.2, 1.1, 0], ["r", 9, 17, 10, 2.2, 1.1, 0]]),
|
|
41
|
-
taskList: draw([["r", 2, 3, 6, 6, 1.5], ["s", "M3.6 6l1.3 1.3L6.6 5", 2], ["r", 2, 14, 6, 6, 1.5, 0], ["r", 10, 5, 12, 2.2, 1.1, 0], ["r", 10, 16, 9, 2.2, 1.1, 0]]),
|
|
42
41
|
blockquote: draw([["r", 2, 3, 20, 18, 3, 0], ["p", quote]]),
|
|
43
|
-
pullquote: draw([["r", 2, 3, 20, 2, 1], ["r", 2, 19, 20, 2, 1], ["r", 2, 6.5, 20, 11, 2, 0], ["p", "M7 9h3v3c0 1.7-1 2.8-2.6 3l-.4-1.2c.8-.3 1.1-.8 1.1-1.8H7zm7 0h3v3c0 1.7-1 2.8-2.6 3l-.4-1.2c.8-.3 1.1-.8 1.1-1.8H14z"]]),
|
|
44
42
|
codeBlock: draw([frame(), ["p", "M9.5 8.5L6 12l3.5 3.5 1.2-1.2L8.4 12l2.3-2.3zm5 0l-1.2 1.2 2.3 2.3-2.3 2.3 1.2 1.2L18 12z"]]),
|
|
45
|
-
preformatted: draw([frame(), ...lines(5, 7, [12, 8, 10])]),
|
|
46
43
|
customHtml: draw([frame(), ["p", "M8 8.5L4.5 12 8 15.5l1.2-1.2L6.9 12l2.3-2.3zm8 0l-1.2 1.2 2.3 2.3-2.3 2.3 1.2 1.2L19.5 12zM13.2 7l-3.6 10h1.7L14.9 7z"]]),
|
|
47
|
-
|
|
44
|
+
testimonials: draw([["r", 1.5, 4, 6.5, 16, 2, 0], ["r", 8.75, 4, 6.5, 16, 2, 0], ["r", 16, 4, 6.5, 16, 2, 0], ["r", 3, 14, 3.5, 1.4, .7], ["r", 10.25, 14, 3.5, 1.4, .7], ["r", 17.5, 14, 3.5, 1.4, .7]]),
|
|
45
|
+
popup: draw([["r", 2, 3, 20, 18, 2.5, 0], ["r", 6, 7, 12, 8, 1.5], ["r", 8, 17, 8, 2, 1]]),
|
|
48
46
|
details: draw([["r", 2, 3, 20, 6, 2], ["s", "M17 5l1.5 1.5L20 5", 2], ["r", 2, 11, 20, 10, 2, 0], ...lines(5, 14, [12, 8])]),
|
|
49
47
|
accordion: draw([["r", 2, 2, 20, 6, 2], ["s", "M16.5 4.3l1.5 1.5 1.5-1.5", 2], ["r", 2, 9.5, 20, 5.5, 2, 0], ["r", 2, 16.5, 20, 5.5, 2, 0]]),
|
|
50
48
|
tabs: draw([["r", 2, 8, 20, 13, 2.5, 0], ["p", "M2 6.5A2.5 2.5 0 0 1 4.5 4H10l1.5 4H2z"], ["r", 12, 4, 6, 3.5, 1.2, 0], ...lines(5, 12, [10, 7])]),
|
|
51
49
|
tab: draw([["p", "M2 6.5A2.5 2.5 0 0 1 4.5 4H10l1.5 4H2z"], ["r", 2, 8, 20, 13, 2.5, 0], ...lines(5, 12, [10, 7])]),
|
|
52
|
-
footnotes: draw([["r", 2, 3, 20, 18, 3, 0], ...lines(5, 7, [14, 10]), ["r", 5, 14, 2, 2, 1], ["r", 9, 14, 10, 2, 1]]),
|
|
53
50
|
// Media
|
|
54
51
|
image: draw(photo),
|
|
55
52
|
gallery: draw([["r", 2, 2, 9, 12, 2, 0], ["r", 13, 2, 9, 7, 2], ["r", 2, 16, 9, 6, 2], ["r", 13, 11, 9, 11, 2, 0]]),
|
|
56
53
|
cover: draw([["r", 2, 3, 20, 18, 3], ["r", 6, 10, 12, 2.2, 1.1, 2], ["r", 8, 14, 8, 1.8, .9, 2]]),
|
|
57
|
-
mediaText: draw([["r", 2, 4, 9, 16, 2], ["c", 5.5, 8.5, 1.4, 2], ...lines(13, 7, [9, 7, 8]), ["r", 13, 15, 6, 3, 1.5, 0]]),
|
|
58
54
|
video: draw([["r", 2, 4, 20, 16, 3, 0], ["c", 12, 12, 5], ["p", play, 2]]),
|
|
59
|
-
youtube: draw([["r", 2, 5, 20, 14, 4], ["p", play, 2]]),
|
|
60
55
|
audio: draw([["r", 2, 2, 20, 20, 4, 0], ["p", "M15 5v8.2A2.8 2.8 0 1 1 13 10.6V7l-4 1v7.2A2.8 2.8 0 1 1 7 12.6V6.5z"]]),
|
|
61
56
|
file: draw([["p", "M6 2h8l5 5v13a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z", 0], ["p", "M14 2l5 5h-3a2 2 0 0 1-2-2z"], ...lines(7, 12, [10, 7])]),
|
|
62
57
|
embed: draw([["r", 2, 3, 20, 18, 3, 0], ["p", "M10.6 13.4a3 3 0 0 0 4.2 0l2.5-2.5a3 3 0 1 0-4.2-4.2l-.8.8 1.2 1.2.8-.8a1.3 1.3 0 0 1 1.8 1.8l-2.5 2.5a1.3 1.3 0 0 1-1.8 0zM13.4 10.6a3 3 0 0 0-4.2 0l-2.5 2.5a3 3 0 1 0 4.2 4.2l.8-.8-1.2-1.2-.8.8a1.3 1.3 0 0 1-1.8-1.8l2.5-2.5a1.3 1.3 0 0 1 1.8 0z"]]),
|
|
@@ -68,8 +63,6 @@ const I = {
|
|
|
68
63
|
columns: draw([["r", 2, 4, 20, 16, 3, 0], ["r", 4, 6, 7, 12, 1.5], ["r", 13, 6, 7, 12, 1.5]]),
|
|
69
64
|
column: draw([["r", 2, 4, 20, 16, 3, 0], ["r", 8, 6, 8, 12, 1.5]]),
|
|
70
65
|
spacer: draw([["r", 2, 2, 20, 3, 1.5, 0], ["r", 2, 19, 20, 3, 1.5, 0], ["p", "M12 6.5l3 3h-2v5h2l-3 3-3-3h2v-5H9z"]]),
|
|
71
|
-
horizontalRule: draw([["r", 2, 5, 20, 3, 1.5, 0], ["r", 2, 10.75, 20, 2.5, 1.25], ["r", 2, 16, 20, 3, 1.5, 0]]),
|
|
72
|
-
pageBreak: draw([["r", 4, 2, 16, 7, 2, 0], ["r", 4, 15, 16, 7, 2, 0], ["r", 2, 11, 4, 2, 1], ["r", 9, 11, 6, 2, 1], ["r", 18, 11, 4, 2, 1]]),
|
|
73
66
|
carousel: draw([["r", 5, 4, 14, 14, 2.5], ["r", 1, 6, 3, 10, 1.5, 0], ["r", 20, 6, 3, 10, 1.5, 0], ["c", 9.5, 20.5, 1], ["c", 12, 20.5, 1, 0], ["c", 14.5, 20.5, 1, 0]]),
|
|
74
67
|
// Thiết kế / widget
|
|
75
68
|
buttons: draw([["r", 1, 7, 10, 10, 5], ["r", 13, 7, 10, 10, 5, 0]]),
|
|
@@ -80,6 +73,7 @@ const I = {
|
|
|
80
73
|
tableHeader: draw([["r", 2, 3, 20, 18, 2.5, 0], ["r", 2, 3, 20, 5, 2.5]]),
|
|
81
74
|
iconBox: draw([["r", 2, 2, 20, 20, 4, 0], ["c", 12, 8.5, 3.5], ["r", 6, 14, 12, 2, 1], ["r", 8, 17.5, 8, 1.6, .8]]),
|
|
82
75
|
imageBox: draw([["r", 2, 2, 20, 20, 4, 0], ["r", 5, 5, 14, 8, 2], ["p", "M6 12l3.5-3.5 2 2L14 8l4 4z", 2], ["r", 6, 15, 12, 2, 1], ["r", 8, 18.5, 8, 1.4, .7]]),
|
|
76
|
+
imageOverlay: draw([["r", 2, 2, 20, 20, 3, 0], ["r", 5, 5, 14, 14, 1.5, 0], ["r", 7, 10.5, 10, 2.2, 1.1], ["r", 9, 14.5, 6, 1.6, .8]]),
|
|
83
77
|
iconList: draw([["c", 5, 6, 2.5], ["c", 5, 12, 2.5], ["c", 5, 18, 2.5], ["r", 9.5, 4.8, 12, 2.4, 1.2, 0], ["r", 9.5, 10.8, 12, 2.4, 1.2, 0], ["r", 9.5, 16.8, 9, 2.4, 1.2, 0]]),
|
|
84
78
|
icon: draw([["r", 2, 2, 20, 20, 5, 0], ["p", star]]),
|
|
85
79
|
counter: draw([["r", 1.5, 5, 21, 14, 3, 0], ["r", 4, 8, 4.5, 8, 1.2], ["r", 9.75, 8, 4.5, 8, 1.2], ["r", 15.5, 8, 4.5, 8, 1.2]]),
|
|
@@ -127,7 +121,6 @@ const I = {
|
|
|
127
121
|
pageList: draw([["r", 2, 2, 8, 6, 1.5], ["r", 7, 10, 15, 4, 1.5, 0], ["r", 7, 17, 15, 4, 1.5, 0], ["r", 4, 8, 1.6, 11, .8, 0]]),
|
|
128
122
|
archives: draw([["r", 2, 3, 20, 6, 2], ["r", 3.5, 10, 17, 11, 2, 0], ["r", 9, 13, 6, 2, 1]]),
|
|
129
123
|
listItem: draw([["c", 6, 12, 2.5], ["r", 10.5, 10.8, 11, 2.4, 1.2, 0]]),
|
|
130
|
-
taskItem: draw([["r", 3, 8, 8, 8, 2], ["r", 13, 10.8, 9, 2.4, 1.2, 0]]),
|
|
131
124
|
map: draw([["p", "M2 6l6-3 8 3 6-3v15l-6 3-8-3-6 3z", 0], ["p", pin], ["c", 12, 8.5, 2.3, 2]]),
|
|
132
125
|
videoPopup: draw([["r", 2, 4, 20, 16, 3, 0], ["c", 12, 12, 5], ["p", play, 2]]),
|
|
133
126
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CONTENT_VERSION } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
|
+
import { useEditor } from "../context";
|
|
4
|
+
import { docFromBlocks, type BNode } from "../core/model";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Xem trước thật của khối khi rê chuột ở bảng chèn: vẽ đúng khối mẫu sẽ được chèn bằng khung soạn chỉ xem (giống thư viện mẫu),
|
|
8
|
+
* nên đồng hồ chạy, thẻ, tab… hiện y như sau khi chèn. Đợi một nhịp để rê qua nhanh không dựng khung.
|
|
9
|
+
*/
|
|
10
|
+
export function LiveBlockPreview({ id, create }: { id: string; create(): BNode }) {
|
|
11
|
+
const { ui } = useEditor();
|
|
12
|
+
const [on, setOn] = useState(false);
|
|
13
|
+
useEffect(() => { setOn(false); const t = setTimeout(() => setOn(true), 160); return () => clearTimeout(t); }, [id]);
|
|
14
|
+
const value = useMemo(() => { try { return { version: CONTENT_VERSION, blocks: [{ type: "richtext", data: docFromBlocks([create()]) }] }; } catch { return null; } }, [id]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
15
|
+
if (!ui.renderPreview || !value) return null;
|
|
16
|
+
return (
|
|
17
|
+
<div className="ecx-block-preview-live" aria-hidden="true">
|
|
18
|
+
{on && <div className="ecx-block-preview-live-inner ecx-lib-preview-inner" style={{ width: 440, transform: `scale(${288 / 440})`, marginTop: -28, height: 190 / (288 / 440) + 28 }}>{ui.renderPreview(value)}</div>}
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -15,11 +15,13 @@ const KEEP = new Set(["imageFit", "imageOpacity", "imageFilter", "imageHoverEffe
|
|
|
15
15
|
* đang chọn ở đầu tab; ô có `state` nằm trong thẻ Rê chuột / Đang mở của mục.
|
|
16
16
|
*/
|
|
17
17
|
/** `legacyOnly`: khối đã chia phần (attrs.parts) — chỉ hiện mục ô rời đã có giá trị (nội dung cũ), để sửa / xoá. */
|
|
18
|
-
|
|
18
|
+
/** `tab="settings"`: chỉ các ô khai `tab: "settings"` (bố cục / cách hiển thị) — hiện ở tab Cài đặt; mặc định là các ô còn lại (tab Kiểu dáng). */
|
|
19
|
+
export function BlockStyleSections({ type, attrs, setAttrs, device, legacyOnly, tab = "style" }: { type: string; attrs: Record<string, any>; setAttrs(a: Record<string, unknown>): void; device: Device; legacyOnly?: boolean; tab?: "style" | "settings" }) {
|
|
19
20
|
const t = useMsgs("blockStyle");
|
|
20
21
|
const sections = new Map<string, Array<[string, ControlSpec]>>();
|
|
21
22
|
for (const [name, spec] of Object.entries(coreControlsFor(type))) {
|
|
22
23
|
if (name === "tabsAlign") continue; // sửa ở tab Cài đặt
|
|
24
|
+
if ((spec.tab ?? "style") !== tab) continue;
|
|
23
25
|
const k = spec.section ?? "text";
|
|
24
26
|
sections.set(k, [...(sections.get(k) ?? []), [name, spec]]);
|
|
25
27
|
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Search } from "lucide-react";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { useEditor } from "../context";
|
|
4
|
+
import { useMsgs } from "../i18n";
|
|
5
|
+
import { copyStyle, hasCopiedStyle, pastedStyle } from "./block-menu";
|
|
6
|
+
import { cx } from "./style-controls";
|
|
7
|
+
import { useInsertOptions } from "./inserter";
|
|
8
|
+
|
|
9
|
+
export interface PaletteActions {
|
|
10
|
+
save(): void;
|
|
11
|
+
toggleList(): void;
|
|
12
|
+
toggleSettings(): void;
|
|
13
|
+
openInserter(query?: string): void;
|
|
14
|
+
}
|
|
15
|
+
interface Command { id: string; group: string; title: string; hint?: string; keywords?: string; run(): void }
|
|
16
|
+
|
|
17
|
+
const norm = (s: string) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/đ/g, "d").toLowerCase();
|
|
18
|
+
const plain = (b: { inline?: Array<{ type: string; text?: string }> }) => (b.inline ?? []).map((n) => (n.type === "text" ? n.text ?? "" : "")).join("").trim();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Bảng lệnh: một ô tìm cho mọi thứ — thao tác (lưu, hoàn tác, nhân đôi, xoá, chuyển…), chế độ xem (khổ máy, danh sách, cài đặt),
|
|
22
|
+
* chèn khối (theo tên, kể cả tên cũ tiếng Việt) và nhảy tới khối trong trang theo chữ đang có. Mở bằng ⌘⇧P / Ctrl+Shift+P hoặc nút trên thanh.
|
|
23
|
+
*/
|
|
24
|
+
export function CommandPalette({ open, onClose, actions }: { open: boolean; onClose(): void; actions: PaletteActions }) {
|
|
25
|
+
const ctx = useEditor();
|
|
26
|
+
const { store, ui, labels } = ctx;
|
|
27
|
+
const t = useMsgs("palette");
|
|
28
|
+
const blocks = useInsertOptions();
|
|
29
|
+
const [q, setQ] = useState("");
|
|
30
|
+
const [active, setActive] = useState(0);
|
|
31
|
+
const input = useRef<HTMLInputElement>(null);
|
|
32
|
+
useEffect(() => { if (open) { setQ(""); setActive(0); requestAnimationFrame(() => input.current?.focus()); } }, [open]);
|
|
33
|
+
|
|
34
|
+
const commands = useMemo<Command[]>(() => {
|
|
35
|
+
if (!open) return [];
|
|
36
|
+
const sel = store.currentIds;
|
|
37
|
+
const first = sel[0];
|
|
38
|
+
const loc = first ? store.locate(first) : null;
|
|
39
|
+
const act = (title: string, run: () => void, hint?: string, keywords = ""): Command => ({ id: `a:${title}`, group: t.groupActions, title, hint, keywords, run });
|
|
40
|
+
const list: Command[] = [
|
|
41
|
+
act(t.save, actions.save, "⌘S", "luu save"),
|
|
42
|
+
act(t.undo, () => store.undo(), "⌘Z", "hoan tac undo"),
|
|
43
|
+
act(t.redo, () => store.redo(), "⌘⇧Z", "lam lai redo"),
|
|
44
|
+
];
|
|
45
|
+
if (loc?.parent) list.push(act(t.selectParent, () => store.select([loc.parent!.id], { editing: null }), "", "khoi cha parent"));
|
|
46
|
+
if (first) {
|
|
47
|
+
list.push(
|
|
48
|
+
act(t.duplicate, () => store.duplicate(sel), "", "nhan doi duplicate copy"),
|
|
49
|
+
act(t.remove, () => store.remove(sel), "", "xoa delete remove"),
|
|
50
|
+
act(t.moveUp, () => store.moveBy(sel, -1), "", "len up"),
|
|
51
|
+
act(t.moveDown, () => store.moveBy(sel, 1), "", "xuong down"),
|
|
52
|
+
act(t.copyStyle, () => { const b = store.block(first); if (b) copyStyle(b.attrs); }, "", "chep kieu style"),
|
|
53
|
+
);
|
|
54
|
+
if (hasCopiedStyle()) list.push(act(t.pasteStyle, () => store.setAttrs(sel, pastedStyle()!), "", "dan kieu style paste"));
|
|
55
|
+
}
|
|
56
|
+
const view = (title: string, run: () => void, keywords: string): Command => ({ id: `v:${title}`, group: t.groupView, title, keywords, run });
|
|
57
|
+
list.push(
|
|
58
|
+
view(t.desktop, () => ui.setDevice("desktop"), "may tinh desktop"),
|
|
59
|
+
view(t.tablet, () => ui.setDevice("tablet"), "may tinh bang tablet"),
|
|
60
|
+
view(t.mobile, () => ui.setDevice("mobile"), "dien thoai mobile phone"),
|
|
61
|
+
view(t.listView, actions.toggleList, "danh sach khoi list tree navigator"),
|
|
62
|
+
view(t.settings, actions.toggleSettings, "cai dat settings inspector bang phai"),
|
|
63
|
+
view(t.inserter, () => actions.openInserter(), "chen khoi insert add"),
|
|
64
|
+
view(t.zoomOut, () => ui.setZoomOut(!ui.zoomOut), "thu nho zoom sap xep"),
|
|
65
|
+
);
|
|
66
|
+
return list;
|
|
67
|
+
}, [open, store, ui, actions, t, store.currentIds.join(",")]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
68
|
+
|
|
69
|
+
const results = useMemo<Command[]>(() => {
|
|
70
|
+
const k = norm(q.trim());
|
|
71
|
+
const match = (c: Command) => !k || norm(`${c.title} ${c.keywords ?? ""}`).includes(k) || k.split(/\s+/).every((w) => norm(`${c.title} ${c.keywords ?? ""}`).includes(w));
|
|
72
|
+
const out = commands.filter(match);
|
|
73
|
+
if (k.length >= 2) {
|
|
74
|
+
// Chèn khối: tên khối (kể cả tên cũ) khớp ô tìm → chạy thì mở bảng chèn đúng từ khoá đó để chèn vào chỗ đang chọn
|
|
75
|
+
const ins = blocks.filter((o) => norm(`${o.title} ${o.keywords} ${(o.aliases ?? []).join(" ")}`).includes(k)).slice(0, 8);
|
|
76
|
+
for (const o of ins) out.push({ id: `i:${o.key}`, group: t.groupInsert, title: `${t.insert}: ${o.title}`, run: () => actions.openInserter(o.title) });
|
|
77
|
+
// Đi tới khối theo chữ đang có trong trang
|
|
78
|
+
let n = 0;
|
|
79
|
+
const docs = store.getState().docs;
|
|
80
|
+
const walk = (docId: string, list: Array<{ id: string; type: string; inline?: Array<{ type: string; text?: string }>; content?: unknown[]; attrs: Record<string, unknown> }>) => {
|
|
81
|
+
for (const b of list) {
|
|
82
|
+
if (n >= 8) return;
|
|
83
|
+
const text = plain(b) || String(b.attrs.title ?? b.attrs.text ?? b.attrs.summary ?? b.attrs.label ?? "");
|
|
84
|
+
if (text && norm(text).includes(k)) { n++; out.push({ id: `g:${b.id}`, group: t.groupGoto, title: `${t.goto}: ${text.slice(0, 60)}`, hint: ctx.registry.get(b.type)?.title ?? b.type, run: () => { store.select([b.id], { editing: null }); requestAnimationFrame(() => ui.blockEl(b.id)?.scrollIntoView({ block: "center", behavior: "smooth" })); } }); }
|
|
85
|
+
if (Array.isArray(b.content)) walk(docId, b.content as never);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
for (const [id, d] of Object.entries(docs)) walk(id, (d as unknown as { blocks: never[] }).blocks);
|
|
89
|
+
}
|
|
90
|
+
return out.slice(0, 40);
|
|
91
|
+
}, [q, commands, blocks, store, ui, ctx.registry, actions, t]);
|
|
92
|
+
|
|
93
|
+
useEffect(() => setActive(0), [q]);
|
|
94
|
+
if (!open) return null;
|
|
95
|
+
const run = (c?: Command) => { if (!c) return; onClose(); setTimeout(() => c.run(), 0); };
|
|
96
|
+
const groups: Array<[string, Command[]]> = [];
|
|
97
|
+
for (const c of results) { const g = groups.find((x) => x[0] === c.group); if (g) g[1].push(c); else groups.push([c.group, [c]]); }
|
|
98
|
+
return (
|
|
99
|
+
<div className="ecx-palette-backdrop" onMouseDown={(e) => { if (e.target === e.currentTarget) onClose(); }}>
|
|
100
|
+
<div className="ecx-palette" role="dialog" aria-modal="true" aria-label={t.title}
|
|
101
|
+
onKeyDown={(e) => {
|
|
102
|
+
if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); onClose(); }
|
|
103
|
+
else if (e.key === "ArrowDown") { e.preventDefault(); setActive((a) => Math.min(results.length - 1, a + 1)); }
|
|
104
|
+
else if (e.key === "ArrowUp") { e.preventDefault(); setActive((a) => Math.max(0, a - 1)); }
|
|
105
|
+
else if (e.key === "Enter") { e.preventDefault(); run(results[active]); }
|
|
106
|
+
}}>
|
|
107
|
+
<div className="ecx-palette-input"><Search className="ecx-icon" aria-hidden /><input ref={input} role="combobox" aria-expanded aria-controls="ecx-palette-list" aria-activedescendant={results[active] ? `ecx-pal-${active}` : undefined} placeholder={t.placeholder} aria-label={t.title} value={q} onChange={(e) => setQ(e.target.value)} /></div>
|
|
108
|
+
<div className="ecx-palette-list" id="ecx-palette-list" role="listbox">
|
|
109
|
+
{groups.map(([g, list]) => (
|
|
110
|
+
<div key={g} role="presentation">
|
|
111
|
+
<div className="ecx-palette-group">{g}</div>
|
|
112
|
+
{list.map((c) => { const i = results.indexOf(c); return (
|
|
113
|
+
<button key={c.id} id={`ecx-pal-${i}`} type="button" role="option" aria-selected={i === active} className={cx("ecx-palette-item", i === active && "is-active")} onMouseMove={() => setActive(i)} onClick={() => run(c)}>
|
|
114
|
+
<span>{c.title}</span>{c.hint && <kbd>{c.hint}</kbd>}
|
|
115
|
+
</button>
|
|
116
|
+
); })}
|
|
117
|
+
</div>
|
|
118
|
+
))}
|
|
119
|
+
{!results.length && <p className="ecx-palette-empty">{t.empty}</p>}
|
|
120
|
+
</div>
|
|
121
|
+
<div className="ecx-palette-foot">{t.hint}{labels ? "" : ""}</div>
|
|
122
|
+
</div>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { ICON_NAMES,
|
|
2
|
-
import { ChevronDown, X } from "lucide-react";
|
|
1
|
+
import { ICON_CORE_COUNT, ICON_NAMES, SVG_ICON_MAX, isSvgIconValue, resolveIcon, toSvgIconValue } from "@ecomcx/ecomweb-content";
|
|
2
|
+
import { ChevronDown, Upload, X } from "lucide-react";
|
|
3
3
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { useMsgs } from "../i18n";
|
|
5
|
+
import { IconSvg } from "./icon-svg"; // đồng thời nạp thư viện icon
|
|
5
6
|
import { cx } from "./style-controls";
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
-
<svg viewBox="0 0 24 24" width={size} height={size} fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d={ICON_PATHS[name]} /></svg>
|
|
9
|
-
);
|
|
8
|
+
const PAGE = 160;
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
* Ô chọn icon: ô hiện icon đang chọn + mũi tên; bấm mở
|
|
13
|
-
* `allowNone`: có lựa chọn "Không có icon" (vd nút).
|
|
11
|
+
* Ô chọn icon: ô hiện icon đang chọn + mũi tên; bấm mở hộp có hai tab — "Thư viện" (≈1.900 icon, có ô tìm) và "SVG riêng" (dán mã SVG hoặc chọn file .svg).
|
|
12
|
+
* `allowNone`: có lựa chọn "Không có icon" (vd nút). `defaultLabel`: thêm ô "mặc định" (giá trị trống, vd icon của từng mục theo icon chung của danh sách);
|
|
13
|
+
* khi đó `noneValue` là giá trị lưu cho "Không có icon".
|
|
14
14
|
*/
|
|
15
|
-
export function IconPicker({ value, onChange, label, allowNone }: { value?: string; onChange(v: string | undefined): void; label: string; allowNone?: boolean }) {
|
|
15
|
+
export function IconPicker({ value, onChange, label, allowNone, defaultLabel, noneValue }: { value?: string; onChange(v: string | undefined): void; label: string; allowNone?: boolean; defaultLabel?: string; noneValue?: string }) {
|
|
16
16
|
const t = useMsgs("controls");
|
|
17
17
|
const [open, setOpen] = useState(false);
|
|
18
|
-
const [q, setQ] = useState("");
|
|
19
18
|
const ref = useRef<HTMLDivElement>(null);
|
|
20
19
|
useEffect(() => {
|
|
21
20
|
if (!open) return;
|
|
@@ -24,28 +23,96 @@ export function IconPicker({ value, onChange, label, allowNone }: { value?: stri
|
|
|
24
23
|
document.addEventListener("mousedown", out, true); document.addEventListener("keydown", esc, true);
|
|
25
24
|
return () => { document.removeEventListener("mousedown", out, true); document.removeEventListener("keydown", esc, true); };
|
|
26
25
|
}, [open]);
|
|
27
|
-
const
|
|
28
|
-
const
|
|
26
|
+
const has = !!value && !!resolveIcon(value);
|
|
27
|
+
const text = has ? (isSvgIconValue(value) ? t.iconCustom : value) : value === noneValue && noneValue ? t.noIcon : defaultLabel ?? t.noIcon;
|
|
29
28
|
return (
|
|
30
29
|
<div className="ecx-icon-picker" ref={ref}>
|
|
31
30
|
<button type="button" className={cx("ecx-icon-picker-btn", open && "is-open")} aria-haspopup="dialog" aria-expanded={open} aria-label={label} onClick={() => setOpen(!open)}>
|
|
32
|
-
{has ? <
|
|
33
|
-
<span className="ecx-icon-picker-name">{has ?
|
|
31
|
+
{has ? <IconSvg name={value} size={22} /> : <span className="ecx-muted">{text}</span>}
|
|
32
|
+
<span className="ecx-icon-picker-name">{has ? text : ""}</span>
|
|
34
33
|
<ChevronDown className="ecx-icon" />
|
|
35
34
|
</button>
|
|
36
35
|
{open && (
|
|
37
36
|
// preventDefault: ô này nằm trong <label> của form — không chặn thì bấm trong bảng kích lại nút mở (bảng đóng rồi mở lại)
|
|
38
37
|
<div className="ecx-icon-pop" role="dialog" aria-label={label} onClick={(e) => e.preventDefault()}>
|
|
39
|
-
<
|
|
40
|
-
<div className="ecx-icon-grid" role="listbox" aria-label={label}>
|
|
41
|
-
{allowNone && <button type="button" role="option" aria-selected={!has} title={t.noIcon} className={cx("ecx-icon-cell", !has && "is-active")} onClick={() => { onChange(undefined); setOpen(false); }}><X className="ecx-icon" /></button>}
|
|
42
|
-
{names.map((n) => (
|
|
43
|
-
<button key={n} type="button" role="option" aria-selected={value === n} title={n} className={cx("ecx-icon-cell", value === n && "is-active")} onClick={() => { onChange(n); setOpen(false); }}><Svg name={n} /></button>
|
|
44
|
-
))}
|
|
45
|
-
</div>
|
|
46
|
-
{!names.length && <p className="ecx-pop-hint">{t.noIconFound}</p>}
|
|
38
|
+
<IconGrid value={value} label={label} allowNone={allowNone} defaultLabel={defaultLabel} noneValue={noneValue} onPick={(v) => { onChange(v); setOpen(false); }} />
|
|
47
39
|
</div>
|
|
48
40
|
)}
|
|
49
41
|
</div>
|
|
50
42
|
);
|
|
51
43
|
}
|
|
44
|
+
|
|
45
|
+
/** Hộp chọn icon: tab Thư viện (tìm + lưới) / SVG riêng — dùng trong ô chọn icon của bảng phải và trong thanh công cụ của khối. */
|
|
46
|
+
export function IconGrid({ value, onPick, label, allowNone, defaultLabel, noneValue, className }: { value?: string; onPick(v: string | undefined): void; label: string; allowNone?: boolean; defaultLabel?: string; noneValue?: string; className?: string }) {
|
|
47
|
+
const t = useMsgs("controls");
|
|
48
|
+
const [tab, setTab] = useState<"library" | "svg">(isSvgIconValue(value) ? "svg" : "library");
|
|
49
|
+
return (
|
|
50
|
+
<div className={className}>
|
|
51
|
+
<div className="ecx-icon-tabs" role="tablist" aria-label={label}>
|
|
52
|
+
<button type="button" role="tab" aria-selected={tab === "library"} className={cx("ecx-icon-tab", tab === "library" && "is-active")} onClick={() => setTab("library")}>{t.iconLibrary}</button>
|
|
53
|
+
<button type="button" role="tab" aria-selected={tab === "svg"} className={cx("ecx-icon-tab", tab === "svg" && "is-active")} onClick={() => setTab("svg")}>{t.iconCustom}</button>
|
|
54
|
+
</div>
|
|
55
|
+
{tab === "library" ? <LibraryTab value={value} onPick={onPick} label={label} allowNone={allowNone} defaultLabel={defaultLabel} noneValue={noneValue} /> : <SvgTab value={value} onPick={onPick} />}
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function LibraryTab({ value, onPick, label, allowNone, defaultLabel, noneValue }: { value?: string; onPick(v: string | undefined): void; label: string; allowNone?: boolean; defaultLabel?: string; noneValue?: string }) {
|
|
61
|
+
const t = useMsgs("controls");
|
|
62
|
+
const [q, setQ] = useState("");
|
|
63
|
+
const [shown, setShown] = useState(PAGE);
|
|
64
|
+
// Bộ lõi (tên quen thuộc) đứng trước, rồi tới thư viện; tìm theo tên không dấu, bỏ gạch nối
|
|
65
|
+
const all = ICON_NAMES as readonly string[];
|
|
66
|
+
const names = useMemo(() => {
|
|
67
|
+
const k = q.trim().toLowerCase().replace(/[\s_]+/g, "-");
|
|
68
|
+
if (!k) return all;
|
|
69
|
+
const starts: string[] = [], has: string[] = [];
|
|
70
|
+
for (const n of all) { const l = n.toLowerCase(); if (l.startsWith(k)) starts.push(n); else if (l.includes(k) || l.replace(/-/g, "").includes(k.replace(/-/g, ""))) has.push(n); }
|
|
71
|
+
return [...starts, ...has];
|
|
72
|
+
}, [q, all]);
|
|
73
|
+
useEffect(() => setShown(PAGE), [q]);
|
|
74
|
+
const has = !!value && !!resolveIcon(value) && !isSvgIconValue(value);
|
|
75
|
+
const none = (noneValue ?? "") !== "" && value === noneValue;
|
|
76
|
+
return (
|
|
77
|
+
<>
|
|
78
|
+
<input className="ecx-input" autoFocus placeholder={`${t.searchIcons} (${all.length})`} value={q} onChange={(e) => setQ(e.target.value)} aria-label={t.searchIcons} />
|
|
79
|
+
<div className="ecx-icon-grid" role="listbox" aria-label={label}>
|
|
80
|
+
{defaultLabel && <button type="button" role="option" aria-selected={!value} title={defaultLabel} className={cx("ecx-icon-cell", !value && "is-active")} onClick={() => onPick(undefined)}><span className="ecx-icon-cell-text">{defaultLabel}</span></button>}
|
|
81
|
+
{(allowNone || noneValue) && <button type="button" role="option" aria-selected={defaultLabel ? none : !has} title={t.noIcon} className={cx("ecx-icon-cell", (defaultLabel ? none : !has) && "is-active")} onClick={() => onPick(noneValue)}><X className="ecx-icon" /></button>}
|
|
82
|
+
{names.slice(0, shown).map((n) => (
|
|
83
|
+
<button key={n} type="button" role="option" aria-selected={value === n} title={n} className={cx("ecx-icon-cell", value === n && "is-active")} onClick={() => onPick(n)}><IconSvg name={n} /></button>
|
|
84
|
+
))}
|
|
85
|
+
</div>
|
|
86
|
+
{names.length > shown && <button type="button" className="ecx-link-btn" onClick={() => setShown(shown + PAGE * 3)}>{t.iconMore.replace("{n}", String(names.length - shown))}</button>}
|
|
87
|
+
{!names.length && <p className="ecx-pop-hint">{t.noIconFound}</p>}
|
|
88
|
+
{all.length <= ICON_CORE_COUNT && <p className="ecx-pop-hint">{t.iconLibraryLoading}</p>}
|
|
89
|
+
</>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function SvgTab({ value, onPick }: { value?: string; onPick(v: string | undefined): void }) {
|
|
94
|
+
const t = useMsgs("controls");
|
|
95
|
+
const [code, setCode] = useState("");
|
|
96
|
+
const [error, setError] = useState(false);
|
|
97
|
+
const file = useRef<HTMLInputElement>(null);
|
|
98
|
+
const parsed = useMemo(() => (code.trim() ? toSvgIconValue(code) : null), [code]);
|
|
99
|
+
useEffect(() => setError(false), [code]);
|
|
100
|
+
const loadFile = async (f?: File | null) => {
|
|
101
|
+
if (!f) return;
|
|
102
|
+
if (f.size > 200_000) { setError(true); return; }
|
|
103
|
+
setCode(await f.text());
|
|
104
|
+
};
|
|
105
|
+
return (
|
|
106
|
+
<div className="ecx-icon-svg">
|
|
107
|
+
<p className="ecx-pop-hint">{t.iconCustomHint}</p>
|
|
108
|
+
<textarea className="ecx-input" rows={5} spellCheck={false} placeholder={'<svg viewBox="0 0 24 24">…</svg>'} aria-label={t.iconCustomCode} value={code} onChange={(e) => setCode(e.target.value)} />
|
|
109
|
+
<div className="ecx-icon-svg-row">
|
|
110
|
+
<button type="button" className="ecx-link-btn" onClick={() => file.current?.click()}><Upload className="ecx-icon ecx-icon-sm" aria-hidden /> {t.iconCustomFile}</button>
|
|
111
|
+
<input ref={file} type="file" accept=".svg,image/svg+xml" hidden onChange={(e) => { void loadFile(e.target.files?.[0]); e.target.value = ""; }} />
|
|
112
|
+
<span className="ecx-icon-svg-preview" aria-label={t.iconCustomPreview}>{parsed ? <IconSvg name={parsed} size={32} /> : isSvgIconValue(value) && !code ? <IconSvg name={value} size={32} /> : null}</span>
|
|
113
|
+
</div>
|
|
114
|
+
{(error || (code.trim() && !parsed)) && <p className="ecx-pop-hint" role="alert">{t.iconCustomInvalid.replace("{max}", String(SVG_ICON_MAX))}</p>}
|
|
115
|
+
<button type="button" className="ecx-btn ecx-btn-primary" disabled={!parsed} onClick={() => parsed && onPick(parsed)}>{t.iconCustomUse}</button>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createElement, type ReactNode } from "react";
|
|
2
|
+
import { registerIconLibrary, resolveIcon, type SvgIconNode } from "@ecomcx/ecomweb-content";
|
|
3
|
+
import { ICON_LIBRARY } from "@ecomcx/ecomweb-content/icon-library";
|
|
4
|
+
|
|
5
|
+
// ≈1.900 icon của thư viện: nạp một lần khi trình soạn khởi động (cùng thư viện với website)
|
|
6
|
+
registerIconLibrary(ICON_LIBRARY);
|
|
7
|
+
|
|
8
|
+
const camel = (props: Record<string, string>): Record<string, string> => Object.fromEntries(Object.entries(props).map(([k, v]) => [k.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()), v]));
|
|
9
|
+
const node = (n: SvgIconNode, key: number): ReactNode => createElement(n.tag, { key, ...camel(n.props) }, ...n.children.map(node));
|
|
10
|
+
|
|
11
|
+
/** Vẽ icon theo giá trị lưu (tên trong bảng / thư viện, hoặc SVG riêng `svg:…` đã lọc) — cùng cách với website. Không có icon → null. */
|
|
12
|
+
export function IconSvg({ name, size = 24, stroke = 2, className, label, style }: { name?: unknown; size?: number; stroke?: number; className?: string; label?: string; style?: import("react").CSSProperties }): ReactNode {
|
|
13
|
+
const r = resolveIcon(name);
|
|
14
|
+
if (!r) return null;
|
|
15
|
+
const a11y = label ? { role: "img", "aria-label": label } : { "aria-hidden": true as const, focusable: "false" as const };
|
|
16
|
+
if (r.kind === "svg") {
|
|
17
|
+
const root = camel(r.icon.root);
|
|
18
|
+
return <svg className={className} style={style && Object.keys(style).length ? style : undefined} width={size} height={size} viewBox={r.icon.viewBox} fill={root.fill ?? "currentColor"} {...(root.stroke ? { stroke: root.stroke } : {})} {...(root.strokeWidth ? { strokeWidth: root.strokeWidth } : {})} {...(root.strokeLinecap ? { strokeLinecap: root.strokeLinecap as "round" } : {})} {...(root.strokeLinejoin ? { strokeLinejoin: root.strokeLinejoin as "round" } : {})} {...a11y}>{r.icon.children.map(node)}</svg>;
|
|
19
|
+
}
|
|
20
|
+
return <svg className={className} style={style && Object.keys(style).length ? style : undefined} width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" {...a11y}><path d={r.d} /></svg>;
|
|
21
|
+
}
|