hd-garden 0.7.0 → 0.8.1
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/.agents/skills/garden-ui/SKILL.md +26 -19
- package/.env.en.example +67 -0
- package/README.en.md +57 -0
- package/README.md +31 -46
- package/app/a2/[slug]/page.tsx +4 -3
- package/app/a2/page.tsx +4 -3
- package/app/about/page.tsx +8 -9
- package/app/admin/page.tsx +7 -6
- package/app/admin/preview/[...path]/page.tsx +2 -1
- package/app/admin/ui/blocks/page.tsx +2 -1
- package/app/admin/ui/catalog/page.tsx +2 -1
- package/app/admin/ui/evals/page.tsx +2 -1
- package/app/admin/ui/page.tsx +2 -1
- package/app/blog/[...path]/page.tsx +14 -5
- package/app/blog/page.tsx +4 -2
- package/app/columns/[column]/[...path]/page.tsx +17 -18
- package/app/columns/page.tsx +12 -8
- package/app/components/AdminUiShell.tsx +4 -3
- package/app/components/ArticlePager.tsx +32 -0
- package/app/components/ArticleReferences.tsx +4 -1
- package/app/components/ArticleTags.tsx +3 -2
- package/app/components/BlogArchive.tsx +20 -16
- package/app/components/Comments.tsx +17 -10
- package/app/components/ContentEmptyState.tsx +29 -28
- package/app/components/ExternalEmbed.tsx +15 -14
- package/app/components/FeedMore.tsx +70 -0
- package/app/components/GardenContentGuide.tsx +10 -2
- package/app/components/GardenDocsNav.tsx +29 -22
- package/app/components/GiscusComments.tsx +2 -1
- package/app/components/GithubLiveOverview.tsx +10 -8
- package/app/components/GithubProfile.tsx +31 -21
- package/app/components/HandbookLangRedirect.tsx +30 -0
- package/app/components/HandbookLangSwitch.tsx +53 -0
- package/app/components/HandbookView.tsx +270 -0
- package/app/components/MarkdownArticle.tsx +39 -9
- package/app/components/MarkdownImage.tsx +6 -4
- package/app/components/SiteChrome.tsx +11 -2
- package/app/components/SiteFooter.tsx +6 -4
- package/app/components/SiteHeader.tsx +38 -25
- package/app/components/TableOfContents.tsx +15 -9
- package/app/components/ThoughtCard.tsx +16 -14
- package/app/components/ThoughtFeed.tsx +41 -0
- package/app/components/UiBlock.tsx +3 -2
- package/app/components/UiLang.tsx +18 -0
- package/app/components/Webmentions.tsx +13 -12
- package/app/components/WidgetChart.tsx +8 -6
- package/app/components/garden/FilePreview.tsx +14 -12
- package/app/components/garden/GardenBlock.tsx +10 -1
- package/app/components/garden/GardenFile.tsx +10 -8
- package/app/components/garden/RefDemo.tsx +62 -0
- package/app/components/garden/blocks.tsx +90 -32
- package/app/components/garden/chrome.tsx +20 -13
- package/app/components/garden/patterns.tsx +497 -0
- package/app/garden/[...slug]/page.tsx +11 -130
- package/app/garden/en/[...slug]/page.tsx +28 -0
- package/app/garden/en/page.tsx +16 -0
- package/app/garden/page.tsx +8 -74
- package/app/garden-modules.d.ts +3 -0
- package/app/globals.css +10 -0
- package/app/layout.tsx +2 -4
- package/app/lib/a2-meta.ts +26 -3
- package/app/lib/content.ts +13 -3
- package/app/lib/garden-docs-meta.ts +84 -5
- package/app/lib/github-id.mjs +8 -0
- package/app/lib/handbook-path.ts +11 -0
- package/app/lib/handbook-switch.mjs +12 -0
- package/app/lib/i18n.ts +621 -0
- package/app/lib/page-alternates.ts +2 -2
- package/app/lib/rehype-field-tables.mjs +47 -0
- package/app/lib/remark-a2.mjs +18 -8
- package/app/lib/remark-link-card.mjs +22 -0
- package/app/lib/remark-ref-demos.mjs +42 -0
- package/app/lib/tag-filter.ts +9 -2
- package/app/not-found.tsx +34 -4
- package/app/page.tsx +17 -14
- package/app/rss.xml/route.ts +2 -1
- package/app/site.config.ts +1 -1
- package/app/styles/a2.css +439 -32
- package/app/styles/shell.css +21 -2
- package/app/tags/page.tsx +7 -5
- package/app/thoughts/[slug]/page.tsx +2 -1
- package/app/thoughts/page/[n]/page.tsx +44 -0
- package/app/thoughts/page.tsx +4 -7
- package/app/ui-parity/page.tsx +38 -22
- package/bin/garden.mjs +133 -19
- package/docs/handbook/ai-writing.md +99 -0
- package/docs/handbook/assets/reading-layout-en.svg +26 -0
- package/docs/handbook/components/card.md +43 -20
- package/docs/handbook/components/chart.md +10 -24
- package/docs/handbook/components/choice.md +42 -0
- package/docs/handbook/components/compare.md +34 -21
- package/docs/handbook/components/figure.md +33 -44
- package/docs/handbook/components/file.md +10 -13
- package/docs/handbook/components/html.md +10 -28
- package/docs/handbook/components/interactive.md +9 -33
- package/docs/handbook/components/list.md +65 -0
- package/docs/handbook/components/relations.md +95 -0
- package/docs/handbook/components/stat.md +10 -26
- package/docs/handbook/components/timeline.md +13 -26
- package/docs/handbook/components.md +210 -19
- package/docs/handbook/config.md +7 -5
- package/docs/handbook/deploy.md +2 -2
- package/docs/handbook/design.md +23 -15
- package/docs/handbook/en/ai-writing.md +99 -0
- package/docs/handbook/en/components/card.md +67 -0
- package/docs/handbook/en/components/chart.md +38 -0
- package/docs/handbook/en/components/choice.md +42 -0
- package/docs/handbook/en/components/compare.md +53 -0
- package/docs/handbook/en/components/figure.md +59 -0
- package/docs/handbook/en/components/file/checklist.md +17 -0
- package/docs/handbook/en/components/file.md +29 -0
- package/docs/handbook/en/components/html.md +49 -0
- package/docs/handbook/en/components/interactive.md +41 -0
- package/docs/handbook/en/components/list.md +65 -0
- package/docs/handbook/en/components/relations.md +95 -0
- package/docs/handbook/en/components/stat.md +39 -0
- package/docs/handbook/en/components/timeline.md +37 -0
- package/docs/handbook/en/components.md +222 -0
- package/docs/handbook/en/config.md +127 -0
- package/docs/handbook/en/deploy.md +273 -0
- package/docs/handbook/en/design.md +88 -0
- package/docs/handbook/en/index.md +192 -0
- package/docs/handbook/en/reference/card.md +85 -0
- package/docs/handbook/en/reference/chart.md +68 -0
- package/docs/handbook/en/reference/choice.md +69 -0
- package/docs/handbook/en/reference/compare.md +59 -0
- package/docs/handbook/en/reference/figure.md +84 -0
- package/docs/handbook/en/reference/file/checklist.md +17 -0
- package/docs/handbook/en/reference/file.md +50 -0
- package/docs/handbook/en/reference/html.md +62 -0
- package/docs/handbook/en/reference/interactive.md +71 -0
- package/docs/handbook/en/reference/list.md +72 -0
- package/docs/handbook/en/reference/relations.md +113 -0
- package/docs/handbook/en/reference/stat.md +60 -0
- package/docs/handbook/en/reference/timeline.md +56 -0
- package/docs/handbook/en/reference.md +26 -0
- package/docs/handbook/en/start.md +92 -0
- package/docs/handbook/en/workflow.md +82 -0
- package/docs/handbook/en/writing.md +157 -0
- package/docs/handbook/index.md +99 -4
- package/docs/handbook/reference/card.md +85 -0
- package/docs/handbook/reference/chart.md +68 -0
- package/docs/handbook/reference/choice.md +69 -0
- package/docs/handbook/reference/compare.md +59 -0
- package/docs/handbook/reference/figure.md +84 -0
- package/docs/handbook/reference/file/checklist.md +17 -0
- package/docs/handbook/reference/file.md +50 -0
- package/docs/handbook/reference/html.md +62 -0
- package/docs/handbook/reference/interactive.md +71 -0
- package/docs/handbook/reference/list.md +72 -0
- package/docs/handbook/reference/relations.md +113 -0
- package/docs/handbook/reference/stat.md +60 -0
- package/docs/handbook/reference/timeline.md +56 -0
- package/docs/handbook/reference.md +26 -0
- package/docs/handbook/start.md +1 -1
- package/docs/handbook/workflow.md +6 -5
- package/docs/handbook/writing.md +21 -12
- package/next.config.ts +6 -0
- package/package.json +2 -1
- package/public/images/garden/ref-card-book.svg +1 -0
- package/public/images/garden/ref-card-lamp.svg +1 -0
- package/scripts/build-content.mjs +57 -16
- package/scripts/favicon-ico.mjs +9 -6
- package/scripts/file-preview.mjs +10 -7
- package/scripts/handbook-allowlist.mjs +18 -0
- package/scripts/handbook-facts.mjs +339 -11
- package/scripts/html-lang.mjs +36 -0
- package/scripts/link-assets.mjs +7 -0
- package/scripts/new-post.mjs +18 -9
- package/scripts/prepare-pages-output.mjs +30 -18
- package/scripts/redirects.mjs +17 -6
- package/scripts/static-404.mjs +25 -0
- package/scripts/ui-cli.mjs +14 -8
- package/templates/garden-blocks.md +67 -0
- package/templates/garden-snippets.plist +61 -0
- package/templates/site/Makefile.en +45 -0
- package/templates/site/README.en.md +15 -0
- package/templates/site/site.config.en.yaml +55 -0
- package/templates/site/site.config.yaml +2 -0
- package/templates/skills-en/garden-interactive-chart/SKILL.md +84 -0
- package/templates/skills-en/garden-interactive-chart/references/shell.html +49 -0
- package/templates/skills-en/garden-interactive-chart/references/style.md +61 -0
- package/templates/skills-en/garden-post-review/SKILL.md +63 -0
- package/templates/skills-en/garden-ui/SKILL.md +118 -0
- package/templates/workflows/pages.en.yml +195 -0
- package/templates/workflows/pages.yml +195 -0
- package/ui/core/apply.mjs +22 -4
- package/ui/core/cli-locale.mjs +67 -0
- package/ui/core/fence.mjs +18 -3
- package/ui/core/fix-prompt.mjs +18 -15
- package/ui/core/garden-fence.mjs +64 -0
- package/ui/core/scan.mjs +19 -3
- package/ui/garden/adjacent.mjs +21 -0
- package/ui/garden/catalog.mjs +205 -4
- package/ui/garden/check.mjs +12 -0
- package/ui/garden/file-block.mjs +8 -7
- package/ui/garden/html-lang.mjs +88 -0
- package/ui/garden/markdown-links.mjs +7 -4
- package/ui/garden/paging.mjs +30 -0
- package/ui/garden/parse.mjs +45 -0
- package/ui/garden/relations.mjs +252 -0
- package/ui/garden/search-index.mjs +6 -4
- package/ui/garden/source-icon.mjs +54 -0
- package/ui/parity-fixtures.mjs +77 -0
- package/ui/preview/dist/garden-ui.css +9 -9
- package/ui/preview/dist/garden-ui.esm.js +206 -169
- package/ui/preview/dist/garden-ui.iife.js +206 -169
- package/ui/prompt/rules.md +28 -1
- package/ui/react/components/chart.tsx +8 -5
- package/ui/react/components/display.tsx +7 -4
- package/ui/react/components/inputs.tsx +6 -3
- package/ui/react/components/layout.tsx +6 -4
- package/ui/react/components/media.tsx +7 -4
- package/ui/react/components/sandbox.tsx +3 -1
- package/ui/react/index.tsx +11 -7
- package/ui/react/lang.tsx +89 -0
- package/ui/react/parity.tsx +8 -6
- package/ui/react/runtime.tsx +3 -1
- package/ui/react/ui.css +9 -9
- package/worker/index.ts +14 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useCallback, useEffect, useId, useRef, useState, type FocusEvent, type MouseEvent } from "react";
|
|
4
4
|
import { createPortal } from "react-dom";
|
|
5
|
+
import { siteLang, ui, type UiLang } from "../../lib/i18n";
|
|
5
6
|
import { previewBody, previewFrameProps } from "../../../ui/garden/file-block.mjs";
|
|
6
7
|
import { ActionGlyph, FileGlyph } from "./file-icons";
|
|
7
8
|
|
|
@@ -25,7 +26,8 @@ type Loaded = { state: "idle" | "loading" | "error" } | { state: "ready"; html:
|
|
|
25
26
|
* The control is a link: without JavaScript it opens the same preview (or the
|
|
26
27
|
* file) in a new tab, next to the card's download link.
|
|
27
28
|
*/
|
|
28
|
-
export function FilePreview({ file, compact }: { file: PreviewFile; compact: boolean }) {
|
|
29
|
+
export function FilePreview({ file, compact, lang }: { file: PreviewFile; compact: boolean; lang?: UiLang }) {
|
|
30
|
+
const t = ui(lang);
|
|
29
31
|
const [open, setOpen] = useState(false);
|
|
30
32
|
const [loaded, setLoaded] = useState<Loaded>({ state: "idle" });
|
|
31
33
|
const trigger = useRef<HTMLAnchorElement>(null);
|
|
@@ -117,7 +119,7 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
|
|
|
117
119
|
if (file.preview === "md" && loaded.state !== "ready" && loaded.state !== "loading") load();
|
|
118
120
|
};
|
|
119
121
|
|
|
120
|
-
const frame = previewFrameProps(file) as { src: string; title: string; referrerPolicy: "no-referrer"; sandbox?: string } | null;
|
|
122
|
+
const frame = previewFrameProps(file, lang ?? siteLang) as { src: string; title: string; referrerPolicy: "no-referrer"; sandbox?: string } | null;
|
|
121
123
|
const drawer = open
|
|
122
124
|
? createPortal(
|
|
123
125
|
<div className="gf-layer">
|
|
@@ -130,13 +132,13 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
|
|
|
130
132
|
{file.meta ? <div className="gf-meta">{file.meta}</div> : null}
|
|
131
133
|
</div>
|
|
132
134
|
<div className="gf-act">
|
|
133
|
-
<a className="gf-btn icon" href={file.url} download={file.name} aria-label={
|
|
135
|
+
<a className="gf-btn icon" href={file.url} download={file.name} aria-label={t.downloadNamed(file.name)} title={t.download}>
|
|
134
136
|
<ActionGlyph name="download" />
|
|
135
137
|
</a>
|
|
136
|
-
<a className="gf-btn icon" href={file.url} target="_blank" rel="noopener noreferrer" aria-label={
|
|
138
|
+
<a className="gf-btn icon" href={file.url} target="_blank" rel="noopener noreferrer" aria-label={t.openNamedInTab(file.name)} title={t.openInTab}>
|
|
137
139
|
<ActionGlyph name="external" />
|
|
138
140
|
</a>
|
|
139
|
-
<button ref={closeButton} type="button" className="gf-btn icon" onClick={close} aria-label=
|
|
141
|
+
<button ref={closeButton} type="button" className="gf-btn icon" onClick={close} aria-label={t.closePreview} title={ui(lang).close.trim()}>
|
|
140
142
|
<ActionGlyph name="close" />
|
|
141
143
|
</button>
|
|
142
144
|
</div>
|
|
@@ -147,14 +149,14 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
|
|
|
147
149
|
<div className="markdown-body prose gf-doc" dangerouslySetInnerHTML={{ __html: loaded.html }} />
|
|
148
150
|
) : loaded.state === "error" ? (
|
|
149
151
|
<div className="gf-state" role="alert">
|
|
150
|
-
<p
|
|
152
|
+
<p>{t.previewFailed}</p>
|
|
151
153
|
<div className="gf-state-act">
|
|
152
|
-
<button type="button" className="gf-btn" onClick={() => load()}
|
|
153
|
-
<a className="gf-btn" href={file.url} download={file.name}
|
|
154
|
+
<button type="button" className="gf-btn" onClick={() => load()}>{t.retry}</button>
|
|
155
|
+
<a className="gf-btn" href={file.url} download={file.name}>{t.downloadFile}</a>
|
|
154
156
|
</div>
|
|
155
157
|
</div>
|
|
156
158
|
) : (
|
|
157
|
-
<div className="gf-state" role="status"
|
|
159
|
+
<div className="gf-state" role="status">{t.previewLoading}</div>
|
|
158
160
|
)}
|
|
159
161
|
</div>
|
|
160
162
|
) : (
|
|
@@ -165,7 +167,7 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
|
|
|
165
167
|
<span tabIndex={0} className="gf-sentinel" data-gf-sentinel="after" onFocus={fromAfter} />
|
|
166
168
|
</div>
|
|
167
169
|
)}
|
|
168
|
-
<p className="gf-hint">
|
|
170
|
+
<p className="gf-hint">{t.escClose}</p>
|
|
169
171
|
</aside>
|
|
170
172
|
</div>,
|
|
171
173
|
document.body,
|
|
@@ -183,10 +185,10 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
|
|
|
183
185
|
data-compact={compact ? "true" : undefined}
|
|
184
186
|
onClick={openDrawer}
|
|
185
187
|
aria-haspopup="dialog"
|
|
186
|
-
aria-label={
|
|
188
|
+
aria-label={t.previewNamed(file.name)}
|
|
187
189
|
>
|
|
188
190
|
<ActionGlyph name="eye" />
|
|
189
|
-
<span className="gf-lb"
|
|
191
|
+
<span className="gf-lb">{t.preview}</span>
|
|
190
192
|
</a>
|
|
191
193
|
{drawer}
|
|
192
194
|
</>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import { useUiLang } from "../UiLang";
|
|
3
4
|
import { readGardenPayload } from "../../lib/remark-garden.mjs";
|
|
4
5
|
import {
|
|
5
6
|
GardenCard,
|
|
@@ -15,8 +16,10 @@ import {
|
|
|
15
16
|
GardenUnknown,
|
|
16
17
|
} from "./blocks";
|
|
17
18
|
import { GardenFile } from "./GardenFile";
|
|
19
|
+
import { GardenChoice, GardenList, GardenRelations } from "./patterns";
|
|
18
20
|
|
|
19
21
|
export function GardenBlock({ dataPayload }: { dataPayload?: string }) {
|
|
22
|
+
const lang = useUiLang();
|
|
20
23
|
const block = readGardenPayload(dataPayload);
|
|
21
24
|
if (!block) return null;
|
|
22
25
|
if (block.legacy && block.suggestion) return <GardenLegacy suggestion={String(block.suggestion)} />;
|
|
@@ -42,7 +45,13 @@ export function GardenBlock({ dataPayload }: { dataPayload?: string }) {
|
|
|
42
45
|
return <GardenInteractive block={{ id: block.id, props, children: block.children }} />;
|
|
43
46
|
case "file":
|
|
44
47
|
// Post bodies render file cards on the server (MarkdownArticle); this path has no Markdown preview.
|
|
45
|
-
return <GardenFile props={props} />;
|
|
48
|
+
return <GardenFile props={props} lang={lang} />;
|
|
49
|
+
case "relations":
|
|
50
|
+
return <GardenRelations props={props} />;
|
|
51
|
+
case "list":
|
|
52
|
+
return <GardenList props={props} />;
|
|
53
|
+
case "choice":
|
|
54
|
+
return <GardenChoice props={props} />;
|
|
46
55
|
default:
|
|
47
56
|
return <GardenUnknown name={String(block.name)} />;
|
|
48
57
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// Server component: the card for `garden file`. Only the 预览 control is a client island.
|
|
2
|
+
import { siteLang, ui, type UiLang } from "../../lib/i18n";
|
|
2
3
|
import { blockFiles, fileMeta } from "../../../ui/garden/file-block.mjs";
|
|
3
4
|
import { ActionGlyph, FileGlyph, safeFileHref } from "./file-icons";
|
|
4
5
|
import { FilePreview, type PreviewFile } from "./FilePreview";
|
|
@@ -19,8 +20,9 @@ type FileEntry = {
|
|
|
19
20
|
|
|
20
21
|
const PREVIEWS = new Set(["md", "html", "pdf"]);
|
|
21
22
|
|
|
22
|
-
export function GardenFile({ props }: { props: Record<string, unknown
|
|
23
|
-
const
|
|
23
|
+
export function GardenFile({ props, lang }: { props: Record<string, unknown>; lang?: UiLang }) {
|
|
24
|
+
const t = ui(lang);
|
|
25
|
+
const block = blockFiles(props, lang ?? siteLang) as { layout?: string; title?: string; files: FileEntry[] };
|
|
24
26
|
const files = block.files.filter((file) => safeFileHref(file.url));
|
|
25
27
|
if (!files.length) return null;
|
|
26
28
|
const group = block.layout === "group";
|
|
@@ -29,7 +31,7 @@ export function GardenFile({ props }: { props: Record<string, unknown> }) {
|
|
|
29
31
|
{group && block.title ? <div className="gf-title">{block.title}</div> : null}
|
|
30
32
|
{files.map((file, index) => {
|
|
31
33
|
const href = safeFileHref(file.url);
|
|
32
|
-
const name = file.name || file.title ||
|
|
34
|
+
const name = file.name || file.title || t.file;
|
|
33
35
|
const title = file.title || name;
|
|
34
36
|
const meta = fileMeta(file);
|
|
35
37
|
const previewSrc = file.preview === "md" ? safeFileHref(file.previewSrc) : "";
|
|
@@ -44,20 +46,20 @@ export function GardenFile({ props }: { props: Record<string, unknown> }) {
|
|
|
44
46
|
{meta ? <div className="gf-meta">{meta}</div> : null}
|
|
45
47
|
</div>
|
|
46
48
|
<div className="gf-act">
|
|
47
|
-
{previewFile ? <FilePreview file={previewFile} compact={group} /> : null}
|
|
49
|
+
{previewFile ? <FilePreview file={previewFile} compact={group} lang={lang} /> : null}
|
|
48
50
|
{file.remote ? (
|
|
49
|
-
<a className="gf-btn icon" href={href} target="_blank" rel="noopener noreferrer" aria-label={
|
|
51
|
+
<a className="gf-btn icon" href={href} target="_blank" rel="noopener noreferrer" aria-label={t.openNamedInTab(name)} title={t.openInTab}>
|
|
50
52
|
<ActionGlyph name="external" />
|
|
51
53
|
</a>
|
|
52
54
|
) : null}
|
|
53
55
|
{group ? (
|
|
54
|
-
<a className="gf-btn icon" href={href} download={file.remote ? undefined : name} aria-label={
|
|
56
|
+
<a className="gf-btn icon" href={href} download={file.remote ? undefined : name} aria-label={t.downloadNamed(name)} title={t.download}>
|
|
55
57
|
<ActionGlyph name="download" />
|
|
56
58
|
</a>
|
|
57
59
|
) : (
|
|
58
|
-
<a className="gf-btn pri" href={href} download={file.remote ? undefined : name} aria-label={
|
|
60
|
+
<a className="gf-btn pri" href={href} download={file.remote ? undefined : name} aria-label={t.downloadNamed(name)}>
|
|
59
61
|
<ActionGlyph name="download" />
|
|
60
|
-
<span className="gf-lb"
|
|
62
|
+
<span className="gf-lb">{t.download}</span>
|
|
61
63
|
</a>
|
|
62
64
|
)}
|
|
63
65
|
</div>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Children, isValidElement, useEffect, useRef, useState, type ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
const LABELS = {
|
|
6
|
+
zh: { preview: "效果", code: "代码", tabs: "示例视图" },
|
|
7
|
+
en: { preview: "Preview", code: "Code", tabs: "Example view" },
|
|
8
|
+
} as const;
|
|
9
|
+
|
|
10
|
+
// Examples on screen right now; while any is visible on a narrow screen the
|
|
11
|
+
// floating contents button steps aside so it never covers code.
|
|
12
|
+
const visible = new Set<Element>();
|
|
13
|
+
const sync = () => document.documentElement.classList.toggle("ref-demo-in", visible.size > 0);
|
|
14
|
+
|
|
15
|
+
/** Code on the left and the real block on the right; tabs below 1024px. */
|
|
16
|
+
export function RefDemo({ children, lang = "zh", name }: { children?: ReactNode; lang?: "zh" | "en"; name?: string }) {
|
|
17
|
+
const t = LABELS[lang];
|
|
18
|
+
const [tab, setTab] = useState<"v" | "c">("v");
|
|
19
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
20
|
+
const [code, preview] = Children.toArray(children).filter(isValidElement);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const el = ref.current;
|
|
24
|
+
if (!el || typeof IntersectionObserver === "undefined") return;
|
|
25
|
+
const io = new IntersectionObserver((entries) => {
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
if (entry.isIntersecting) visible.add(entry.target);
|
|
28
|
+
else visible.delete(entry.target);
|
|
29
|
+
}
|
|
30
|
+
sync();
|
|
31
|
+
});
|
|
32
|
+
io.observe(el);
|
|
33
|
+
return () => {
|
|
34
|
+
io.disconnect();
|
|
35
|
+
visible.delete(el);
|
|
36
|
+
sync();
|
|
37
|
+
};
|
|
38
|
+
}, []);
|
|
39
|
+
|
|
40
|
+
const tabButton = (id: "v" | "c", label: string) => (
|
|
41
|
+
<button type="button" role="tab" aria-selected={tab === id} data-t={id} onClick={() => setTab(id)}>
|
|
42
|
+
{label}
|
|
43
|
+
</button>
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div className="ref-demo" ref={ref} data-tab={tab} data-name={name}>
|
|
48
|
+
<div className="ref-tabs" role="tablist" aria-label={t.tabs}>
|
|
49
|
+
{tabButton("v", t.preview)}
|
|
50
|
+
{tabButton("c", t.code)}
|
|
51
|
+
</div>
|
|
52
|
+
<div className="ref-pane" data-p="c" data-off={tab !== "c" ? "" : undefined}>
|
|
53
|
+
<p className="ref-pane-h">{t.code}</p>
|
|
54
|
+
{code}
|
|
55
|
+
</div>
|
|
56
|
+
<div className="ref-pane" data-p="v" data-off={tab !== "v" ? "" : undefined}>
|
|
57
|
+
<p className="ref-pane-h">{t.preview}</p>
|
|
58
|
+
{preview}
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -2,12 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect, useId, useRef, useState, type ReactNode } from "react";
|
|
4
4
|
import ReactMarkdown from "react-markdown";
|
|
5
|
-
import {
|
|
5
|
+
import { useUi, useUiLang } from "../UiLang";
|
|
6
|
+
import { langOf, ui } from "../../lib/i18n";
|
|
7
|
+
import { interactiveUiSource } from "../../../ui/core/garden-fence.mjs";
|
|
6
8
|
import { markdownPlugins } from "../../lib/markdown-plugins";
|
|
7
9
|
import { compileUiBlock } from "../../../ui/core/compile.mjs";
|
|
8
10
|
import { splitCompareLine } from "../../../ui/garden/compare-line.mjs";
|
|
9
|
-
import { UiErrorCard, UiFigure, type Diagnostic, type UiPayload } from "../../../ui/react";
|
|
11
|
+
import { RuntimeLangProvider, UiErrorCard, UiFigure, type Diagnostic, type UiPayload } from "../../../ui/react";
|
|
10
12
|
import { FullscreenFrame, SandboxFrame } from "./chrome";
|
|
13
|
+
import { SourceMark, VerdictIcon } from "./patterns";
|
|
14
|
+
|
|
15
|
+
const BASE_PATH = (process.env.NEXT_PUBLIC_BASE_PATH ?? "").replace(/\/+$/u, "");
|
|
16
|
+
|
|
17
|
+
/** A `/public` path (`/images/a.svg`) gets the site's basePath in front. */
|
|
18
|
+
function publicSrc(src: string) {
|
|
19
|
+
if (!BASE_PATH || !src.startsWith("/") || src.startsWith("//") || src.startsWith(`${BASE_PATH}/`)) return src;
|
|
20
|
+
return `${BASE_PATH}${src}`;
|
|
21
|
+
}
|
|
11
22
|
|
|
12
23
|
function Rich({ text }: { text: string }) {
|
|
13
24
|
return (
|
|
@@ -31,15 +42,16 @@ function SourceChip({ source }: { source: unknown }) {
|
|
|
31
42
|
if (item && typeof item === "object" && "url" in item) {
|
|
32
43
|
const url = String((item as { url?: unknown }).url ?? "");
|
|
33
44
|
const site = String((item as { site?: unknown }).site ?? hostOf(url));
|
|
34
|
-
|
|
45
|
+
const icon = (item as { icon?: unknown }).icon;
|
|
46
|
+
return { url, site, icon: typeof icon === "string" ? icon : undefined };
|
|
35
47
|
}
|
|
36
48
|
return null;
|
|
37
|
-
}).filter((item): item is { url: string; site: string } => Boolean(item?.url));
|
|
49
|
+
}).filter((item): item is { url: string; site: string; icon?: string } => Boolean(item?.url));
|
|
38
50
|
if (!list.length) return null;
|
|
39
51
|
return (
|
|
40
|
-
<span className="g-srcs">
|
|
52
|
+
<span className={list.length > 1 ? "g-srcs is-multi" : "g-srcs"}>
|
|
41
53
|
<a className="g-src ext" href={list[0].url} target="_blank" rel="noopener noreferrer">
|
|
42
|
-
<
|
|
54
|
+
<SourceMark href={list[0].url} icon={list[0].icon} />
|
|
43
55
|
<span>{list[0].site}</span>
|
|
44
56
|
</a>
|
|
45
57
|
{list.length > 1 ? <span className="g-src-more">+{list.length - 1}</span> : null}
|
|
@@ -52,22 +64,36 @@ function itemsOf(props: Record<string, unknown>) {
|
|
|
52
64
|
}
|
|
53
65
|
|
|
54
66
|
export function GardenCard({ props }: { props: Record<string, unknown> }) {
|
|
67
|
+
if (Array.isArray(props.cards)) {
|
|
68
|
+
return (
|
|
69
|
+
<div className="g-entities">
|
|
70
|
+
{props.cards.map((card, index) => (
|
|
71
|
+
<GardenCardItem key={index} props={(card ?? {}) as Record<string, unknown>} row />
|
|
72
|
+
))}
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return <GardenCardItem props={props} />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function GardenCardItem({ props, row = false }: { props: Record<string, unknown>; row?: boolean }) {
|
|
55
80
|
const url = typeof props.url === "string" ? props.url : "";
|
|
56
81
|
const title = String(props.title ?? url);
|
|
57
82
|
const description = typeof props.description === "string" ? props.description : typeof props.text === "string" ? props.text : "";
|
|
58
83
|
const points = Array.isArray(props.points) ? props.points : Array.isArray(props.items) ? props.items : [];
|
|
59
84
|
return (
|
|
60
|
-
<article className="g-entity">
|
|
85
|
+
<article className={`g-entity${row ? " g-entity-row" : ""}`}>
|
|
61
86
|
{typeof props.image === "string" ? (
|
|
62
87
|
<div className="g-entity-img">
|
|
63
88
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
64
|
-
<img src={props.image} alt={String(props.alt ?? title)} />
|
|
89
|
+
<img src={publicSrc(props.image)} alt={String(props.alt ?? title)} />
|
|
65
90
|
</div>
|
|
66
91
|
) : null}
|
|
67
92
|
<div className="g-entity-b">
|
|
68
93
|
<h4 className="g-entity-t">{url ? <a className="ext" href={url} target="_blank" rel="noopener noreferrer"><Rich text={title} /></a> : <Rich text={title} />}</h4>
|
|
69
94
|
{typeof props.meta === "string" ? <p className="g-entity-m"><Rich text={props.meta} /></p> : null}
|
|
70
95
|
{description ? <p><Rich text={description} /> <SourceChip source={props.source} /></p> : null}
|
|
96
|
+
{typeof props.takeaway === "string" ? <p className="g-entity-k"><Rich text={props.takeaway} /></p> : null}
|
|
71
97
|
</div>
|
|
72
98
|
{points.length ? (
|
|
73
99
|
<ol className="g-entity-list">
|
|
@@ -117,8 +143,31 @@ function CompareLine({ line }: { line: string }) {
|
|
|
117
143
|
);
|
|
118
144
|
}
|
|
119
145
|
|
|
146
|
+
function verdictOf(entry: unknown) {
|
|
147
|
+
if (!entry || typeof entry !== "object") return "";
|
|
148
|
+
const value = String((entry as Record<string, unknown>).verdict ?? "").trim().toLowerCase();
|
|
149
|
+
return value === "good" || value === "bad" ? value : "";
|
|
150
|
+
}
|
|
151
|
+
|
|
120
152
|
export function GardenCompare({ props }: { props: Record<string, unknown> }) {
|
|
121
153
|
const items = Array.isArray(props.items) ? props.items as unknown[] : [];
|
|
154
|
+
if (items.some((entry) => verdictOf(entry))) {
|
|
155
|
+
return (
|
|
156
|
+
<div className="g-verdicts" role="group">
|
|
157
|
+
{items.map((entry, index) => {
|
|
158
|
+
const item = (entry && typeof entry === "object" ? entry : { text: String(entry ?? "") }) as Record<string, unknown>;
|
|
159
|
+
const verdict = verdictOf(item);
|
|
160
|
+
const texts = item.text == null ? [] : Array.isArray(item.text) ? item.text : [item.text];
|
|
161
|
+
return (
|
|
162
|
+
<section className={`g-verdict${verdict ? ` is-${verdict}` : ""}`} key={index}>
|
|
163
|
+
<h4><VerdictIcon verdict={verdict} /><Rich text={String(item.title ?? "")} /></h4>
|
|
164
|
+
{texts.map((paragraph, paragraphIndex) => <p key={paragraphIndex}><Rich text={String(paragraph)} /></p>)}
|
|
165
|
+
</section>
|
|
166
|
+
);
|
|
167
|
+
})}
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
122
171
|
return (
|
|
123
172
|
<div className="g-compare" role="group">
|
|
124
173
|
{items.map((entry, index) => {
|
|
@@ -126,7 +175,8 @@ export function GardenCompare({ props }: { props: Record<string, unknown> }) {
|
|
|
126
175
|
return <section className="g-cmp" key={index}><CompareLine line={String(entry)} /></section>;
|
|
127
176
|
}
|
|
128
177
|
if (!entry || typeof entry !== "object") return null;
|
|
129
|
-
const { title, text, ...rest } = entry as Record<string, unknown>;
|
|
178
|
+
const { title, text, verdict: _verdict, ...rest } = entry as Record<string, unknown>;
|
|
179
|
+
void _verdict;
|
|
130
180
|
const texts = text == null ? [] : Array.isArray(text) ? text : [text];
|
|
131
181
|
return (
|
|
132
182
|
<section className="g-cmp" key={index}>
|
|
@@ -187,7 +237,8 @@ function chartRows(props: Record<string, unknown>) {
|
|
|
187
237
|
export function GardenChart({ props }: { props: Record<string, unknown> }) {
|
|
188
238
|
const rows = chartRows(props);
|
|
189
239
|
const max = Math.max(...rows.map((row) => row.y), 1);
|
|
190
|
-
const
|
|
240
|
+
const t = useUi();
|
|
241
|
+
const title = String(props.title ?? t.chart);
|
|
191
242
|
return (
|
|
192
243
|
<figure className="g-chart" role="group" aria-label={title}>
|
|
193
244
|
{props.title ? <p className="g-chart-t">{title}</p> : null}
|
|
@@ -201,9 +252,9 @@ export function GardenChart({ props }: { props: Record<string, unknown> }) {
|
|
|
201
252
|
))}
|
|
202
253
|
</div>
|
|
203
254
|
<details className="g-chart-data">
|
|
204
|
-
<summary
|
|
255
|
+
<summary>{t.dataTable}</summary>
|
|
205
256
|
<table>
|
|
206
|
-
<thead><tr><th>{String(props.xLabel ?? (typeof props.x === "string" ? props.x :
|
|
257
|
+
<thead><tr><th>{String(props.xLabel ?? (typeof props.x === "string" ? props.x : t.item))}</th><th>{String(props.yLabel ?? t.value)}</th></tr></thead>
|
|
207
258
|
<tbody>
|
|
208
259
|
{rows.map((row) => <tr key={row.x}><td>{row.x}</td><td>{row.label}</td></tr>)}
|
|
209
260
|
</tbody>
|
|
@@ -216,11 +267,11 @@ export function GardenChart({ props }: { props: Record<string, unknown> }) {
|
|
|
216
267
|
|
|
217
268
|
type Part = { id: string; label: string; text: string };
|
|
218
269
|
|
|
219
|
-
function partsOf(props: Record<string, unknown
|
|
270
|
+
function partsOf(props: Record<string, unknown>, partLabel: (n: number) => string): Part[] {
|
|
220
271
|
if (!Array.isArray(props.parts)) return [];
|
|
221
272
|
return props.parts.map((part, index) => {
|
|
222
273
|
const item = part as Record<string, unknown>;
|
|
223
|
-
const label = String(item.label ?? item.name ??
|
|
274
|
+
const label = String(item.label ?? item.name ?? partLabel(index + 1));
|
|
224
275
|
return { id: String(item.id ?? item.name ?? label), label, text: String(item.text ?? "") };
|
|
225
276
|
});
|
|
226
277
|
}
|
|
@@ -240,7 +291,8 @@ function partOn(selected: string, token: string) {
|
|
|
240
291
|
}
|
|
241
292
|
|
|
242
293
|
export function GardenFigure({ props, children }: { props: Record<string, unknown>; children?: string }) {
|
|
243
|
-
const
|
|
294
|
+
const t = useUi();
|
|
295
|
+
const parts = partsOf(props, t.part);
|
|
244
296
|
const [selected, setSelected] = useState(parts[0]?.id ?? "all");
|
|
245
297
|
const svgRef = useRef<HTMLDivElement>(null);
|
|
246
298
|
const base = useId();
|
|
@@ -286,7 +338,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
|
|
|
286
338
|
const body = (
|
|
287
339
|
<figure className="g-explore" data-g="explore" data-sel={selected} ref={svgRef}>
|
|
288
340
|
{parts.length ? (
|
|
289
|
-
<div role="tablist" aria-label=
|
|
341
|
+
<div role="tablist" aria-label={t.pickPart}>
|
|
290
342
|
{parts.map((part, index) => (
|
|
291
343
|
<button
|
|
292
344
|
key={part.id}
|
|
@@ -312,7 +364,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
|
|
|
312
364
|
{typeof props.image === "string" ? (
|
|
313
365
|
<div className="fig-card">
|
|
314
366
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
315
|
-
<img src={props.image} alt={String(props.alt ?? props.caption ?? "")} />
|
|
367
|
+
<img src={publicSrc(props.image)} alt={String(props.alt ?? props.caption ?? "")} />
|
|
316
368
|
</div>
|
|
317
369
|
) : null}
|
|
318
370
|
{typeof props.mermaid === "string" ? <pre className="code"><code>{props.mermaid}</code></pre> : null}
|
|
@@ -324,7 +376,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
|
|
|
324
376
|
{typeof props.caption === "string" ? <figcaption><Rich text={props.caption} /></figcaption> : null}
|
|
325
377
|
</figure>
|
|
326
378
|
);
|
|
327
|
-
return parts.length || svg ? <FullscreenFrame label=
|
|
379
|
+
return parts.length || svg ? <FullscreenFrame label={t.diagram}>{body}</FullscreenFrame> : body;
|
|
328
380
|
}
|
|
329
381
|
|
|
330
382
|
function htmlHasScript(source: string) {
|
|
@@ -336,6 +388,8 @@ function isLightWidget(source: string) {
|
|
|
336
388
|
}
|
|
337
389
|
|
|
338
390
|
function bindGardenWidgets(root: HTMLElement) {
|
|
391
|
+
const attr = root.closest("[lang]")?.getAttribute("lang");
|
|
392
|
+
const t = ui(attr ? langOf(attr) : undefined);
|
|
339
393
|
root.querySelectorAll<HTMLElement>("[data-g=tabs]").forEach((widget) => {
|
|
340
394
|
const list = widget.querySelector<HTMLElement>(":scope > [role=tablist]");
|
|
341
395
|
const tabs = [...(list?.querySelectorAll<HTMLElement>(":scope > [role=tab]") ?? [])];
|
|
@@ -373,12 +427,12 @@ function bindGardenWidgets(root: HTMLElement) {
|
|
|
373
427
|
const chosen = form.querySelector<HTMLInputElement>("input:checked");
|
|
374
428
|
if (!chosen) {
|
|
375
429
|
out.className = "g-quiz-out";
|
|
376
|
-
out.textContent =
|
|
430
|
+
out.textContent = t.pickAnswer;
|
|
377
431
|
return;
|
|
378
432
|
}
|
|
379
433
|
const correct = chosen.value === form.dataset.answer;
|
|
380
434
|
out.className = `g-quiz-out ${correct ? "ok" : "no"}`;
|
|
381
|
-
out.textContent = correct ?
|
|
435
|
+
out.textContent = correct ? t.correct : t.wrong;
|
|
382
436
|
if (explain) explain.hidden = !correct;
|
|
383
437
|
});
|
|
384
438
|
});
|
|
@@ -446,7 +500,7 @@ function bindGardenWidgets(root: HTMLElement) {
|
|
|
446
500
|
const equation = calc.querySelector("[data-calc]");
|
|
447
501
|
if (equation && spec.show) {
|
|
448
502
|
const shown = spec.show.replace(/\{(\w+)\}/g, (_, key: string) => String(values[key]));
|
|
449
|
-
equation.textContent = `${shown} = ${spec.format === "GB" ? `${Math.round(result).toLocaleString("en-US")}
|
|
503
|
+
equation.textContent = `${shown} = ${spec.format === "GB" ? `${Math.round(result).toLocaleString("en-US")} ${t.bytes}` : String(Math.round(result * 1e4) / 1e4)}`;
|
|
450
504
|
}
|
|
451
505
|
};
|
|
452
506
|
calc.addEventListener("input", run);
|
|
@@ -484,9 +538,10 @@ function LightGardenHtml({ code }: { code: string }) {
|
|
|
484
538
|
|
|
485
539
|
export function GardenHtml({ props, children }: { props: Record<string, unknown>; children?: string }) {
|
|
486
540
|
const code = typeof props.code === "string" ? props.code : children || "";
|
|
541
|
+
const t = useUi();
|
|
487
542
|
const title = String(props.title ?? "HTML");
|
|
488
543
|
if (!code && typeof props.src === "string") {
|
|
489
|
-
return <p className="g-embed-note">
|
|
544
|
+
return <p className="g-embed-note">{t.htmlAt(String(props.src))}</p>;
|
|
490
545
|
}
|
|
491
546
|
if (htmlHasScript(code)) {
|
|
492
547
|
return <SandboxFrame html={code} title={title} />;
|
|
@@ -499,36 +554,38 @@ export function GardenHtml({ props, children }: { props: Record<string, unknown>
|
|
|
499
554
|
}
|
|
500
555
|
|
|
501
556
|
export function GardenInteractive({ block }: { block: { id?: string | null; props: Record<string, unknown>; children?: string } }) {
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
...(block.props.expect ? { expect: block.props.expect } : {}),
|
|
507
|
-
};
|
|
508
|
-
const source = `---\n${stringify(header)}---\n${block.children ?? ""}`;
|
|
557
|
+
const t = useUi();
|
|
558
|
+
const lang = useUiLang();
|
|
559
|
+
// Same source `garden ui check` validates (ui/core/garden-fence.mjs).
|
|
560
|
+
const { source, header } = interactiveUiSource({ id: block.id ?? null, props: block.props, children: block.children ?? "" });
|
|
509
561
|
const { payload, check } = compileUiBlock(source);
|
|
510
562
|
if (!payload) {
|
|
511
563
|
return (
|
|
564
|
+
<RuntimeLangProvider lang={lang}>
|
|
512
565
|
<UiErrorCard
|
|
513
566
|
id={String(header.id)}
|
|
514
567
|
caption={String(header.caption)}
|
|
515
568
|
diagnostics={(check.diagnostics ?? []) as Diagnostic[]}
|
|
516
569
|
/>
|
|
570
|
+
</RuntimeLangProvider>
|
|
517
571
|
);
|
|
518
572
|
}
|
|
519
573
|
return (
|
|
520
|
-
<
|
|
574
|
+
<RuntimeLangProvider lang={lang}>
|
|
575
|
+
<FullscreenFrame label={String(header.caption || t.interactive)}>
|
|
521
576
|
<div className="g-play">
|
|
522
577
|
<UiFigure payload={payload as UiPayload} />
|
|
523
578
|
</div>
|
|
524
579
|
</FullscreenFrame>
|
|
580
|
+
</RuntimeLangProvider>
|
|
525
581
|
);
|
|
526
582
|
}
|
|
527
583
|
|
|
528
584
|
export function GardenIdea({ text }: { text: string }) {
|
|
585
|
+
const t = useUi();
|
|
529
586
|
return (
|
|
530
587
|
<aside className="g-idea">
|
|
531
|
-
<p className="g-example-label"
|
|
588
|
+
<p className="g-example-label">{t.ideaLabel}</p>
|
|
532
589
|
<p>{text}</p>
|
|
533
590
|
</aside>
|
|
534
591
|
);
|
|
@@ -539,7 +596,8 @@ export function GardenLegacy({ suggestion }: { suggestion: string }) {
|
|
|
539
596
|
}
|
|
540
597
|
|
|
541
598
|
export function GardenUnknown({ name }: { name: string }) {
|
|
542
|
-
|
|
599
|
+
const t = useUi();
|
|
600
|
+
return <p className="g-embed-note">{t.unknownBlock(name)}</p>;
|
|
543
601
|
}
|
|
544
602
|
|
|
545
603
|
export function GardenMessage({ children }: { children: ReactNode }) {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import { useUi } from "../UiLang";
|
|
4
|
+
|
|
3
5
|
import {
|
|
4
6
|
useEffect,
|
|
5
7
|
useId,
|
|
@@ -47,7 +49,9 @@ function writeClipboard(text: string) {
|
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
export function CopyButton({ getText, label
|
|
52
|
+
export function CopyButton({ getText, label }: { getText: () => string; label?: string }) {
|
|
53
|
+
const t = useUi();
|
|
54
|
+
label = label ?? t.copyCode;
|
|
51
55
|
const [done, setDone] = useState(false);
|
|
52
56
|
const timer = useRef<number>(0);
|
|
53
57
|
|
|
@@ -56,8 +60,8 @@ export function CopyButton({ getText, label = "复制代码" }: { getText: () =>
|
|
|
56
60
|
<button
|
|
57
61
|
type="button"
|
|
58
62
|
className={`ux-btn ux-copy${done ? " ux-done" : ""}`}
|
|
59
|
-
aria-label={done ?
|
|
60
|
-
title={done ?
|
|
63
|
+
aria-label={done ? t.copied : label}
|
|
64
|
+
title={done ? t.copied : label}
|
|
61
65
|
onClick={() => {
|
|
62
66
|
const text = getText();
|
|
63
67
|
writeClipboard(text).then(() => {
|
|
@@ -67,9 +71,9 @@ export function CopyButton({ getText, label = "复制代码" }: { getText: () =>
|
|
|
67
71
|
}).catch(() => setDone(false));
|
|
68
72
|
}}
|
|
69
73
|
>
|
|
70
|
-
{done ?
|
|
74
|
+
{done ? t.copied : COPY_ICON}
|
|
71
75
|
</button>
|
|
72
|
-
<span className="ux-live" aria-live="polite">{done ?
|
|
76
|
+
<span className="ux-live" aria-live="polite">{done ? t.copiedLive : ""}</span>
|
|
73
77
|
</>
|
|
74
78
|
);
|
|
75
79
|
}
|
|
@@ -88,12 +92,12 @@ export function CodeCopy({ children }: { children: ReactNode }) {
|
|
|
88
92
|
);
|
|
89
93
|
}
|
|
90
94
|
|
|
91
|
-
function diffLines(source: string) {
|
|
95
|
+
function diffLines(source: string, t: ReturnType<typeof useUi>) {
|
|
92
96
|
return source.replace(/\n$/u, "").split("\n").map((line) => {
|
|
93
97
|
const kind = line.startsWith("+") ? "add" : line.startsWith("-") ? "del" : "ctx";
|
|
94
98
|
const body = kind === "ctx" ? line.replace(/^ /u, "") : line.slice(1);
|
|
95
99
|
const mark = kind === "add" ? "+" : kind === "del" ? "−" : " ";
|
|
96
|
-
const spoken = kind === "add" ?
|
|
100
|
+
const spoken = kind === "add" ? t.diffAdded : kind === "del" ? t.diffRemoved : "";
|
|
97
101
|
return { kind, body, mark, spoken };
|
|
98
102
|
});
|
|
99
103
|
}
|
|
@@ -109,11 +113,12 @@ export function CodeDiff({
|
|
|
109
113
|
}) {
|
|
110
114
|
const [tab, setTab] = useState(0);
|
|
111
115
|
const base = useId();
|
|
112
|
-
const
|
|
116
|
+
const t = useUi();
|
|
117
|
+
const tabs = t.codeTabs;
|
|
113
118
|
|
|
114
119
|
return (
|
|
115
120
|
<div className="g-tabs g-code-group g-codediff" data-g="tabs">
|
|
116
|
-
<div role="tablist" aria-label=
|
|
121
|
+
<div role="tablist" aria-label={t.codeTabsLabel}>
|
|
117
122
|
{tabs.map((label, index) => (
|
|
118
123
|
<button
|
|
119
124
|
key={label}
|
|
@@ -146,7 +151,7 @@ export function CodeDiff({
|
|
|
146
151
|
<div role="tabpanel" id={`${base}-p1`} aria-labelledby={`${base}-t1`} hidden={tab !== 1}>
|
|
147
152
|
<pre className="code g-diff">
|
|
148
153
|
<code>
|
|
149
|
-
{diffLines(diff).map((line, index) => (
|
|
154
|
+
{diffLines(diff, t).map((line, index) => (
|
|
150
155
|
<span className={`d-${line.kind}`} key={index}>
|
|
151
156
|
<span className="d-m" aria-hidden="true">{line.mark}</span>
|
|
152
157
|
{line.body}
|
|
@@ -164,7 +169,9 @@ function fullscreenElement() {
|
|
|
164
169
|
return document.fullscreenElement || (document as Document & { webkitFullscreenElement?: Element }).webkitFullscreenElement || null;
|
|
165
170
|
}
|
|
166
171
|
|
|
167
|
-
export function FullscreenFrame({ children, label
|
|
172
|
+
export function FullscreenFrame({ children, label }: { children: ReactNode; label?: string }) {
|
|
173
|
+
const t = useUi();
|
|
174
|
+
label = label ?? t.demo;
|
|
168
175
|
const ref = useRef<HTMLDivElement>(null);
|
|
169
176
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
170
177
|
const restoreRef = useRef<HTMLElement | null>(null);
|
|
@@ -255,8 +262,8 @@ export function FullscreenFrame({ children, label = "演示" }: { children: Reac
|
|
|
255
262
|
ref={buttonRef}
|
|
256
263
|
type="button"
|
|
257
264
|
className="ux-btn ux-fsb"
|
|
258
|
-
aria-label={active ?
|
|
259
|
-
title={active ?
|
|
265
|
+
aria-label={active ? t.exitFullscreen : t.fullscreen}
|
|
266
|
+
title={active ? t.exitFullscreen : t.fullscreen}
|
|
260
267
|
onClick={() => (active ? close() : open())}
|
|
261
268
|
>
|
|
262
269
|
{active ? EXIT_ICON : ENTER_ICON}
|