@handylib/kai-code 0.1.0 → 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/README.md CHANGED
@@ -85,6 +85,18 @@ const fileSystem: KaiCodeFileSystem = {
85
85
  async createFile(path, contents = "") {
86
86
  await myStorage.writeText(path, contents);
87
87
  },
88
+ async deleteFile(path) {
89
+ await myStorage.remove(path);
90
+ },
91
+ async deleteDirectory(path) {
92
+ await myStorage.removeTree(path);
93
+ },
94
+ async moveFile(oldPath, newPath) {
95
+ await myStorage.move(oldPath, newPath);
96
+ },
97
+ async moveDirectory(oldPath, newPath) {
98
+ await myStorage.moveTree(oldPath, newPath);
99
+ },
88
100
  watch(listener) {
89
101
  // Optional: call listener when files or directories change outside KaiCode.
90
102
  return myStorage.subscribe(listener);
@@ -116,7 +128,9 @@ Optional methods:
116
128
  | `createDirectory(path)` | Enables **New Folder** and creates the directory, including empty directories. |
117
129
  | `deleteFile(path)` | Enables deleting files from the Explorer context menu. |
118
130
  | `deleteDirectory(path)` | Enables deleting folders. The adapter must delete the folder and all of its descendants. |
119
- | `renameFile(oldPath, newPath)` | Reserved for file operations added by the host application. |
131
+ | `moveFile(oldPath, newPath)` | Enables moving files from the Explorer context menu. |
132
+ | `moveDirectory(oldPath, newPath)` | Enables moving folders. The adapter must move the folder and all of its descendants. |
133
+ | `renameFile(oldPath, newPath)` | Backward-compatible fallback for `moveFile`. Prefer implementing `moveFile`. |
120
134
  | `watch(listener)` | Refresh the Explorer when the mounted filesystem changes. Return an unsubscribe function. |
121
135
 
122
136
  Directory entries are traversed recursively when loading the Explorer. Adapter
@@ -147,10 +161,13 @@ be created directly with `createMemoryFileSystem(initialFiles)`.
147
161
  ## Explorer controls
148
162
 
149
163
  - Right-click a file or folder to open its context menu. Available create and
150
- delete actions depend on the optional filesystem methods you provide.
164
+ delete and move actions depend on the optional filesystem methods you provide.
151
165
  - Use **New File** and **New Folder** in the Explorer header to create at the
152
166
  workspace root. Context-menu create actions use the selected folder (or the
153
167
  selected file's parent).
168
+ - **Move** opens a destination-path field. Enter a relative path from the
169
+ workspace root. A file can use `moveFile` (or legacy `renameFile`); folders
170
+ require `moveDirectory`.
154
171
  - Deleting a folder asks for confirmation and removes all descendant documents
155
172
  from the open tabs. Unsaved changes in deleted files are discarded.
156
173
  - Drag the divider beside the Explorer to resize it. You can also focus the
package/dist/index.cjs CHANGED
@@ -67,6 +67,63 @@ function createMemoryFileSystem(initialFiles = {}) {
67
67
  const notify = () => {
68
68
  listeners.forEach((listener) => listener());
69
69
  };
70
+ const moveFile = async (oldPath, newPath) => {
71
+ const oldNormalized = normalizePath(oldPath);
72
+ const newNormalized = normalizePath(newPath);
73
+ const contents = files.get(oldNormalized);
74
+ if (contents === void 0) {
75
+ throw new Error(`File not found: ${oldNormalized}`);
76
+ }
77
+ if (!newNormalized) throw new Error("Destination file path must not be empty.");
78
+ if (oldNormalized === newNormalized) return;
79
+ if (files.has(newNormalized) || directories.has(newNormalized)) {
80
+ throw new Error(`Path already exists: ${newNormalized}`);
81
+ }
82
+ addParentDirectories(newNormalized);
83
+ files.delete(oldNormalized);
84
+ files.set(newNormalized, contents);
85
+ notify();
86
+ };
87
+ const moveDirectory = async (oldPath, newPath) => {
88
+ const oldNormalized = normalizePath(oldPath);
89
+ const newNormalized = normalizePath(newPath);
90
+ if (!directories.has(oldNormalized)) {
91
+ throw new Error(`Directory not found: ${oldNormalized}`);
92
+ }
93
+ if (!newNormalized) {
94
+ throw new Error("Destination directory path must not be empty.");
95
+ }
96
+ if (oldNormalized === newNormalized) return;
97
+ if (newNormalized.startsWith(`${oldNormalized}/`)) {
98
+ throw new Error("A directory cannot be moved inside itself.");
99
+ }
100
+ if (files.has(newNormalized) || directories.has(newNormalized)) {
101
+ throw new Error(`Path already exists: ${newNormalized}`);
102
+ }
103
+ addParentDirectories(newNormalized);
104
+ const oldPrefix = `${oldNormalized}/`;
105
+ const directoriesToMove = [...directories].filter(
106
+ (path) => path === oldNormalized || path.startsWith(oldPrefix)
107
+ );
108
+ const filesToMove = [...files.entries()].filter(
109
+ ([path]) => path.startsWith(oldPrefix)
110
+ );
111
+ for (const [path] of filesToMove) {
112
+ const destination = `${newNormalized}${path.slice(oldNormalized.length)}`;
113
+ if (files.has(destination) || directories.has(destination)) {
114
+ throw new Error(`Path already exists: ${destination}`);
115
+ }
116
+ }
117
+ directoriesToMove.forEach((path) => directories.delete(path));
118
+ filesToMove.forEach(([path]) => files.delete(path));
119
+ directoriesToMove.forEach(
120
+ (path) => directories.add(`${newNormalized}${path.slice(oldNormalized.length)}`)
121
+ );
122
+ filesToMove.forEach(
123
+ ([path, contents]) => files.set(`${newNormalized}${path.slice(oldNormalized.length)}`, contents)
124
+ );
125
+ notify();
126
+ };
70
127
  return {
71
128
  async readDirectory(directory = "") {
72
129
  const base = normalizePath(directory);
@@ -178,24 +235,9 @@ function createMemoryFileSystem(initialFiles = {}) {
178
235
  }
179
236
  notify();
180
237
  },
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
- },
238
+ moveFile,
239
+ moveDirectory,
240
+ renameFile: moveFile,
199
241
  watch(listener) {
200
242
  listeners.add(listener);
201
243
  return () => listeners.delete(listener);
@@ -397,6 +439,8 @@ function KaiCode({
397
439
  const [creationRequest, setCreationRequest] = (0, import_react2.useState)(null);
398
440
  const [creationName, setCreationName] = (0, import_react2.useState)("");
399
441
  const [deleteTarget, setDeleteTarget] = (0, import_react2.useState)(null);
442
+ const [moveTarget, setMoveTarget] = (0, import_react2.useState)(null);
443
+ const [moveDestination, setMoveDestination] = (0, import_react2.useState)("");
400
444
  const [loadingTree, setLoadingTree] = (0, import_react2.useState)(true);
401
445
  const [loadingDocument, setLoadingDocument] = (0, import_react2.useState)(false);
402
446
  const [failedDocument, setFailedDocument] = (0, import_react2.useState)(null);
@@ -405,9 +449,12 @@ function KaiCode({
405
449
  const saveRef = (0, import_react2.useRef)(async () => void 0);
406
450
  const activeFileRef = (0, import_react2.useRef)(null);
407
451
  const resizeStartRef = (0, import_react2.useRef)(null);
452
+ const suppressWatchRef = (0, import_react2.useRef)(false);
408
453
  const createInputId = (0, import_react2.useId)();
409
454
  const createTitleId = (0, import_react2.useId)();
410
455
  const deleteTitleId = (0, import_react2.useId)();
456
+ const moveInputId = (0, import_react2.useId)();
457
+ const moveTitleId = (0, import_react2.useId)();
411
458
  const isLight = theme === "light";
412
459
  const reloadFiles = (0, import_react2.useCallback)(async () => {
413
460
  setLoadingTree(true);
@@ -434,6 +481,7 @@ function KaiCode({
434
481
  (0, import_react2.useEffect)(() => {
435
482
  void reloadFiles();
436
483
  return fs.watch?.(() => {
484
+ if (suppressWatchRef.current) return;
437
485
  void reloadFiles();
438
486
  });
439
487
  }, [fs, reloadFiles]);
@@ -471,6 +519,7 @@ function KaiCode({
471
519
  setContextMenu(null);
472
520
  setCreationRequest(null);
473
521
  setDeleteTarget(null);
522
+ setMoveTarget(null);
474
523
  }
475
524
  const modifierPressed = event.ctrlKey || event.metaKey;
476
525
  if (awaitingCollapseChord) {
@@ -570,7 +619,8 @@ function KaiCode({
570
619
  event.preventDefault();
571
620
  event.stopPropagation();
572
621
  const canDelete = entry.type === "file" ? fs.deleteFile : fs.deleteDirectory;
573
- if (readOnly || !fs.createFile && !fs.createDirectory && !canDelete) {
622
+ const canMove = entry.type === "file" ? fs.moveFile || fs.renameFile : fs.moveDirectory;
623
+ if (readOnly || !fs.createFile && !fs.createDirectory && !canDelete && !canMove) {
574
624
  return;
575
625
  }
576
626
  setContextMenu({
@@ -671,6 +721,69 @@ function KaiCode({
671
721
  setError(`Unable to delete ${target.path}: ${errorMessage(deleteError)}`);
672
722
  }
673
723
  }, [deleteTarget, documents, fs, reloadFiles, tree]);
724
+ const moveEntry = (0, import_react2.useCallback)(
725
+ async (event) => {
726
+ event.preventDefault();
727
+ if (!moveTarget) return;
728
+ const rawDestination = moveDestination.trim().replace(/\\/g, "/");
729
+ const destination = normalizePath(rawDestination);
730
+ if (!destination || rawDestination.startsWith("/") || destination.split("/").some((part) => !part || part === "." || part === "..")) {
731
+ setError("Enter a valid relative destination path.");
732
+ return;
733
+ }
734
+ const oldPath = moveTarget.path;
735
+ const newPath = destination;
736
+ const updatePath = (path) => moveTarget.type === "file" ? path === oldPath ? newPath : path : path === oldPath || path.startsWith(`${oldPath}/`) ? `${newPath}${path.slice(oldPath.length)}` : path;
737
+ try {
738
+ suppressWatchRef.current = true;
739
+ try {
740
+ if (moveTarget.type === "file") {
741
+ if (fs.moveFile) {
742
+ await fs.moveFile(oldPath, newPath);
743
+ } else if (fs.renameFile) {
744
+ await fs.renameFile(oldPath, newPath);
745
+ } else {
746
+ throw new Error("File moving is not supported.");
747
+ }
748
+ } else {
749
+ if (!fs.moveDirectory) {
750
+ throw new Error("Folder moving is not supported.");
751
+ }
752
+ await fs.moveDirectory(oldPath, newPath);
753
+ }
754
+ } finally {
755
+ suppressWatchRef.current = false;
756
+ }
757
+ setDocuments((current) => {
758
+ const next = { ...current };
759
+ Object.keys(current).forEach((path) => {
760
+ const updatedPath = updatePath(path);
761
+ if (updatedPath !== path) {
762
+ const contents = current[path];
763
+ if (contents !== void 0) {
764
+ next[updatedPath] = contents;
765
+ delete next[path];
766
+ }
767
+ }
768
+ });
769
+ return next;
770
+ });
771
+ setDirtyFiles((current) => new Set([...current].map(updatePath)));
772
+ setOpenFiles((current) => current.map(updatePath));
773
+ if (activeFileRef.current) {
774
+ const updatedActiveFile = updatePath(activeFileRef.current);
775
+ activeFileRef.current = updatedActiveFile;
776
+ setActiveFile(updatedActiveFile);
777
+ }
778
+ setMoveTarget(null);
779
+ setError(null);
780
+ await reloadFiles();
781
+ } catch (moveError) {
782
+ setError(`Unable to move ${oldPath}: ${errorMessage(moveError)}`);
783
+ }
784
+ },
785
+ [fs, moveDestination, moveTarget, reloadFiles]
786
+ );
674
787
  const editorOptions = (0, import_react2.useMemo)(
675
788
  () => ({
676
789
  automaticLayout: true,
@@ -878,6 +991,20 @@ function KaiCode({
878
991
  children: "New Folder"
879
992
  }
880
993
  ),
994
+ (contextMenu.entry.type === "file" && (fs.moveFile || fs.renameFile) || contextMenu.entry.type === "directory" && fs.moveDirectory) && !readOnly && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
995
+ "button",
996
+ {
997
+ role: "menuitem",
998
+ onClick: () => {
999
+ setMoveTarget(contextMenu.entry);
1000
+ setMoveDestination("");
1001
+ setError(null);
1002
+ setContextMenu(null);
1003
+ },
1004
+ type: "button",
1005
+ children: "Move\u2026"
1006
+ }
1007
+ ),
881
1008
  (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
1009
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "kai-code__context-divider" }),
883
1010
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
@@ -1081,6 +1208,58 @@ function KaiCode({
1081
1208
  )
1082
1209
  }
1083
1210
  ),
1211
+ moveTarget && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1212
+ "div",
1213
+ {
1214
+ className: "kai-code__dialog-backdrop",
1215
+ onMouseDown: (event) => {
1216
+ if (event.target === event.currentTarget) setMoveTarget(null);
1217
+ },
1218
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
1219
+ "form",
1220
+ {
1221
+ className: "kai-code__dialog",
1222
+ role: "dialog",
1223
+ "aria-modal": "true",
1224
+ "aria-labelledby": moveTitleId,
1225
+ onSubmit: (event) => void moveEntry(event),
1226
+ children: [
1227
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("h2", { id: moveTitleId, children: [
1228
+ "Move ",
1229
+ moveTarget.type === "file" ? "File" : "Folder"
1230
+ ] }),
1231
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: moveTarget.path }),
1232
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { htmlFor: moveInputId, children: "Destination path" }),
1233
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1234
+ "input",
1235
+ {
1236
+ id: moveInputId,
1237
+ autoFocus: true,
1238
+ value: moveDestination,
1239
+ onChange: (event) => setMoveDestination(event.target.value),
1240
+ placeholder: moveTarget.type === "file" ? "src/components/Example.tsx" : "src/components"
1241
+ }
1242
+ ),
1243
+ error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "kai-code__dialog-error", role: "alert", children: error }),
1244
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "kai-code__dialog-actions", children: [
1245
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1246
+ "button",
1247
+ {
1248
+ onClick: () => {
1249
+ setMoveTarget(null);
1250
+ setError(null);
1251
+ },
1252
+ type: "button",
1253
+ children: "Cancel"
1254
+ }
1255
+ ),
1256
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "is-primary", type: "submit", children: "Move" })
1257
+ ] })
1258
+ ]
1259
+ }
1260
+ )
1261
+ }
1262
+ ),
1084
1263
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("footer", { className: "kai-code__statusbar", children: [
1085
1264
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "kai-code__status-left", children: [
1086
1265
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "kai-code__status-branch", children: "\u2442 main" }),
package/dist/index.d.cts CHANGED
@@ -28,6 +28,10 @@ interface KaiCodeFileSystem {
28
28
  deleteFile?(path: string): Promise<void>;
29
29
  /** Must delete the directory and all of its descendants. */
30
30
  deleteDirectory?(path: string): Promise<void>;
31
+ moveFile?(oldPath: string, newPath: string): Promise<void>;
32
+ /** Must move the directory and all of its descendants. */
33
+ moveDirectory?(oldPath: string, newPath: string): Promise<void>;
34
+ /** @deprecated Use `moveFile` instead. */
31
35
  renameFile?(oldPath: string, newPath: string): Promise<void>;
32
36
  watch?(listener: () => void): () => void;
33
37
  }
package/dist/index.d.ts CHANGED
@@ -28,6 +28,10 @@ interface KaiCodeFileSystem {
28
28
  deleteFile?(path: string): Promise<void>;
29
29
  /** Must delete the directory and all of its descendants. */
30
30
  deleteDirectory?(path: string): Promise<void>;
31
+ moveFile?(oldPath: string, newPath: string): Promise<void>;
32
+ /** Must move the directory and all of its descendants. */
33
+ moveDirectory?(oldPath: string, newPath: string): Promise<void>;
34
+ /** @deprecated Use `moveFile` instead. */
31
35
  renameFile?(oldPath: string, newPath: string): Promise<void>;
32
36
  watch?(listener: () => void): () => void;
33
37
  }
package/dist/index.js CHANGED
@@ -35,6 +35,63 @@ function createMemoryFileSystem(initialFiles = {}) {
35
35
  const notify = () => {
36
36
  listeners.forEach((listener) => listener());
37
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
+ };
38
95
  return {
39
96
  async readDirectory(directory = "") {
40
97
  const base = normalizePath(directory);
@@ -146,24 +203,9 @@ function createMemoryFileSystem(initialFiles = {}) {
146
203
  }
147
204
  notify();
148
205
  },
149
- async renameFile(oldPath, newPath) {
150
- const oldNormalized = normalizePath(oldPath);
151
- const newNormalized = normalizePath(newPath);
152
- const contents = files.get(oldNormalized);
153
- if (contents === void 0) {
154
- throw new Error(`File not found: ${oldNormalized}`);
155
- }
156
- if (files.has(newNormalized)) {
157
- throw new Error(`File already exists: ${newNormalized}`);
158
- }
159
- if (!newNormalized || directories.has(newNormalized)) {
160
- throw new Error(`Invalid file path: ${newNormalized}`);
161
- }
162
- files.delete(oldNormalized);
163
- addParentDirectories(newNormalized);
164
- files.set(newNormalized, contents);
165
- notify();
166
- },
206
+ moveFile,
207
+ moveDirectory,
208
+ renameFile: moveFile,
167
209
  watch(listener) {
168
210
  listeners.add(listener);
169
211
  return () => listeners.delete(listener);
@@ -365,6 +407,8 @@ function KaiCode({
365
407
  const [creationRequest, setCreationRequest] = useState(null);
366
408
  const [creationName, setCreationName] = useState("");
367
409
  const [deleteTarget, setDeleteTarget] = useState(null);
410
+ const [moveTarget, setMoveTarget] = useState(null);
411
+ const [moveDestination, setMoveDestination] = useState("");
368
412
  const [loadingTree, setLoadingTree] = useState(true);
369
413
  const [loadingDocument, setLoadingDocument] = useState(false);
370
414
  const [failedDocument, setFailedDocument] = useState(null);
@@ -373,9 +417,12 @@ function KaiCode({
373
417
  const saveRef = useRef(async () => void 0);
374
418
  const activeFileRef = useRef(null);
375
419
  const resizeStartRef = useRef(null);
420
+ const suppressWatchRef = useRef(false);
376
421
  const createInputId = useId();
377
422
  const createTitleId = useId();
378
423
  const deleteTitleId = useId();
424
+ const moveInputId = useId();
425
+ const moveTitleId = useId();
379
426
  const isLight = theme === "light";
380
427
  const reloadFiles = useCallback(async () => {
381
428
  setLoadingTree(true);
@@ -402,6 +449,7 @@ function KaiCode({
402
449
  useEffect(() => {
403
450
  void reloadFiles();
404
451
  return fs.watch?.(() => {
452
+ if (suppressWatchRef.current) return;
405
453
  void reloadFiles();
406
454
  });
407
455
  }, [fs, reloadFiles]);
@@ -439,6 +487,7 @@ function KaiCode({
439
487
  setContextMenu(null);
440
488
  setCreationRequest(null);
441
489
  setDeleteTarget(null);
490
+ setMoveTarget(null);
442
491
  }
443
492
  const modifierPressed = event.ctrlKey || event.metaKey;
444
493
  if (awaitingCollapseChord) {
@@ -538,7 +587,8 @@ function KaiCode({
538
587
  event.preventDefault();
539
588
  event.stopPropagation();
540
589
  const canDelete = entry.type === "file" ? fs.deleteFile : fs.deleteDirectory;
541
- if (readOnly || !fs.createFile && !fs.createDirectory && !canDelete) {
590
+ const canMove = entry.type === "file" ? fs.moveFile || fs.renameFile : fs.moveDirectory;
591
+ if (readOnly || !fs.createFile && !fs.createDirectory && !canDelete && !canMove) {
542
592
  return;
543
593
  }
544
594
  setContextMenu({
@@ -639,6 +689,69 @@ function KaiCode({
639
689
  setError(`Unable to delete ${target.path}: ${errorMessage(deleteError)}`);
640
690
  }
641
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
+ );
642
755
  const editorOptions = useMemo(
643
756
  () => ({
644
757
  automaticLayout: true,
@@ -846,6 +959,20 @@ function KaiCode({
846
959
  children: "New Folder"
847
960
  }
848
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
+ ),
849
976
  (contextMenu.entry.type === "file" && fs.deleteFile || contextMenu.entry.type === "directory" && fs.deleteDirectory) && !readOnly && /* @__PURE__ */ jsxs(Fragment, { children: [
850
977
  /* @__PURE__ */ jsx("div", { className: "kai-code__context-divider" }),
851
978
  /* @__PURE__ */ jsx(
@@ -1049,6 +1176,58 @@ function KaiCode({
1049
1176
  )
1050
1177
  }
1051
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
+ ),
1052
1231
  /* @__PURE__ */ jsxs("footer", { className: "kai-code__statusbar", children: [
1053
1232
  /* @__PURE__ */ jsxs("div", { className: "kai-code__status-left", children: [
1054
1233
  /* @__PURE__ */ jsx("span", { className: "kai-code__status-branch", children: "\u2442 main" }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handylib/kai-code",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "publishConfig": {