@kubohiroya/turbowarp-plus-editor-project-folder 0.9.0 → 0.9.2

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.
@@ -1,8 +1,11 @@
1
1
  import type { Storage } from './source.js';
2
+ export declare function isProjectFolderEmpty(directory: FileSystemDirectoryHandle): Promise<boolean>;
3
+ export declare function projectFolderOverwriteWarning(locale?: string): "指定されたフォルダに保存されている内容は上書きされます" | "Contents in the selected folder will be overwritten.";
2
4
  /** Create and validate a project-folder snapshot in a directory chosen by the user. */
3
- export declare function createProjectFolder({ directory, files, storage, current }: {
5
+ export declare function createProjectFolder({ directory, files, storage, overwrite, current }: {
4
6
  directory: FileSystemDirectoryHandle;
5
7
  files: ReadonlyMap<string, Uint8Array>;
6
8
  storage: Storage;
9
+ overwrite?: boolean;
7
10
  current?: () => boolean;
8
11
  }): Promise<import("./source.js").FolderSource>;
@@ -1,14 +1,22 @@
1
1
  export declare class ExplorerFileError extends Error {
2
- readonly code: 'invalidName' | 'exists' | 'sourceChanged' | 'originalKept' | 'cleanupFailed';
3
- constructor(code: 'invalidName' | 'exists' | 'sourceChanged' | 'originalKept' | 'cleanupFailed');
2
+ readonly code: 'invalidName' | 'exists' | 'invalidDestination' | 'sourceChanged' | 'originalKept' | 'cleanupFailed';
3
+ constructor(code: 'invalidName' | 'exists' | 'invalidDestination' | 'sourceChanged' | 'originalKept' | 'cleanupFailed');
4
4
  }
5
5
  export declare function parentPath(path: string): string;
6
6
  export declare function resolveExplorerDirectory(root: FileSystemDirectoryHandle, path: string): Promise<FileSystemDirectoryHandle>;
7
+ export declare function explorerNameAvailable(directory: FileSystemDirectoryHandle, name: string): Promise<boolean>;
8
+ export declare function createExplorerDirectory(parent: FileSystemDirectoryHandle, name: string, guard: () => void): Promise<void>;
7
9
  export declare function copyExplorerFile(directory: FileSystemDirectoryHandle, file: File, guard: () => void): Promise<void>;
8
10
  export declare function renameExplorerEntry(parent: FileSystemDirectoryHandle, entry: {
9
11
  name: string;
10
12
  kind: FileSystemHandleKind;
11
13
  }, name: string, guard: () => void): Promise<void>;
14
+ export declare function canMoveExplorerEntry(path: string, destination: string): boolean;
15
+ export declare function moveExplorerEntry(root: FileSystemDirectoryHandle, entry: {
16
+ path: string;
17
+ name: string;
18
+ kind: FileSystemHandleKind;
19
+ }, destination: FileSystemDirectoryHandle, destinationPath: string, guard: () => void): Promise<void>;
12
20
  export declare function deleteExplorerEntry(parent: FileSystemDirectoryHandle, entry: {
13
21
  name: string;
14
22
  kind: FileSystemHandleKind;
@@ -5,6 +5,8 @@ export declare const actionMessages: (locale?: string) => {
5
5
  renameTitle: string;
6
6
  delete: string;
7
7
  deleteTitle: string;
8
+ createFolder: string;
9
+ folderName: string;
8
10
  newName: string;
9
11
  cancel: string;
10
12
  remove: (name: string, folder: boolean) => string;
@@ -15,6 +17,7 @@ export declare const actionMessages: (locale?: string) => {
15
17
  filesOnly: string;
16
18
  invalidName: string;
17
19
  exists: string;
20
+ invalidDestination: string;
18
21
  denied: string;
19
22
  conflict: string;
20
23
  sourceChanged: string;
@@ -28,7 +31,8 @@ export type MenuAnchor = {
28
31
  x: number;
29
32
  y: number;
30
33
  };
31
- export declare const actionCss = "\n.twpf-explorer-actions{position:fixed;z-index:2147483647;max-width:calc(100vw - 8px);padding:4px;\nborder:1px solid GrayText;border-radius:5px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}\n.twpf-explorer-actions button{display:block;width:100%;border:0;padding:.35em .65em;text-align:start;background:Canvas;color:CanvasText;\nfont:inherit;cursor:pointer;border-radius:3px}\n.twpf-explorer-actions button:is(:hover,:focus-visible){background:Highlight;color:HighlightText}\n.twpf-explorer-dialog{max-width:min(28em,calc(100vw - 2em));color:CanvasText;background:Canvas;\nborder:1px solid GrayText;border-radius:6px;padding:1em;overflow-wrap:anywhere}\n.twpf-explorer-dialog::backdrop{background:#0006}\n.twpf-explorer-dialog h2{font-size:1.1em;margin:0 0 .75em}\n.twpf-explorer-dialog input{display:block;width:100%;box-sizing:border-box;margin-top:.3em}\n.twpf-explorer-dialog-footer{display:flex;justify-content:flex-end;gap:.5em;margin-top:1em}\n.twpf-explorer-drop{min-height:100%;box-sizing:border-box}\n.twpf-explorer [data-drop-active],.twpf-explorer[data-drop-active]{outline:2px solid Highlight;outline-offset:-2px}\n.twpf-explorer-feedback{white-space:pre-wrap;overflow-wrap:anywhere;padding:.25em .5em}\n";
34
+ export type FileAction = 'createFolder' | 'rename' | 'delete';
35
+ export declare const actionCss = "\n.twpf-explorer-actions{position:fixed;z-index:2147483647;max-width:calc(100vw - 8px);padding:4px;\nborder:1px solid GrayText;border-radius:5px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}\n.twpf-explorer-actions button{display:block;width:100%;border:0;padding:.35em .65em;text-align:start;background:Canvas;color:CanvasText;\nfont:inherit;cursor:pointer;border-radius:3px}\n.twpf-explorer-actions button:is(:hover,:focus-visible){background:Highlight;color:HighlightText}\n.twpf-explorer-dialog{max-width:min(28em,calc(100vw - 2em));color:CanvasText;background:Canvas;\nborder:1px solid GrayText;border-radius:6px;padding:1em;overflow-wrap:anywhere}\n.twpf-explorer-dialog::backdrop{background:#0006}\n.twpf-explorer-dialog h2{font-size:1.1em;margin:0 0 .75em}\n.twpf-explorer-dialog input{display:block;width:100%;box-sizing:border-box;margin-top:.3em}\n.twpf-explorer-dialog-footer{display:flex;justify-content:flex-end;gap:.5em;margin-top:1em}\n.twpf-explorer-drop{min-height:100%;box-sizing:border-box}\n.twpf-explorer [data-drop-active],.twpf-explorer[data-drop-active]{outline:2px solid Highlight;outline-offset:-2px}\n.twpf-explorer-item[data-move-unavailable]{color:GrayText;opacity:.65;filter:grayscale(1)}\n.twpf-explorer-item[data-move-active] .twpf-explorer-name{font-weight:700}\n.twpf-explorer-drag-image{position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;\ndisplay:flex;align-items:center;gap:.4em;max-width:18em;white-space:nowrap;padding:.3em .5em;\nborder:1px solid GrayText;border-radius:4px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}\n.twpf-explorer-feedback{white-space:pre-wrap;overflow-wrap:anywhere;padding:.25em .5em}\n";
32
36
  export declare function ExplorerMenu({ anchor, label, actions, close }: {
33
37
  anchor: MenuAnchor;
34
38
  label: string;
@@ -38,11 +42,11 @@ export declare function ExplorerMenu({ anchor, label, actions, close }: {
38
42
  }[];
39
43
  close(restoreFocus: boolean): void;
40
44
  }): React.ReactPortal;
41
- export declare function ExplorerFileDialog({ anchor, name, folder, rename, message, close, submit }: {
45
+ export declare function ExplorerFileDialog({ anchor, name, folder, action, message, close, submit }: {
42
46
  anchor: MenuAnchor;
43
47
  name: string;
44
48
  folder: boolean;
45
- rename: boolean;
49
+ action: FileAction;
46
50
  message: ActionMessages;
47
51
  close(): void;
48
52
  submit(name: string): void;
@@ -53,7 +53,7 @@ export declare function describeProjectFolder(project: Project, locale?: string,
53
53
  folder?: string;
54
54
  }): Map<string, string>;
55
55
  /**
56
- * A read-only tree of a folder for the Editor's side panel, built on Headless Tree. Folders load when expanded, folders
56
+ * A tree of a folder for the Editor's side panel, built on Headless Tree. Folders load when expanded, folders
57
57
  * sort before files, and `.git` is hidden. Keyboard: arrow keys, Home and End move and expand; Enter or Space opens a
58
58
  * file. Needs React 16.14–18 from the host.
59
59
  */
@@ -64,6 +64,8 @@ export type FollowFileStatuses = (directory: FileSystemDirectoryHandle, update:
64
64
  type FolderViewProps = {
65
65
  vm: object;
66
66
  locale?: string;
67
+ /** Saves the current project to a local folder. */
68
+ createFolder?(): Promise<unknown>;
67
69
  marks?: FollowMarks;
68
70
  fileStatuses?: FollowFileStatuses;
69
71
  git?: FollowExplorerGit;
@@ -76,7 +78,8 @@ export type { FileBaseline, FilePreviewRequest, ReadFileBaseline } from './file-
76
78
  /**
77
79
  * Renders the explorer of the project folder open for `vm`, for a side panel of the host: its files with what each is
78
80
  * for, and `marks` (such as the Git status) when given. It follows the folder as it opens, closes and saves; with no
79
- * folder open it offers Open Project Folder…, and it renders nothing while no project folder is mounted for `vm`.
81
+ * folder open it offers to create one when the host supplies a command, or to open an existing one.
82
+ * It renders nothing while no project folder is mounted for `vm`.
80
83
  * Pass the same `marks` function across renders, or the marks are followed again.
81
84
  */
82
- export declare function renderProjectFolderExplorer(createElement: (component: any, props: any, ...children: any[]) => any, { vm, locale, marks, fileStatuses, git, openFile, readBaseline }: FolderViewProps): any;
85
+ export declare function renderProjectFolderExplorer(createElement: (component: any, props: any, ...children: any[]) => any, props: FolderViewProps): any;
package/dist/explorer.js CHANGED
@@ -1,4 +1,4 @@
1
- import { C as _asyncIterator, S as _objectSpread2, d as manifestFile, g as projectFile, r as FolderError, t as followProjectFolder, u as isObject } from "./shared-JdaDvk2R.js";
1
+ import { E as _asyncIterator, T as _objectSpread2, a as projectFolderOverwriteWarning, m as manifestFile, o as FolderError, p as isObject, t as followProjectFolder, y as projectFile } from "./shared-Dz9dzxQF.js";
2
2
  import * as React from "react";
3
3
  import { useEffect, useState } from "react";
4
4
  import { createPortal } from "react-dom";
@@ -936,14 +936,23 @@ async function resolveExplorerDirectory(root, path) {
936
936
  if (((_await$root$resolve = await root.resolve(directory)) === null || _await$root$resolve === void 0 ? void 0 : _await$root$resolve.join("/")) !== path) throw new ExplorerFileError("sourceChanged");
937
937
  return directory;
938
938
  }
939
- async function assertUnused(directory, name) {
939
+ async function explorerNameAvailable(directory, name) {
940
940
  try {
941
941
  await directory.getFileHandle(name);
942
942
  } catch (error) {
943
- if (error instanceof DOMException && error.name === "NotFoundError") return;
943
+ if (error instanceof DOMException && error.name === "NotFoundError") return true;
944
944
  if (!(error instanceof DOMException) || error.name !== "TypeMismatchError") throw error;
945
945
  }
946
- throw new ExplorerFileError("exists");
946
+ return false;
947
+ }
948
+ async function assertUnused(directory, name) {
949
+ if (!await explorerNameAvailable(directory, name)) throw new ExplorerFileError("exists");
950
+ }
951
+ async function createExplorerDirectory(parent, name, guard) {
952
+ validateName(name);
953
+ await assertUnused(parent, name);
954
+ guard();
955
+ await parent.getDirectoryHandle(name, { create: true });
947
956
  }
948
957
  var sameVersion = (a, b) => a.size === b.size && a.lastModified === b.lastModified;
949
958
  async function writeNewFile(directory, name, file, guard) {
@@ -1068,13 +1077,26 @@ async function removeIncompleteCopy(parent, name) {
1068
1077
  }
1069
1078
  async function renameExplorerEntry(parent, entry, name, guard) {
1070
1079
  if (name === entry.name) return;
1080
+ await relocateEntry(parent, entry, parent, name, guard);
1081
+ }
1082
+ function canMoveExplorerEntry(path, destination) {
1083
+ return parentPath(path) !== destination && path !== destination && !destination.startsWith(`${path}/`);
1084
+ }
1085
+ async function moveExplorerEntry(root, entry, destination, destinationPath, guard) {
1086
+ if (!canMoveExplorerEntry(entry.path, destinationPath)) throw new ExplorerFileError("invalidDestination");
1087
+ const parent = await resolveExplorerDirectory(root, parentPath(entry.path));
1088
+ guard();
1089
+ await relocateEntry(parent, entry, destination, entry.name, guard);
1090
+ }
1091
+ async function relocateEntry(sourceParent, entry, destination, name, guard) {
1092
+ validateName(entry.name);
1071
1093
  validateName(name);
1072
- const copy = await copyEntry(parent, entry.kind === "file" ? await parent.getFileHandle(entry.name) : await parent.getDirectoryHandle(entry.name), parent, name, guard);
1094
+ const copy = await copyEntry(sourceParent, entry.kind === "file" ? await sourceParent.getFileHandle(entry.name) : await sourceParent.getDirectoryHandle(entry.name), destination, name, guard);
1073
1095
  try {
1074
1096
  await copy.verify();
1075
1097
  guard();
1076
1098
  } catch (error) {
1077
- await removeIncompleteCopy(parent, name);
1099
+ await removeIncompleteCopy(destination, name);
1078
1100
  throw error;
1079
1101
  }
1080
1102
  try {
@@ -1120,6 +1142,8 @@ var actionMessages = (locale = "en") => locale.toLowerCase().startsWith("ja") ?
1120
1142
  renameTitle: "名前を変更",
1121
1143
  delete: "削除…",
1122
1144
  deleteTitle: "削除",
1145
+ createFolder: "新規フォルダを作成",
1146
+ folderName: "フォルダ名",
1123
1147
  newName: "新しい名前",
1124
1148
  cancel: "キャンセル",
1125
1149
  remove: (name, folder) => folder ? `「${name}」とその中身を削除しますか?` : `「${name}」を削除しますか?`,
@@ -1130,6 +1154,7 @@ var actionMessages = (locale = "en") => locale.toLowerCase().startsWith("ja") ?
1130
1154
  filesOnly: "ファイルをドロップしてください。",
1131
1155
  invalidName: "この名前は使用できません。",
1132
1156
  exists: "同じ名前のファイルまたはフォルダがあります。",
1157
+ invalidDestination: "このフォルダには移動できません。",
1133
1158
  denied: "このフォルダへの書き込みが許可されていません。",
1134
1159
  conflict: "プロジェクトフォルダが変更されたか、別の操作が実行中です。",
1135
1160
  sourceChanged: "操作中に対象が変更されました。",
@@ -1142,6 +1167,8 @@ var actionMessages = (locale = "en") => locale.toLowerCase().startsWith("ja") ?
1142
1167
  renameTitle: "Rename",
1143
1168
  delete: "Delete…",
1144
1169
  deleteTitle: "Delete",
1170
+ createFolder: "Create New Folder",
1171
+ folderName: "Folder name",
1145
1172
  newName: "New name",
1146
1173
  cancel: "Cancel",
1147
1174
  remove: (name, folder) => folder ? `Delete “${name}” and its contents?` : `Delete “${name}”?`,
@@ -1152,6 +1179,7 @@ var actionMessages = (locale = "en") => locale.toLowerCase().startsWith("ja") ?
1152
1179
  filesOnly: "Drop files here.",
1153
1180
  invalidName: "This name cannot be used.",
1154
1181
  exists: "A file or folder with this name already exists.",
1182
+ invalidDestination: "This item cannot be moved to that folder.",
1155
1183
  denied: "Permission to write to this folder was not granted.",
1156
1184
  conflict: "The project folder changed or another operation is running.",
1157
1185
  sourceChanged: "The item changed during the operation.",
@@ -1173,6 +1201,11 @@ border:1px solid GrayText;border-radius:6px;padding:1em;overflow-wrap:anywhere}
1173
1201
  .twpf-explorer-dialog-footer{display:flex;justify-content:flex-end;gap:.5em;margin-top:1em}
1174
1202
  .twpf-explorer-drop{min-height:100%;box-sizing:border-box}
1175
1203
  .twpf-explorer [data-drop-active],.twpf-explorer[data-drop-active]{outline:2px solid Highlight;outline-offset:-2px}
1204
+ .twpf-explorer-item[data-move-unavailable]{color:GrayText;opacity:.65;filter:grayscale(1)}
1205
+ .twpf-explorer-item[data-move-active] .twpf-explorer-name{font-weight:700}
1206
+ .twpf-explorer-drag-image{position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;
1207
+ display:flex;align-items:center;gap:.4em;max-width:18em;white-space:nowrap;padding:.3em .5em;
1208
+ border:1px solid GrayText;border-radius:4px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}
1176
1209
  .twpf-explorer-feedback{white-space:pre-wrap;overflow-wrap:anywhere;padding:.25em .5em}
1177
1210
  `;
1178
1211
  function ExplorerMenu({ anchor, label, actions, close }) {
@@ -1233,11 +1266,11 @@ function ExplorerMenu({ anchor, label, actions, close }) {
1233
1266
  onClick: action.run
1234
1267
  }, action.label))), document.body);
1235
1268
  }
1236
- function ExplorerFileDialog({ anchor, name, folder, rename, message, close, submit }) {
1269
+ function ExplorerFileDialog({ anchor, name, folder, action, message, close, submit }) {
1237
1270
  const ref = React.useRef(null);
1238
1271
  const input = React.useRef(null);
1239
1272
  const cancel = React.useRef(null);
1240
- const title = rename ? message.renameTitle : message.deleteTitle;
1273
+ const title = action === "createFolder" ? message.createFolder : action === "rename" ? message.renameTitle : message.deleteTitle;
1241
1274
  React.useLayoutEffect(() => {
1242
1275
  var _cancel$current;
1243
1276
  const dialog = ref.current;
@@ -1263,11 +1296,11 @@ function ExplorerFileDialog({ anchor, name, folder, rename, message, close, subm
1263
1296
  var _input$current$value, _input$current;
1264
1297
  event.preventDefault();
1265
1298
  submit((_input$current$value = (_input$current = input.current) === null || _input$current === void 0 ? void 0 : _input$current.value) !== null && _input$current$value !== void 0 ? _input$current$value : name);
1266
- } }, /* @__PURE__ */ React.createElement("h2", null, title), rename ? /* @__PURE__ */ React.createElement("label", null, message.newName, /* @__PURE__ */ React.createElement("input", {
1299
+ } }, /* @__PURE__ */ React.createElement("h2", null, title), action === "delete" ? /* @__PURE__ */ React.createElement("p", null, message.remove(name, folder)) : /* @__PURE__ */ React.createElement("label", null, action === "rename" ? message.newName : message.folderName, /* @__PURE__ */ React.createElement("input", {
1267
1300
  ref: input,
1268
- defaultValue: name,
1301
+ defaultValue: action === "rename" ? name : "",
1269
1302
  required: true
1270
- })) : /* @__PURE__ */ React.createElement("p", null, message.remove(name, folder)), /* @__PURE__ */ React.createElement("div", { className: "twpf-explorer-dialog-footer" }, /* @__PURE__ */ React.createElement("button", {
1303
+ })), /* @__PURE__ */ React.createElement("div", { className: "twpf-explorer-dialog-footer" }, /* @__PURE__ */ React.createElement("button", {
1271
1304
  ref: cancel,
1272
1305
  type: "button",
1273
1306
  onClick: close
@@ -1387,7 +1420,7 @@ function createFilePreview(directory, documentId, { name, path }, locale, readBa
1387
1420
  status.textContent = message.large;
1388
1421
  return;
1389
1422
  }
1390
- const [bytes, { decodeText, mountTextFile }] = await Promise.all([file.arrayBuffer(), import("./shared-kI7qYraF.js")]);
1423
+ const [bytes, { decodeText, mountTextFile }] = await Promise.all([file.arrayBuffer(), import("./shared-Dco5rGKE.js")]);
1391
1424
  if (disposed) return;
1392
1425
  const content = decodeText(bytes);
1393
1426
  if (content === null) {
@@ -1433,6 +1466,9 @@ var messages = {
1433
1466
  empty: "This folder is empty.",
1434
1467
  noFolder: "Open a project folder to show its files here.",
1435
1468
  open: "Open Project Folder…",
1469
+ create: "Create Project Folder",
1470
+ creating: "Creating project folder…",
1471
+ createFailed: "Could not create the project folder.",
1436
1472
  unavailable: "File tabs are unavailable in this editor layout.",
1437
1473
  failed: "This folder could not be read.",
1438
1474
  denied: "Permission to read this folder was not granted.",
@@ -1466,6 +1502,9 @@ var messages = {
1466
1502
  empty: "このフォルダは空です。",
1467
1503
  noFolder: "プロジェクトフォルダを開くと、ここにファイルを表示します。",
1468
1504
  open: "プロジェクトフォルダを開く…",
1505
+ create: "プロジェクトフォルダを作成",
1506
+ creating: "プロジェクトフォルダを作成しています…",
1507
+ createFailed: "プロジェクトフォルダを作成できませんでした。",
1469
1508
  unavailable: "このエディターのレイアウトではファイルをタブで開けません。",
1470
1509
  failed: "このフォルダを読み込めません。",
1471
1510
  denied: "このフォルダを読む許可がありません。",
@@ -1540,8 +1579,10 @@ var rootId = "/";
1540
1579
  var errorId = (parent) => `${parent}\0error`;
1541
1580
  var failure = (error, message) => error instanceof DOMException && (error.name === "NotAllowedError" || error.name === "SecurityError") ? message.denied : message.failed;
1542
1581
  var prefix = "twpf-explorer";
1582
+ var entryDragType = "application/x-twpf-explorer-entry";
1543
1583
  var css = `.${prefix}{font:inherit;color:inherit}
1544
- .${prefix}-item{display:flex;align-items:center;gap:.4em;width:100%;box-sizing:border-box;margin:0;border:0;border-radius:3px;
1584
+ .${prefix}-item{display:grid;grid-template-columns:1em 20px minmax(0,1fr);align-items:center;gap:.2em .4em;
1585
+ width:100%;box-sizing:border-box;margin:0;border:0;border-radius:3px;
1545
1586
  padding-block:.15em;padding-inline-end:.4em;background:none;color:inherit;font:inherit;text-align:start;cursor:pointer;white-space:nowrap}
1546
1587
  .${prefix}-item:hover{background:rgba(128,128,128,.12)}
1547
1588
  .${prefix}-item[aria-selected="true"]{background:rgba(128,128,128,.24)}
@@ -1551,6 +1592,7 @@ padding-block:.15em;padding-inline-end:.4em;background:none;color:inherit;font:i
1551
1592
  .${prefix}-icon img{width:20px;height:20px;object-fit:contain;border-radius:2px;
1552
1593
  background:repeating-conic-gradient(rgba(128,128,128,.22) 0 25%,transparent 0 50%) 0 0/8px 8px}
1553
1594
  .${prefix}-icon img[hidden]{display:none}
1595
+ .${prefix}-label{grid-column:3;display:flex;align-items:center;gap:.4em;min-width:0}
1554
1596
  .${prefix}-filename{display:inline-flex;min-width:0}
1555
1597
  .${prefix}-name{overflow:hidden;text-overflow:ellipsis}
1556
1598
  .${prefix}-item[data-staged] .${prefix}-filename{font-weight:700}
@@ -1559,7 +1601,9 @@ background:repeating-conic-gradient(rgba(128,128,128,.22) 0 25%,transparent 0 50
1559
1601
  .${prefix}-item[data-revision="merged"] .${prefix}-filename{color:#17603e;color:light-dark(#17603e,#85e3ad)}
1560
1602
  .${prefix}-unsaved{flex:none}
1561
1603
  .${prefix}-mark{flex:none;font-size:.85em;padding:0 .3em;border:1px solid currentColor;border-radius:3px;opacity:.85}
1562
- .${prefix}-description{overflow:hidden;text-overflow:ellipsis;opacity:.7;font-size:.9em}
1604
+ .${prefix}-description{grid-column:3;min-width:0;padding:.2em .4em;border-radius:3px;background:rgba(128,128,128,.12);
1605
+ color:inherit;color:color-mix(in srgb,currentColor 78%,transparent);font-size:.75em;line-height:1.4;
1606
+ white-space:normal;overflow-wrap:anywhere}
1563
1607
  .${prefix}-status{opacity:.7;font-style:italic;padding-block:.15em}
1564
1608
  .${prefix}-empty{padding:.5em}
1565
1609
  .${prefix}-empty p{margin:0 0 .6em}
@@ -1697,6 +1741,7 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
1697
1741
  const [loadedFolders] = React.useState(() => /* @__PURE__ */ new Set());
1698
1742
  const [rootLoaded, setRootLoaded] = React.useState(false);
1699
1743
  const [drag, setDrag] = React.useState(null);
1744
+ const [move, setMove] = React.useState();
1700
1745
  const [menu, setMenu] = React.useState();
1701
1746
  const [dialog, setDialog] = React.useState();
1702
1747
  const [feedback, setFeedback] = React.useState("");
@@ -1907,8 +1952,79 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
1907
1952
  var _target$closest$getAt, _target$closest;
1908
1953
  return (_target$closest$getAt = (_target$closest = target.closest("[data-drop-directory]")) === null || _target$closest === void 0 ? void 0 : _target$closest.getAttribute("data-drop-directory")) !== null && _target$closest$getAt !== void 0 ? _target$closest$getAt : "";
1909
1954
  };
1955
+ const moveDropPath = (target) => {
1956
+ const row = target.closest(`.${prefix}-item`);
1957
+ const path = row ? row.getAttribute("data-move-directory") : "";
1958
+ return path !== null && (move === null || move === void 0 ? void 0 : move.destinations.has(path)) ? path : null;
1959
+ };
1960
+ const startMove = (event, entry) => {
1961
+ var _document$defaultView;
1962
+ if (running.current || dialog) {
1963
+ event.preventDefault();
1964
+ return;
1965
+ }
1966
+ event.stopPropagation();
1967
+ setMenu(void 0);
1968
+ setDrag(null);
1969
+ event.dataTransfer.effectAllowed = "move";
1970
+ event.dataTransfer.setData(entryDragType, entry.path);
1971
+ const row = event.currentTarget;
1972
+ const document = row.ownerDocument;
1973
+ const image = document.createElement("div");
1974
+ image.className = `${prefix}-drag-image`;
1975
+ const icon = row.querySelector(`.${prefix}-icon`);
1976
+ if (icon) image.appendChild(icon.cloneNode(true));
1977
+ const name = document.createElement("span");
1978
+ name.className = `${prefix}-name`;
1979
+ name.textContent = entry.name;
1980
+ image.appendChild(name);
1981
+ document.body.appendChild(image);
1982
+ event.dataTransfer.setDragImage(image, -12, -12);
1983
+ (_document$defaultView = document.defaultView) === null || _document$defaultView === void 0 || _document$defaultView.requestAnimationFrame(() => image.remove());
1984
+ setMove({
1985
+ entry,
1986
+ destinations: /* @__PURE__ */ new Set()
1987
+ });
1988
+ const candidates = ["", ...tree.getItems().filter((item) => item.isFolder()).map((item) => item.getId())];
1989
+ Promise.all(candidates.filter((path) => canMoveExplorerEntry(entry.path, path)).map(async (path) => {
1990
+ try {
1991
+ return await explorerNameAvailable(await directoryAt(path || rootId), entry.name) ? path : null;
1992
+ } catch (_unused3) {
1993
+ return null;
1994
+ }
1995
+ })).then((paths) => {
1996
+ if (mounted.current) setMove((current) => (current === null || current === void 0 ? void 0 : current.entry) === entry ? {
1997
+ entry,
1998
+ destinations: new Set(paths.filter((path) => path !== null))
1999
+ } : current);
2000
+ });
2001
+ };
2002
+ const endMove = () => {
2003
+ setMove(void 0);
2004
+ setDrag(null);
2005
+ };
2006
+ const dropMove = (event, entry) => {
2007
+ event.preventDefault();
2008
+ event.stopPropagation();
2009
+ const path = moveDropPath(event.target);
2010
+ endMove();
2011
+ if (path === null || event.dataTransfer.getData(entryDragType) !== entry.path) return;
2012
+ run(async () => {
2013
+ let moved = false;
2014
+ try {
2015
+ await write(path, (destination, guard) => moveExplorerEntry(directory, entry, destination, path, guard));
2016
+ moved = true;
2017
+ } finally {
2018
+ if (mounted.current) forget(entry.path);
2019
+ await reread(parentPath(entry.path));
2020
+ await reread(path, moved);
2021
+ await (git === null || git === void 0 ? void 0 : git.refresh());
2022
+ }
2023
+ });
2024
+ };
1910
2025
  const acceptsFiles = (event) => Boolean(mutate && event.dataTransfer.types.includes("Files"));
1911
2026
  const drop = (event) => {
2027
+ if (move && event.dataTransfer.types.includes(entryDragType)) return dropMove(event, move.entry);
1912
2028
  if (!acceptsFiles(event)) return;
1913
2029
  event.preventDefault();
1914
2030
  event.stopPropagation();
@@ -1969,16 +2085,22 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
1969
2085
  };
1970
2086
  const submitFileAction = (name) => {
1971
2087
  if (!dialog) return;
1972
- const { entry, rename } = dialog;
2088
+ const { entry, action } = dialog;
1973
2089
  setDialog(void 0);
1974
- if (rename && entry.name === name) return;
2090
+ if (action === "rename" && entry.name === name) return;
1975
2091
  run(async () => {
1976
- const parent = parentPath(entry.path);
2092
+ const parent = action === "createFolder" ? entry.path : parentPath(entry.path);
2093
+ let changed = false;
1977
2094
  try {
1978
- await write(parent, (handle, guard) => rename ? renameExplorerEntry(handle, entry, name, guard) : deleteExplorerEntry(handle, entry, guard));
2095
+ await write(parent, (handle, guard) => {
2096
+ if (action === "createFolder") return createExplorerDirectory(handle, name, guard);
2097
+ if (action === "rename") return renameExplorerEntry(handle, entry, name, guard);
2098
+ return deleteExplorerEntry(handle, entry, guard);
2099
+ });
2100
+ changed = true;
1979
2101
  } finally {
1980
- if (mounted.current) forget(entry.path);
1981
- await reread(parent);
2102
+ if (mounted.current && action !== "createFolder") forget(entry.path);
2103
+ await reread(parent, action === "createFolder" && changed);
1982
2104
  await (git === null || git === void 0 ? void 0 : git.refresh());
1983
2105
  }
1984
2106
  });
@@ -2009,6 +2131,8 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2009
2131
  markLabel,
2010
2132
  description
2011
2133
  ].filter(Boolean).join(message.separator);
2134
+ const moveDestination = (entry === null || entry === void 0 ? void 0 : entry.kind) === "directory" && (move === null || move === void 0 ? void 0 : move.destinations.has(entry.path));
2135
+ const moveActive = moveDestination && drag === entry.path;
2012
2136
  const rows = [/* @__PURE__ */ React.createElement("button", _objectSpread2(_objectSpread2({}, itemProps), {}, {
2013
2137
  key: item.getId(),
2014
2138
  type: "button",
@@ -2018,9 +2142,15 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2018
2142
  "data-path": data === null || data === void 0 ? void 0 : data.path,
2019
2143
  "data-staged": (status === null || status === void 0 ? void 0 : status.staged) ? "" : void 0,
2020
2144
  "data-revision": status === null || status === void 0 ? void 0 : status.revision,
2021
- title: unsaved || (status === null || status === void 0 ? void 0 : status.staged) || (status === null || status === void 0 ? void 0 : status.revision) ? accessibleName : void 0,
2145
+ title: accessibleName || void 0,
2022
2146
  "data-drop-directory": (entry === null || entry === void 0 ? void 0 : entry.kind) === "directory" ? entry.path : entry ? parentPath(entry.path) : void 0,
2023
2147
  "data-drop-active": (entry === null || entry === void 0 ? void 0 : entry.kind) === "directory" && drag === entry.path ? "" : void 0,
2148
+ "data-move-directory": (entry === null || entry === void 0 ? void 0 : entry.kind) === "directory" ? entry.path : void 0,
2149
+ "data-move-unavailable": move && !moveDestination ? "" : void 0,
2150
+ "data-move-active": moveActive ? "" : void 0,
2151
+ draggable: entry && mutate ? !working : void 0,
2152
+ onDragStart: entry && mutate ? (event) => startMove(event, entry) : void 0,
2153
+ onDragEnd: entry && mutate ? endMove : void 0,
2024
2154
  "aria-haspopup": entry && (mutate || git) ? "menu" : void 0,
2025
2155
  onContextMenu: entry ? (event) => showMenu(event, entry) : void 0,
2026
2156
  onKeyDown: entry ? (event) => {
@@ -2035,13 +2165,13 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2035
2165
  kind: icon,
2036
2166
  path: data.path,
2037
2167
  readFile: icon === "image" ? readFile : void 0
2038
- }) : null, /* @__PURE__ */ React.createElement("span", { className: `${prefix}-filename` }, /* @__PURE__ */ React.createElement("span", { className: `${prefix}-name` }, item.getItemName()), unsaved ? /* @__PURE__ */ React.createElement("span", {
2168
+ }) : null, /* @__PURE__ */ React.createElement("span", { className: `${prefix}-label` }, /* @__PURE__ */ React.createElement("span", { className: `${prefix}-filename` }, /* @__PURE__ */ React.createElement("span", { className: `${prefix}-name` }, item.getItemName()), unsaved ? /* @__PURE__ */ React.createElement("span", {
2039
2169
  className: `${prefix}-unsaved`,
2040
2170
  "aria-hidden": "true"
2041
2171
  }, "*") : null), markLabel ? /* @__PURE__ */ React.createElement("span", {
2042
2172
  className: `${prefix}-mark`,
2043
2173
  "aria-hidden": "true"
2044
- }, markLabel) : null, description ? /* @__PURE__ */ React.createElement("span", {
2174
+ }, markLabel) : null), description ? /* @__PURE__ */ React.createElement("span", {
2045
2175
  className: `${prefix}-description`,
2046
2176
  "aria-hidden": "true"
2047
2177
  }, description) : null)];
@@ -2059,6 +2189,14 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2059
2189
  "data-drop-active": drag === "" ? "" : void 0,
2060
2190
  onDrop: drop,
2061
2191
  onDragOver: (event) => {
2192
+ if (move && event.dataTransfer.types.includes(entryDragType)) {
2193
+ const path = running.current ? null : moveDropPath(event.target);
2194
+ event.stopPropagation();
2195
+ if (path !== null) event.preventDefault();
2196
+ event.dataTransfer.dropEffect = path === null ? "none" : "move";
2197
+ setDrag(path);
2198
+ return;
2199
+ }
2062
2200
  if (!acceptsFiles(event)) return;
2063
2201
  event.preventDefault();
2064
2202
  event.stopPropagation();
@@ -2078,34 +2216,44 @@ function Explorer({ directory, locale, descriptions, marks, fileStatuses, unsave
2078
2216
  anchor: menu.anchor,
2079
2217
  label: actions.actions,
2080
2218
  close: closeMenu,
2081
- actions: [...mutate ? [{
2082
- label: actions.rename,
2083
- run: () => {
2084
- closeMenu(false);
2085
- setDialog(_objectSpread2(_objectSpread2({}, menu), {}, { rename: true }));
2086
- }
2087
- }, {
2088
- label: actions.delete,
2089
- run: () => {
2090
- closeMenu(false);
2091
- setDialog(_objectSpread2(_objectSpread2({}, menu), {}, { rename: false }));
2092
- }
2093
- }] : [], ...git ? ["stage", "unstage"].map((kind) => ({
2094
- label: actions[kind],
2095
- run: () => {
2096
- closeMenu(true);
2097
- run(async () => {
2098
- var _result$error;
2099
- const result = await git.change(kind, menu.entry, isCurrent !== null && isCurrent !== void 0 ? isCurrent : (() => mounted.current));
2100
- if (!result.done) return (_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : actions.conflict;
2101
- });
2102
- }
2103
- })) : []]
2219
+ actions: [
2220
+ ...mutate && menu.entry.kind === "directory" ? [{
2221
+ label: actions.createFolder,
2222
+ run: () => {
2223
+ closeMenu(false);
2224
+ setDialog(_objectSpread2(_objectSpread2({}, menu), {}, { action: "createFolder" }));
2225
+ }
2226
+ }] : [],
2227
+ ...mutate ? [{
2228
+ label: actions.rename,
2229
+ run: () => {
2230
+ closeMenu(false);
2231
+ setDialog(_objectSpread2(_objectSpread2({}, menu), {}, { action: "rename" }));
2232
+ }
2233
+ }, {
2234
+ label: actions.delete,
2235
+ run: () => {
2236
+ closeMenu(false);
2237
+ setDialog(_objectSpread2(_objectSpread2({}, menu), {}, { action: "delete" }));
2238
+ }
2239
+ }] : [],
2240
+ ...git ? ["stage", "unstage"].map((kind) => ({
2241
+ label: actions[kind],
2242
+ run: () => {
2243
+ closeMenu(true);
2244
+ run(async () => {
2245
+ var _result$error;
2246
+ const result = await git.change(kind, menu.entry, isCurrent !== null && isCurrent !== void 0 ? isCurrent : (() => mounted.current));
2247
+ if (!result.done) return (_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : actions.conflict;
2248
+ });
2249
+ }
2250
+ })) : []
2251
+ ]
2104
2252
  }) : null, dialog ? /* @__PURE__ */ React.createElement(ExplorerFileDialog, {
2105
2253
  anchor: dialog.anchor,
2106
2254
  name: dialog.entry.name,
2107
2255
  folder: dialog.entry.kind === "directory",
2108
- rename: dialog.rename,
2256
+ action: dialog.action,
2109
2257
  message: actions,
2110
2258
  close: () => setDialog(void 0),
2111
2259
  submit: submitFileAction
@@ -2118,7 +2266,7 @@ var keyFor = (directory) => {
2118
2266
  return keys.get(directory);
2119
2267
  };
2120
2268
  /**
2121
- * A read-only tree of a folder for the Editor's side panel, built on Headless Tree. Folders load when expanded, folders
2269
+ * A tree of a folder for the Editor's side panel, built on Headless Tree. Folders load when expanded, folders
2122
2270
  * sort before files, and `.git` is hidden. Keyboard: arrow keys, Home and End move and expand; Enter or Space opens a
2123
2271
  * file. Needs React 16.14–18 from the host.
2124
2272
  */
@@ -2126,8 +2274,22 @@ function ProjectFolderExplorer(props) {
2126
2274
  return /* @__PURE__ */ React.createElement(Explorer, _objectSpread2({ key: keyFor(props.directory) }, props));
2127
2275
  }
2128
2276
  var readJson = async (directory, name) => JSON.parse(await (await (await directory.getFileHandle(name)).getFile()).text());
2129
- function FolderView({ vm, locale = "en", marks: followMarks, fileStatuses: followStatuses, git: followGit, openFile, readBaseline }) {
2277
+ function FolderView({ vm, locale = "en", marks: followMarks, fileStatuses: followStatuses, git: followGit, openFile, readBaseline, createFolder }) {
2130
2278
  const message = messagesFor(locale);
2279
+ const [creating, setCreating] = React.useState(false);
2280
+ const [createError, setCreateError] = React.useState("");
2281
+ const create = async () => {
2282
+ if (!createFolder || creating) return;
2283
+ setCreating(true);
2284
+ setCreateError("");
2285
+ try {
2286
+ await createFolder();
2287
+ } catch (_unused4) {
2288
+ setCreateError(message.createFailed);
2289
+ } finally {
2290
+ setCreating(false);
2291
+ }
2292
+ };
2131
2293
  const [{ folder, directory, version }, setFollowed] = React.useState({
2132
2294
  directory: null,
2133
2295
  version: 0
@@ -2203,10 +2365,14 @@ function FolderView({ vm, locale = "en", marks: followMarks, fileStatuses: follo
2203
2365
  }));
2204
2366
  }, [directory, followGit]);
2205
2367
  if (!folder) return null;
2206
- if (!directory) return /* @__PURE__ */ React.createElement("div", { className: `${prefix}-empty` }, /* @__PURE__ */ React.createElement("style", null, css), /* @__PURE__ */ React.createElement("p", null, message.noFolder), /* @__PURE__ */ React.createElement("button", {
2368
+ if (!directory) return /* @__PURE__ */ React.createElement("div", { className: `${prefix}-empty` }, /* @__PURE__ */ React.createElement("style", null, css), createFolder ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("p", null, projectFolderOverwriteWarning(locale)), /* @__PURE__ */ React.createElement("button", {
2369
+ type: "button",
2370
+ disabled: creating,
2371
+ onClick: () => void create()
2372
+ }, message.create), creating && /* @__PURE__ */ React.createElement("p", { role: "status" }, message.creating), createError && /* @__PURE__ */ React.createElement("p", { role: "alert" }, createError)) : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("p", null, message.noFolder), /* @__PURE__ */ React.createElement("button", {
2207
2373
  type: "button",
2208
- onClick: () => folder.open(locale !== null && locale !== void 0 ? locale : "en")
2209
- }, message.open));
2374
+ onClick: () => folder.open(locale)
2375
+ }, message.open)));
2210
2376
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(ProjectFolderExplorer, {
2211
2377
  directory,
2212
2378
  locale,
@@ -2230,18 +2396,11 @@ function FolderView({ vm, locale = "en", marks: followMarks, fileStatuses: follo
2230
2396
  /**
2231
2397
  * Renders the explorer of the project folder open for `vm`, for a side panel of the host: its files with what each is
2232
2398
  * for, and `marks` (such as the Git status) when given. It follows the folder as it opens, closes and saves; with no
2233
- * folder open it offers Open Project Folder…, and it renders nothing while no project folder is mounted for `vm`.
2399
+ * folder open it offers to create one when the host supplies a command, or to open an existing one.
2400
+ * It renders nothing while no project folder is mounted for `vm`.
2234
2401
  * Pass the same `marks` function across renders, or the marks are followed again.
2235
2402
  */
2236
- function renderProjectFolderExplorer(createElement, { vm, locale = "en", marks, fileStatuses, git, openFile, readBaseline }) {
2237
- return createElement(FolderView, {
2238
- vm,
2239
- locale,
2240
- marks,
2241
- fileStatuses,
2242
- git,
2243
- openFile,
2244
- readBaseline
2245
- });
2403
+ function renderProjectFolderExplorer(createElement, props) {
2404
+ return createElement(FolderView, props);
2246
2405
  }
2247
2406
  export { ProjectFolderExplorer, describeProjectFolder, renderProjectFolderExplorer };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export type { ReloadTarget } from './block-reload.js';
4
4
  export { followProjectFolder } from './folder-registry.js';
5
5
  export type { FollowedFolder } from './folder-registry.js';
6
6
  export type { DeserializeBlocks } from './script-reload.js';
7
- export { createProjectFolder } from './create-folder.js';
7
+ export { createProjectFolder, projectFolderOverwriteWarning } from './create-folder.js';
8
8
  type MenuElement = (component: any, props: any, ...children: any[]) => any;
9
9
  interface MenuProps {
10
10
  vm: FolderVM;
@@ -29,8 +29,9 @@ export declare function mountProjectFolder({ vm, document, host, locale, autoRel
29
29
  status: "saved" | "unchanged";
30
30
  sourceRemoved: string[];
31
31
  }>;
32
- createFromSnapshot({ prepareFiles, directory, current }: {
32
+ createFromSnapshot({ prepareFiles, collectEdits, directory, current }: {
33
33
  prepareFiles: () => Promise<ReadonlyMap<string, Uint8Array>>;
34
+ collectEdits?: () => Promise<void>;
34
35
  directory?: FileSystemDirectoryHandle;
35
36
  current?: () => boolean;
36
37
  }): Promise<import("./session.js").OpenResult>;