@kubohiroya/turbowarp-plus-editor-workbench 0.24.0 → 0.25.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/README.ja.md +42 -0
- package/README.md +42 -0
- package/dist/new-project.d.ts +71 -0
- package/dist/new-project.js +89 -0
- package/dist/project-notice.d.ts +12 -0
- package/dist/project-notice.js +56 -0
- package/dist/project-title.d.ts +49 -0
- package/dist/project-title.js +83 -0
- package/package.json +13 -1
- package/src/new-project.ts +151 -0
- package/src/project-file-menu.css +61 -0
- package/src/project-notice.ts +59 -0
- package/src/project-title.ts +108 -0
package/README.ja.md
CHANGED
|
@@ -185,6 +185,48 @@ entries.newProject = {defaultTitle: 'Scratch の作品', browserFolders: true, d
|
|
|
185
185
|
|
|
186
186
|
そのうえで、以前の「名前を変えて保存」の動き(上の 1〜4 を含む)は `mode: 'save'` にして `entries.saveNew`(と `projectSaveShortcut` の第 3 引数)に結線する。
|
|
187
187
|
|
|
188
|
+
**「新しいプロジェクトを作成…」の結線(`@kubohiroya/turbowarp-plus-editor-workbench/new-project`)。** `createNewProjectHandler(host)` は `entries.createIn` と `entries.newProject` に渡す `{createIn, newProject}` を返す。段取りの順序、GitHub の作成とその文言はパッケージが持ち、ホストは GUI と保存場所に依存するものだけを渡す。
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
const {createIn, newProject} = createNewProjectHandler({
|
|
192
|
+
locale, defaultTitle, browserFolders, deviceFolders,
|
|
193
|
+
create, // GUI の「新しいプロジェクトを作成」(メモリ上)
|
|
194
|
+
openDefaultProject: signal => ..., // create() して、初期の作品が表示され題名が戻るまで待つ。
|
|
195
|
+
// 今の作品を残すと選んだら false
|
|
196
|
+
nameProject: (title, saveName) => ..., // properties.apply({title, saveName}) を一度で
|
|
197
|
+
pickDeviceFolder: () => ..., // 手元の空のフォルダ、取り消したら null
|
|
198
|
+
createFolder: (destination, title, signal) => ..., // 'browser' か選んだフォルダに作って開く
|
|
199
|
+
repositoryCreation, // {connect, licenses, list, advance, open, start(draft, serialize, current, signal, target)}
|
|
200
|
+
serialize: title => ..., // 最初のコミットに使うプロジェクトフォルダの内容
|
|
201
|
+
trackProject: () => () => stillTheSameProject(),
|
|
202
|
+
markSaved, notify: text => showNotice(document, text, {locale})
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
- 手元のフォルダは、ブラウザーが選択画面を許すクリックの直後に、最初に選ぶ。選択の取り消し、今の作品を残す選択(`openDefaultProject` が `false`)、フォルダを作れなかったときは `cancelled` を返す。
|
|
207
|
+
- フォルダとリポジトリの作品は初期の作品から始め、`nameProject(title, saveName)` で名前を付ける。
|
|
208
|
+
- GitHub では、同じ作成先と名前(大文字・小文字を区別しない、`list()` から)の止まった作成があれば、新しく作らずに続きから再開する。なければ `start` が作業用の写し(`'browser'` か選んだフォルダ)を作る。結果が偽なら取り消しとし、それ以外はそのまま `advance` に渡して、`advance` を `open` の段まで繰り返す(9 段を超えるか、`unknown` が続いたら止める)。デバイスコードは `notify` で知らせ、新しい作品では `open` の前に `markSaved` を呼んで、開くときに何も尋ねないようにする。`popupBlocked`・`nameConflict` などの失敗は、ダイアログに出す文言に置き換える。
|
|
209
|
+
|
|
210
|
+
**作品名の確定(`@kubohiroya/turbowarp-plus-editor-workbench/project-title`)。** `followProjectTitleCommits(document, {readTitle, onCommit})` はメニューバーの作品名の入力欄(`projectTitleFieldClassName`)を見張り、利用者が Enter(IME の変換中は除く)か入力欄から離れることで確定した編集ごとに 1 回、`onCommit(title)` を呼ぶ。読み込みやプロパティで変わった作品名と、変わらなかった作品名は知らせない。戻り値は見張りをやめる関数。続く `reflectProjectTitle({document, locale, title, saveName, location, gitBound, renameBrowserFolder, setSaveName, notify, current})` が作品名を反映する。
|
|
211
|
+
|
|
212
|
+
| 作品 | 結果 |
|
|
213
|
+
| --- | --- |
|
|
214
|
+
| `location: 'browser'`(ブラウザー内のフォルダ) | `renameBrowserFolder(title)`(失敗は無視) |
|
|
215
|
+
| `location: 'device'` | 手元のフォルダの名前は変わらないと知らせる |
|
|
216
|
+
| `gitBound: true` | Git のリポジトリの名前はまだ変わらないと知らせる(フォルダの知らせの後に続ける) |
|
|
217
|
+
| 作品名が保存用の名前に使えず、`saveName` が無く、フォルダかリポジトリに保存している | `askSaveName` で尋ね、OK なら `setSaveName(name)` |
|
|
218
|
+
| `location: 'none'` で Git にも結び付いていない、または作品名が空 | 何もしない |
|
|
219
|
+
|
|
220
|
+
`current()` は待つたびに確かめるので、別の作品を開く直前に確定した作品名は何も変えない。ホストは次のように結線する。
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
const stop = followProjectTitleCommits(document, {readTitle: () => store.getState().scratchGui.projectTitle,
|
|
224
|
+
onCommit: title => void reflectProjectTitle({document, locale, title, saveName: projectSaveName(), location, gitBound,
|
|
225
|
+
renameBrowserFolder: title => browserStorage.rename(entry.id, title), setSaveName, notify, current}).catch(() => {})});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**通知(`@kubohiroya/turbowarp-plus-editor-workbench/project-notice`)。** `showNotice(document, text, {locale, timeout = 12000})` は、エディターを止めない通知を窓の下に出す(`role="status"`、`data-testid="project-notice-snackbar"`)。閉じるボタンは X のアイコンで、名前は `閉じる`/`Dismiss`。`timeout` が過ぎると、通知の中にフォーカスが無ければ消える。同じ文書に出ている通知は置き換え、戻り値は通知を消す関数。見た目は `project-file-menu.css`(`.tw-project-notice`)にある。
|
|
229
|
+
|
|
188
230
|
## 検証
|
|
189
231
|
|
|
190
232
|
`npm test` で配分・縦横比・設定の読み書きを、`npm run test:browser` で独立した試験用ホストを検証する。試験用ホストを手動で開くには、このディレクトリで `npm install` と `python3 -m http.server` を実行し、`/example/` を表示する。
|
package/README.md
CHANGED
|
@@ -187,6 +187,48 @@ A host that wired the old Save As must, for the copy path, stop:
|
|
|
187
187
|
|
|
188
188
|
and wire `entries.saveNew` (and the third argument of `projectSaveShortcut`) to the old Save As behaviour with `mode: 'save'`, keeping all of the above there.
|
|
189
189
|
|
|
190
|
+
**Wiring Create New Project… (`@kubohiroya/turbowarp-plus-editor-workbench/new-project`).** `createNewProjectHandler(host)` returns `{createIn, newProject}` to pass as `entries.createIn` and `entries.newProject`. It owns the order of the steps, the GitHub setup and its messages; the host supplies only what depends on the GUI and its storage:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
const {createIn, newProject} = createNewProjectHandler({
|
|
194
|
+
locale, defaultTitle, browserFolders, deviceFolders,
|
|
195
|
+
create, // the GUI's own Create New Project (in memory)
|
|
196
|
+
openDefaultProject: signal => ..., // create(), wait until the default project is shown and its title reset;
|
|
197
|
+
// false when the person kept the current project
|
|
198
|
+
nameProject: (title, saveName) => ..., // properties.apply({title, saveName}) in one step
|
|
199
|
+
pickDeviceFolder: () => ..., // an empty folder on the device, or null
|
|
200
|
+
createFolder: (destination, title, signal) => ..., // 'browser' or the picked folder; opens it
|
|
201
|
+
repositoryCreation, // {connect, licenses, list, advance, open, start(draft, serialize, current, signal, target)}
|
|
202
|
+
serialize: title => ..., // the project folder snapshot for the first commit
|
|
203
|
+
trackProject: () => () => stillTheSameProject(),
|
|
204
|
+
markSaved, notify: text => showNotice(document, text, {locale})
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
- The device folder is picked first, while the click still allows the picker; a cancelled picker, a kept project (`openDefaultProject` resolves `false`) or a folder that was not made resolves `cancelled`.
|
|
209
|
+
- A folder or repository project starts from the default project, named with `nameProject(title, saveName)`.
|
|
210
|
+
- For GitHub, a stopped setup of the same owner and name (case-insensitively, from `list()`) is resumed instead of starting another. Otherwise `start` creates the working copy (`'browser'` or the picked folder); a falsy result cancels and any other result goes on to `advance`, and `advance` runs until the `open` phase (it gives up after nine steps, or when `unknown` repeats); the device code is shown with `notify`; `markSaved` runs before `open` for a fresh project so opening asks nothing. `popupBlocked`, `nameConflict` and other coded failures become localized messages that the dialog shows.
|
|
211
|
+
|
|
212
|
+
**Project title commits (`@kubohiroya/turbowarp-plus-editor-workbench/project-title`).** `followProjectTitleCommits(document, {readTitle, onCommit})` watches the menu bar's title field (`projectTitleFieldClassName`) and calls `onCommit(title)` once per edit the person commits, on Enter (not while an IME is composing) or when the field loses focus; titles set by loading or the properties, and unchanged titles, are not reported. It returns a function that stops watching. `reflectProjectTitle({document, locale, title, saveName, location, gitBound, renameBrowserFolder, setSaveName, notify, current})` then reflects the title:
|
|
213
|
+
|
|
214
|
+
| Project | Result |
|
|
215
|
+
| --- | --- |
|
|
216
|
+
| `location: 'browser'` (a folder in the browser's storage) | `renameBrowserFolder(title)` (failures ignored) |
|
|
217
|
+
| `location: 'device'` | notice: the folder on the device keeps its name |
|
|
218
|
+
| `gitBound: true` | notice: the Git repository keeps its name for now (joined after the folder note) |
|
|
219
|
+
| title not a valid save name, no `saveName`, saved in a folder or repository | `askSaveName`, then `setSaveName(name)` on OK |
|
|
220
|
+
| `location: 'none'` and not Git-bound, or an empty title | nothing |
|
|
221
|
+
|
|
222
|
+
`current()` is checked after each wait, so a title committed just before another project opened changes nothing. A host wires it as:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const stop = followProjectTitleCommits(document, {readTitle: () => store.getState().scratchGui.projectTitle,
|
|
226
|
+
onCommit: title => void reflectProjectTitle({document, locale, title, saveName: projectSaveName(), location, gitBound,
|
|
227
|
+
renameBrowserFolder: title => browserStorage.rename(entry.id, title), setSaveName, notify, current}).catch(() => {})});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
**Notices (`@kubohiroya/turbowarp-plus-editor-workbench/project-notice`).** `showNotice(document, text, {locale, timeout = 12000})` shows a snackbar at the bottom of the window (`role="status"`, `data-testid="project-notice-snackbar"`) with an X dismiss button named `閉じる` / `Dismiss`. It hides itself after `timeout` unless focus is inside it, replaces the notice already shown in that document, and returns a function that removes it. Its styles are in `project-file-menu.css` (`.tw-project-notice`).
|
|
231
|
+
|
|
190
232
|
## Verification
|
|
191
233
|
|
|
192
234
|
Run `npm test` to verify split sizing, aspect ratios, and settings persistence. Run `npm run test:browser` to test the standalone host. To open the test host manually, run `npm install` and `python3 -m http.server` in this directory, then open `/example/`.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { CreationInteraction, GitHubCreationAccount, GitHubRepositoryDraft, RepositoryCreationControls, RepositoryCreationProgress } from '@kubohiroya/turbowarp-plus-editor-git/project-repository-creation';
|
|
2
|
+
import type { NewProjectChoices, NewProjectRequest, NewProjectResult } from './project-file-dialogs.ts';
|
|
3
|
+
/** Where a new project folder goes: a new folder in the browser's storage, or an empty folder picked on the device. */
|
|
4
|
+
export type NewProjectFolderDestination = 'browser' | FileSystemDirectoryHandle;
|
|
5
|
+
/** The host's GitHub repository creation workflow, as used by Create New Project…. */
|
|
6
|
+
export interface NewProjectRepositoryCreation extends Pick<RepositoryCreationControls, 'list' | 'advance' | 'open'> {
|
|
7
|
+
/** Reuses a current session or opens sign-in, as for the new project dialog's GitHub choice. */
|
|
8
|
+
connect(interaction: CreationInteraction): Promise<GitHubCreationAccount | null>;
|
|
9
|
+
licenses?: readonly {
|
|
10
|
+
id: string;
|
|
11
|
+
label: string;
|
|
12
|
+
}[];
|
|
13
|
+
/**
|
|
14
|
+
* Starts creating the repository from the serialized project, with its working copy in `target`. Resolves the
|
|
15
|
+
* progress to {@link NewProjectRepositoryCreation.advance}, or a falsy value when the creation was cancelled.
|
|
16
|
+
* Any other result is passed on to `advance` as it is.
|
|
17
|
+
*/
|
|
18
|
+
start(draft: GitHubRepositoryDraft, serialize: () => Promise<Uint8Array>, current: () => boolean, signal: AbortSignal, target: NewProjectFolderDestination): Promise<RepositoryCreationProgress | boolean | null | undefined>;
|
|
19
|
+
}
|
|
20
|
+
/** What the host supplies for {@link createNewProjectHandler}: only what depends on the GUI and its storage. */
|
|
21
|
+
export interface NewProjectHost {
|
|
22
|
+
locale: string;
|
|
23
|
+
/** The GUI's own Create New Project: the default project in memory. */
|
|
24
|
+
create(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Replaces the open project with the GUI's default project, as {@link NewProjectHost.create} does, and resolves
|
|
27
|
+
* once it is shown and the GUI has reset its title. Resolves `false` when the person kept the current project at
|
|
28
|
+
* the unsaved-changes question, and rejects when it fails to load or `signal` aborts.
|
|
29
|
+
*/
|
|
30
|
+
openDefaultProject(signal: AbortSignal): Promise<boolean>;
|
|
31
|
+
/** Names the new default project: its title and save name, applied together. */
|
|
32
|
+
nameProject(title: string, saveName: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Picks an empty folder on the device; `null` when the person cancelled. It is called first, during the click
|
|
35
|
+
* that asked for it, so the browser still allows the picker. Without it a device folder is cancelled.
|
|
36
|
+
*/
|
|
37
|
+
pickDeviceFolder?(): Promise<FileSystemDirectoryHandle | null>;
|
|
38
|
+
/** Creates a project folder at `destination` from the open (new default) project and opens it. */
|
|
39
|
+
createFolder(destination: NewProjectFolderDestination, title: string, signal: AbortSignal): Promise<boolean>;
|
|
40
|
+
/** Offers a new GitHub repository. Needs {@link NewProjectHost.serialize}. */
|
|
41
|
+
repositoryCreation?: NewProjectRepositoryCreation;
|
|
42
|
+
/** Serializes the open project as a project folder titled `title`, for the repository's first commit. */
|
|
43
|
+
serialize?(title: string): Promise<Uint8Array>;
|
|
44
|
+
/**
|
|
45
|
+
* Called once the new default project is named; returns a check that stays true while that project stays open.
|
|
46
|
+
* Without it the project counts as current until the dialog is dismissed.
|
|
47
|
+
*/
|
|
48
|
+
trackProject?(): () => boolean;
|
|
49
|
+
/** Marks the open project as saved, so opening the new repository asks nothing about the default project. */
|
|
50
|
+
markSaved?(): void;
|
|
51
|
+
/** Shows a short notice, such as the GitHub device code (see `showNotice`). */
|
|
52
|
+
notify(text: string): void;
|
|
53
|
+
/** The localized title of a new project. */
|
|
54
|
+
defaultTitle?: string;
|
|
55
|
+
/** Offers a project folder kept in the browser. */
|
|
56
|
+
browserFolders?: boolean;
|
|
57
|
+
/** Offers a project folder on the device (see {@link NewProjectChoices.deviceFolders}). */
|
|
58
|
+
deviceFolders?: boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create New Project…: the new project dialog's choices and its `createIn`. The in-memory project is the GUI's own
|
|
62
|
+
* `create`. A folder or a GitHub repository starts from the same default project, named by the dialog. The device
|
|
63
|
+
* folder is picked before anything else, while the click still allows the picker. A GitHub repository is created,
|
|
64
|
+
* its first commit pushed and its root opened as the project folder; pressing Create again after a stopped setup
|
|
65
|
+
* resumes it instead of making another repository. Pass both to `openNewProjectDialog` or the host's new project
|
|
66
|
+
* entry.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createNewProjectHandler(host: NewProjectHost): {
|
|
69
|
+
createIn(request: NewProjectRequest, signal: AbortSignal): Promise<NewProjectResult>;
|
|
70
|
+
newProject: NewProjectChoices;
|
|
71
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create New Project…: the new project dialog's choices and its `createIn`. The in-memory project is the GUI's own
|
|
3
|
+
* `create`. A folder or a GitHub repository starts from the same default project, named by the dialog. The device
|
|
4
|
+
* folder is picked before anything else, while the click still allows the picker. A GitHub repository is created,
|
|
5
|
+
* its first commit pushed and its root opened as the project folder; pressing Create again after a stopped setup
|
|
6
|
+
* resumes it instead of making another repository. Pass both to `openNewProjectDialog` or the host's new project
|
|
7
|
+
* entry.
|
|
8
|
+
*/
|
|
9
|
+
export function createNewProjectHandler(host) {
|
|
10
|
+
const ja = host.locale.startsWith('ja');
|
|
11
|
+
const label = (jp, en) => ja ? jp : en;
|
|
12
|
+
const repositoryCreation = host.repositoryCreation;
|
|
13
|
+
const repositoryError = (error) => {
|
|
14
|
+
const code = error?.code;
|
|
15
|
+
if (!code)
|
|
16
|
+
return error;
|
|
17
|
+
return new Error(code === 'popupBlocked' ?
|
|
18
|
+
label('GitHub の認証画面を開けませんでした。ポップアップを許可して、もう一度「リポジトリを作成」を押してください。', 'Could not open the GitHub authorization window. Allow pop-ups and press "Create Repository" again.') :
|
|
19
|
+
code === 'nameConflict' ? label('同じ名前のリポジトリがすでにあります。', 'A repository with this name already exists.') :
|
|
20
|
+
label('リポジトリの作成を完了できませんでした。もう一度「リポジトリを作成」を押すと続きから再開します。', 'Could not finish creating the repository. Press "Create Repository" again to continue where it stopped.'));
|
|
21
|
+
};
|
|
22
|
+
// Creates the repository, pushes the first commit and opens its root as the project folder.
|
|
23
|
+
const finishRepository = async (creation, started, signal, fresh) => {
|
|
24
|
+
const interaction = { signal, deviceCode({ userCode, verificationUri }) {
|
|
25
|
+
host.notify(label(`GitHub で認証してください:${verificationUri}(コード ${userCode})`, `Authorize on GitHub: ${verificationUri} (code ${userCode})`));
|
|
26
|
+
} };
|
|
27
|
+
let progress = started;
|
|
28
|
+
try {
|
|
29
|
+
for (let step = 0; progress.phase !== 'open'; step++) {
|
|
30
|
+
const { progress: next } = await creation.advance(progress, interaction);
|
|
31
|
+
if (step > 8 || next.phase === 'unknown' && progress.phase === 'unknown') {
|
|
32
|
+
throw Object.assign(new Error('unknown'), { code: 'unknown' });
|
|
33
|
+
}
|
|
34
|
+
progress = next;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw repositoryError(error);
|
|
39
|
+
}
|
|
40
|
+
// The default project only stood in for the repository's project, so opening it asks nothing.
|
|
41
|
+
if (fresh)
|
|
42
|
+
host.markSaved?.();
|
|
43
|
+
return creation.open(progress);
|
|
44
|
+
};
|
|
45
|
+
const createIn = async (request, signal) => {
|
|
46
|
+
if (request.destination === 'memory') {
|
|
47
|
+
host.create();
|
|
48
|
+
return 'created';
|
|
49
|
+
}
|
|
50
|
+
const { title, saveName } = request;
|
|
51
|
+
if (request.destination === 'github') {
|
|
52
|
+
if (!repositoryCreation || !host.serialize)
|
|
53
|
+
return 'cancelled';
|
|
54
|
+
// Pressing Create again after a stopped setup resumes it instead of making another repository.
|
|
55
|
+
const pending = (await repositoryCreation.list()).find(entry => entry.owner.toLowerCase() === request.draft.owner.toLowerCase() &&
|
|
56
|
+
entry.name.toLowerCase() === request.draft.name.toLowerCase());
|
|
57
|
+
if (pending)
|
|
58
|
+
return await finishRepository(repositoryCreation, pending, signal, false) ? 'created' : 'cancelled';
|
|
59
|
+
// The folder is picked first, while the click that asked for it still allows the picker.
|
|
60
|
+
const target = request.workingCopy === 'device' ? await host.pickDeviceFolder?.() : 'browser';
|
|
61
|
+
if (!target || !await host.openDefaultProject(signal))
|
|
62
|
+
return 'cancelled';
|
|
63
|
+
host.nameProject(title, saveName);
|
|
64
|
+
const open = host.trackProject?.() ?? (() => true);
|
|
65
|
+
const current = () => !signal.aborted && open();
|
|
66
|
+
const progress = await repositoryCreation.start(request.draft, () => host.serialize(title), current, signal, target)
|
|
67
|
+
.catch(error => { throw repositoryError(error); });
|
|
68
|
+
if (!progress)
|
|
69
|
+
return 'cancelled';
|
|
70
|
+
// Any other result goes on to advance, as the Editor's own flow did.
|
|
71
|
+
return await finishRepository(repositoryCreation, progress, signal, true) ?
|
|
72
|
+
'created' : 'cancelled';
|
|
73
|
+
}
|
|
74
|
+
const target = request.destination === 'device' ? await host.pickDeviceFolder?.() : 'browser';
|
|
75
|
+
if (!target || !await host.openDefaultProject(signal))
|
|
76
|
+
return 'cancelled';
|
|
77
|
+
host.nameProject(title, saveName);
|
|
78
|
+
return await host.createFolder(target, title, signal) ? 'created' : 'cancelled';
|
|
79
|
+
};
|
|
80
|
+
return { createIn, newProject: {
|
|
81
|
+
defaultTitle: host.defaultTitle,
|
|
82
|
+
browserFolders: host.browserFolders,
|
|
83
|
+
deviceFolders: host.deviceFolders,
|
|
84
|
+
github: repositoryCreation && {
|
|
85
|
+
connectForCreation: interaction => repositoryCreation.connect(interaction),
|
|
86
|
+
licenses: repositoryCreation.licenses
|
|
87
|
+
}
|
|
88
|
+
} };
|
|
89
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The class of the notice; its styles are in `project-file-menu.css`. */
|
|
2
|
+
export declare const projectNoticeClassName = "tw-project-notice";
|
|
3
|
+
/**
|
|
4
|
+
* Shows a short notice at the bottom of the window that does not block the editor, such as what a title change
|
|
5
|
+
* could not rename. It has a dismiss button (an X with a localized `title` and `aria-label`) and hides itself after
|
|
6
|
+
* `timeout` milliseconds (12 s by default) unless focus is inside it. A new notice replaces the one shown in the same
|
|
7
|
+
* document. Returns a function that removes the notice.
|
|
8
|
+
*/
|
|
9
|
+
export declare function showNotice(document: Document, text: string, { locale, timeout }: {
|
|
10
|
+
locale: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}): () => void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** The class of the notice; its styles are in `project-file-menu.css`. */
|
|
2
|
+
export const projectNoticeClassName = 'tw-project-notice';
|
|
3
|
+
const shown = new WeakMap();
|
|
4
|
+
/**
|
|
5
|
+
* Shows a short notice at the bottom of the window that does not block the editor, such as what a title change
|
|
6
|
+
* could not rename. It has a dismiss button (an X with a localized `title` and `aria-label`) and hides itself after
|
|
7
|
+
* `timeout` milliseconds (12 s by default) unless focus is inside it. A new notice replaces the one shown in the same
|
|
8
|
+
* document. Returns a function that removes the notice.
|
|
9
|
+
*/
|
|
10
|
+
export function showNotice(document, text, { locale, timeout = 12000 }) {
|
|
11
|
+
shown.get(document)?.();
|
|
12
|
+
const view = document.defaultView;
|
|
13
|
+
const ja = locale.startsWith('ja');
|
|
14
|
+
const notice = document.createElement('aside');
|
|
15
|
+
notice.className = projectNoticeClassName;
|
|
16
|
+
notice.setAttribute('data-project-tools', '');
|
|
17
|
+
notice.setAttribute('data-testid', 'project-notice-snackbar');
|
|
18
|
+
const message = document.createElement('span');
|
|
19
|
+
message.setAttribute('role', 'status');
|
|
20
|
+
message.setAttribute('aria-atomic', 'true');
|
|
21
|
+
message.textContent = text;
|
|
22
|
+
const dismiss = document.createElement('button');
|
|
23
|
+
dismiss.type = 'button';
|
|
24
|
+
const name = ja ? '閉じる' : 'Dismiss';
|
|
25
|
+
dismiss.title = name;
|
|
26
|
+
dismiss.setAttribute('aria-label', name);
|
|
27
|
+
const svg = 'http://www.w3.org/2000/svg';
|
|
28
|
+
const icon = document.createElementNS(svg, 'svg');
|
|
29
|
+
icon.setAttribute('viewBox', '0 0 16 16');
|
|
30
|
+
icon.setAttribute('width', '16');
|
|
31
|
+
icon.setAttribute('height', '16');
|
|
32
|
+
icon.setAttribute('aria-hidden', 'true');
|
|
33
|
+
const path = document.createElementNS(svg, 'path');
|
|
34
|
+
path.setAttribute('d', 'M4 4l8 8M12 4l-8 8');
|
|
35
|
+
path.setAttribute('stroke', 'currentColor');
|
|
36
|
+
path.setAttribute('stroke-width', '2');
|
|
37
|
+
path.setAttribute('stroke-linecap', 'round');
|
|
38
|
+
icon.append(path);
|
|
39
|
+
dismiss.append(icon);
|
|
40
|
+
notice.append(message, dismiss);
|
|
41
|
+
let timer = 0;
|
|
42
|
+
const remove = () => {
|
|
43
|
+
view?.clearTimeout(timer);
|
|
44
|
+
notice.remove();
|
|
45
|
+
if (shown.get(document) === remove)
|
|
46
|
+
shown.delete(document);
|
|
47
|
+
};
|
|
48
|
+
dismiss.addEventListener('click', remove);
|
|
49
|
+
document.body.append(notice);
|
|
50
|
+
shown.set(document, remove);
|
|
51
|
+
timer = view?.setTimeout(() => {
|
|
52
|
+
if (!notice.contains(document.activeElement))
|
|
53
|
+
remove();
|
|
54
|
+
}, timeout) ?? 0;
|
|
55
|
+
return remove;
|
|
56
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls `onCommit` with the new title when the person commits an edit of the menu bar's title field
|
|
3
|
+
* ({@link projectTitleFieldClassName}), on Enter or when the field loses focus, as the GUI's own field does. Each
|
|
4
|
+
* edit is reported once. Enter while an IME is composing does not commit, and a title that did not change is not
|
|
5
|
+
* reported. Titles set by loading, a new project or the properties are not edits of the field, so they are not
|
|
6
|
+
* reported either. `readTitle` reads the project title the GUI holds; it is read one tick after the event, once the
|
|
7
|
+
* GUI has written the field's value. Returns a function that stops watching.
|
|
8
|
+
*/
|
|
9
|
+
export declare function followProjectTitleCommits(document: Document, { readTitle, onCommit }: {
|
|
10
|
+
readTitle(): string;
|
|
11
|
+
onCommit(title: string): void;
|
|
12
|
+
}): () => void;
|
|
13
|
+
/** Where the open project is saved, for {@link reflectProjectTitle}. */
|
|
14
|
+
export type ProjectTitleLocation = 'browser' | 'device' | 'none';
|
|
15
|
+
export interface ReflectProjectTitleOptions {
|
|
16
|
+
/** The document that shows the save name question. */
|
|
17
|
+
document: Document;
|
|
18
|
+
locale: string;
|
|
19
|
+
/** The title the person committed. */
|
|
20
|
+
title: string;
|
|
21
|
+
/** The project's save name, `''` when it has none. */
|
|
22
|
+
saveName: string;
|
|
23
|
+
/**
|
|
24
|
+
* Where the project is saved: `browser` for a project folder in the browser's own storage, which can be renamed;
|
|
25
|
+
* `device` for a folder on the device, which the browser cannot rename; `none` when it is not saved in a folder.
|
|
26
|
+
*/
|
|
27
|
+
location: ProjectTitleLocation;
|
|
28
|
+
/** Whether the folder is bound to a Git repository, whose name does not change. */
|
|
29
|
+
gitBound: boolean;
|
|
30
|
+
/** Renames the project folder in the browser's storage; failures are ignored. Needed for `browser`. */
|
|
31
|
+
renameBrowserFolder?(title: string): Promise<void>;
|
|
32
|
+
/** Sets the project's save name, after the person entered one. */
|
|
33
|
+
setSaveName(name: string): void;
|
|
34
|
+
/** Shows a short notice, such as with {@link showNotice}. */
|
|
35
|
+
notify(text: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Whether the same project is still open. Checked after each wait, so a title committed just before another
|
|
38
|
+
* project opened does not rename or name the new one. Defaults to always true.
|
|
39
|
+
*/
|
|
40
|
+
current?(): boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Reflects a title the person committed in the menu bar on what the project is saved in. A folder in the browser's
|
|
44
|
+
* storage is renamed to the title; a folder on the device and a Git repository keep their names, and a notice says
|
|
45
|
+
* so. When the project is saved in a folder or repository, has no save name and the title cannot be used as one
|
|
46
|
+
* ({@link isValidSaveName}), the person is asked for a save name ({@link askSaveName}). An empty title, or a project
|
|
47
|
+
* that is not saved in a folder, changes nothing.
|
|
48
|
+
*/
|
|
49
|
+
export declare function reflectProjectTitle({ document, locale, title, saveName, location, gitBound, renameBrowserFolder, setSaveName, notify, current }: ReflectProjectTitleOptions): Promise<void>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { projectTitleFieldClassName } from "./info-menu.js";
|
|
2
|
+
import { askSaveName, isValidSaveName, suggestSaveName } from "./save-name.js";
|
|
3
|
+
/**
|
|
4
|
+
* Calls `onCommit` with the new title when the person commits an edit of the menu bar's title field
|
|
5
|
+
* ({@link projectTitleFieldClassName}), on Enter or when the field loses focus, as the GUI's own field does. Each
|
|
6
|
+
* edit is reported once. Enter while an IME is composing does not commit, and a title that did not change is not
|
|
7
|
+
* reported. Titles set by loading, a new project or the properties are not edits of the field, so they are not
|
|
8
|
+
* reported either. `readTitle` reads the project title the GUI holds; it is read one tick after the event, once the
|
|
9
|
+
* GUI has written the field's value. Returns a function that stops watching.
|
|
10
|
+
*/
|
|
11
|
+
export function followProjectTitleCommits(document, { readTitle, onCommit }) {
|
|
12
|
+
const view = document.defaultView;
|
|
13
|
+
if (!view)
|
|
14
|
+
return () => { };
|
|
15
|
+
const isTitleField = (target) => target instanceof view.HTMLInputElement && target.classList.contains(projectTitleFieldClassName);
|
|
16
|
+
let before = null;
|
|
17
|
+
let scheduled = false;
|
|
18
|
+
let stopped = false;
|
|
19
|
+
const focus = (event) => {
|
|
20
|
+
if (isTitleField(event.target) && !scheduled)
|
|
21
|
+
before = readTitle();
|
|
22
|
+
};
|
|
23
|
+
const settle = (event) => {
|
|
24
|
+
if (before === null || scheduled || !isTitleField(event.target))
|
|
25
|
+
return;
|
|
26
|
+
if (event.type === 'keydown' && (event.key !== 'Enter' || event.isComposing))
|
|
27
|
+
return;
|
|
28
|
+
const previous = before;
|
|
29
|
+
scheduled = true;
|
|
30
|
+
// The field writes the title after this event (Enter also blurs it); read it once the GUI has handled both.
|
|
31
|
+
view.setTimeout(() => {
|
|
32
|
+
scheduled = false;
|
|
33
|
+
if (stopped)
|
|
34
|
+
return;
|
|
35
|
+
const title = readTitle();
|
|
36
|
+
before = isTitleField(document.activeElement) ? title : null;
|
|
37
|
+
if (title !== previous)
|
|
38
|
+
onCommit(title);
|
|
39
|
+
}, 0);
|
|
40
|
+
};
|
|
41
|
+
document.addEventListener('focusin', focus, true);
|
|
42
|
+
document.addEventListener('keydown', settle, true);
|
|
43
|
+
document.addEventListener('change', settle, true);
|
|
44
|
+
document.addEventListener('focusout', settle, true);
|
|
45
|
+
return () => {
|
|
46
|
+
stopped = true;
|
|
47
|
+
document.removeEventListener('focusin', focus, true);
|
|
48
|
+
document.removeEventListener('keydown', settle, true);
|
|
49
|
+
document.removeEventListener('change', settle, true);
|
|
50
|
+
document.removeEventListener('focusout', settle, true);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reflects a title the person committed in the menu bar on what the project is saved in. A folder in the browser's
|
|
55
|
+
* storage is renamed to the title; a folder on the device and a Git repository keep their names, and a notice says
|
|
56
|
+
* so. When the project is saved in a folder or repository, has no save name and the title cannot be used as one
|
|
57
|
+
* ({@link isValidSaveName}), the person is asked for a save name ({@link askSaveName}). An empty title, or a project
|
|
58
|
+
* that is not saved in a folder, changes nothing.
|
|
59
|
+
*/
|
|
60
|
+
export async function reflectProjectTitle({ document, locale, title, saveName, location, gitBound, renameBrowserFolder, setSaveName, notify, current = () => true }) {
|
|
61
|
+
if (location === 'none' && !gitBound || !title.trim())
|
|
62
|
+
return;
|
|
63
|
+
const ja = locale.startsWith('ja');
|
|
64
|
+
if (location === 'browser' && renameBrowserFolder)
|
|
65
|
+
await renameBrowserFolder(title).catch(() => { });
|
|
66
|
+
if (!current())
|
|
67
|
+
return;
|
|
68
|
+
if (!isValidSaveName(title) && !saveName) {
|
|
69
|
+
const name = await askSaveName({ document, locale, title, current: suggestSaveName(title) || undefined });
|
|
70
|
+
if (name && current())
|
|
71
|
+
setSaveName(name);
|
|
72
|
+
}
|
|
73
|
+
const notes = [];
|
|
74
|
+
if (location === 'device')
|
|
75
|
+
notes.push(ja ?
|
|
76
|
+
'この作品のフォルダは手元のフォルダなので、フォルダ名は変わりません(ブラウザー版では手元のフォルダ名を変えられません)。' :
|
|
77
|
+
'This project folder is on your device, so its name stays as it is (the browser version cannot rename folders on your device).');
|
|
78
|
+
// Renaming the GitHub repository needs the keybroker's rename route first (#681).
|
|
79
|
+
if (gitBound)
|
|
80
|
+
notes.push(ja ? 'Git のリポジトリの名前は、まだ変わりません。' : 'The Git repository keeps its name for now.');
|
|
81
|
+
if (notes.length && current())
|
|
82
|
+
notify(notes.join(' '));
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-workbench",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "The TurboWarp Plus Editor workbench: layout, dockable sprite tabs, stage, debugger tab and file menu",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -50,6 +50,10 @@
|
|
|
50
50
|
"types": "./dist/ime-key.d.ts",
|
|
51
51
|
"default": "./dist/ime-key.js"
|
|
52
52
|
},
|
|
53
|
+
"./new-project": {
|
|
54
|
+
"types": "./dist/new-project.d.ts",
|
|
55
|
+
"default": "./dist/new-project.js"
|
|
56
|
+
},
|
|
53
57
|
"./project-file-dialogs": {
|
|
54
58
|
"types": "./dist/project-file-dialogs.d.ts",
|
|
55
59
|
"default": "./dist/project-file-dialogs.js"
|
|
@@ -76,6 +80,10 @@
|
|
|
76
80
|
"types": "./dist/project-messages.d.ts",
|
|
77
81
|
"default": "./dist/project-messages.js"
|
|
78
82
|
},
|
|
83
|
+
"./project-notice": {
|
|
84
|
+
"types": "./dist/project-notice.d.ts",
|
|
85
|
+
"default": "./dist/project-notice.js"
|
|
86
|
+
},
|
|
79
87
|
"./project-screen-state": {
|
|
80
88
|
"types": "./dist/project-screen-state.d.ts",
|
|
81
89
|
"default": "./dist/project-screen-state.js"
|
|
@@ -89,6 +97,10 @@
|
|
|
89
97
|
"types": "./dist/project-size-approval.d.ts",
|
|
90
98
|
"default": "./dist/project-size-approval.js"
|
|
91
99
|
},
|
|
100
|
+
"./project-title": {
|
|
101
|
+
"types": "./dist/project-title.d.ts",
|
|
102
|
+
"default": "./dist/project-title.js"
|
|
103
|
+
},
|
|
92
104
|
"./sprite-layout-menu.css": "./src/sprite-layout-menu.css",
|
|
93
105
|
"./sprite-tabs": {
|
|
94
106
|
"types": "./dist/sprite-tabs.d.ts",
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type {CreationInteraction, GitHubCreationAccount, GitHubRepositoryDraft, RepositoryCreationControls,
|
|
2
|
+
RepositoryCreationProgress} from '@kubohiroya/turbowarp-plus-editor-git/project-repository-creation';
|
|
3
|
+
import type {NewProjectChoices, NewProjectRequest, NewProjectResult} from './project-file-dialogs.ts';
|
|
4
|
+
|
|
5
|
+
/** Where a new project folder goes: a new folder in the browser's storage, or an empty folder picked on the device. */
|
|
6
|
+
export type NewProjectFolderDestination = 'browser' | FileSystemDirectoryHandle;
|
|
7
|
+
|
|
8
|
+
/** The host's GitHub repository creation workflow, as used by Create New Project…. */
|
|
9
|
+
export interface NewProjectRepositoryCreation extends Pick<RepositoryCreationControls, 'list' | 'advance' | 'open'> {
|
|
10
|
+
/** Reuses a current session or opens sign-in, as for the new project dialog's GitHub choice. */
|
|
11
|
+
connect(interaction: CreationInteraction): Promise<GitHubCreationAccount | null>;
|
|
12
|
+
licenses?: readonly {id: string; label: string}[];
|
|
13
|
+
/**
|
|
14
|
+
* Starts creating the repository from the serialized project, with its working copy in `target`. Resolves the
|
|
15
|
+
* progress to {@link NewProjectRepositoryCreation.advance}, or a falsy value when the creation was cancelled.
|
|
16
|
+
* Any other result is passed on to `advance` as it is.
|
|
17
|
+
*/
|
|
18
|
+
start(draft: GitHubRepositoryDraft, serialize: () => Promise<Uint8Array>, current: () => boolean, signal: AbortSignal,
|
|
19
|
+
target: NewProjectFolderDestination): Promise<RepositoryCreationProgress | boolean | null | undefined>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What the host supplies for {@link createNewProjectHandler}: only what depends on the GUI and its storage. */
|
|
23
|
+
export interface NewProjectHost {
|
|
24
|
+
locale: string;
|
|
25
|
+
/** The GUI's own Create New Project: the default project in memory. */
|
|
26
|
+
create(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Replaces the open project with the GUI's default project, as {@link NewProjectHost.create} does, and resolves
|
|
29
|
+
* once it is shown and the GUI has reset its title. Resolves `false` when the person kept the current project at
|
|
30
|
+
* the unsaved-changes question, and rejects when it fails to load or `signal` aborts.
|
|
31
|
+
*/
|
|
32
|
+
openDefaultProject(signal: AbortSignal): Promise<boolean>;
|
|
33
|
+
/** Names the new default project: its title and save name, applied together. */
|
|
34
|
+
nameProject(title: string, saveName: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Picks an empty folder on the device; `null` when the person cancelled. It is called first, during the click
|
|
37
|
+
* that asked for it, so the browser still allows the picker. Without it a device folder is cancelled.
|
|
38
|
+
*/
|
|
39
|
+
pickDeviceFolder?(): Promise<FileSystemDirectoryHandle | null>;
|
|
40
|
+
/** Creates a project folder at `destination` from the open (new default) project and opens it. */
|
|
41
|
+
createFolder(destination: NewProjectFolderDestination, title: string, signal: AbortSignal): Promise<boolean>;
|
|
42
|
+
/** Offers a new GitHub repository. Needs {@link NewProjectHost.serialize}. */
|
|
43
|
+
repositoryCreation?: NewProjectRepositoryCreation;
|
|
44
|
+
/** Serializes the open project as a project folder titled `title`, for the repository's first commit. */
|
|
45
|
+
serialize?(title: string): Promise<Uint8Array>;
|
|
46
|
+
/**
|
|
47
|
+
* Called once the new default project is named; returns a check that stays true while that project stays open.
|
|
48
|
+
* Without it the project counts as current until the dialog is dismissed.
|
|
49
|
+
*/
|
|
50
|
+
trackProject?(): () => boolean;
|
|
51
|
+
/** Marks the open project as saved, so opening the new repository asks nothing about the default project. */
|
|
52
|
+
markSaved?(): void;
|
|
53
|
+
/** Shows a short notice, such as the GitHub device code (see `showNotice`). */
|
|
54
|
+
notify(text: string): void;
|
|
55
|
+
/** The localized title of a new project. */
|
|
56
|
+
defaultTitle?: string;
|
|
57
|
+
/** Offers a project folder kept in the browser. */
|
|
58
|
+
browserFolders?: boolean;
|
|
59
|
+
/** Offers a project folder on the device (see {@link NewProjectChoices.deviceFolders}). */
|
|
60
|
+
deviceFolders?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Create New Project…: the new project dialog's choices and its `createIn`. The in-memory project is the GUI's own
|
|
65
|
+
* `create`. A folder or a GitHub repository starts from the same default project, named by the dialog. The device
|
|
66
|
+
* folder is picked before anything else, while the click still allows the picker. A GitHub repository is created,
|
|
67
|
+
* its first commit pushed and its root opened as the project folder; pressing Create again after a stopped setup
|
|
68
|
+
* resumes it instead of making another repository. Pass both to `openNewProjectDialog` or the host's new project
|
|
69
|
+
* entry.
|
|
70
|
+
*/
|
|
71
|
+
export function createNewProjectHandler(host: NewProjectHost): {
|
|
72
|
+
createIn(request: NewProjectRequest, signal: AbortSignal): Promise<NewProjectResult>;
|
|
73
|
+
newProject: NewProjectChoices;
|
|
74
|
+
} {
|
|
75
|
+
const ja = host.locale.startsWith('ja');
|
|
76
|
+
const label = (jp: string, en: string) => ja ? jp : en;
|
|
77
|
+
const repositoryCreation = host.repositoryCreation;
|
|
78
|
+
const repositoryError = (error: unknown) => {
|
|
79
|
+
const code = (error as {code?: string})?.code;
|
|
80
|
+
if (!code) return error;
|
|
81
|
+
return new Error(code === 'popupBlocked' ?
|
|
82
|
+
label('GitHub の認証画面を開けませんでした。ポップアップを許可して、もう一度「リポジトリを作成」を押してください。',
|
|
83
|
+
'Could not open the GitHub authorization window. Allow pop-ups and press "Create Repository" again.') :
|
|
84
|
+
code === 'nameConflict' ? label('同じ名前のリポジトリがすでにあります。', 'A repository with this name already exists.') :
|
|
85
|
+
label('リポジトリの作成を完了できませんでした。もう一度「リポジトリを作成」を押すと続きから再開します。',
|
|
86
|
+
'Could not finish creating the repository. Press "Create Repository" again to continue where it stopped.'));
|
|
87
|
+
};
|
|
88
|
+
// Creates the repository, pushes the first commit and opens its root as the project folder.
|
|
89
|
+
const finishRepository = async (creation: NewProjectRepositoryCreation, started: RepositoryCreationProgress,
|
|
90
|
+
signal: AbortSignal, fresh: boolean) => {
|
|
91
|
+
const interaction: CreationInteraction = {signal, deviceCode({userCode, verificationUri}) {
|
|
92
|
+
host.notify(label(`GitHub で認証してください:${verificationUri}(コード ${userCode})`,
|
|
93
|
+
`Authorize on GitHub: ${verificationUri} (code ${userCode})`));
|
|
94
|
+
}};
|
|
95
|
+
let progress = started;
|
|
96
|
+
try {
|
|
97
|
+
for (let step = 0; progress.phase !== 'open'; step++) {
|
|
98
|
+
const {progress: next} = await creation.advance(progress, interaction);
|
|
99
|
+
if (step > 8 || next.phase === 'unknown' && progress.phase === 'unknown') {
|
|
100
|
+
throw Object.assign(new Error('unknown'), {code: 'unknown'});
|
|
101
|
+
}
|
|
102
|
+
progress = next;
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw repositoryError(error);
|
|
106
|
+
}
|
|
107
|
+
// The default project only stood in for the repository's project, so opening it asks nothing.
|
|
108
|
+
if (fresh) host.markSaved?.();
|
|
109
|
+
return creation.open(progress);
|
|
110
|
+
};
|
|
111
|
+
const createIn = async (request: NewProjectRequest, signal: AbortSignal): Promise<NewProjectResult> => {
|
|
112
|
+
if (request.destination === 'memory') {
|
|
113
|
+
host.create();
|
|
114
|
+
return 'created';
|
|
115
|
+
}
|
|
116
|
+
const {title, saveName} = request;
|
|
117
|
+
if (request.destination === 'github') {
|
|
118
|
+
if (!repositoryCreation || !host.serialize) return 'cancelled';
|
|
119
|
+
// Pressing Create again after a stopped setup resumes it instead of making another repository.
|
|
120
|
+
const pending = (await repositoryCreation.list()).find(entry =>
|
|
121
|
+
entry.owner.toLowerCase() === request.draft.owner.toLowerCase() &&
|
|
122
|
+
entry.name.toLowerCase() === request.draft.name.toLowerCase());
|
|
123
|
+
if (pending) return await finishRepository(repositoryCreation, pending, signal, false) ? 'created' : 'cancelled';
|
|
124
|
+
// The folder is picked first, while the click that asked for it still allows the picker.
|
|
125
|
+
const target = request.workingCopy === 'device' ? await host.pickDeviceFolder?.() : 'browser';
|
|
126
|
+
if (!target || !await host.openDefaultProject(signal)) return 'cancelled';
|
|
127
|
+
host.nameProject(title, saveName);
|
|
128
|
+
const open = host.trackProject?.() ?? (() => true);
|
|
129
|
+
const current = () => !signal.aborted && open();
|
|
130
|
+
const progress = await repositoryCreation.start(request.draft, () => host.serialize(title), current, signal, target)
|
|
131
|
+
.catch(error => {throw repositoryError(error);});
|
|
132
|
+
if (!progress) return 'cancelled';
|
|
133
|
+
// Any other result goes on to advance, as the Editor's own flow did.
|
|
134
|
+
return await finishRepository(repositoryCreation, progress as RepositoryCreationProgress, signal, true) ?
|
|
135
|
+
'created' : 'cancelled';
|
|
136
|
+
}
|
|
137
|
+
const target = request.destination === 'device' ? await host.pickDeviceFolder?.() : 'browser';
|
|
138
|
+
if (!target || !await host.openDefaultProject(signal)) return 'cancelled';
|
|
139
|
+
host.nameProject(title, saveName);
|
|
140
|
+
return await host.createFolder(target, title, signal) ? 'created' : 'cancelled';
|
|
141
|
+
};
|
|
142
|
+
return {createIn, newProject: {
|
|
143
|
+
defaultTitle: host.defaultTitle,
|
|
144
|
+
browserFolders: host.browserFolders,
|
|
145
|
+
deviceFolders: host.deviceFolders,
|
|
146
|
+
github: repositoryCreation && {
|
|
147
|
+
connectForCreation: interaction => repositoryCreation.connect(interaction),
|
|
148
|
+
licenses: repositoryCreation.licenses
|
|
149
|
+
}
|
|
150
|
+
}};
|
|
151
|
+
}
|
|
@@ -147,3 +147,64 @@ ul:has(> li .tw-project-file-menu__item) {
|
|
|
147
147
|
.tw-project-file-dialog [role="status"]:empty {
|
|
148
148
|
display: none;
|
|
149
149
|
}
|
|
150
|
+
|
|
151
|
+
/* A notice that does not block the editor (showNotice), anchored like the editor's other snackbars. */
|
|
152
|
+
.tw-project-notice {
|
|
153
|
+
box-sizing: border-box;
|
|
154
|
+
position: fixed;
|
|
155
|
+
/* Fixed bottom/right can follow an overflowing layout viewport on mobile. */
|
|
156
|
+
bottom: auto;
|
|
157
|
+
top: 100vh;
|
|
158
|
+
top: 100dvh;
|
|
159
|
+
left: 50vw;
|
|
160
|
+
right: auto;
|
|
161
|
+
transform: translate(-50%, -100%);
|
|
162
|
+
z-index: 10000;
|
|
163
|
+
display: flex;
|
|
164
|
+
align-items: flex-start;
|
|
165
|
+
gap: 16px;
|
|
166
|
+
width: calc(100vw - 32px);
|
|
167
|
+
max-width: 760px;
|
|
168
|
+
max-height: calc(100vh - 32px);
|
|
169
|
+
max-height: calc(100dvh - 32px);
|
|
170
|
+
margin: 0 auto;
|
|
171
|
+
padding: 14px 16px;
|
|
172
|
+
padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
|
|
173
|
+
overflow: auto;
|
|
174
|
+
border: 1px solid #90a4bf;
|
|
175
|
+
border-radius: 8px;
|
|
176
|
+
background: #243247;
|
|
177
|
+
color: #fff;
|
|
178
|
+
font: 14px/1.5 system-ui, sans-serif;
|
|
179
|
+
box-shadow: 0 4px 20px #0006;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.tw-project-notice > span {
|
|
183
|
+
flex: 1 1 200px;
|
|
184
|
+
min-width: 0;
|
|
185
|
+
overflow-wrap: anywhere;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.tw-project-notice > button {
|
|
189
|
+
display: inline-flex;
|
|
190
|
+
flex: none;
|
|
191
|
+
align-items: center;
|
|
192
|
+
justify-content: center;
|
|
193
|
+
min-width: 44px;
|
|
194
|
+
min-height: 44px;
|
|
195
|
+
padding: 0;
|
|
196
|
+
border: 1px solid currentColor;
|
|
197
|
+
border-radius: 6px;
|
|
198
|
+
background: transparent;
|
|
199
|
+
color: inherit;
|
|
200
|
+
cursor: pointer;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.tw-project-notice > button:hover {
|
|
204
|
+
background: #ffffff1a;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
.tw-project-notice > button:focus-visible {
|
|
208
|
+
outline: 3px solid #ffd166;
|
|
209
|
+
outline-offset: 2px;
|
|
210
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** The class of the notice; its styles are in `project-file-menu.css`. */
|
|
2
|
+
export const projectNoticeClassName = 'tw-project-notice';
|
|
3
|
+
|
|
4
|
+
const shown = new WeakMap<Document, () => void>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shows a short notice at the bottom of the window that does not block the editor, such as what a title change
|
|
8
|
+
* could not rename. It has a dismiss button (an X with a localized `title` and `aria-label`) and hides itself after
|
|
9
|
+
* `timeout` milliseconds (12 s by default) unless focus is inside it. A new notice replaces the one shown in the same
|
|
10
|
+
* document. Returns a function that removes the notice.
|
|
11
|
+
*/
|
|
12
|
+
export function showNotice(document: Document, text: string, {locale, timeout = 12000}: {
|
|
13
|
+
locale: string;
|
|
14
|
+
timeout?: number;
|
|
15
|
+
}): () => void {
|
|
16
|
+
shown.get(document)?.();
|
|
17
|
+
const view = document.defaultView;
|
|
18
|
+
const ja = locale.startsWith('ja');
|
|
19
|
+
const notice = document.createElement('aside');
|
|
20
|
+
notice.className = projectNoticeClassName;
|
|
21
|
+
notice.setAttribute('data-project-tools', '');
|
|
22
|
+
notice.setAttribute('data-testid', 'project-notice-snackbar');
|
|
23
|
+
const message = document.createElement('span');
|
|
24
|
+
message.setAttribute('role', 'status');
|
|
25
|
+
message.setAttribute('aria-atomic', 'true');
|
|
26
|
+
message.textContent = text;
|
|
27
|
+
const dismiss = document.createElement('button');
|
|
28
|
+
dismiss.type = 'button';
|
|
29
|
+
const name = ja ? '閉じる' : 'Dismiss';
|
|
30
|
+
dismiss.title = name;
|
|
31
|
+
dismiss.setAttribute('aria-label', name);
|
|
32
|
+
const svg = 'http://www.w3.org/2000/svg';
|
|
33
|
+
const icon = document.createElementNS(svg, 'svg');
|
|
34
|
+
icon.setAttribute('viewBox', '0 0 16 16');
|
|
35
|
+
icon.setAttribute('width', '16');
|
|
36
|
+
icon.setAttribute('height', '16');
|
|
37
|
+
icon.setAttribute('aria-hidden', 'true');
|
|
38
|
+
const path = document.createElementNS(svg, 'path');
|
|
39
|
+
path.setAttribute('d', 'M4 4l8 8M12 4l-8 8');
|
|
40
|
+
path.setAttribute('stroke', 'currentColor');
|
|
41
|
+
path.setAttribute('stroke-width', '2');
|
|
42
|
+
path.setAttribute('stroke-linecap', 'round');
|
|
43
|
+
icon.append(path);
|
|
44
|
+
dismiss.append(icon);
|
|
45
|
+
notice.append(message, dismiss);
|
|
46
|
+
let timer = 0;
|
|
47
|
+
const remove = () => {
|
|
48
|
+
view?.clearTimeout(timer);
|
|
49
|
+
notice.remove();
|
|
50
|
+
if (shown.get(document) === remove) shown.delete(document);
|
|
51
|
+
};
|
|
52
|
+
dismiss.addEventListener('click', remove);
|
|
53
|
+
document.body.append(notice);
|
|
54
|
+
shown.set(document, remove);
|
|
55
|
+
timer = view?.setTimeout(() => {
|
|
56
|
+
if (!notice.contains(document.activeElement)) remove();
|
|
57
|
+
}, timeout) ?? 0;
|
|
58
|
+
return remove;
|
|
59
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {projectTitleFieldClassName} from './info-menu.tsx';
|
|
2
|
+
import {askSaveName, isValidSaveName, suggestSaveName} from './save-name.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Calls `onCommit` with the new title when the person commits an edit of the menu bar's title field
|
|
6
|
+
* ({@link projectTitleFieldClassName}), on Enter or when the field loses focus, as the GUI's own field does. Each
|
|
7
|
+
* edit is reported once. Enter while an IME is composing does not commit, and a title that did not change is not
|
|
8
|
+
* reported. Titles set by loading, a new project or the properties are not edits of the field, so they are not
|
|
9
|
+
* reported either. `readTitle` reads the project title the GUI holds; it is read one tick after the event, once the
|
|
10
|
+
* GUI has written the field's value. Returns a function that stops watching.
|
|
11
|
+
*/
|
|
12
|
+
export function followProjectTitleCommits(document: Document, {readTitle, onCommit}: {
|
|
13
|
+
readTitle(): string;
|
|
14
|
+
onCommit(title: string): void;
|
|
15
|
+
}): () => void {
|
|
16
|
+
const view = document.defaultView;
|
|
17
|
+
if (!view) return () => {};
|
|
18
|
+
const isTitleField = (target: EventTarget | null) =>
|
|
19
|
+
target instanceof view.HTMLInputElement && target.classList.contains(projectTitleFieldClassName);
|
|
20
|
+
let before: string | null = null;
|
|
21
|
+
let scheduled = false;
|
|
22
|
+
let stopped = false;
|
|
23
|
+
const focus = (event: Event) => {
|
|
24
|
+
if (isTitleField(event.target) && !scheduled) before = readTitle();
|
|
25
|
+
};
|
|
26
|
+
const settle = (event: Event) => {
|
|
27
|
+
if (before === null || scheduled || !isTitleField(event.target)) return;
|
|
28
|
+
if (event.type === 'keydown' && ((event as KeyboardEvent).key !== 'Enter' || (event as KeyboardEvent).isComposing)) return;
|
|
29
|
+
const previous = before;
|
|
30
|
+
scheduled = true;
|
|
31
|
+
// The field writes the title after this event (Enter also blurs it); read it once the GUI has handled both.
|
|
32
|
+
view.setTimeout(() => {
|
|
33
|
+
scheduled = false;
|
|
34
|
+
if (stopped) return;
|
|
35
|
+
const title = readTitle();
|
|
36
|
+
before = isTitleField(document.activeElement) ? title : null;
|
|
37
|
+
if (title !== previous) onCommit(title);
|
|
38
|
+
}, 0);
|
|
39
|
+
};
|
|
40
|
+
document.addEventListener('focusin', focus, true);
|
|
41
|
+
document.addEventListener('keydown', settle, true);
|
|
42
|
+
document.addEventListener('change', settle, true);
|
|
43
|
+
document.addEventListener('focusout', settle, true);
|
|
44
|
+
return () => {
|
|
45
|
+
stopped = true;
|
|
46
|
+
document.removeEventListener('focusin', focus, true);
|
|
47
|
+
document.removeEventListener('keydown', settle, true);
|
|
48
|
+
document.removeEventListener('change', settle, true);
|
|
49
|
+
document.removeEventListener('focusout', settle, true);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Where the open project is saved, for {@link reflectProjectTitle}. */
|
|
54
|
+
export type ProjectTitleLocation = 'browser' | 'device' | 'none';
|
|
55
|
+
|
|
56
|
+
export interface ReflectProjectTitleOptions {
|
|
57
|
+
/** The document that shows the save name question. */
|
|
58
|
+
document: Document;
|
|
59
|
+
locale: string;
|
|
60
|
+
/** The title the person committed. */
|
|
61
|
+
title: string;
|
|
62
|
+
/** The project's save name, `''` when it has none. */
|
|
63
|
+
saveName: string;
|
|
64
|
+
/**
|
|
65
|
+
* Where the project is saved: `browser` for a project folder in the browser's own storage, which can be renamed;
|
|
66
|
+
* `device` for a folder on the device, which the browser cannot rename; `none` when it is not saved in a folder.
|
|
67
|
+
*/
|
|
68
|
+
location: ProjectTitleLocation;
|
|
69
|
+
/** Whether the folder is bound to a Git repository, whose name does not change. */
|
|
70
|
+
gitBound: boolean;
|
|
71
|
+
/** Renames the project folder in the browser's storage; failures are ignored. Needed for `browser`. */
|
|
72
|
+
renameBrowserFolder?(title: string): Promise<void>;
|
|
73
|
+
/** Sets the project's save name, after the person entered one. */
|
|
74
|
+
setSaveName(name: string): void;
|
|
75
|
+
/** Shows a short notice, such as with {@link showNotice}. */
|
|
76
|
+
notify(text: string): void;
|
|
77
|
+
/**
|
|
78
|
+
* Whether the same project is still open. Checked after each wait, so a title committed just before another
|
|
79
|
+
* project opened does not rename or name the new one. Defaults to always true.
|
|
80
|
+
*/
|
|
81
|
+
current?(): boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Reflects a title the person committed in the menu bar on what the project is saved in. A folder in the browser's
|
|
86
|
+
* storage is renamed to the title; a folder on the device and a Git repository keep their names, and a notice says
|
|
87
|
+
* so. When the project is saved in a folder or repository, has no save name and the title cannot be used as one
|
|
88
|
+
* ({@link isValidSaveName}), the person is asked for a save name ({@link askSaveName}). An empty title, or a project
|
|
89
|
+
* that is not saved in a folder, changes nothing.
|
|
90
|
+
*/
|
|
91
|
+
export async function reflectProjectTitle({document, locale, title, saveName, location, gitBound, renameBrowserFolder,
|
|
92
|
+
setSaveName, notify, current = () => true}: ReflectProjectTitleOptions): Promise<void> {
|
|
93
|
+
if (location === 'none' && !gitBound || !title.trim()) return;
|
|
94
|
+
const ja = locale.startsWith('ja');
|
|
95
|
+
if (location === 'browser' && renameBrowserFolder) await renameBrowserFolder(title).catch(() => {});
|
|
96
|
+
if (!current()) return;
|
|
97
|
+
if (!isValidSaveName(title) && !saveName) {
|
|
98
|
+
const name = await askSaveName({document, locale, title, current: suggestSaveName(title) || undefined});
|
|
99
|
+
if (name && current()) setSaveName(name);
|
|
100
|
+
}
|
|
101
|
+
const notes: string[] = [];
|
|
102
|
+
if (location === 'device') notes.push(ja ?
|
|
103
|
+
'この作品のフォルダは手元のフォルダなので、フォルダ名は変わりません(ブラウザー版では手元のフォルダ名を変えられません)。' :
|
|
104
|
+
'This project folder is on your device, so its name stays as it is (the browser version cannot rename folders on your device).');
|
|
105
|
+
// Renaming the GitHub repository needs the keybroker's rename route first (#681).
|
|
106
|
+
if (gitBound) notes.push(ja ? 'Git のリポジトリの名前は、まだ変わりません。' : 'The Git repository keeps its name for now.');
|
|
107
|
+
if (notes.length && current()) notify(notes.join(' '));
|
|
108
|
+
}
|