@kubohiroya/turbowarp-plus-editor-workbench 0.23.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 +93 -1
- package/README.md +93 -1
- package/dist/new-project.d.ts +71 -0
- package/dist/new-project.js +89 -0
- package/dist/project-file-dialogs.d.ts +104 -4
- package/dist/project-file-dialogs.js +357 -6
- package/dist/project-file-menu.d.ts +16 -1
- package/dist/project-file-menu.js +14 -4
- 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/dist/save-name.d.ts +33 -0
- package/dist/save-name.js +126 -0
- package/package.json +13 -1
- package/src/new-project.ts +151 -0
- package/src/project-file-dialogs.ts +408 -7
- package/src/project-file-menu.css +116 -0
- package/src/project-file-menu.tsx +31 -6
- package/src/project-notice.ts +59 -0
- package/src/project-title.ts +108 -0
- package/src/save-name.ts +130 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The save name is the name a project is saved under where a project title cannot be used as is: a folder in the
|
|
3
|
+
* browser or on the device, and a GitHub repository. It follows the GitHub repository name rule, so one name works
|
|
4
|
+
* for all of them: letters, digits, `-`, `_` and `.`, up to 100 characters, neither `.` nor `..`, and not ending in
|
|
5
|
+
* `.git` (in any case).
|
|
6
|
+
*/
|
|
7
|
+
export declare function isValidSaveName(name: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Suggests a save name for a project title: its ASCII letters and digits in lower case, with accents removed and
|
|
10
|
+
* every other run of characters turned into one `-`. Returns `''` when nothing usable remains, as for a title
|
|
11
|
+
* written only in Japanese, so the person enters the name themselves.
|
|
12
|
+
*/
|
|
13
|
+
export declare function suggestSaveName(title: string): string;
|
|
14
|
+
/** The texts that explain the save name, shared by {@link askSaveName} and the new project form. */
|
|
15
|
+
export declare function saveNameTexts(locale: string): {
|
|
16
|
+
title: string;
|
|
17
|
+
explanation: string;
|
|
18
|
+
allowed: string;
|
|
19
|
+
invalid: string;
|
|
20
|
+
};
|
|
21
|
+
/** Marks a save name input valid or invalid and returns whether its trimmed value is a valid save name. */
|
|
22
|
+
export declare function checkSaveNameInput(input: HTMLInputElement, error: HTMLElement, locale: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Asks for a save name in a modal dialog, for a title that cannot be used as a folder or repository name. The input
|
|
25
|
+
* starts with `current`, or else with {@link suggestSaveName} of the title, and OK stays disabled until the name is
|
|
26
|
+
* valid. Resolves the trimmed name on OK and `null` on Cancel or Escape, or when the document cannot show a dialog.
|
|
27
|
+
*/
|
|
28
|
+
export declare function askSaveName({ document, locale, title, current }: {
|
|
29
|
+
document: Document;
|
|
30
|
+
locale: string;
|
|
31
|
+
title: string;
|
|
32
|
+
current?: string;
|
|
33
|
+
}): Promise<string | null>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { dialogAction, dialogActions } from '@kubohiroya/turbowarp-plus-editor-windows';
|
|
2
|
+
/**
|
|
3
|
+
* The save name is the name a project is saved under where a project title cannot be used as is: a folder in the
|
|
4
|
+
* browser or on the device, and a GitHub repository. It follows the GitHub repository name rule, so one name works
|
|
5
|
+
* for all of them: letters, digits, `-`, `_` and `.`, up to 100 characters, neither `.` nor `..`, and not ending in
|
|
6
|
+
* `.git` (in any case).
|
|
7
|
+
*/
|
|
8
|
+
export function isValidSaveName(name) {
|
|
9
|
+
return /^[A-Za-z0-9_.-]{1,100}$/.test(name) && name !== '.' && name !== '..' && !/\.git$/i.test(name);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Suggests a save name for a project title: its ASCII letters and digits in lower case, with accents removed and
|
|
13
|
+
* every other run of characters turned into one `-`. Returns `''` when nothing usable remains, as for a title
|
|
14
|
+
* written only in Japanese, so the person enters the name themselves.
|
|
15
|
+
*/
|
|
16
|
+
export function suggestSaveName(title) {
|
|
17
|
+
let name = title.normalize('NFKD').replace(/\p{M}/gu, '').toLowerCase()
|
|
18
|
+
.replace(/[^a-z0-9._-]+/g, '-').replace(/-{2,}/g, '-').replace(/^-+|-+$/g, '');
|
|
19
|
+
name = name.slice(0, 100).replace(/-+$/, '');
|
|
20
|
+
while (/\.git$/.test(name))
|
|
21
|
+
name = name.slice(0, -4).replace(/[-.]+$/, '');
|
|
22
|
+
return isValidSaveName(name) && /[a-z0-9]/.test(name) ? name : '';
|
|
23
|
+
}
|
|
24
|
+
/** The texts that explain the save name, shared by {@link askSaveName} and the new project form. */
|
|
25
|
+
export function saveNameTexts(locale) {
|
|
26
|
+
const ja = locale.startsWith('ja');
|
|
27
|
+
return {
|
|
28
|
+
title: ja ? '保存用の名前' : 'Save Name',
|
|
29
|
+
explanation: ja ?
|
|
30
|
+
'作品名に、フォルダ名やリポジトリ名に使えない文字が含まれています。保存に使う名前を入力してください。' :
|
|
31
|
+
'The project title contains characters that cannot be used in folder or repository names. ' +
|
|
32
|
+
'Enter the name to save it under.',
|
|
33
|
+
allowed: ja ? '使える文字:半角英数字、- _ . (100 文字まで。. や .. と、末尾の .git は使えません)' :
|
|
34
|
+
'Allowed: Letters A–Z and a–z, digits, -, _ and . (up to 100 characters; not . or .., and not ending in .git)',
|
|
35
|
+
invalid: ja ? 'この名前は使えません。使える文字の範囲を確かめてください。' :
|
|
36
|
+
'This name cannot be used. Check the allowed characters.'
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Marks a save name input valid or invalid and returns whether its trimmed value is a valid save name. */
|
|
40
|
+
export function checkSaveNameInput(input, error, locale) {
|
|
41
|
+
const valid = isValidSaveName(input.value.trim());
|
|
42
|
+
input.setAttribute('aria-invalid', String(!valid));
|
|
43
|
+
error.textContent = valid ? '' : saveNameTexts(locale).invalid;
|
|
44
|
+
return valid;
|
|
45
|
+
}
|
|
46
|
+
let dialogs = 0;
|
|
47
|
+
/**
|
|
48
|
+
* Asks for a save name in a modal dialog, for a title that cannot be used as a folder or repository name. The input
|
|
49
|
+
* starts with `current`, or else with {@link suggestSaveName} of the title, and OK stays disabled until the name is
|
|
50
|
+
* valid. Resolves the trimmed name on OK and `null` on Cancel or Escape, or when the document cannot show a dialog.
|
|
51
|
+
*/
|
|
52
|
+
export function askSaveName({ document, locale, title, current }) {
|
|
53
|
+
if (!document?.body)
|
|
54
|
+
return Promise.resolve(null);
|
|
55
|
+
const text = saveNameTexts(locale);
|
|
56
|
+
const ja = locale.startsWith('ja');
|
|
57
|
+
return new Promise(resolve => {
|
|
58
|
+
const dialog = document.createElement('dialog');
|
|
59
|
+
dialog.className = 'tw-project-file-dialog';
|
|
60
|
+
dialog.setAttribute('aria-label', text.title);
|
|
61
|
+
dialog.setAttribute('data-save-name-dialog', '');
|
|
62
|
+
const form = document.createElement('form');
|
|
63
|
+
const heading = document.createElement('h2');
|
|
64
|
+
heading.textContent = text.title;
|
|
65
|
+
const explanation = document.createElement('p');
|
|
66
|
+
explanation.textContent = text.explanation;
|
|
67
|
+
const projectTitle = document.createElement('p');
|
|
68
|
+
projectTitle.textContent = (ja ? '作品名:' : 'Project title: ') + title;
|
|
69
|
+
const label = document.createElement('label');
|
|
70
|
+
const input = document.createElement('input');
|
|
71
|
+
input.name = 'save-name';
|
|
72
|
+
input.maxLength = 100;
|
|
73
|
+
input.autocomplete = 'off';
|
|
74
|
+
input.spellcheck = false;
|
|
75
|
+
input.value = current ?? suggestSaveName(title);
|
|
76
|
+
const allowed = document.createElement('p');
|
|
77
|
+
allowed.id = `tw-save-name-allowed-${++dialogs}`;
|
|
78
|
+
allowed.textContent = text.allowed;
|
|
79
|
+
input.setAttribute('aria-describedby', allowed.id);
|
|
80
|
+
label.append(text.title, input);
|
|
81
|
+
const error = document.createElement('p');
|
|
82
|
+
error.setAttribute('role', 'alert');
|
|
83
|
+
const cancel = document.createElement('button');
|
|
84
|
+
cancel.type = 'button';
|
|
85
|
+
cancel.textContent = ja ? 'キャンセル' : 'Cancel';
|
|
86
|
+
const ok = document.createElement('button');
|
|
87
|
+
ok.type = 'submit';
|
|
88
|
+
ok.textContent = 'OK';
|
|
89
|
+
form.append(heading, explanation, projectTitle, label, allowed, error, dialogActions(document, dialogAction(cancel, 'cancel'), dialogAction(ok, 'confirm')));
|
|
90
|
+
dialog.append(form);
|
|
91
|
+
const check = () => {
|
|
92
|
+
const valid = checkSaveNameInput(input, error, locale);
|
|
93
|
+
ok.disabled = !valid;
|
|
94
|
+
return valid;
|
|
95
|
+
};
|
|
96
|
+
let settled = false;
|
|
97
|
+
const finish = (answer) => {
|
|
98
|
+
if (settled)
|
|
99
|
+
return;
|
|
100
|
+
settled = true;
|
|
101
|
+
if (dialog.open && typeof dialog.close === 'function')
|
|
102
|
+
dialog.close();
|
|
103
|
+
dialog.remove();
|
|
104
|
+
resolve(answer);
|
|
105
|
+
};
|
|
106
|
+
input.addEventListener('input', check);
|
|
107
|
+
cancel.addEventListener('click', () => finish(null));
|
|
108
|
+
dialog.addEventListener('cancel', event => { event.preventDefault(); finish(null); });
|
|
109
|
+
form.addEventListener('submit', event => {
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
if (check())
|
|
112
|
+
finish(input.value.trim());
|
|
113
|
+
});
|
|
114
|
+
// The first check shows nothing for an empty suggestion; the disabled OK is enough until the person types.
|
|
115
|
+
ok.disabled = !isValidSaveName(input.value.trim());
|
|
116
|
+
if (input.value)
|
|
117
|
+
check();
|
|
118
|
+
document.body.append(dialog);
|
|
119
|
+
if (typeof dialog.showModal === 'function')
|
|
120
|
+
dialog.showModal();
|
|
121
|
+
else
|
|
122
|
+
dialog.setAttribute('open', '');
|
|
123
|
+
input.focus();
|
|
124
|
+
input.select?.();
|
|
125
|
+
});
|
|
126
|
+
}
|
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
|
+
}
|