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