@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.
- package/README.ja.md +51 -1
- package/README.md +51 -1
- 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/save-name.d.ts +33 -0
- package/dist/save-name.js +126 -0
- package/package.json +1 -1
- package/src/project-file-dialogs.ts +408 -7
- package/src/project-file-menu.css +55 -0
- package/src/project-file-menu.tsx +31 -6
- package/src/save-name.ts +130 -0
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import { dialogAction, dialogActions } from '@kubohiroya/turbowarp-plus-editor-windows';
|
|
2
|
+
import { checkSaveNameInput, isValidSaveName, saveNameTexts, suggestSaveName } from './save-name.js';
|
|
3
|
+
export { askSaveName, isValidSaveName, suggestSaveName } from './save-name.js';
|
|
4
|
+
/**
|
|
5
|
+
* The titles of the dialogs this package fills, by the File menu command that opens them: `save` is "Save Project"
|
|
6
|
+
* (the save form for a project with no save target yet), `save-as` is "Save a Copy of the Project" and `new` is
|
|
7
|
+
* "Create New Project".
|
|
8
|
+
*/
|
|
9
|
+
export function projectFileDialogTitle(command, locale) {
|
|
10
|
+
const ja = locale.startsWith('ja');
|
|
11
|
+
return command === 'new' ? (ja ? '新しいプロジェクトを作成' : 'Create New Project') :
|
|
12
|
+
command === 'save' ? (ja ? 'プロジェクトを保存' : 'Save Project') :
|
|
13
|
+
(ja ? 'プロジェクトのコピーを保存' : 'Save a Copy of the Project');
|
|
14
|
+
}
|
|
2
15
|
/**
|
|
3
16
|
* Saving over the last file or into the project folder first collects edits held by other windows,
|
|
4
17
|
* as Save As does, so a save that does not move the focus to the editor misses nothing. A failed
|
|
@@ -20,11 +33,25 @@ export function collectBeforeSaving(storage, folder, collect) {
|
|
|
20
33
|
folder: folder?.save ? { ...folder, save: first(folder.save) } : folder
|
|
21
34
|
};
|
|
22
35
|
}
|
|
23
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Save Project and Ctrl/⌘+S: overwrite the project's file or save into its folder; with no save target yet, run
|
|
38
|
+
* `saveNew`, which opens the save form whose result becomes the target. Pass the save-to-a-new-place action here,
|
|
39
|
+
* never Save a Copy, or a project without a target would only ever be copied.
|
|
40
|
+
*/
|
|
41
|
+
export function projectSaveShortcut(storage, folder, saveNew) {
|
|
24
42
|
if (storage.canSave)
|
|
25
|
-
(storage.overwrite || folder?.save ||
|
|
43
|
+
(storage.overwrite || folder?.save || saveNew)?.();
|
|
26
44
|
}
|
|
27
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Saves the project as an SB3: picks the file (or downloads it), collects edits held by other windows, serializes
|
|
47
|
+
* it under the file's title and writes it, then runs `committed`.
|
|
48
|
+
*
|
|
49
|
+
* - `mode: 'copy'` (the default, Save a Copy of the Project): the project stays as it was; its file, folder, title
|
|
50
|
+
* and unsaved state do not change. `storage.copied` runs last.
|
|
51
|
+
* - `mode: 'save'` (Save Project with no save target yet): the file becomes the project's save target through
|
|
52
|
+
* `storage.finish(handle, title, unchanged())`.
|
|
53
|
+
*/
|
|
54
|
+
export async function saveProjectAs(storage, name, { current, unchanged, serialize, committed, collect, mode = 'copy' }) {
|
|
28
55
|
if (!storage.canSave || !current())
|
|
29
56
|
return false;
|
|
30
57
|
const filename = name + '.sb3';
|
|
@@ -48,7 +75,10 @@ export async function saveProjectAs(storage, name, { current, unchanged, seriali
|
|
|
48
75
|
if (!current())
|
|
49
76
|
return false;
|
|
50
77
|
committed();
|
|
51
|
-
|
|
78
|
+
if (mode === 'save')
|
|
79
|
+
storage.finish(handle, title, unchanged ? unchanged() : true);
|
|
80
|
+
else
|
|
81
|
+
storage.copied?.(handle, title);
|
|
52
82
|
return true;
|
|
53
83
|
}
|
|
54
84
|
catch (error) {
|
|
@@ -56,8 +86,7 @@ export async function saveProjectAs(storage, name, { current, unchanged, seriali
|
|
|
56
86
|
return false;
|
|
57
87
|
}
|
|
58
88
|
}
|
|
59
|
-
const validRepositoryName =
|
|
60
|
-
name !== '.' && name !== '..' && !/\.git$/i.test(name);
|
|
89
|
+
const validRepositoryName = isValidSaveName;
|
|
61
90
|
function parseGitHubRepositoryUrl(url) {
|
|
62
91
|
const match = /^https:\/\/github\.com\/([A-Za-z0-9][A-Za-z0-9-]{0,38})\/([A-Za-z0-9_.-]{1,104})\/?$/i.exec(url.trim());
|
|
63
92
|
const name = match?.[2].replace(/\.git$/iu, '');
|
|
@@ -93,6 +122,9 @@ function createGitHubFields(document, label, provider, report) {
|
|
|
93
122
|
const name = document.createElement('input');
|
|
94
123
|
name.name = 'github-name';
|
|
95
124
|
name.maxLength = 100;
|
|
125
|
+
// A name the person typed is kept; until then the name follows the suggestion from the form.
|
|
126
|
+
let nameEdited = false;
|
|
127
|
+
name.addEventListener('input', () => { nameEdited = true; });
|
|
96
128
|
appendField(panel, label('リポジトリ名', 'Repository name'), name);
|
|
97
129
|
const url = document.createElement('input');
|
|
98
130
|
url.name = 'github-url';
|
|
@@ -142,6 +174,10 @@ function createGitHubFields(document, label, provider, report) {
|
|
|
142
174
|
return {
|
|
143
175
|
panel,
|
|
144
176
|
get connected() { return account !== null; },
|
|
177
|
+
suggestName(value) {
|
|
178
|
+
if (!nameEdited)
|
|
179
|
+
name.value = value;
|
|
180
|
+
},
|
|
145
181
|
async connect(open, interaction) {
|
|
146
182
|
const result = await provider.connectForCreation(interaction);
|
|
147
183
|
if (!open() || !result)
|
|
@@ -161,6 +197,7 @@ function createGitHubFields(document, label, provider, report) {
|
|
|
161
197
|
throw invalidDraft(label('https://github.com/所有者/リポジトリ名 の形式で入力してください。', 'Enter a URL in the form https://github.com/owner/repository.'));
|
|
162
198
|
requestedOwner = parsed.owner;
|
|
163
199
|
name.value = parsed.name;
|
|
200
|
+
nameEdited = true;
|
|
164
201
|
url.value = '';
|
|
165
202
|
if (account)
|
|
166
203
|
fillOwners(account);
|
|
@@ -626,3 +663,317 @@ export function renderExtensionsForm(document, locale, close, add, { list, chang
|
|
|
626
663
|
};
|
|
627
664
|
return form;
|
|
628
665
|
}
|
|
666
|
+
const deviceFolderReason = (label) => label('このブラウザーでは端末のフォルダを開けません。Chrome か Edge を使うか、ブラウザーに保存したフォルダを開いてください。', 'This browser cannot open folders on your device. Use Chrome or Edge, or open a folder saved in this browser.');
|
|
667
|
+
let newProjectForms = 0;
|
|
668
|
+
/**
|
|
669
|
+
* The Create New Project… form. The in-memory project is selected first, so Create alone does what Create New
|
|
670
|
+
* Project always did. A folder or a GitHub repository also takes a title, and a save name when the title cannot be
|
|
671
|
+
* used as one. `close` receives `created`, or `cancelled` when the person dismissed the form.
|
|
672
|
+
*/
|
|
673
|
+
export function renderNewProjectForm(document, locale, options, close) {
|
|
674
|
+
const label = (ja, en) => locale.startsWith('ja') ? ja : en;
|
|
675
|
+
const id = `tw-new-project-${++newProjectForms}`;
|
|
676
|
+
const { browserFolders, deviceFolders, github: githubProvider } = options;
|
|
677
|
+
const workingCopies = [...(deviceFolders !== undefined ? ['device'] : []), ...(browserFolders ? ['browser'] : [])];
|
|
678
|
+
const form = document.createElement('form');
|
|
679
|
+
form.setAttribute('data-new-project-form', '');
|
|
680
|
+
const fields = document.createElement('div');
|
|
681
|
+
const destinations = document.createElement('fieldset');
|
|
682
|
+
const destinationLegend = document.createElement('legend');
|
|
683
|
+
destinationLegend.textContent = label('作成先', 'Create in');
|
|
684
|
+
destinations.append(destinationLegend);
|
|
685
|
+
const radio = (parent, group, value, title, reason) => {
|
|
686
|
+
const input = document.createElement('input');
|
|
687
|
+
input.type = 'radio';
|
|
688
|
+
input.name = group;
|
|
689
|
+
input.value = value;
|
|
690
|
+
const row = document.createElement('label');
|
|
691
|
+
row.append(input, title);
|
|
692
|
+
parent.append(row);
|
|
693
|
+
if (reason) {
|
|
694
|
+
input.disabled = true;
|
|
695
|
+
row.title = reason;
|
|
696
|
+
const note = document.createElement('p');
|
|
697
|
+
note.id = `${id}-${group}-${value}-reason`;
|
|
698
|
+
note.textContent = reason;
|
|
699
|
+
input.setAttribute('aria-describedby', note.id);
|
|
700
|
+
parent.append(note);
|
|
701
|
+
}
|
|
702
|
+
return input;
|
|
703
|
+
};
|
|
704
|
+
const unavailable = deviceFolders === false ? deviceFolderReason(label) : undefined;
|
|
705
|
+
radio(destinations, 'destination', 'memory', label('メモリ上の SB3', 'In memory (SB3)')).checked = true;
|
|
706
|
+
if (browserFolders)
|
|
707
|
+
radio(destinations, 'destination', 'browser', label('ブラウザー内のプロジェクトフォルダ', 'Project folder in this browser'));
|
|
708
|
+
if (deviceFolders !== undefined)
|
|
709
|
+
radio(destinations, 'destination', 'device', label('手元のプロジェクトフォルダ', 'Project folder on this device'), unavailable);
|
|
710
|
+
if (githubProvider && workingCopies.length)
|
|
711
|
+
radio(destinations, 'destination', 'github', label('Git のリモートリポジトリ', 'Git remote repository (GitHub)'));
|
|
712
|
+
fields.append(destinations);
|
|
713
|
+
const title = document.createElement('input');
|
|
714
|
+
title.name = 'title';
|
|
715
|
+
title.maxLength = 100;
|
|
716
|
+
title.value = options.defaultTitle ?? label('新しいプロジェクト', 'Untitled Project');
|
|
717
|
+
const titleField = appendField(fields, label('作品名', 'Project title'), title);
|
|
718
|
+
const texts = saveNameTexts(locale);
|
|
719
|
+
const saveNameBlock = document.createElement('div');
|
|
720
|
+
const saveNameExplanation = document.createElement('p');
|
|
721
|
+
saveNameExplanation.textContent = texts.explanation;
|
|
722
|
+
const saveName = document.createElement('input');
|
|
723
|
+
saveName.name = 'save-name';
|
|
724
|
+
saveName.maxLength = 100;
|
|
725
|
+
saveName.autocomplete = 'off';
|
|
726
|
+
saveName.spellcheck = false;
|
|
727
|
+
const allowed = document.createElement('p');
|
|
728
|
+
allowed.id = `${id}-save-name-allowed`;
|
|
729
|
+
allowed.textContent = texts.allowed;
|
|
730
|
+
saveName.setAttribute('aria-describedby', allowed.id);
|
|
731
|
+
const saveNameError = document.createElement('p');
|
|
732
|
+
saveNameError.setAttribute('role', 'alert');
|
|
733
|
+
saveNameBlock.append(saveNameExplanation);
|
|
734
|
+
const saveNameField = appendField(saveNameBlock, texts.title, saveName);
|
|
735
|
+
saveNameBlock.append(allowed, saveNameError);
|
|
736
|
+
fields.append(saveNameBlock);
|
|
737
|
+
let saveNameEdited = false;
|
|
738
|
+
const status = document.createElement('p');
|
|
739
|
+
status.setAttribute('role', 'alert');
|
|
740
|
+
const report = (message) => { status.textContent = message; };
|
|
741
|
+
const progress = document.createElement('p');
|
|
742
|
+
progress.setAttribute('role', 'status');
|
|
743
|
+
const github = githubProvider && workingCopies.length ?
|
|
744
|
+
createGitHubFields(document, label, githubProvider, report) : null;
|
|
745
|
+
const workingCopy = document.createElement('fieldset');
|
|
746
|
+
if (github) {
|
|
747
|
+
const legend = document.createElement('legend');
|
|
748
|
+
legend.textContent = label('作業用の写しの置き場所', 'Keep the working copy');
|
|
749
|
+
workingCopy.append(legend);
|
|
750
|
+
if (deviceFolders !== undefined)
|
|
751
|
+
radio(workingCopy, 'working-copy', 'device', label('手元のプロジェクトフォルダ', 'Project folder on this device'), unavailable);
|
|
752
|
+
if (browserFolders)
|
|
753
|
+
radio(workingCopy, 'working-copy', 'browser', label('ブラウザー内のプロジェクトフォルダ', 'Project folder in this browser'));
|
|
754
|
+
const preferred = [...workingCopy.querySelectorAll('input')].find(input => !input.disabled);
|
|
755
|
+
if (preferred)
|
|
756
|
+
preferred.checked = true;
|
|
757
|
+
github.panel.append(workingCopy);
|
|
758
|
+
fields.append(github.panel);
|
|
759
|
+
}
|
|
760
|
+
const review = document.createElement('div');
|
|
761
|
+
const device = document.createElement('div');
|
|
762
|
+
const controller = new AbortController();
|
|
763
|
+
let dismissed = false;
|
|
764
|
+
const open = () => !dismissed && form.isConnected;
|
|
765
|
+
const interaction = {
|
|
766
|
+
signal: controller.signal,
|
|
767
|
+
deviceCode({ userCode, verificationUri }) {
|
|
768
|
+
if (!open())
|
|
769
|
+
return;
|
|
770
|
+
const link = document.createElement('a');
|
|
771
|
+
link.href = verificationUri;
|
|
772
|
+
link.target = '_blank';
|
|
773
|
+
link.rel = 'noopener noreferrer';
|
|
774
|
+
link.textContent = label('GitHub で認証する', 'Authorize on GitHub');
|
|
775
|
+
const code = document.createElement('strong');
|
|
776
|
+
code.textContent = userCode;
|
|
777
|
+
device.replaceChildren(link, ' ', code);
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
const back = document.createElement('button');
|
|
781
|
+
back.type = 'button';
|
|
782
|
+
back.textContent = label('戻る', 'Back');
|
|
783
|
+
const cancel = document.createElement('button');
|
|
784
|
+
cancel.type = 'button';
|
|
785
|
+
cancel.textContent = label('キャンセル', 'Cancel');
|
|
786
|
+
cancel.setAttribute('data-new-project-cancel', '');
|
|
787
|
+
const submit = document.createElement('button');
|
|
788
|
+
submit.type = 'submit';
|
|
789
|
+
form.append(fields, review, device, progress, status, dialogActions(document, back, dialogAction(cancel, 'cancel'), dialogAction(submit, 'confirm')));
|
|
790
|
+
let busy = false;
|
|
791
|
+
let reviewed = null;
|
|
792
|
+
const finish = (result) => {
|
|
793
|
+
if (dismissed)
|
|
794
|
+
return;
|
|
795
|
+
dismissed = true;
|
|
796
|
+
controller.abort();
|
|
797
|
+
close(result);
|
|
798
|
+
};
|
|
799
|
+
cancel.onclick = () => finish('cancelled');
|
|
800
|
+
const destination = () => (form.querySelector('input[name="destination"]:checked')?.value ??
|
|
801
|
+
'memory');
|
|
802
|
+
const selectedWorkingCopy = () => workingCopy.querySelector('input:checked')?.value;
|
|
803
|
+
const titleValue = () => title.value.trim();
|
|
804
|
+
const titleUsable = () => isValidSaveName(titleValue());
|
|
805
|
+
const effectiveSaveName = () => titleUsable() ? titleValue() : saveName.value.trim();
|
|
806
|
+
const refresh = () => {
|
|
807
|
+
const selected = destination();
|
|
808
|
+
const reviewing = reviewed !== null;
|
|
809
|
+
fields.hidden = reviewing;
|
|
810
|
+
review.hidden = !reviewing;
|
|
811
|
+
back.hidden = !reviewing;
|
|
812
|
+
back.disabled = busy;
|
|
813
|
+
destinations.disabled = busy || reviewing;
|
|
814
|
+
titleField.hidden = selected === 'memory';
|
|
815
|
+
title.disabled = busy || reviewing || selected === 'memory';
|
|
816
|
+
// Only a folder takes the save name here; for GitHub the repository name is the save name.
|
|
817
|
+
saveNameBlock.hidden = selected === 'memory' || titleUsable();
|
|
818
|
+
saveNameField.hidden = selected === 'github';
|
|
819
|
+
saveName.disabled = busy || reviewing || saveNameBlock.hidden || selected === 'github';
|
|
820
|
+
if (github) {
|
|
821
|
+
github.panel.hidden = selected !== 'github';
|
|
822
|
+
github.panel.disabled = busy || reviewing || github.panel.hidden;
|
|
823
|
+
}
|
|
824
|
+
submit.disabled = busy;
|
|
825
|
+
submit.textContent = selected === 'github' ? (reviewing ? label('リポジトリを作成', 'Create Repository') :
|
|
826
|
+
github.connected ? label('作成内容を確認', 'Review Repository') : label('GitHub へ接続…', 'Connect to GitHub…')) :
|
|
827
|
+
selected === 'device' ? label('フォルダを選んで作成…', 'Choose a Folder and Create…') : label('作成', 'Create');
|
|
828
|
+
};
|
|
829
|
+
const followTitle = () => {
|
|
830
|
+
if (!titleUsable() && !saveNameEdited)
|
|
831
|
+
saveName.value = suggestSaveName(titleValue());
|
|
832
|
+
if (!saveNameBlock.hidden && saveName.value)
|
|
833
|
+
checkSaveNameInput(saveName, saveNameError, locale);
|
|
834
|
+
else {
|
|
835
|
+
saveName.removeAttribute('aria-invalid');
|
|
836
|
+
saveNameError.textContent = '';
|
|
837
|
+
}
|
|
838
|
+
github?.suggestName(effectiveSaveName());
|
|
839
|
+
};
|
|
840
|
+
title.addEventListener('input', () => { report(''); refresh(); followTitle(); });
|
|
841
|
+
saveName.addEventListener('input', () => {
|
|
842
|
+
saveNameEdited = true;
|
|
843
|
+
checkSaveNameInput(saveName, saveNameError, locale);
|
|
844
|
+
github?.suggestName(effectiveSaveName());
|
|
845
|
+
});
|
|
846
|
+
destinations.addEventListener('change', () => { report(''); refresh(); followTitle(); });
|
|
847
|
+
back.onclick = () => {
|
|
848
|
+
reviewed = null;
|
|
849
|
+
report('');
|
|
850
|
+
refresh();
|
|
851
|
+
submit.focus();
|
|
852
|
+
};
|
|
853
|
+
refresh();
|
|
854
|
+
followTitle();
|
|
855
|
+
if (github && document.defaultView?.MutationObserver) {
|
|
856
|
+
const observer = new document.defaultView.MutationObserver(() => {
|
|
857
|
+
if (!open()) {
|
|
858
|
+
observer.disconnect();
|
|
859
|
+
controller.abort();
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
863
|
+
}
|
|
864
|
+
const run = async (request) => {
|
|
865
|
+
progress.textContent = label('作成しています…', 'Creating…');
|
|
866
|
+
try {
|
|
867
|
+
const result = await options.createIn(request, controller.signal);
|
|
868
|
+
if (open() && result === 'created')
|
|
869
|
+
finish('created');
|
|
870
|
+
}
|
|
871
|
+
finally {
|
|
872
|
+
if (open())
|
|
873
|
+
progress.textContent = '';
|
|
874
|
+
}
|
|
875
|
+
};
|
|
876
|
+
form.onsubmit = async (event) => {
|
|
877
|
+
event.preventDefault();
|
|
878
|
+
if (!open() || busy)
|
|
879
|
+
return;
|
|
880
|
+
const selected = destination();
|
|
881
|
+
report('');
|
|
882
|
+
if (selected === 'memory' && options.create) {
|
|
883
|
+
options.create();
|
|
884
|
+
finish('created');
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
busy = true;
|
|
888
|
+
refresh();
|
|
889
|
+
let focus = null;
|
|
890
|
+
try {
|
|
891
|
+
if (selected === 'memory') {
|
|
892
|
+
await run({ destination: 'memory' });
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
const projectTitle = titleValue();
|
|
896
|
+
if (!projectTitle) {
|
|
897
|
+
report(label('作品名を入力してください。', 'Enter a project title.'));
|
|
898
|
+
focus = title;
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
if (selected !== 'github') {
|
|
902
|
+
const name = effectiveSaveName();
|
|
903
|
+
if (!isValidSaveName(name)) {
|
|
904
|
+
checkSaveNameInput(saveName, saveNameError, locale);
|
|
905
|
+
report(texts.invalid);
|
|
906
|
+
focus = saveName;
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
await run({ destination: selected, title: projectTitle, saveName: name });
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
if (!reviewed) {
|
|
913
|
+
github.applyUrl();
|
|
914
|
+
if (!github.connected) {
|
|
915
|
+
report(label('GitHub に接続しています…', 'Connecting to GitHub…'));
|
|
916
|
+
if (await github.connect(open, interaction))
|
|
917
|
+
focus = submit;
|
|
918
|
+
if (open())
|
|
919
|
+
report('');
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
const place = selectedWorkingCopy();
|
|
923
|
+
if (!place)
|
|
924
|
+
throw new Error(label('作業用の写しの置き場所を選んでください。', 'Choose where to keep the working copy.'));
|
|
925
|
+
reviewed = github.draft(projectTitle);
|
|
926
|
+
focus = github.review(reviewed, review);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
await run({ destination: 'github', title: projectTitle, saveName: reviewed.name, draft: reviewed,
|
|
930
|
+
workingCopy: selectedWorkingCopy() });
|
|
931
|
+
}
|
|
932
|
+
catch (error) {
|
|
933
|
+
if (open())
|
|
934
|
+
report(error instanceof Error ? error.message : String(error));
|
|
935
|
+
}
|
|
936
|
+
finally {
|
|
937
|
+
device.replaceChildren();
|
|
938
|
+
busy = false;
|
|
939
|
+
if (open()) {
|
|
940
|
+
refresh();
|
|
941
|
+
focus?.focus();
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
return form;
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Shows {@link renderNewProjectForm} in a modal dialog and resolves `created` or `cancelled` when it closes.
|
|
949
|
+
* Escape cancels it. Without a document body it resolves `cancelled`.
|
|
950
|
+
*/
|
|
951
|
+
export function openNewProjectDialog(document, locale, options) {
|
|
952
|
+
if (!document?.body)
|
|
953
|
+
return Promise.resolve('cancelled');
|
|
954
|
+
return new Promise(resolve => {
|
|
955
|
+
const dialog = document.createElement('dialog');
|
|
956
|
+
dialog.className = 'tw-project-file-dialog';
|
|
957
|
+
dialog.setAttribute('data-new-project-dialog', '');
|
|
958
|
+
const heading = document.createElement('h2');
|
|
959
|
+
heading.textContent = projectFileDialogTitle('new', locale);
|
|
960
|
+
dialog.setAttribute('aria-label', heading.textContent);
|
|
961
|
+
const form = renderNewProjectForm(document, locale, options, result => {
|
|
962
|
+
if (dialog.open && typeof dialog.close === 'function')
|
|
963
|
+
dialog.close();
|
|
964
|
+
dialog.remove();
|
|
965
|
+
resolve(result);
|
|
966
|
+
});
|
|
967
|
+
dialog.addEventListener('cancel', event => {
|
|
968
|
+
event.preventDefault();
|
|
969
|
+
form.querySelector('[data-new-project-cancel]')?.click();
|
|
970
|
+
});
|
|
971
|
+
dialog.append(heading, form);
|
|
972
|
+
document.body.append(dialog);
|
|
973
|
+
if (typeof dialog.showModal === 'function')
|
|
974
|
+
dialog.showModal();
|
|
975
|
+
else
|
|
976
|
+
dialog.setAttribute('open', '');
|
|
977
|
+
form.querySelector('input[name="destination"]:checked')?.focus();
|
|
978
|
+
});
|
|
979
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type { Action, FolderActions, ProjectFileStorage } from './project-file-dialogs.js';
|
|
2
|
+
import type { Action, FolderActions, NewProjectChoices, NewProjectRequest, NewProjectResult, ProjectFileStorage } from './project-file-dialogs.js';
|
|
3
3
|
interface SaveWorkspace {
|
|
4
4
|
saveScope: 'project' | 'tab';
|
|
5
5
|
canSaveActive: boolean;
|
|
@@ -9,6 +9,7 @@ interface SaveWorkspace {
|
|
|
9
9
|
type SaveEntries = {
|
|
10
10
|
storage: ProjectFileStorage;
|
|
11
11
|
folder?: FolderActions;
|
|
12
|
+
saveNew?: Action;
|
|
12
13
|
saveAs?: Action;
|
|
13
14
|
saveCommit?: Action;
|
|
14
15
|
};
|
|
@@ -23,13 +24,27 @@ export default function ProjectFileMenu({ locale, MenuItem, MenuSection, close,
|
|
|
23
24
|
close: Action;
|
|
24
25
|
workspace?: SaveWorkspace;
|
|
25
26
|
entries: {
|
|
27
|
+
/** Creates the default project in memory. Without `createIn`, Create New Project calls it directly. */
|
|
26
28
|
create: Action;
|
|
29
|
+
/**
|
|
30
|
+
* With it, Create New Project… opens the dialog that chooses where to create the project, and calls this
|
|
31
|
+
* for a folder or a GitHub repository (see `renderNewProjectForm`). The in-memory choice still calls `create`.
|
|
32
|
+
*/
|
|
33
|
+
createIn?: (request: NewProjectRequest, signal: AbortSignal) => Promise<NewProjectResult>;
|
|
34
|
+
/** What the Create New Project… dialog offers besides the in-memory project. */
|
|
35
|
+
newProject?: NewProjectChoices;
|
|
27
36
|
newWindow?: Action;
|
|
28
37
|
open: Action;
|
|
29
38
|
externalYaml?: React.ReactNode;
|
|
30
39
|
folder?: FolderActions;
|
|
31
40
|
openRemote?: Action;
|
|
32
41
|
storage: ProjectFileStorage;
|
|
42
|
+
/**
|
|
43
|
+
* Save Project for a project with no save target (no file handle, no folder): opens the save form titled
|
|
44
|
+
* `projectFileDialogTitle('save', locale)`, whose file or folder becomes the project's target.
|
|
45
|
+
*/
|
|
46
|
+
saveNew?: Action;
|
|
47
|
+
/** Save a Copy of the Project…: writes a copy and never changes the project's target. */
|
|
33
48
|
saveAs?: Action;
|
|
34
49
|
saveCommit?: Action;
|
|
35
50
|
exportProject?: Action;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { isMacPlatform } from '@kubohiroya/turbowarp-plus-editor-project-model/project-search-connections';
|
|
3
|
-
|
|
3
|
+
import { openNewProjectDialog } from './project-file-dialogs.js';
|
|
4
|
+
function saveEntries(locale, { storage, folder, saveNew, saveAs, saveCommit }) {
|
|
4
5
|
const ja = locale.startsWith('ja');
|
|
5
6
|
return [
|
|
6
|
-
|
|
7
|
-
['save
|
|
7
|
+
// Overwrites the file or saves into the folder; with no target yet, saveNew picks one that becomes the target.
|
|
8
|
+
['save', ja ? 'プロジェクトを保存' : 'Save Project',
|
|
9
|
+
storage.canSave ? storage.overwrite || folder?.save || saveNew : undefined],
|
|
10
|
+
// Saves a copy and leaves the project where it is; the command id stays `save-as` for icons and hosts.
|
|
11
|
+
['save-as', ja ? 'プロジェクトのコピーを保存…' : 'Save a Copy of the Project…', storage.canSave ? saveAs : undefined],
|
|
8
12
|
...(saveCommit ? [['save-commit', ja ? '保存してコミット…' : 'Save and Commit…', saveCommit]] : [])
|
|
9
13
|
];
|
|
10
14
|
}
|
|
@@ -66,8 +70,14 @@ export default function ProjectFileMenu({ locale, MenuItem, MenuSection, close,
|
|
|
66
70
|
showShortcut && React.createElement("kbd", { className: "tw-project-file-menu__shortcut", "aria-hidden": "true" }, mac ? '⌘S' : 'Ctrl+S')));
|
|
67
71
|
};
|
|
68
72
|
const e = entries;
|
|
73
|
+
const createIn = e.createIn;
|
|
74
|
+
const createNew = createIn ? ['new', ja ? '新しいプロジェクトを作成…' : 'Create New Project…', () => {
|
|
75
|
+
if (typeof document === 'undefined')
|
|
76
|
+
return;
|
|
77
|
+
void openNewProjectDialog(document, locale, { ...e.newProject, create: e.create, createIn });
|
|
78
|
+
}] : ['new', ja ? '新しいプロジェクトを作成' : 'Create New Project', e.create];
|
|
69
79
|
return React.createElement(React.Fragment, null,
|
|
70
|
-
item(
|
|
80
|
+
item(createNew),
|
|
71
81
|
e.newWindow && item(['window', ja ? '新しいウィンドウ' : 'New Window', e.newWindow]),
|
|
72
82
|
React.createElement(MenuSection, null,
|
|
73
83
|
item(['open', ja ? 'SB3ファイルを開く…' : 'Open SB3 File…', e.open]),
|
|
@@ -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.24.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",
|