Skip to content

Commit 8a64757

Browse files
committed
1章の記述修正、ファイルエディターの改善など
1 parent 5517ec6 commit 8a64757

File tree

9 files changed

+211
-105
lines changed

9 files changed

+211
-105
lines changed

app/[docs_id]/markdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const components: Components = {
8686
switch (match[1]) {
8787
case "python":
8888
return (
89-
<div className="bg-base-300 border border-primary m-2 p-4 rounded-lg">
89+
<div className="bg-base-300 border border-primary m-2 p-4 pr-1 rounded-lg">
9090
<PythonEmbeddedTerminal
9191
content={String(props.children).replace(/\n$/, "")}
9292
/>

app/[docs_id]/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readFile } from "node:fs/promises";
44
import { join } from "node:path";
55
import { MarkdownSection, splitMarkdown } from "./splitMarkdown";
66
import { Section } from "./section";
7+
import * as pyodideLock from "pyodide/pyodide-lock.json";
78

89
export default async function Page({
910
params,
@@ -30,6 +31,11 @@ export default async function Page({
3031
notFound();
3132
}
3233

34+
mdContent = mdContent.replaceAll(
35+
"{process.env.PYODIDE_PYTHON_VERSION}",
36+
String(pyodideLock.info.python)
37+
);
38+
3339
const splitMdContent: MarkdownSection[] = await splitMarkdown(mdContent);
3440

3541
return (

app/terminal/editor.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
.embedded-editor > .ace_editor {
99
border-bottom-left-radius: 0.5rem;
1010
border-bottom-right-radius: 0.5rem;
11-
border-top-right-radius: 0.5rem;
1211
}

app/terminal/editor.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import "ace-builds/src-min-noconflict/ext-searchbox";
1111
import "ace-builds/src-min-noconflict/mode-python";
1212
import { useEffect } from "react";
1313
import { useSectionCode } from "../[docs_id]/section";
14+
import clsx from "clsx";
1415
// snippetを有効化するにはsnippetもimportする必要がある: import "ace-builds/src-min-noconflict/snippets/python";
1516

1617
interface EditorProps {
@@ -33,7 +34,41 @@ export function EditorComponent(props: EditorProps) {
3334

3435
return (
3536
<div className="embedded-editor">
36-
<div className="font-mono text-sm mt-2 mb-1 ml-4 ">{props.filename}</div>
37+
<div className="flex flex-row items-center">
38+
<div className="font-mono text-sm mt-2 mb-1 ml-4 mr-2">
39+
{props.filename}
40+
</div>
41+
<button
42+
className={clsx(
43+
"btn btn-xs btn-soft btn-warning mt-1 mb-1",
44+
// btn-warning は文字色を変えるがsvgの色は変えてくれないので、 stroke-warning を追加指定している
45+
"stroke-warning hover:stroke-warning-content active:stroke-warning-content",
46+
// codeの内容が変更された場合のみ表示する
47+
code == props.initContent && "invisible"
48+
)}
49+
onClick={() => writeFile(props.filename, props.initContent)}
50+
>
51+
{/*<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->*/}
52+
<svg
53+
className="w-3 h-3"
54+
viewBox="0 0 24 24"
55+
fill="none"
56+
xmlns="http://www.w3.org/2000/svg"
57+
>
58+
<g id="Edit / Undo">
59+
<path
60+
id="Vector"
61+
d="M10 8H5V3M5.29102 16.3569C6.22284 17.7918 7.59014 18.8902 9.19218 19.4907C10.7942 20.0913 12.547 20.1624 14.1925 19.6937C15.8379 19.225 17.2893 18.2413 18.3344 16.8867C19.3795 15.5321 19.963 13.878 19.9989 12.1675C20.0347 10.4569 19.5211 8.78001 18.5337 7.38281C17.5462 5.98561 16.1366 4.942 14.5122 4.40479C12.8878 3.86757 11.1341 3.86499 9.5083 4.39795C7.88252 4.93091 6.47059 5.97095 5.47949 7.36556"
62+
// stroke="#000000"
63+
strokeWidth="2"
64+
strokeLinecap="round"
65+
strokeLinejoin="round"
66+
/>
67+
</g>
68+
</svg>
69+
元の内容に戻す
70+
</button>
71+
</div>
3772
<AceEditor
3873
name={`ace-editor-${props.filename}`}
3974
mode={props.language}

app/terminal/file.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { usePathname } from "next/navigation";
34
import {
45
createContext,
56
ReactNode,
@@ -16,6 +17,8 @@ import {
1617
files["ファイル名"]
1718
でどこからでも同じファイルの中身を取得でき、
1819
writeFile() で書き込むこともできる。
20+
21+
ファイルはページのURLごとに独立して管理している。
1922
*/
2023
interface IFileContext {
2124
files: Record<string, string | undefined>;
@@ -26,16 +29,25 @@ const FileContext = createContext<IFileContext>(null!);
2629
export const useFile = () => useContext(FileContext);
2730

2831
export function FileProvider({ children }: { children: ReactNode }) {
29-
const [files, setFiles] = useState<Record<string, string>>({});
30-
const writeFile = useCallback((name: string, content: string) => {
31-
setFiles((files) => {
32-
files[name] = content;
33-
return { ...files };
34-
});
35-
}, []);
32+
const [files, setFiles] = useState<Record<string, Record<string, string>>>(
33+
{}
34+
);
35+
const pathname = usePathname();
36+
if (!(pathname in files)) {
37+
files[pathname] = {};
38+
}
39+
const writeFile = useCallback(
40+
(name: string, content: string) => {
41+
setFiles((files) => {
42+
files[pathname][name] = content;
43+
return { ...files };
44+
});
45+
},
46+
[pathname]
47+
);
3648

3749
return (
38-
<FileContext.Provider value={{ files, writeFile }}>
50+
<FileContext.Provider value={{ files: files[pathname], writeFile }}>
3951
{children}
4052
</FileContext.Provider>
4153
);

app/terminal/repl.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function ReplTerminal(props: ReplComponentProps) {
8080
onOutput(cmd.output);
8181
}
8282
}
83+
terminalInstanceRef.current!.scrollToTop();
8384
},
8485
});
8586

@@ -180,6 +181,8 @@ export function ReplTerminal(props: ReplComponentProps) {
180181
}
181182
}
182183
updateBuffer(() => [""]);
184+
// なぜかそのままscrollToTop()を呼ぶとスクロールせず、setTimeoutを入れるとscrollする(治安bad)
185+
setTimeout(() => terminalInstanceRef.current!.scrollToTop());
183186
})();
184187
}
185188
}, [

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"ace-builds": "^1.43.2",
2222
"async-mutex": "^0.5.0",
2323
"chalk": "^5.5.0",
24+
"clsx": "^2.1.1",
2425
"next": "<15.4",
2526
"prismjs": "^1.30.0",
2627
"pyodide": "^0.28.1",

0 commit comments

Comments
 (0)