@handylib/kai-code 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +229 -2
- package/dist/index.cjs +1300 -0
- package/dist/index.css +599 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +1268 -0
- package/package.json +60 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1268 @@
|
|
|
1
|
+
// src/KaiCode.tsx
|
|
2
|
+
import Editor from "@monaco-editor/react";
|
|
3
|
+
import {
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useId,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState
|
|
10
|
+
} from "react";
|
|
11
|
+
|
|
12
|
+
// src/fileSystem.ts
|
|
13
|
+
function createMemoryFileSystem(initialFiles = {}) {
|
|
14
|
+
const files = new Map(
|
|
15
|
+
Object.entries(initialFiles).map(([path, contents]) => [
|
|
16
|
+
normalizePath(path),
|
|
17
|
+
contents
|
|
18
|
+
])
|
|
19
|
+
);
|
|
20
|
+
const directories = /* @__PURE__ */ new Set();
|
|
21
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
22
|
+
const addParentDirectories = (path) => {
|
|
23
|
+
const parts = path.split("/");
|
|
24
|
+
parts.pop();
|
|
25
|
+
let parent = "";
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
parent = parent ? `${parent}/${part}` : part;
|
|
28
|
+
if (files.has(parent)) {
|
|
29
|
+
throw new Error(`A file blocks this directory path: ${parent}`);
|
|
30
|
+
}
|
|
31
|
+
directories.add(parent);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
for (const path of files.keys()) addParentDirectories(path);
|
|
35
|
+
const notify = () => {
|
|
36
|
+
listeners.forEach((listener) => listener());
|
|
37
|
+
};
|
|
38
|
+
const moveFile = async (oldPath, newPath) => {
|
|
39
|
+
const oldNormalized = normalizePath(oldPath);
|
|
40
|
+
const newNormalized = normalizePath(newPath);
|
|
41
|
+
const contents = files.get(oldNormalized);
|
|
42
|
+
if (contents === void 0) {
|
|
43
|
+
throw new Error(`File not found: ${oldNormalized}`);
|
|
44
|
+
}
|
|
45
|
+
if (!newNormalized) throw new Error("Destination file path must not be empty.");
|
|
46
|
+
if (oldNormalized === newNormalized) return;
|
|
47
|
+
if (files.has(newNormalized) || directories.has(newNormalized)) {
|
|
48
|
+
throw new Error(`Path already exists: ${newNormalized}`);
|
|
49
|
+
}
|
|
50
|
+
addParentDirectories(newNormalized);
|
|
51
|
+
files.delete(oldNormalized);
|
|
52
|
+
files.set(newNormalized, contents);
|
|
53
|
+
notify();
|
|
54
|
+
};
|
|
55
|
+
const moveDirectory = async (oldPath, newPath) => {
|
|
56
|
+
const oldNormalized = normalizePath(oldPath);
|
|
57
|
+
const newNormalized = normalizePath(newPath);
|
|
58
|
+
if (!directories.has(oldNormalized)) {
|
|
59
|
+
throw new Error(`Directory not found: ${oldNormalized}`);
|
|
60
|
+
}
|
|
61
|
+
if (!newNormalized) {
|
|
62
|
+
throw new Error("Destination directory path must not be empty.");
|
|
63
|
+
}
|
|
64
|
+
if (oldNormalized === newNormalized) return;
|
|
65
|
+
if (newNormalized.startsWith(`${oldNormalized}/`)) {
|
|
66
|
+
throw new Error("A directory cannot be moved inside itself.");
|
|
67
|
+
}
|
|
68
|
+
if (files.has(newNormalized) || directories.has(newNormalized)) {
|
|
69
|
+
throw new Error(`Path already exists: ${newNormalized}`);
|
|
70
|
+
}
|
|
71
|
+
addParentDirectories(newNormalized);
|
|
72
|
+
const oldPrefix = `${oldNormalized}/`;
|
|
73
|
+
const directoriesToMove = [...directories].filter(
|
|
74
|
+
(path) => path === oldNormalized || path.startsWith(oldPrefix)
|
|
75
|
+
);
|
|
76
|
+
const filesToMove = [...files.entries()].filter(
|
|
77
|
+
([path]) => path.startsWith(oldPrefix)
|
|
78
|
+
);
|
|
79
|
+
for (const [path] of filesToMove) {
|
|
80
|
+
const destination = `${newNormalized}${path.slice(oldNormalized.length)}`;
|
|
81
|
+
if (files.has(destination) || directories.has(destination)) {
|
|
82
|
+
throw new Error(`Path already exists: ${destination}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
directoriesToMove.forEach((path) => directories.delete(path));
|
|
86
|
+
filesToMove.forEach(([path]) => files.delete(path));
|
|
87
|
+
directoriesToMove.forEach(
|
|
88
|
+
(path) => directories.add(`${newNormalized}${path.slice(oldNormalized.length)}`)
|
|
89
|
+
);
|
|
90
|
+
filesToMove.forEach(
|
|
91
|
+
([path, contents]) => files.set(`${newNormalized}${path.slice(oldNormalized.length)}`, contents)
|
|
92
|
+
);
|
|
93
|
+
notify();
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
async readDirectory(directory = "") {
|
|
97
|
+
const base = normalizePath(directory);
|
|
98
|
+
const prefix = base ? `${base}/` : "";
|
|
99
|
+
const entries = /* @__PURE__ */ new Map();
|
|
100
|
+
for (const path of directories) {
|
|
101
|
+
if (!path.startsWith(prefix) || path === base) continue;
|
|
102
|
+
const remainder = path.slice(prefix.length);
|
|
103
|
+
if (remainder.includes("/")) continue;
|
|
104
|
+
entries.set(path, {
|
|
105
|
+
type: "directory",
|
|
106
|
+
name: remainder,
|
|
107
|
+
path
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
for (const path of files.keys()) {
|
|
111
|
+
if (!path.startsWith(prefix)) continue;
|
|
112
|
+
const remainder = path.slice(prefix.length);
|
|
113
|
+
const slash = remainder.indexOf("/");
|
|
114
|
+
if (slash === -1) {
|
|
115
|
+
entries.set(path, {
|
|
116
|
+
type: "file",
|
|
117
|
+
name: remainder,
|
|
118
|
+
path
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
const name = remainder.slice(0, slash);
|
|
122
|
+
const childPath = `${prefix}${name}`;
|
|
123
|
+
entries.set(childPath, {
|
|
124
|
+
type: "directory",
|
|
125
|
+
name,
|
|
126
|
+
path: childPath
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return [...entries.values()].sort((left, right) => {
|
|
131
|
+
if (left.type !== right.type) return left.type === "directory" ? -1 : 1;
|
|
132
|
+
return left.name.localeCompare(right.name);
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
async readFile(path) {
|
|
136
|
+
const normalized = normalizePath(path);
|
|
137
|
+
const contents = files.get(normalized);
|
|
138
|
+
if (contents === void 0) {
|
|
139
|
+
throw new Error(`File not found: ${normalized}`);
|
|
140
|
+
}
|
|
141
|
+
return contents;
|
|
142
|
+
},
|
|
143
|
+
async writeFile(path, contents) {
|
|
144
|
+
const normalized = normalizePath(path);
|
|
145
|
+
if (!normalized) throw new Error("File path must not be empty.");
|
|
146
|
+
if (directories.has(normalized)) {
|
|
147
|
+
throw new Error(`A directory already exists at ${normalized}`);
|
|
148
|
+
}
|
|
149
|
+
addParentDirectories(normalized);
|
|
150
|
+
files.set(normalized, contents);
|
|
151
|
+
notify();
|
|
152
|
+
},
|
|
153
|
+
async createFile(path, contents = "") {
|
|
154
|
+
const normalized = normalizePath(path);
|
|
155
|
+
if (!normalized) throw new Error("File path must not be empty.");
|
|
156
|
+
if (files.has(normalized)) {
|
|
157
|
+
throw new Error(`File already exists: ${normalized}`);
|
|
158
|
+
}
|
|
159
|
+
if (directories.has(normalized)) {
|
|
160
|
+
throw new Error(`A directory already exists at ${normalized}`);
|
|
161
|
+
}
|
|
162
|
+
addParentDirectories(normalized);
|
|
163
|
+
files.set(normalized, contents);
|
|
164
|
+
notify();
|
|
165
|
+
},
|
|
166
|
+
async createDirectory(path) {
|
|
167
|
+
const normalized = normalizePath(path);
|
|
168
|
+
if (!normalized) throw new Error("Directory path must not be empty.");
|
|
169
|
+
if (files.has(normalized) || directories.has(normalized)) {
|
|
170
|
+
throw new Error(`Path already exists: ${normalized}`);
|
|
171
|
+
}
|
|
172
|
+
const parts = normalized.split("/");
|
|
173
|
+
let directory = "";
|
|
174
|
+
for (const part of parts) {
|
|
175
|
+
directory = directory ? `${directory}/${part}` : part;
|
|
176
|
+
if (files.has(directory)) {
|
|
177
|
+
throw new Error(`A file blocks this directory path: ${directory}`);
|
|
178
|
+
}
|
|
179
|
+
directories.add(directory);
|
|
180
|
+
}
|
|
181
|
+
notify();
|
|
182
|
+
},
|
|
183
|
+
async deleteFile(path) {
|
|
184
|
+
const normalized = normalizePath(path);
|
|
185
|
+
if (!files.delete(normalized)) {
|
|
186
|
+
throw new Error(`File not found: ${normalized}`);
|
|
187
|
+
}
|
|
188
|
+
notify();
|
|
189
|
+
},
|
|
190
|
+
async deleteDirectory(path) {
|
|
191
|
+
const normalized = normalizePath(path);
|
|
192
|
+
if (!directories.has(normalized)) {
|
|
193
|
+
throw new Error(`Directory not found: ${normalized}`);
|
|
194
|
+
}
|
|
195
|
+
const prefix = `${normalized}/`;
|
|
196
|
+
for (const file of files.keys()) {
|
|
197
|
+
if (file.startsWith(prefix)) files.delete(file);
|
|
198
|
+
}
|
|
199
|
+
for (const directory of directories) {
|
|
200
|
+
if (directory === normalized || directory.startsWith(prefix)) {
|
|
201
|
+
directories.delete(directory);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
notify();
|
|
205
|
+
},
|
|
206
|
+
moveFile,
|
|
207
|
+
moveDirectory,
|
|
208
|
+
renameFile: moveFile,
|
|
209
|
+
watch(listener) {
|
|
210
|
+
listeners.add(listener);
|
|
211
|
+
return () => listeners.delete(listener);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function normalizePath(path) {
|
|
216
|
+
return path.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/KaiCode.tsx
|
|
220
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
221
|
+
var DEFAULT_FILES = {
|
|
222
|
+
"src/App.tsx": `export function App() {
|
|
223
|
+
return <h1>Hello from KaiCode</h1>;
|
|
224
|
+
}
|
|
225
|
+
`,
|
|
226
|
+
"README.md": "# Welcome to KaiCode\n\nChoose a file to start editing."
|
|
227
|
+
};
|
|
228
|
+
var languageByExtension = {
|
|
229
|
+
c: "c",
|
|
230
|
+
css: "css",
|
|
231
|
+
go: "go",
|
|
232
|
+
htm: "html",
|
|
233
|
+
html: "html",
|
|
234
|
+
js: "javascript",
|
|
235
|
+
json: "json",
|
|
236
|
+
jsx: "javascript",
|
|
237
|
+
md: "markdown",
|
|
238
|
+
mjs: "javascript",
|
|
239
|
+
py: "python",
|
|
240
|
+
rs: "rust",
|
|
241
|
+
scss: "scss",
|
|
242
|
+
sh: "shell",
|
|
243
|
+
sql: "sql",
|
|
244
|
+
svg: "xml",
|
|
245
|
+
ts: "typescript",
|
|
246
|
+
tsx: "typescript",
|
|
247
|
+
txt: "plaintext",
|
|
248
|
+
xml: "xml",
|
|
249
|
+
yaml: "yaml",
|
|
250
|
+
yml: "yaml"
|
|
251
|
+
};
|
|
252
|
+
function getLanguage(path) {
|
|
253
|
+
const extension = path.split(".").pop()?.toLowerCase() ?? "";
|
|
254
|
+
return languageByExtension[extension] ?? "plaintext";
|
|
255
|
+
}
|
|
256
|
+
function getFileGlyph(path) {
|
|
257
|
+
const extension = path.split(".").pop()?.toLowerCase();
|
|
258
|
+
if (extension === "tsx" || extension === "jsx") return "\u25C7";
|
|
259
|
+
if (extension === "ts" || extension === "js") return "\u25C6";
|
|
260
|
+
if (extension === "json") return "{}";
|
|
261
|
+
if (extension === "md") return "M";
|
|
262
|
+
if (extension === "css" || extension === "scss") return "#";
|
|
263
|
+
return "\xB7";
|
|
264
|
+
}
|
|
265
|
+
function errorMessage(error) {
|
|
266
|
+
return error instanceof Error ? error.message : String(error);
|
|
267
|
+
}
|
|
268
|
+
async function readTree(fileSystem, directory = "") {
|
|
269
|
+
const entries = await fileSystem.readDirectory(directory);
|
|
270
|
+
const nodes = await Promise.all(
|
|
271
|
+
entries.map(async (entry) => {
|
|
272
|
+
if (entry.type === "file") return entry;
|
|
273
|
+
return {
|
|
274
|
+
...entry,
|
|
275
|
+
children: await readTree(fileSystem, entry.path)
|
|
276
|
+
};
|
|
277
|
+
})
|
|
278
|
+
);
|
|
279
|
+
return nodes.sort((left, right) => {
|
|
280
|
+
if (left.type !== right.type) return left.type === "directory" ? -1 : 1;
|
|
281
|
+
return left.name.localeCompare(right.name);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
function firstFile(nodes) {
|
|
285
|
+
for (const node of nodes) {
|
|
286
|
+
if (node.type === "file") return node.path;
|
|
287
|
+
const nested = firstFile(node.children ?? []);
|
|
288
|
+
if (nested) return nested;
|
|
289
|
+
}
|
|
290
|
+
return void 0;
|
|
291
|
+
}
|
|
292
|
+
function directoryPaths(nodes) {
|
|
293
|
+
return nodes.flatMap(
|
|
294
|
+
(node) => node.type === "directory" ? [node.path, ...directoryPaths(node.children ?? [])] : []
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function filesInDirectory(nodes, directoryPath) {
|
|
298
|
+
const directory = findDirectory(nodes, directoryPath);
|
|
299
|
+
return directory ? filePaths(directory.children ?? []) : [];
|
|
300
|
+
}
|
|
301
|
+
function findDirectory(nodes, path) {
|
|
302
|
+
for (const node of nodes) {
|
|
303
|
+
if (node.type !== "directory") continue;
|
|
304
|
+
if (node.path === path) return node;
|
|
305
|
+
const nested = findDirectory(node.children ?? [], path);
|
|
306
|
+
if (nested) return nested;
|
|
307
|
+
}
|
|
308
|
+
return void 0;
|
|
309
|
+
}
|
|
310
|
+
function filePaths(nodes) {
|
|
311
|
+
return nodes.flatMap(
|
|
312
|
+
(node) => node.type === "file" ? [node.path] : filePaths(node.children ?? [])
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
function parentDirectory(path) {
|
|
316
|
+
const separator = path.lastIndexOf("/");
|
|
317
|
+
return separator === -1 ? "" : path.slice(0, separator);
|
|
318
|
+
}
|
|
319
|
+
function FileTree({
|
|
320
|
+
nodes,
|
|
321
|
+
activeFile,
|
|
322
|
+
onOpen,
|
|
323
|
+
collapsedFolders,
|
|
324
|
+
onToggleDirectory,
|
|
325
|
+
onContextMenu,
|
|
326
|
+
depth = 0
|
|
327
|
+
}) {
|
|
328
|
+
return /* @__PURE__ */ jsx(Fragment, { children: nodes.map(
|
|
329
|
+
(node) => node.type === "directory" ? /* @__PURE__ */ jsxs(
|
|
330
|
+
"details",
|
|
331
|
+
{
|
|
332
|
+
className: "kai-code__directory",
|
|
333
|
+
open: !collapsedFolders.has(node.path),
|
|
334
|
+
onToggle: (event) => onToggleDirectory(node.path, event.currentTarget.open),
|
|
335
|
+
children: [
|
|
336
|
+
/* @__PURE__ */ jsxs(
|
|
337
|
+
"summary",
|
|
338
|
+
{
|
|
339
|
+
onContextMenu: (event) => onContextMenu(node, event),
|
|
340
|
+
style: { "--tree-depth": depth },
|
|
341
|
+
children: [
|
|
342
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__chevron", "aria-hidden": "true", children: "\u25B8" }),
|
|
343
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__folder-icon", "aria-hidden": "true", children: "\u25B0" }),
|
|
344
|
+
node.name
|
|
345
|
+
]
|
|
346
|
+
}
|
|
347
|
+
),
|
|
348
|
+
/* @__PURE__ */ jsx(
|
|
349
|
+
FileTree,
|
|
350
|
+
{
|
|
351
|
+
nodes: node.children ?? [],
|
|
352
|
+
activeFile,
|
|
353
|
+
onOpen,
|
|
354
|
+
collapsedFolders,
|
|
355
|
+
onToggleDirectory,
|
|
356
|
+
onContextMenu,
|
|
357
|
+
depth: depth + 1
|
|
358
|
+
}
|
|
359
|
+
)
|
|
360
|
+
]
|
|
361
|
+
},
|
|
362
|
+
node.path
|
|
363
|
+
) : /* @__PURE__ */ jsxs(
|
|
364
|
+
"button",
|
|
365
|
+
{
|
|
366
|
+
className: `kai-code__file${activeFile === node.path ? " is-active" : ""}`,
|
|
367
|
+
onClick: () => onOpen(node.path),
|
|
368
|
+
onContextMenu: (event) => onContextMenu(node, event),
|
|
369
|
+
style: { "--tree-depth": depth },
|
|
370
|
+
type: "button",
|
|
371
|
+
children: [
|
|
372
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__file-icon", "aria-hidden": "true", children: getFileGlyph(node.path) }),
|
|
373
|
+
node.name
|
|
374
|
+
]
|
|
375
|
+
},
|
|
376
|
+
node.path
|
|
377
|
+
)
|
|
378
|
+
) });
|
|
379
|
+
}
|
|
380
|
+
function KaiCode({
|
|
381
|
+
fileSystem,
|
|
382
|
+
initialFiles,
|
|
383
|
+
initialActiveFile,
|
|
384
|
+
theme = "vs-dark",
|
|
385
|
+
options,
|
|
386
|
+
height = 640,
|
|
387
|
+
readOnly = false,
|
|
388
|
+
className,
|
|
389
|
+
onFileChange,
|
|
390
|
+
onSave
|
|
391
|
+
}) {
|
|
392
|
+
const [memoryFileSystem] = useState(
|
|
393
|
+
() => createMemoryFileSystem(initialFiles ?? DEFAULT_FILES)
|
|
394
|
+
);
|
|
395
|
+
const fs = fileSystem ?? memoryFileSystem;
|
|
396
|
+
const [tree, setTree] = useState([]);
|
|
397
|
+
const [activeFile, setActiveFile] = useState(null);
|
|
398
|
+
const [openFiles, setOpenFiles] = useState([]);
|
|
399
|
+
const [documents, setDocuments] = useState({});
|
|
400
|
+
const [dirtyFiles, setDirtyFiles] = useState(() => /* @__PURE__ */ new Set());
|
|
401
|
+
const [explorerVisible, setExplorerVisible] = useState(true);
|
|
402
|
+
const [sidebarWidth, setSidebarWidth] = useState(230);
|
|
403
|
+
const [collapsedFolders, setCollapsedFolders] = useState(
|
|
404
|
+
() => /* @__PURE__ */ new Set()
|
|
405
|
+
);
|
|
406
|
+
const [contextMenu, setContextMenu] = useState(null);
|
|
407
|
+
const [creationRequest, setCreationRequest] = useState(null);
|
|
408
|
+
const [creationName, setCreationName] = useState("");
|
|
409
|
+
const [deleteTarget, setDeleteTarget] = useState(null);
|
|
410
|
+
const [moveTarget, setMoveTarget] = useState(null);
|
|
411
|
+
const [moveDestination, setMoveDestination] = useState("");
|
|
412
|
+
const [loadingTree, setLoadingTree] = useState(true);
|
|
413
|
+
const [loadingDocument, setLoadingDocument] = useState(false);
|
|
414
|
+
const [failedDocument, setFailedDocument] = useState(null);
|
|
415
|
+
const [documentRequest, setDocumentRequest] = useState(0);
|
|
416
|
+
const [error, setError] = useState(null);
|
|
417
|
+
const saveRef = useRef(async () => void 0);
|
|
418
|
+
const activeFileRef = useRef(null);
|
|
419
|
+
const resizeStartRef = useRef(null);
|
|
420
|
+
const suppressWatchRef = useRef(false);
|
|
421
|
+
const createInputId = useId();
|
|
422
|
+
const createTitleId = useId();
|
|
423
|
+
const deleteTitleId = useId();
|
|
424
|
+
const moveInputId = useId();
|
|
425
|
+
const moveTitleId = useId();
|
|
426
|
+
const isLight = theme === "light";
|
|
427
|
+
const reloadFiles = useCallback(async () => {
|
|
428
|
+
setLoadingTree(true);
|
|
429
|
+
try {
|
|
430
|
+
const nextTree = await readTree(fs);
|
|
431
|
+
setTree(nextTree);
|
|
432
|
+
setError(null);
|
|
433
|
+
const requested = initialActiveFile ? initialActiveFile : void 0;
|
|
434
|
+
const current = activeFileRef.current;
|
|
435
|
+
const selected = current && findFile(nextTree, current) ? current : requested && findFile(nextTree, requested) ? requested : firstFile(nextTree) ?? null;
|
|
436
|
+
activeFileRef.current = selected;
|
|
437
|
+
setActiveFile(selected);
|
|
438
|
+
if (selected) {
|
|
439
|
+
setOpenFiles(
|
|
440
|
+
(files) => files.includes(selected) ? files : [...files, selected]
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
} catch (loadError) {
|
|
444
|
+
setError(`Unable to load files: ${errorMessage(loadError)}`);
|
|
445
|
+
} finally {
|
|
446
|
+
setLoadingTree(false);
|
|
447
|
+
}
|
|
448
|
+
}, [fs, initialActiveFile]);
|
|
449
|
+
useEffect(() => {
|
|
450
|
+
void reloadFiles();
|
|
451
|
+
return fs.watch?.(() => {
|
|
452
|
+
if (suppressWatchRef.current) return;
|
|
453
|
+
void reloadFiles();
|
|
454
|
+
});
|
|
455
|
+
}, [fs, reloadFiles]);
|
|
456
|
+
useEffect(() => {
|
|
457
|
+
if (!activeFile || documents[activeFile] !== void 0) return;
|
|
458
|
+
let cancelled = false;
|
|
459
|
+
setLoadingDocument(true);
|
|
460
|
+
setFailedDocument(null);
|
|
461
|
+
fs.readFile(activeFile).then((contents) => {
|
|
462
|
+
if (!cancelled) {
|
|
463
|
+
setDocuments((current) => ({ ...current, [activeFile]: contents }));
|
|
464
|
+
setFailedDocument(null);
|
|
465
|
+
setError(null);
|
|
466
|
+
}
|
|
467
|
+
}).catch((readError) => {
|
|
468
|
+
if (!cancelled) {
|
|
469
|
+
setFailedDocument(activeFile);
|
|
470
|
+
setError(`Unable to open ${activeFile}: ${errorMessage(readError)}`);
|
|
471
|
+
}
|
|
472
|
+
}).finally(() => {
|
|
473
|
+
if (!cancelled) setLoadingDocument(false);
|
|
474
|
+
});
|
|
475
|
+
return () => {
|
|
476
|
+
cancelled = true;
|
|
477
|
+
};
|
|
478
|
+
}, [activeFile, documentRequest, documents, fs]);
|
|
479
|
+
const collapseAll = useCallback(() => {
|
|
480
|
+
setCollapsedFolders(new Set(directoryPaths(tree)));
|
|
481
|
+
}, [tree]);
|
|
482
|
+
useEffect(() => {
|
|
483
|
+
let chordTimer;
|
|
484
|
+
let awaitingCollapseChord = false;
|
|
485
|
+
const handleKeyDown = (event) => {
|
|
486
|
+
if (event.key === "Escape") {
|
|
487
|
+
setContextMenu(null);
|
|
488
|
+
setCreationRequest(null);
|
|
489
|
+
setDeleteTarget(null);
|
|
490
|
+
setMoveTarget(null);
|
|
491
|
+
}
|
|
492
|
+
const modifierPressed = event.ctrlKey || event.metaKey;
|
|
493
|
+
if (awaitingCollapseChord) {
|
|
494
|
+
if (["Control", "Meta", "Shift", "Alt"].includes(event.key)) return;
|
|
495
|
+
awaitingCollapseChord = false;
|
|
496
|
+
if (chordTimer) clearTimeout(chordTimer);
|
|
497
|
+
if (modifierPressed && (event.key === "0" || event.code === "Digit0" || event.code === "Numpad0")) {
|
|
498
|
+
event.preventDefault();
|
|
499
|
+
event.stopPropagation();
|
|
500
|
+
collapseAll();
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (!modifierPressed || event.shiftKey) return;
|
|
505
|
+
if (event.key.toLowerCase() === "b") {
|
|
506
|
+
event.preventDefault();
|
|
507
|
+
event.stopPropagation();
|
|
508
|
+
setExplorerVisible((visible) => !visible);
|
|
509
|
+
} else if (event.key.toLowerCase() === "k") {
|
|
510
|
+
event.preventDefault();
|
|
511
|
+
event.stopPropagation();
|
|
512
|
+
awaitingCollapseChord = true;
|
|
513
|
+
chordTimer = setTimeout(() => {
|
|
514
|
+
awaitingCollapseChord = false;
|
|
515
|
+
}, 1200);
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
window.addEventListener("keydown", handleKeyDown, true);
|
|
519
|
+
return () => {
|
|
520
|
+
window.removeEventListener("keydown", handleKeyDown, true);
|
|
521
|
+
if (chordTimer) clearTimeout(chordTimer);
|
|
522
|
+
};
|
|
523
|
+
}, [collapseAll]);
|
|
524
|
+
const saveFile = useCallback(async () => {
|
|
525
|
+
if (!activeFile || documents[activeFile] === void 0 || readOnly) return;
|
|
526
|
+
const contents = documents[activeFile];
|
|
527
|
+
try {
|
|
528
|
+
await fs.writeFile(activeFile, contents);
|
|
529
|
+
} catch (saveError) {
|
|
530
|
+
setError(`Unable to save ${activeFile}: ${errorMessage(saveError)}`);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
setDirtyFiles((current) => {
|
|
534
|
+
const next = new Set(current);
|
|
535
|
+
next.delete(activeFile);
|
|
536
|
+
return next;
|
|
537
|
+
});
|
|
538
|
+
setError(null);
|
|
539
|
+
onSave?.(activeFile, contents);
|
|
540
|
+
}, [activeFile, documents, fs, onSave, readOnly]);
|
|
541
|
+
saveRef.current = saveFile;
|
|
542
|
+
const openFile = useCallback((path) => {
|
|
543
|
+
activeFileRef.current = path;
|
|
544
|
+
setActiveFile(path);
|
|
545
|
+
setOpenFiles(
|
|
546
|
+
(current) => current.includes(path) ? current : [...current, path]
|
|
547
|
+
);
|
|
548
|
+
}, []);
|
|
549
|
+
const closeFile = useCallback(
|
|
550
|
+
(path) => {
|
|
551
|
+
if (dirtyFiles.has(path)) {
|
|
552
|
+
setError(`Save changes before closing ${path}.`);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const currentIndex = openFiles.indexOf(path);
|
|
556
|
+
const next = openFiles.filter((file) => file !== path);
|
|
557
|
+
setOpenFiles(next);
|
|
558
|
+
if (activeFile === path) {
|
|
559
|
+
const nextActive = next[Math.max(0, currentIndex - 1)] ?? null;
|
|
560
|
+
activeFileRef.current = nextActive;
|
|
561
|
+
setActiveFile(nextActive);
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
[activeFile, dirtyFiles, openFiles]
|
|
565
|
+
);
|
|
566
|
+
const changeDocument = useCallback(
|
|
567
|
+
(contents) => {
|
|
568
|
+
if (!activeFile || contents === void 0) return;
|
|
569
|
+
setDocuments((current) => ({ ...current, [activeFile]: contents }));
|
|
570
|
+
setDirtyFiles((current) => new Set(current).add(activeFile));
|
|
571
|
+
onFileChange?.(activeFile, contents);
|
|
572
|
+
},
|
|
573
|
+
[activeFile, onFileChange]
|
|
574
|
+
);
|
|
575
|
+
const beginCreate = useCallback(
|
|
576
|
+
(kind, parentPath) => {
|
|
577
|
+
if (readOnly) return;
|
|
578
|
+
setContextMenu(null);
|
|
579
|
+
setError(null);
|
|
580
|
+
setCreationName("");
|
|
581
|
+
setCreationRequest({ kind, parentPath });
|
|
582
|
+
},
|
|
583
|
+
[readOnly]
|
|
584
|
+
);
|
|
585
|
+
const openContextMenu = useCallback(
|
|
586
|
+
(entry, event) => {
|
|
587
|
+
event.preventDefault();
|
|
588
|
+
event.stopPropagation();
|
|
589
|
+
const canDelete = entry.type === "file" ? fs.deleteFile : fs.deleteDirectory;
|
|
590
|
+
const canMove = entry.type === "file" ? fs.moveFile || fs.renameFile : fs.moveDirectory;
|
|
591
|
+
if (readOnly || !fs.createFile && !fs.createDirectory && !canDelete && !canMove) {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
setContextMenu({
|
|
595
|
+
entry,
|
|
596
|
+
x: Math.max(4, Math.min(event.clientX, window.innerWidth - 220)),
|
|
597
|
+
y: Math.max(4, Math.min(event.clientY, window.innerHeight - 170))
|
|
598
|
+
});
|
|
599
|
+
},
|
|
600
|
+
[fs, readOnly]
|
|
601
|
+
);
|
|
602
|
+
const toggleDirectory = useCallback((path, expanded) => {
|
|
603
|
+
setCollapsedFolders((current) => {
|
|
604
|
+
const next = new Set(current);
|
|
605
|
+
if (expanded) next.delete(path);
|
|
606
|
+
else next.add(path);
|
|
607
|
+
return next;
|
|
608
|
+
});
|
|
609
|
+
}, []);
|
|
610
|
+
const submitCreation = useCallback(
|
|
611
|
+
async (event) => {
|
|
612
|
+
event.preventDefault();
|
|
613
|
+
if (!creationRequest) return;
|
|
614
|
+
const name = creationName.trim();
|
|
615
|
+
if (!name || name === "." || name === ".." || /[/\\]/.test(name)) {
|
|
616
|
+
setError("Enter a name without path separators.");
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const path = normalizePath(
|
|
620
|
+
creationRequest.parentPath ? `${creationRequest.parentPath}/${name}` : name
|
|
621
|
+
);
|
|
622
|
+
try {
|
|
623
|
+
if (creationRequest.kind === "file") {
|
|
624
|
+
if (!fs.createFile) {
|
|
625
|
+
throw new Error("File creation is not supported.");
|
|
626
|
+
}
|
|
627
|
+
await fs.createFile(path, "");
|
|
628
|
+
setDocuments((current) => ({ ...current, [path]: "" }));
|
|
629
|
+
} else {
|
|
630
|
+
if (!fs.createDirectory) {
|
|
631
|
+
throw new Error("Folder creation is not supported.");
|
|
632
|
+
}
|
|
633
|
+
await fs.createDirectory(path);
|
|
634
|
+
}
|
|
635
|
+
setCreationRequest(null);
|
|
636
|
+
setError(null);
|
|
637
|
+
await reloadFiles();
|
|
638
|
+
if (creationRequest.kind === "file") openFile(path);
|
|
639
|
+
} catch (createError) {
|
|
640
|
+
setError(`Unable to create ${path}: ${errorMessage(createError)}`);
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
[creationName, creationRequest, fs, openFile, reloadFiles]
|
|
644
|
+
);
|
|
645
|
+
const confirmDelete = useCallback(async () => {
|
|
646
|
+
if (!deleteTarget) return;
|
|
647
|
+
const target = deleteTarget;
|
|
648
|
+
try {
|
|
649
|
+
if (target.type === "file") {
|
|
650
|
+
if (!fs.deleteFile) {
|
|
651
|
+
throw new Error("File deletion is not supported.");
|
|
652
|
+
}
|
|
653
|
+
await fs.deleteFile(target.path);
|
|
654
|
+
} else {
|
|
655
|
+
if (!fs.deleteDirectory) {
|
|
656
|
+
throw new Error("Folder deletion is not supported.");
|
|
657
|
+
}
|
|
658
|
+
await fs.deleteDirectory(target.path);
|
|
659
|
+
}
|
|
660
|
+
const affectedFiles = target.type === "file" ? [target.path] : [
|
|
661
|
+
.../* @__PURE__ */ new Set([
|
|
662
|
+
...filesInDirectory(tree, target.path),
|
|
663
|
+
...Object.keys(documents).filter(
|
|
664
|
+
(path) => path.startsWith(`${target.path}/`)
|
|
665
|
+
)
|
|
666
|
+
])
|
|
667
|
+
];
|
|
668
|
+
const affected = new Set(affectedFiles);
|
|
669
|
+
setOpenFiles((current) => current.filter((path) => !affected.has(path)));
|
|
670
|
+
setDirtyFiles((current) => {
|
|
671
|
+
const next = new Set(current);
|
|
672
|
+
affected.forEach((path) => next.delete(path));
|
|
673
|
+
return next;
|
|
674
|
+
});
|
|
675
|
+
setDocuments((current) => {
|
|
676
|
+
const next = { ...current };
|
|
677
|
+
affected.forEach((path) => delete next[path]);
|
|
678
|
+
return next;
|
|
679
|
+
});
|
|
680
|
+
if (activeFileRef.current && affected.has(activeFileRef.current)) {
|
|
681
|
+
activeFileRef.current = null;
|
|
682
|
+
setActiveFile(null);
|
|
683
|
+
}
|
|
684
|
+
setDeleteTarget(null);
|
|
685
|
+
setContextMenu(null);
|
|
686
|
+
setError(null);
|
|
687
|
+
await reloadFiles();
|
|
688
|
+
} catch (deleteError) {
|
|
689
|
+
setError(`Unable to delete ${target.path}: ${errorMessage(deleteError)}`);
|
|
690
|
+
}
|
|
691
|
+
}, [deleteTarget, documents, fs, reloadFiles, tree]);
|
|
692
|
+
const moveEntry = useCallback(
|
|
693
|
+
async (event) => {
|
|
694
|
+
event.preventDefault();
|
|
695
|
+
if (!moveTarget) return;
|
|
696
|
+
const rawDestination = moveDestination.trim().replace(/\\/g, "/");
|
|
697
|
+
const destination = normalizePath(rawDestination);
|
|
698
|
+
if (!destination || rawDestination.startsWith("/") || destination.split("/").some((part) => !part || part === "." || part === "..")) {
|
|
699
|
+
setError("Enter a valid relative destination path.");
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const oldPath = moveTarget.path;
|
|
703
|
+
const newPath = destination;
|
|
704
|
+
const updatePath = (path) => moveTarget.type === "file" ? path === oldPath ? newPath : path : path === oldPath || path.startsWith(`${oldPath}/`) ? `${newPath}${path.slice(oldPath.length)}` : path;
|
|
705
|
+
try {
|
|
706
|
+
suppressWatchRef.current = true;
|
|
707
|
+
try {
|
|
708
|
+
if (moveTarget.type === "file") {
|
|
709
|
+
if (fs.moveFile) {
|
|
710
|
+
await fs.moveFile(oldPath, newPath);
|
|
711
|
+
} else if (fs.renameFile) {
|
|
712
|
+
await fs.renameFile(oldPath, newPath);
|
|
713
|
+
} else {
|
|
714
|
+
throw new Error("File moving is not supported.");
|
|
715
|
+
}
|
|
716
|
+
} else {
|
|
717
|
+
if (!fs.moveDirectory) {
|
|
718
|
+
throw new Error("Folder moving is not supported.");
|
|
719
|
+
}
|
|
720
|
+
await fs.moveDirectory(oldPath, newPath);
|
|
721
|
+
}
|
|
722
|
+
} finally {
|
|
723
|
+
suppressWatchRef.current = false;
|
|
724
|
+
}
|
|
725
|
+
setDocuments((current) => {
|
|
726
|
+
const next = { ...current };
|
|
727
|
+
Object.keys(current).forEach((path) => {
|
|
728
|
+
const updatedPath = updatePath(path);
|
|
729
|
+
if (updatedPath !== path) {
|
|
730
|
+
const contents = current[path];
|
|
731
|
+
if (contents !== void 0) {
|
|
732
|
+
next[updatedPath] = contents;
|
|
733
|
+
delete next[path];
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
return next;
|
|
738
|
+
});
|
|
739
|
+
setDirtyFiles((current) => new Set([...current].map(updatePath)));
|
|
740
|
+
setOpenFiles((current) => current.map(updatePath));
|
|
741
|
+
if (activeFileRef.current) {
|
|
742
|
+
const updatedActiveFile = updatePath(activeFileRef.current);
|
|
743
|
+
activeFileRef.current = updatedActiveFile;
|
|
744
|
+
setActiveFile(updatedActiveFile);
|
|
745
|
+
}
|
|
746
|
+
setMoveTarget(null);
|
|
747
|
+
setError(null);
|
|
748
|
+
await reloadFiles();
|
|
749
|
+
} catch (moveError) {
|
|
750
|
+
setError(`Unable to move ${oldPath}: ${errorMessage(moveError)}`);
|
|
751
|
+
}
|
|
752
|
+
},
|
|
753
|
+
[fs, moveDestination, moveTarget, reloadFiles]
|
|
754
|
+
);
|
|
755
|
+
const editorOptions = useMemo(
|
|
756
|
+
() => ({
|
|
757
|
+
automaticLayout: true,
|
|
758
|
+
fontSize: 13,
|
|
759
|
+
fontFamily: 'Consolas, "Courier New", monospace',
|
|
760
|
+
minimap: { enabled: true },
|
|
761
|
+
padding: { top: 12 },
|
|
762
|
+
scrollBeyondLastLine: false,
|
|
763
|
+
smoothScrolling: true,
|
|
764
|
+
tabSize: 2,
|
|
765
|
+
...options,
|
|
766
|
+
readOnly
|
|
767
|
+
}),
|
|
768
|
+
[options, readOnly]
|
|
769
|
+
);
|
|
770
|
+
const activeLanguage = activeFile ? findFileEntry(tree, activeFile)?.language ?? getLanguage(activeFile) : null;
|
|
771
|
+
return /* @__PURE__ */ jsxs(
|
|
772
|
+
"section",
|
|
773
|
+
{
|
|
774
|
+
className: `kai-code${isLight ? " kai-code--light" : ""}${className ? ` ${className}` : ""}`,
|
|
775
|
+
style: { height },
|
|
776
|
+
"aria-label": "KaiCode editor",
|
|
777
|
+
children: [
|
|
778
|
+
/* @__PURE__ */ jsxs("header", { className: "kai-code__titlebar", children: [
|
|
779
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__brand-mark", "aria-hidden": "true", children: "K" }),
|
|
780
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__brand", children: "KaiCode" }),
|
|
781
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__title-divider" }),
|
|
782
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__workspace-title", children: "Workspace" }),
|
|
783
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__title-actions", children: /* @__PURE__ */ jsx(
|
|
784
|
+
"button",
|
|
785
|
+
{
|
|
786
|
+
className: "kai-code__title-button",
|
|
787
|
+
disabled: !activeFile || !dirtyFiles.has(activeFile) || readOnly,
|
|
788
|
+
onClick: () => void saveFile(),
|
|
789
|
+
type: "button",
|
|
790
|
+
children: "Save"
|
|
791
|
+
}
|
|
792
|
+
) })
|
|
793
|
+
] }),
|
|
794
|
+
/* @__PURE__ */ jsxs(
|
|
795
|
+
"div",
|
|
796
|
+
{
|
|
797
|
+
className: "kai-code__workbench",
|
|
798
|
+
onClick: () => setContextMenu(null),
|
|
799
|
+
children: [
|
|
800
|
+
/* @__PURE__ */ jsxs("nav", { className: "kai-code__activity-bar", "aria-label": "Workbench", children: [
|
|
801
|
+
/* @__PURE__ */ jsx(
|
|
802
|
+
"button",
|
|
803
|
+
{
|
|
804
|
+
className: `kai-code__activity-button${explorerVisible ? " is-active" : ""}`,
|
|
805
|
+
"aria-label": "Toggle Explorer",
|
|
806
|
+
"aria-expanded": explorerVisible,
|
|
807
|
+
title: "Explorer (Ctrl+B)",
|
|
808
|
+
onClick: () => setExplorerVisible((visible) => !visible),
|
|
809
|
+
type: "button",
|
|
810
|
+
children: /* @__PURE__ */ jsxs("svg", { viewBox: "0 0 24 24", "aria-hidden": "true", children: [
|
|
811
|
+
/* @__PURE__ */ jsx("path", { d: "M3.5 5.5h6l2 2h9v11h-17z" }),
|
|
812
|
+
/* @__PURE__ */ jsx("path", { d: "M3.5 8h17" })
|
|
813
|
+
] })
|
|
814
|
+
}
|
|
815
|
+
),
|
|
816
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__activity-spacer" }),
|
|
817
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__activity-avatar", "aria-hidden": "true", children: "K" })
|
|
818
|
+
] }),
|
|
819
|
+
explorerVisible && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
820
|
+
/* @__PURE__ */ jsxs(
|
|
821
|
+
"aside",
|
|
822
|
+
{
|
|
823
|
+
className: "kai-code__sidebar",
|
|
824
|
+
style: { flexBasis: sidebarWidth },
|
|
825
|
+
children: [
|
|
826
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__sidebar-heading", children: [
|
|
827
|
+
/* @__PURE__ */ jsx("span", { children: "EXPLORER" }),
|
|
828
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__sidebar-actions", children: [
|
|
829
|
+
fs.createFile && !readOnly && /* @__PURE__ */ jsx(
|
|
830
|
+
"button",
|
|
831
|
+
{
|
|
832
|
+
className: "kai-code__icon-button",
|
|
833
|
+
"aria-label": "New File",
|
|
834
|
+
title: "New File",
|
|
835
|
+
onClick: () => beginCreate("file", ""),
|
|
836
|
+
type: "button",
|
|
837
|
+
children: "+"
|
|
838
|
+
}
|
|
839
|
+
),
|
|
840
|
+
fs.createDirectory && !readOnly && /* @__PURE__ */ jsx(
|
|
841
|
+
"button",
|
|
842
|
+
{
|
|
843
|
+
className: "kai-code__icon-button",
|
|
844
|
+
"aria-label": "New Folder",
|
|
845
|
+
title: "New Folder",
|
|
846
|
+
onClick: () => beginCreate("directory", ""),
|
|
847
|
+
type: "button",
|
|
848
|
+
children: "\u25B0+"
|
|
849
|
+
}
|
|
850
|
+
),
|
|
851
|
+
/* @__PURE__ */ jsx(
|
|
852
|
+
"button",
|
|
853
|
+
{
|
|
854
|
+
className: "kai-code__icon-button",
|
|
855
|
+
"aria-label": "Collapse All",
|
|
856
|
+
title: "Collapse All (Ctrl+K Ctrl+0)",
|
|
857
|
+
onClick: collapseAll,
|
|
858
|
+
type: "button",
|
|
859
|
+
children: "\u229F"
|
|
860
|
+
}
|
|
861
|
+
)
|
|
862
|
+
] })
|
|
863
|
+
] }),
|
|
864
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__workspace-row", children: [
|
|
865
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__chevron", "aria-hidden": "true", children: "\u25BE" }),
|
|
866
|
+
/* @__PURE__ */ jsx("span", { children: "WORKSPACE" })
|
|
867
|
+
] }),
|
|
868
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__file-tree", children: loadingTree && tree.length === 0 ? /* @__PURE__ */ jsx("div", { className: "kai-code__empty", children: "Loading files\u2026" }) : tree.length ? /* @__PURE__ */ jsx(
|
|
869
|
+
FileTree,
|
|
870
|
+
{
|
|
871
|
+
nodes: tree,
|
|
872
|
+
activeFile,
|
|
873
|
+
onOpen: openFile,
|
|
874
|
+
collapsedFolders,
|
|
875
|
+
onToggleDirectory: toggleDirectory,
|
|
876
|
+
onContextMenu: openContextMenu
|
|
877
|
+
}
|
|
878
|
+
) : /* @__PURE__ */ jsx("div", { className: "kai-code__empty", children: "No files in this workspace" }) })
|
|
879
|
+
]
|
|
880
|
+
}
|
|
881
|
+
),
|
|
882
|
+
/* @__PURE__ */ jsx(
|
|
883
|
+
"div",
|
|
884
|
+
{
|
|
885
|
+
className: "kai-code__resizer",
|
|
886
|
+
role: "separator",
|
|
887
|
+
"aria-orientation": "vertical",
|
|
888
|
+
"aria-label": "Resize Explorer",
|
|
889
|
+
"aria-valuenow": sidebarWidth,
|
|
890
|
+
"aria-valuemin": 160,
|
|
891
|
+
"aria-valuemax": 480,
|
|
892
|
+
tabIndex: 0,
|
|
893
|
+
onPointerDown: (event) => {
|
|
894
|
+
event.preventDefault();
|
|
895
|
+
resizeStartRef.current = {
|
|
896
|
+
x: event.clientX,
|
|
897
|
+
width: sidebarWidth
|
|
898
|
+
};
|
|
899
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
900
|
+
},
|
|
901
|
+
onPointerMove: (event) => {
|
|
902
|
+
const start = resizeStartRef.current;
|
|
903
|
+
if (!start) return;
|
|
904
|
+
setSidebarWidth(
|
|
905
|
+
Math.max(
|
|
906
|
+
160,
|
|
907
|
+
Math.min(
|
|
908
|
+
480,
|
|
909
|
+
Math.round(start.width + event.clientX - start.x)
|
|
910
|
+
)
|
|
911
|
+
)
|
|
912
|
+
);
|
|
913
|
+
},
|
|
914
|
+
onPointerUp: () => {
|
|
915
|
+
resizeStartRef.current = null;
|
|
916
|
+
},
|
|
917
|
+
onKeyDown: (event) => {
|
|
918
|
+
const direction = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0;
|
|
919
|
+
if (!direction) return;
|
|
920
|
+
event.preventDefault();
|
|
921
|
+
setSidebarWidth(
|
|
922
|
+
(width) => Math.max(160, Math.min(480, width + direction * 12))
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
)
|
|
927
|
+
] }),
|
|
928
|
+
contextMenu && /* @__PURE__ */ jsxs(
|
|
929
|
+
"div",
|
|
930
|
+
{
|
|
931
|
+
className: "kai-code__context-menu",
|
|
932
|
+
role: "menu",
|
|
933
|
+
"aria-label": "File actions",
|
|
934
|
+
style: { left: contextMenu.x, top: contextMenu.y },
|
|
935
|
+
onClick: (event) => event.stopPropagation(),
|
|
936
|
+
onContextMenu: (event) => event.preventDefault(),
|
|
937
|
+
children: [
|
|
938
|
+
fs.createFile && !readOnly && /* @__PURE__ */ jsx(
|
|
939
|
+
"button",
|
|
940
|
+
{
|
|
941
|
+
role: "menuitem",
|
|
942
|
+
onClick: () => beginCreate(
|
|
943
|
+
"file",
|
|
944
|
+
contextMenu.entry.type === "directory" ? contextMenu.entry.path : parentDirectory(contextMenu.entry.path)
|
|
945
|
+
),
|
|
946
|
+
type: "button",
|
|
947
|
+
children: "New File"
|
|
948
|
+
}
|
|
949
|
+
),
|
|
950
|
+
fs.createDirectory && !readOnly && /* @__PURE__ */ jsx(
|
|
951
|
+
"button",
|
|
952
|
+
{
|
|
953
|
+
role: "menuitem",
|
|
954
|
+
onClick: () => beginCreate(
|
|
955
|
+
"directory",
|
|
956
|
+
contextMenu.entry.type === "directory" ? contextMenu.entry.path : parentDirectory(contextMenu.entry.path)
|
|
957
|
+
),
|
|
958
|
+
type: "button",
|
|
959
|
+
children: "New Folder"
|
|
960
|
+
}
|
|
961
|
+
),
|
|
962
|
+
(contextMenu.entry.type === "file" && (fs.moveFile || fs.renameFile) || contextMenu.entry.type === "directory" && fs.moveDirectory) && !readOnly && /* @__PURE__ */ jsx(
|
|
963
|
+
"button",
|
|
964
|
+
{
|
|
965
|
+
role: "menuitem",
|
|
966
|
+
onClick: () => {
|
|
967
|
+
setMoveTarget(contextMenu.entry);
|
|
968
|
+
setMoveDestination("");
|
|
969
|
+
setError(null);
|
|
970
|
+
setContextMenu(null);
|
|
971
|
+
},
|
|
972
|
+
type: "button",
|
|
973
|
+
children: "Move\u2026"
|
|
974
|
+
}
|
|
975
|
+
),
|
|
976
|
+
(contextMenu.entry.type === "file" && fs.deleteFile || contextMenu.entry.type === "directory" && fs.deleteDirectory) && !readOnly && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
977
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__context-divider" }),
|
|
978
|
+
/* @__PURE__ */ jsx(
|
|
979
|
+
"button",
|
|
980
|
+
{
|
|
981
|
+
className: "is-danger",
|
|
982
|
+
role: "menuitem",
|
|
983
|
+
onClick: () => {
|
|
984
|
+
setError(null);
|
|
985
|
+
setDeleteTarget(contextMenu.entry);
|
|
986
|
+
setContextMenu(null);
|
|
987
|
+
},
|
|
988
|
+
type: "button",
|
|
989
|
+
children: "Delete"
|
|
990
|
+
}
|
|
991
|
+
)
|
|
992
|
+
] })
|
|
993
|
+
]
|
|
994
|
+
}
|
|
995
|
+
),
|
|
996
|
+
/* @__PURE__ */ jsxs("main", { className: "kai-code__editor-area", children: [
|
|
997
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__tabs", role: "tablist", "aria-label": "Open files", children: openFiles.map((path) => {
|
|
998
|
+
const fileName = path.split("/").pop() ?? path;
|
|
999
|
+
return /* @__PURE__ */ jsxs(
|
|
1000
|
+
"div",
|
|
1001
|
+
{
|
|
1002
|
+
className: `kai-code__tab${activeFile === path ? " is-active" : ""}`,
|
|
1003
|
+
role: "presentation",
|
|
1004
|
+
children: [
|
|
1005
|
+
/* @__PURE__ */ jsxs(
|
|
1006
|
+
"button",
|
|
1007
|
+
{
|
|
1008
|
+
className: "kai-code__tab-label",
|
|
1009
|
+
onClick: () => openFile(path),
|
|
1010
|
+
role: "tab",
|
|
1011
|
+
"aria-selected": activeFile === path,
|
|
1012
|
+
type: "button",
|
|
1013
|
+
children: [
|
|
1014
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__file-icon", "aria-hidden": "true", children: getFileGlyph(path) }),
|
|
1015
|
+
fileName,
|
|
1016
|
+
dirtyFiles.has(path) && /* @__PURE__ */ jsx("span", { className: "kai-code__dirty-dot", "aria-label": "Unsaved" })
|
|
1017
|
+
]
|
|
1018
|
+
}
|
|
1019
|
+
),
|
|
1020
|
+
/* @__PURE__ */ jsx(
|
|
1021
|
+
"button",
|
|
1022
|
+
{
|
|
1023
|
+
className: "kai-code__tab-close",
|
|
1024
|
+
"aria-label": `Close ${fileName}`,
|
|
1025
|
+
onClick: () => closeFile(path),
|
|
1026
|
+
type: "button",
|
|
1027
|
+
children: "\xD7"
|
|
1028
|
+
}
|
|
1029
|
+
)
|
|
1030
|
+
]
|
|
1031
|
+
},
|
|
1032
|
+
path
|
|
1033
|
+
);
|
|
1034
|
+
}) }),
|
|
1035
|
+
activeFile && /* @__PURE__ */ jsx("div", { className: "kai-code__breadcrumbs", children: activeFile.split("/").map((part, index, parts) => /* @__PURE__ */ jsxs("span", { children: [
|
|
1036
|
+
index > 0 && /* @__PURE__ */ jsx("span", { className: "kai-code__breadcrumb-separator", children: "\u203A" }),
|
|
1037
|
+
part,
|
|
1038
|
+
index < parts.length - 1 && " "
|
|
1039
|
+
] }, `${part}-${index}`)) }),
|
|
1040
|
+
error && /* @__PURE__ */ jsxs("div", { className: "kai-code__error", role: "alert", children: [
|
|
1041
|
+
/* @__PURE__ */ jsx("span", { children: error }),
|
|
1042
|
+
/* @__PURE__ */ jsx(
|
|
1043
|
+
"button",
|
|
1044
|
+
{
|
|
1045
|
+
"aria-label": "Dismiss error",
|
|
1046
|
+
onClick: () => setError(null),
|
|
1047
|
+
type: "button",
|
|
1048
|
+
children: "\xD7"
|
|
1049
|
+
}
|
|
1050
|
+
)
|
|
1051
|
+
] }),
|
|
1052
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__monaco", children: activeFile ? loadingDocument && documents[activeFile] === void 0 ? /* @__PURE__ */ jsxs("div", { className: "kai-code__empty", children: [
|
|
1053
|
+
"Opening ",
|
|
1054
|
+
activeFile,
|
|
1055
|
+
"\u2026"
|
|
1056
|
+
] }) : failedDocument === activeFile ? /* @__PURE__ */ jsxs("div", { className: "kai-code__empty", role: "alert", children: [
|
|
1057
|
+
"Could not load this file.",
|
|
1058
|
+
" ",
|
|
1059
|
+
/* @__PURE__ */ jsx(
|
|
1060
|
+
"button",
|
|
1061
|
+
{
|
|
1062
|
+
className: "kai-code__retry",
|
|
1063
|
+
onClick: () => setDocumentRequest((request) => request + 1),
|
|
1064
|
+
type: "button",
|
|
1065
|
+
children: "Retry"
|
|
1066
|
+
}
|
|
1067
|
+
)
|
|
1068
|
+
] }) : /* @__PURE__ */ jsx(
|
|
1069
|
+
Editor,
|
|
1070
|
+
{
|
|
1071
|
+
height: "100%",
|
|
1072
|
+
language: activeLanguage ?? void 0,
|
|
1073
|
+
path: activeFile,
|
|
1074
|
+
value: documents[activeFile] ?? "",
|
|
1075
|
+
theme,
|
|
1076
|
+
options: editorOptions,
|
|
1077
|
+
onChange: changeDocument,
|
|
1078
|
+
onMount: (instance, monaco) => {
|
|
1079
|
+
instance.addCommand(
|
|
1080
|
+
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS,
|
|
1081
|
+
() => void saveRef.current()
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
},
|
|
1085
|
+
activeFile
|
|
1086
|
+
) : /* @__PURE__ */ jsxs("div", { className: "kai-code__welcome", children: [
|
|
1087
|
+
/* @__PURE__ */ jsx("div", { className: "kai-code__welcome-mark", children: "K" }),
|
|
1088
|
+
/* @__PURE__ */ jsx("h1", { children: "KaiCode" }),
|
|
1089
|
+
/* @__PURE__ */ jsx("p", { children: "Select a file from the Explorer to start editing." })
|
|
1090
|
+
] }) })
|
|
1091
|
+
] })
|
|
1092
|
+
]
|
|
1093
|
+
}
|
|
1094
|
+
),
|
|
1095
|
+
creationRequest && /* @__PURE__ */ jsx(
|
|
1096
|
+
"div",
|
|
1097
|
+
{
|
|
1098
|
+
className: "kai-code__dialog-backdrop",
|
|
1099
|
+
onMouseDown: (event) => {
|
|
1100
|
+
if (event.target === event.currentTarget) setCreationRequest(null);
|
|
1101
|
+
},
|
|
1102
|
+
children: /* @__PURE__ */ jsxs(
|
|
1103
|
+
"form",
|
|
1104
|
+
{
|
|
1105
|
+
className: "kai-code__dialog",
|
|
1106
|
+
role: "dialog",
|
|
1107
|
+
"aria-modal": "true",
|
|
1108
|
+
"aria-labelledby": createTitleId,
|
|
1109
|
+
onSubmit: (event) => void submitCreation(event),
|
|
1110
|
+
children: [
|
|
1111
|
+
/* @__PURE__ */ jsxs("h2", { id: createTitleId, children: [
|
|
1112
|
+
"New ",
|
|
1113
|
+
creationRequest.kind === "file" ? "File" : "Folder"
|
|
1114
|
+
] }),
|
|
1115
|
+
/* @__PURE__ */ jsx("p", { children: creationRequest.parentPath || "Workspace root" }),
|
|
1116
|
+
/* @__PURE__ */ jsx("label", { htmlFor: createInputId, children: creationRequest.kind === "file" ? "File name" : "Folder name" }),
|
|
1117
|
+
/* @__PURE__ */ jsx(
|
|
1118
|
+
"input",
|
|
1119
|
+
{
|
|
1120
|
+
id: createInputId,
|
|
1121
|
+
autoFocus: true,
|
|
1122
|
+
value: creationName,
|
|
1123
|
+
onChange: (event) => setCreationName(event.target.value),
|
|
1124
|
+
placeholder: creationRequest.kind === "file" ? "Example.tsx" : "components"
|
|
1125
|
+
}
|
|
1126
|
+
),
|
|
1127
|
+
error && /* @__PURE__ */ jsx("p", { className: "kai-code__dialog-error", role: "alert", children: error }),
|
|
1128
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__dialog-actions", children: [
|
|
1129
|
+
/* @__PURE__ */ jsx("button", { onClick: () => setCreationRequest(null), type: "button", children: "Cancel" }),
|
|
1130
|
+
/* @__PURE__ */ jsx("button", { className: "is-primary", type: "submit", children: "Create" })
|
|
1131
|
+
] })
|
|
1132
|
+
]
|
|
1133
|
+
}
|
|
1134
|
+
)
|
|
1135
|
+
}
|
|
1136
|
+
),
|
|
1137
|
+
deleteTarget && /* @__PURE__ */ jsx(
|
|
1138
|
+
"div",
|
|
1139
|
+
{
|
|
1140
|
+
className: "kai-code__dialog-backdrop",
|
|
1141
|
+
onMouseDown: (event) => {
|
|
1142
|
+
if (event.target === event.currentTarget) setDeleteTarget(null);
|
|
1143
|
+
},
|
|
1144
|
+
children: /* @__PURE__ */ jsxs(
|
|
1145
|
+
"div",
|
|
1146
|
+
{
|
|
1147
|
+
className: "kai-code__dialog",
|
|
1148
|
+
role: "dialog",
|
|
1149
|
+
"aria-modal": "true",
|
|
1150
|
+
"aria-labelledby": deleteTitleId,
|
|
1151
|
+
children: [
|
|
1152
|
+
/* @__PURE__ */ jsxs("h2", { id: deleteTitleId, children: [
|
|
1153
|
+
"Delete ",
|
|
1154
|
+
deleteTarget.type === "file" ? "file" : "folder",
|
|
1155
|
+
"?"
|
|
1156
|
+
] }),
|
|
1157
|
+
/* @__PURE__ */ jsxs("p", { children: [
|
|
1158
|
+
/* @__PURE__ */ jsx("strong", { children: deleteTarget.name }),
|
|
1159
|
+
deleteTarget.type === "directory" && " and all of its contents will be permanently deleted."
|
|
1160
|
+
] }),
|
|
1161
|
+
error && /* @__PURE__ */ jsx("p", { className: "kai-code__dialog-error", role: "alert", children: error }),
|
|
1162
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__dialog-actions", children: [
|
|
1163
|
+
/* @__PURE__ */ jsx("button", { onClick: () => setDeleteTarget(null), type: "button", children: "Cancel" }),
|
|
1164
|
+
/* @__PURE__ */ jsx(
|
|
1165
|
+
"button",
|
|
1166
|
+
{
|
|
1167
|
+
className: "is-danger",
|
|
1168
|
+
onClick: () => void confirmDelete(),
|
|
1169
|
+
type: "button",
|
|
1170
|
+
children: "Delete"
|
|
1171
|
+
}
|
|
1172
|
+
)
|
|
1173
|
+
] })
|
|
1174
|
+
]
|
|
1175
|
+
}
|
|
1176
|
+
)
|
|
1177
|
+
}
|
|
1178
|
+
),
|
|
1179
|
+
moveTarget && /* @__PURE__ */ jsx(
|
|
1180
|
+
"div",
|
|
1181
|
+
{
|
|
1182
|
+
className: "kai-code__dialog-backdrop",
|
|
1183
|
+
onMouseDown: (event) => {
|
|
1184
|
+
if (event.target === event.currentTarget) setMoveTarget(null);
|
|
1185
|
+
},
|
|
1186
|
+
children: /* @__PURE__ */ jsxs(
|
|
1187
|
+
"form",
|
|
1188
|
+
{
|
|
1189
|
+
className: "kai-code__dialog",
|
|
1190
|
+
role: "dialog",
|
|
1191
|
+
"aria-modal": "true",
|
|
1192
|
+
"aria-labelledby": moveTitleId,
|
|
1193
|
+
onSubmit: (event) => void moveEntry(event),
|
|
1194
|
+
children: [
|
|
1195
|
+
/* @__PURE__ */ jsxs("h2", { id: moveTitleId, children: [
|
|
1196
|
+
"Move ",
|
|
1197
|
+
moveTarget.type === "file" ? "File" : "Folder"
|
|
1198
|
+
] }),
|
|
1199
|
+
/* @__PURE__ */ jsx("p", { children: moveTarget.path }),
|
|
1200
|
+
/* @__PURE__ */ jsx("label", { htmlFor: moveInputId, children: "Destination path" }),
|
|
1201
|
+
/* @__PURE__ */ jsx(
|
|
1202
|
+
"input",
|
|
1203
|
+
{
|
|
1204
|
+
id: moveInputId,
|
|
1205
|
+
autoFocus: true,
|
|
1206
|
+
value: moveDestination,
|
|
1207
|
+
onChange: (event) => setMoveDestination(event.target.value),
|
|
1208
|
+
placeholder: moveTarget.type === "file" ? "src/components/Example.tsx" : "src/components"
|
|
1209
|
+
}
|
|
1210
|
+
),
|
|
1211
|
+
error && /* @__PURE__ */ jsx("p", { className: "kai-code__dialog-error", role: "alert", children: error }),
|
|
1212
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__dialog-actions", children: [
|
|
1213
|
+
/* @__PURE__ */ jsx(
|
|
1214
|
+
"button",
|
|
1215
|
+
{
|
|
1216
|
+
onClick: () => {
|
|
1217
|
+
setMoveTarget(null);
|
|
1218
|
+
setError(null);
|
|
1219
|
+
},
|
|
1220
|
+
type: "button",
|
|
1221
|
+
children: "Cancel"
|
|
1222
|
+
}
|
|
1223
|
+
),
|
|
1224
|
+
/* @__PURE__ */ jsx("button", { className: "is-primary", type: "submit", children: "Move" })
|
|
1225
|
+
] })
|
|
1226
|
+
]
|
|
1227
|
+
}
|
|
1228
|
+
)
|
|
1229
|
+
}
|
|
1230
|
+
),
|
|
1231
|
+
/* @__PURE__ */ jsxs("footer", { className: "kai-code__statusbar", children: [
|
|
1232
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__status-left", children: [
|
|
1233
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__status-branch", children: "\u2442 main" }),
|
|
1234
|
+
/* @__PURE__ */ jsx("span", { children: error ? "Problems" : "Ready" })
|
|
1235
|
+
] }),
|
|
1236
|
+
/* @__PURE__ */ jsxs("div", { className: "kai-code__status-right", children: [
|
|
1237
|
+
activeLanguage && /* @__PURE__ */ jsx("span", { children: activeLanguage }),
|
|
1238
|
+
/* @__PURE__ */ jsx("span", { children: "UTF-8" }),
|
|
1239
|
+
/* @__PURE__ */ jsx("span", { children: "LF" }),
|
|
1240
|
+
/* @__PURE__ */ jsx("span", { className: "kai-code__status-kai", children: "KaiCode" })
|
|
1241
|
+
] })
|
|
1242
|
+
] })
|
|
1243
|
+
]
|
|
1244
|
+
}
|
|
1245
|
+
);
|
|
1246
|
+
}
|
|
1247
|
+
function findFile(nodes, path) {
|
|
1248
|
+
return nodes.some(
|
|
1249
|
+
(node) => node.type === "file" && node.path === path || node.type === "directory" && findFile(node.children ?? [], path)
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
function findFileEntry(nodes, path) {
|
|
1253
|
+
for (const node of nodes) {
|
|
1254
|
+
if (node.type === "file" && node.path === path) return node;
|
|
1255
|
+
if (node.type === "directory") {
|
|
1256
|
+
const nested = findFileEntry(node.children ?? [], path);
|
|
1257
|
+
if (nested) return nested;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return void 0;
|
|
1261
|
+
}
|
|
1262
|
+
var KaiCode_default = KaiCode;
|
|
1263
|
+
export {
|
|
1264
|
+
KaiCode,
|
|
1265
|
+
createMemoryFileSystem,
|
|
1266
|
+
KaiCode_default as default,
|
|
1267
|
+
normalizePath
|
|
1268
|
+
};
|