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