@kubohiroya/turbowarp-plus-editor-project-folder 0.12.0 → 0.13.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.
@@ -7,6 +7,11 @@ export declare function resolveExplorerDirectory(root: FileSystemDirectoryHandle
7
7
  export declare function explorerNameAvailable(directory: FileSystemDirectoryHandle, name: string): Promise<boolean>;
8
8
  export declare function createExplorerDirectory(parent: FileSystemDirectoryHandle, name: string, guard: () => void): Promise<void>;
9
9
  export declare function copyExplorerFile(directory: FileSystemDirectoryHandle, file: File, guard: () => void): Promise<void>;
10
+ /**
11
+ * Copy a folder dropped from outside, such as from Finder or File Explorer, into `parent` with everything in it.
12
+ * Entries the tree hides, such as `.git`, are left out. If any part fails, the partial copy is removed.
13
+ */
14
+ export declare function copyExplorerDirectory(parent: FileSystemDirectoryHandle, source: FileSystemDirectoryHandle, guard: () => void): Promise<void>;
10
15
  export declare function renameExplorerEntry(parent: FileSystemDirectoryHandle, entry: {
11
16
  name: string;
12
17
  kind: FileSystemHandleKind;
package/dist/explorer.js CHANGED
@@ -983,6 +983,49 @@ async function copyExplorerFile(directory, file, guard) {
983
983
  validateName(file.name);
984
984
  await writeNewFile(directory, file.name, file, guard);
985
985
  }
986
+ /**
987
+ * Copy a folder dropped from outside, such as from Finder or File Explorer, into `parent` with everything in it.
988
+ * Entries the tree hides, such as `.git`, are left out. If any part fails, the partial copy is removed.
989
+ */
990
+ async function copyExplorerDirectory(parent, source, guard) {
991
+ validateName(source.name);
992
+ await assertUnused(parent, source.name);
993
+ guard();
994
+ const destination = await parent.getDirectoryHandle(source.name, { create: true });
995
+ try {
996
+ await copyDirectoryContents(source, destination, guard);
997
+ } catch (error) {
998
+ await removeIncompleteCopy(parent, source.name);
999
+ throw error;
1000
+ }
1001
+ }
1002
+ async function copyDirectoryContents(source, destination, guard) {
1003
+ var _iteratorAbruptCompletion = false;
1004
+ var _didIteratorError = false;
1005
+ var _iteratorError;
1006
+ try {
1007
+ for (var _iterator = _asyncIterator(source.values()), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
1008
+ const handle = _step.value;
1009
+ if (hiddenEntry(handle.name)) continue;
1010
+ validateName(handle.name);
1011
+ if (handle.kind === "file") {
1012
+ await writeNewFile(destination, handle.name, await handle.getFile(), guard);
1013
+ continue;
1014
+ }
1015
+ guard();
1016
+ await copyDirectoryContents(handle, await destination.getDirectoryHandle(handle.name, { create: true }), guard);
1017
+ }
1018
+ } catch (err) {
1019
+ _didIteratorError = true;
1020
+ _iteratorError = err;
1021
+ } finally {
1022
+ try {
1023
+ if (_iteratorAbruptCompletion && _iterator.return != null) await _iterator.return();
1024
+ } finally {
1025
+ if (_didIteratorError) throw _iteratorError;
1026
+ }
1027
+ }
1028
+ }
986
1029
  async function copyEntry(sourceParent, source, parent, name, guard) {
987
1030
  validateName(source.name);
988
1031
  if (source.kind === "file") {
@@ -1007,25 +1050,25 @@ async function copyEntry(sourceParent, source, parent, name, guard) {
1007
1050
  const directory = source;
1008
1051
  const children = /* @__PURE__ */ new Map();
1009
1052
  try {
1010
- var _iteratorAbruptCompletion = false;
1011
- var _didIteratorError = false;
1012
- var _iteratorError;
1053
+ var _iteratorAbruptCompletion2 = false;
1054
+ var _didIteratorError2 = false;
1055
+ var _iteratorError2;
1013
1056
  try {
1014
- for (var _iterator = _asyncIterator(directory.values()), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
1015
- const handle = _step.value;
1057
+ for (var _iterator2 = _asyncIterator(directory.values()), _step2; _iteratorAbruptCompletion2 = !(_step2 = await _iterator2.next()).done; _iteratorAbruptCompletion2 = false) {
1058
+ const handle = _step2.value;
1016
1059
  children.set(handle.name, {
1017
1060
  handle,
1018
1061
  copy: await copyEntry(directory, handle, destination, handle.name, guard)
1019
1062
  });
1020
1063
  }
1021
1064
  } catch (err) {
1022
- _didIteratorError = true;
1023
- _iteratorError = err;
1065
+ _didIteratorError2 = true;
1066
+ _iteratorError2 = err;
1024
1067
  } finally {
1025
1068
  try {
1026
- if (_iteratorAbruptCompletion && _iterator.return != null) await _iterator.return();
1069
+ if (_iteratorAbruptCompletion2 && _iterator2.return != null) await _iterator2.return();
1027
1070
  } finally {
1028
- if (_didIteratorError) throw _iteratorError;
1071
+ if (_didIteratorError2) throw _iteratorError2;
1029
1072
  }
1030
1073
  }
1031
1074
  } catch (error) {
@@ -1034,12 +1077,12 @@ async function copyEntry(sourceParent, source, parent, name, guard) {
1034
1077
  }
1035
1078
  const verify = async () => {
1036
1079
  let count = 0;
1037
- var _iteratorAbruptCompletion2 = false;
1038
- var _didIteratorError2 = false;
1039
- var _iteratorError2;
1080
+ var _iteratorAbruptCompletion3 = false;
1081
+ var _didIteratorError3 = false;
1082
+ var _iteratorError3;
1040
1083
  try {
1041
- for (var _iterator2 = _asyncIterator(directory.values()), _step2; _iteratorAbruptCompletion2 = !(_step2 = await _iterator2.next()).done; _iteratorAbruptCompletion2 = false) {
1042
- const handle = _step2.value;
1084
+ for (var _iterator3 = _asyncIterator(directory.values()), _step3; _iteratorAbruptCompletion3 = !(_step3 = await _iterator3.next()).done; _iteratorAbruptCompletion3 = false) {
1085
+ const handle = _step3.value;
1043
1086
  {
1044
1087
  const child = children.get(handle.name);
1045
1088
  if (!child || !await handle.isSameEntry(child.handle)) throw new ExplorerFileError("sourceChanged");
@@ -1048,13 +1091,13 @@ async function copyEntry(sourceParent, source, parent, name, guard) {
1048
1091
  }
1049
1092
  }
1050
1093
  } catch (err) {
1051
- _didIteratorError2 = true;
1052
- _iteratorError2 = err;
1094
+ _didIteratorError3 = true;
1095
+ _iteratorError3 = err;
1053
1096
  } finally {
1054
1097
  try {
1055
- if (_iteratorAbruptCompletion2 && _iterator2.return != null) await _iterator2.return();
1098
+ if (_iteratorAbruptCompletion3 && _iterator3.return != null) await _iterator3.return();
1056
1099
  } finally {
1057
- if (_didIteratorError2) throw _iteratorError2;
1100
+ if (_didIteratorError3) throw _iteratorError3;
1058
1101
  }
1059
1102
  }
1060
1103
  if (count !== children.size) throw new ExplorerFileError("sourceChanged");
@@ -1109,24 +1152,24 @@ async function deleteExplorerEntry(parent, entry, guard) {
1109
1152
  validateName(entry.name);
1110
1153
  if (entry.kind === "directory") {
1111
1154
  const check = async (directory) => {
1112
- var _iteratorAbruptCompletion3 = false;
1113
- var _didIteratorError3 = false;
1114
- var _iteratorError3;
1155
+ var _iteratorAbruptCompletion4 = false;
1156
+ var _didIteratorError4 = false;
1157
+ var _iteratorError4;
1115
1158
  try {
1116
- for (var _iterator3 = _asyncIterator(directory.values()), _step3; _iteratorAbruptCompletion3 = !(_step3 = await _iterator3.next()).done; _iteratorAbruptCompletion3 = false) {
1117
- const child = _step3.value;
1159
+ for (var _iterator4 = _asyncIterator(directory.values()), _step4; _iteratorAbruptCompletion4 = !(_step4 = await _iterator4.next()).done; _iteratorAbruptCompletion4 = false) {
1160
+ const child = _step4.value;
1118
1161
  guard();
1119
1162
  validateName(child.name);
1120
1163
  if (child.kind === "directory") await check(child);
1121
1164
  }
1122
1165
  } catch (err) {
1123
- _didIteratorError3 = true;
1124
- _iteratorError3 = err;
1166
+ _didIteratorError4 = true;
1167
+ _iteratorError4 = err;
1125
1168
  } finally {
1126
1169
  try {
1127
- if (_iteratorAbruptCompletion3 && _iterator3.return != null) await _iterator3.return();
1170
+ if (_iteratorAbruptCompletion4 && _iterator4.return != null) await _iterator4.return();
1128
1171
  } finally {
1129
- if (_didIteratorError3) throw _iteratorError3;
1172
+ if (_didIteratorError4) throw _iteratorError4;
1130
1173
  }
1131
1174
  }
1132
1175
  };
@@ -2238,22 +2281,40 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2238
2281
  if (item.kind !== "file") return [];
2239
2282
  const entry = (_item$webkitGetAsEntr = item.webkitGetAsEntry) === null || _item$webkitGetAsEntr === void 0 ? void 0 : _item$webkitGetAsEntr.call(item);
2240
2283
  if (entry === null || entry === void 0 ? void 0 : entry.isDirectory) {
2284
+ var _item$getAsFileSystem;
2285
+ const folder = (_item$getAsFileSystem = item.getAsFileSystemHandle) === null || _item$getAsFileSystem === void 0 ? void 0 : _item$getAsFileSystem.call(item);
2286
+ if (folder) return [{
2287
+ name: entry.name,
2288
+ folder
2289
+ }];
2241
2290
  problems.push(`${entry.name} — ${actions.filesOnly}`);
2242
2291
  return [];
2243
2292
  }
2244
2293
  const file = item.getAsFile();
2245
- return file ? [file] : [];
2246
- }) : Array.from(event.dataTransfer.files);
2294
+ return file ? [{
2295
+ name: file.name,
2296
+ file
2297
+ }] : [];
2298
+ }) : Array.from(event.dataTransfer.files, (file) => ({
2299
+ name: file.name,
2300
+ file
2301
+ }));
2247
2302
  run(async () => {
2248
2303
  if (!dropped.length) return problems.join("\n") || actions.filesOnly;
2249
2304
  try {
2250
2305
  await write(path, async (parent, guard) => {
2251
- for (const file of dropped) {
2306
+ for (const item of dropped) {
2252
2307
  guard();
2253
2308
  try {
2254
- await copyExplorerFile(parent, file, guard);
2309
+ if ("file" in item) {
2310
+ await copyExplorerFile(parent, item.file, guard);
2311
+ continue;
2312
+ }
2313
+ const folder = await item.folder;
2314
+ if ((folder === null || folder === void 0 ? void 0 : folder.kind) !== "directory") throw new DOMException("Not a folder", "NotFoundError");
2315
+ await copyExplorerDirectory(parent, folder, guard);
2255
2316
  } catch (error) {
2256
- problems.push(`${actions.copyFailed} ${file.name} — ${errorText(error)}`);
2317
+ problems.push(`${actions.copyFailed} ${item.name} — ${errorText(error)}`);
2257
2318
  }
2258
2319
  }
2259
2320
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-project-folder",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Read and save expanded Scratch project folders in TurboWarp Plus Editor",
5
5
  "type": "module",
6
6
  "license": "MPL-2.0",
@@ -81,7 +81,39 @@ export async function copyExplorerFile(directory: FileSystemDirectoryHandle, fil
81
81
  await writeNewFile(directory, file.name, file, guard);
82
82
  }
83
83
 
84
- type CopiedEntry = {verify(): Promise<void>; removeSource(): Promise<void>};
84
+ /**
85
+ * Copy a folder dropped from outside, such as from Finder or File Explorer, into `parent` with everything in it.
86
+ * Entries the tree hides, such as `.git`, are left out. If any part fails, the partial copy is removed.
87
+ */
88
+ export async function copyExplorerDirectory(parent: FileSystemDirectoryHandle, source: FileSystemDirectoryHandle,
89
+ guard: () => void) {
90
+ validateName(source.name);
91
+ await assertUnused(parent, source.name);
92
+ guard();
93
+ const destination = await parent.getDirectoryHandle(source.name, {create: true});
94
+ try {
95
+ await copyDirectoryContents(source as DirectoryEntries, destination, guard);
96
+ } catch (error) {
97
+ await removeIncompleteCopy(parent, source.name);
98
+ throw error;
99
+ }
100
+ }
101
+
102
+ async function copyDirectoryContents(source: DirectoryEntries, destination: FileSystemDirectoryHandle, guard: () => void) {
103
+ for await (const handle of source.values()) {
104
+ if (hiddenEntry(handle.name)) continue;
105
+ validateName(handle.name);
106
+ if (handle.kind === 'file') {
107
+ await writeNewFile(destination, handle.name, await (handle as FileSystemFileHandle).getFile(), guard);
108
+ continue;
109
+ }
110
+ guard();
111
+ const directory = await destination.getDirectoryHandle(handle.name, {create: true});
112
+ await copyDirectoryContents(handle as DirectoryEntries, directory, guard);
113
+ }
114
+ }
115
+
116
+ type CopiedEntry ={verify(): Promise<void>; removeSource(): Promise<void>};
85
117
 
86
118
  async function copyEntry(sourceParent: FileSystemDirectoryHandle, source: FileSystemHandle, parent: FileSystemDirectoryHandle,
87
119
  name: string, guard: () => void): Promise<CopiedEntry> {
package/src/explorer.tsx CHANGED
@@ -9,7 +9,7 @@ import {hiddenEntry, readExplorerState, subscribeExplorerState, writeExplorerSta
9
9
  import type {ExplorerTreeState} from './explorer-state.js';
10
10
  import type {ExplorerMutation, FollowedFolder} from './folder-registry.js';
11
11
  import type {Json, Project} from './source.js';
12
- import {canMoveExplorerEntry, copyExplorerFile, createExplorerDirectory, deleteExplorerEntry, ExplorerFileError,
12
+ import {canMoveExplorerEntry, copyExplorerDirectory, copyExplorerFile, createExplorerDirectory, deleteExplorerEntry, ExplorerFileError,
13
13
  explorerNameAvailable, moveExplorerEntry, parentPath, renameExplorerEntry, resolveExplorerDirectory} from './explorer-files.js';
14
14
  import {actionCss, actionMessages, ExplorerFileDialog, ExplorerMenu} from './explorer-menu.js';
15
15
  import type {FileAction, MenuAnchor} from './explorer-menu.js';
@@ -630,24 +630,38 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
630
630
  const path = dropPath(event.target);
631
631
  const items = Array.from(event.dataTransfer.items);
632
632
  const problems: string[] = [];
633
- const dropped = items.length ? items.flatMap(item => {
634
- if (item.kind !== 'file') return [];
635
- const entry = item.webkitGetAsEntry?.();
636
- if (entry?.isDirectory) {
637
- problems.push(`${entry.name} — ${actions.filesOnly}`);
638
- return [];
639
- }
640
- const file = item.getAsFile();
641
- return file ? [file] : [];
642
- }) : Array.from(event.dataTransfer.files);
633
+ // A folder's handle has to be asked for while the drop event runs; the items are emptied after it.
634
+ type Dropped = {name: string; file: File} | {name: string; folder: Promise<FileSystemHandle | null>};
635
+ const dropped: Dropped[] =
636
+ items.length ? items.flatMap((item): Dropped[] => {
637
+ if (item.kind !== 'file') return [];
638
+ const entry = item.webkitGetAsEntry?.();
639
+ if (entry?.isDirectory) {
640
+ const folder = (item as DataTransferItem & {
641
+ getAsFileSystemHandle?(): Promise<FileSystemHandle | null>;
642
+ }).getAsFileSystemHandle?.();
643
+ if (folder) return [{name: entry.name, folder}];
644
+ problems.push(`${entry.name} — ${actions.filesOnly}`);
645
+ return [];
646
+ }
647
+ const file = item.getAsFile();
648
+ return file ? [{name: file.name, file}] : [];
649
+ }) : Array.from(event.dataTransfer.files, file => ({name: file.name, file}));
643
650
  void run(async () => {
644
651
  if (!dropped.length) return problems.join('\n') || actions.filesOnly;
645
652
  try {
646
653
  await write(path, async (parent, guard) => {
647
- for (const file of dropped) {
654
+ for (const item of dropped) {
648
655
  guard();
649
- try {await copyExplorerFile(parent, file, guard);}
650
- catch (error) {problems.push(`${actions.copyFailed} ${file.name} — ${errorText(error)}`);}
656
+ try {
657
+ if ('file' in item) {
658
+ await copyExplorerFile(parent, item.file, guard);
659
+ continue;
660
+ }
661
+ const folder = await item.folder;
662
+ if (folder?.kind !== 'directory') throw new DOMException('Not a folder', 'NotFoundError');
663
+ await copyExplorerDirectory(parent, folder as FileSystemDirectoryHandle, guard);
664
+ } catch (error) {problems.push(`${actions.copyFailed} ${item.name} — ${errorText(error)}`);}
651
665
  }
652
666
  });
653
667
  } finally {