@kubohiroya/turbowarp-plus-editor-workbench 0.23.0 → 0.24.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.
@@ -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
+ }