mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-23 22:29:33 +01:00
Merge pull request #680: feat(cases): rename a case and its folder, with a lock and live-session handling
This commit is contained in:
commit
2fdbe786b3
18 files changed
+1454
-45
No files matched your search
@@ -1924,6 +1924,7 @@ const SSE_EVENTS = {
|
||||
CASE_CREATED: 'case:created',
|
||||
CASE_LINKED: 'case:linked',
|
||||
CASE_DELETED: 'case:deleted',
|
||||
CASE_RENAMED: 'case:renamed',
|
||||
CASE_ORDER_CHANGED: 'case:order-changed',
|
||||
DOCKER_EXPORT_COMPLETE: 'docker:exportComplete',
|
||||
DOCKER_EXPORT_FAILED: 'docker:exportFailed',
|
||||
|
||||
@@ -86,6 +86,17 @@
|
||||
'Web tab settings': '网页标签设置',
|
||||
'New group': '新建分组',
|
||||
'Rename group': '重命名分组',
|
||||
'Rename case': '重命名案例',
|
||||
Rename: '重命名',
|
||||
'New name': '新名称',
|
||||
'letters, numbers, - and _': '字母、数字、- 和 _',
|
||||
'Also rename the folder on disk': '同时重命名磁盘上的文件夹',
|
||||
'A case under the cases folder is renamed together with its folder.':
|
||||
'案例文件夹下的案例会连同其文件夹一起重命名。',
|
||||
'Stop sessions and rename': '停止会话并重命名',
|
||||
'Enter a different name.': '请输入不同的名称。',
|
||||
'Use only letters, numbers, hyphens and underscores.': '只能使用字母、数字、连字符和下划线。',
|
||||
'Failed to rename the case.': '重命名案例失败。',
|
||||
'Move group up': '上移分组',
|
||||
'Move group down': '下移分组',
|
||||
'Delete group': '删除分组',
|
||||
@@ -1336,6 +1347,12 @@
|
||||
(_m, version, shown, more) => `Codeman 已更新到 v${version}:${shown}${more ? `(另有 ${more} 项)` : ''}`,
|
||||
],
|
||||
[/^Selected: (.+)$/, (_m, value) => `已选择:${value}`],
|
||||
[/^Case renamed to "(.+)"$/, (_m, name) => `案例已重命名为“${name}”`],
|
||||
[/^New name for "(.+)"$/, (_m, name) => `“${name}”的新名称`],
|
||||
[
|
||||
/^These sessions have the folder open: (.+)\. They will be closed \(their tabs go away and nothing is restarted\) before the folder is renamed\.$/,
|
||||
(_m, list) => `这些会话正在使用该文件夹:${list}。重命名前将关闭它们(其标签页会消失,不会重新启动)。`,
|
||||
],
|
||||
[/^Failed to (.+)$/, (_m, action) => `操作失败:${action}`],
|
||||
[/^Will create: (.+)$/, (_m, path) => `将创建:${path}`],
|
||||
// The spreadsheet preview: an HTTP status, and the notice bar's counts.
|
||||
|
||||
@@ -3818,6 +3818,35 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Rename a case (and its folder): opened by the Manage tab's pencil button, see renameCase() in session-ui.js.
|
||||
Placed after the case modal so it stacks above it. -->
|
||||
<div class="modal" id="renameCaseModal">
|
||||
<div class="modal-backdrop" onclick="app.closeRenameCase()"></div>
|
||||
<div class="modal-content modal-sm">
|
||||
<div class="modal-header">
|
||||
<h3>Rename case</h3>
|
||||
<button class="modal-close" onclick="app.closeRenameCase()" aria-label="Cancel">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-row">
|
||||
<label for="renameCaseInput" id="renameCaseLabel">New name</label>
|
||||
<input type="text" id="renameCaseInput" class="set-input" maxlength="100" autocomplete="off" spellcheck="false"
|
||||
placeholder="letters, numbers, - and _" onkeydown="if (event.key === 'Enter') app.submitRenameCase()">
|
||||
</div>
|
||||
<div class="form-row hidden" id="renameCaseFolderRow">
|
||||
<label class="checkbox-row"><input type="checkbox" id="renameCaseFolder"> Also rename the folder on disk</label>
|
||||
<span class="form-hint" id="renameCaseFolderHint" data-i18n-skip></span>
|
||||
</div>
|
||||
<p class="form-hint" id="renameCaseNote">A case under the cases folder is renamed together with its folder.</p>
|
||||
<p class="form-hint hidden" id="renameCaseError" role="alert" style="color: var(--danger, #e5534b);"></p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn-toolbar" onclick="app.closeRenameCase()">Cancel</button>
|
||||
<button class="btn-toolbar btn-primary" id="renameCaseSubmit" onclick="app.submitRenameCase()">Rename</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Mobile Case Picker (Bottom Sheet) -->
|
||||
<div class="modal mobile-case-picker" id="mobileCasePickerModal">
|
||||
<div class="modal-backdrop" onclick="app.closeMobileCasePicker()"></div>
|
||||
|
||||
@@ -4975,6 +4975,12 @@ Object.assign(CodemanApp.prototype, {
|
||||
title="${reorderTitle || 'Move up'}" ${isFirst ? 'disabled' : ''}>▲</button>
|
||||
<button class="case-manage-btn" onclick="app.moveCaseDown(${escapeHtml(JSON.stringify(c.name))})"
|
||||
title="${reorderTitle || 'Move down'}" ${isLast ? 'disabled' : ''}>▼</button>
|
||||
${
|
||||
c.location === 'docker' || c.location === 'remote'
|
||||
? ''
|
||||
: `<button class="case-manage-btn" onclick="app.renameCase(${escapeHtml(JSON.stringify(c.name))})"
|
||||
title="Rename case (and its folder)">✎</button>`
|
||||
}
|
||||
<button class="case-manage-btn case-manage-btn-delete" onclick="app.deleteCase(${escapeHtml(JSON.stringify(c.name))})"
|
||||
title="Delete case">✕</button>
|
||||
</div>
|
||||
@@ -5007,6 +5013,113 @@ Object.assign(CodemanApp.prototype, {
|
||||
await this.saveCaseOrder(reordered.map(c => c.name));
|
||||
},
|
||||
|
||||
/**
|
||||
* Rename a case and its folder (POST /api/cases/:name/rename), in the app's own dialog.
|
||||
*
|
||||
* A linked case shows a checkbox for renaming the real folder too (unticked = the name only). When live
|
||||
* sessions have the folder open the server refuses and lists them; the dialog then lists them and its button
|
||||
* becomes "Stop sessions and rename", so they are only stopped after a second, explicit press. Errors from
|
||||
* the server (a taken name, a folder another program has open) stay inside the dialog so the name can be fixed.
|
||||
*/
|
||||
renameCase(name) {
|
||||
const info = (this.cases || []).find((c) => c.name === name);
|
||||
this._renameCaseState = { name, linked: Boolean(info?.linked), stop: false, busy: false };
|
||||
const input = document.getElementById('renameCaseInput');
|
||||
const label = document.getElementById('renameCaseLabel');
|
||||
if (label) label.textContent = `New name for "${name}"`;
|
||||
if (input) input.value = name;
|
||||
const row = document.getElementById('renameCaseFolderRow');
|
||||
row?.classList.toggle('hidden', !info?.linked);
|
||||
const box = document.getElementById('renameCaseFolder');
|
||||
if (box) box.checked = false;
|
||||
const hint = document.getElementById('renameCaseFolderHint');
|
||||
if (hint) hint.textContent = info?.linked ? `${info.path} (unticked: only the name in this list changes)` : '';
|
||||
document.getElementById('renameCaseNote')?.classList.toggle('hidden', Boolean(info?.linked));
|
||||
this._renameCaseNotice('');
|
||||
const submit = document.getElementById('renameCaseSubmit');
|
||||
if (submit) submit.textContent = 'Rename';
|
||||
document.getElementById('renameCaseModal')?.classList.add('active');
|
||||
input?.focus?.();
|
||||
input?.select?.();
|
||||
},
|
||||
|
||||
closeRenameCase() {
|
||||
document.getElementById('renameCaseModal')?.classList.remove('active');
|
||||
this._renameCaseState = null;
|
||||
},
|
||||
|
||||
/** Show (or clear, with '') a message inside the rename dialog. */
|
||||
_renameCaseNotice(message) {
|
||||
const el = document.getElementById('renameCaseError');
|
||||
if (!el) return;
|
||||
el.textContent = message;
|
||||
el.classList.toggle('hidden', !message);
|
||||
},
|
||||
|
||||
async submitRenameCase() {
|
||||
const state = this._renameCaseState;
|
||||
if (!state || state.busy) return;
|
||||
const name = state.name;
|
||||
const newName = (document.getElementById('renameCaseInput')?.value || '').trim();
|
||||
if (!newName || newName === name) {
|
||||
this._renameCaseNotice('Enter a different name.');
|
||||
return;
|
||||
}
|
||||
if (!/^[a-zA-Z0-9_-]+$/.test(newName)) {
|
||||
this._renameCaseNotice('Use only letters, numbers, hyphens and underscores.');
|
||||
return;
|
||||
}
|
||||
const body = { newName };
|
||||
if (state.linked) body.renameFolder = Boolean(document.getElementById('renameCaseFolder')?.checked);
|
||||
if (state.stop) body.stopSessions = true;
|
||||
state.busy = true;
|
||||
const submit = document.getElementById('renameCaseSubmit');
|
||||
if (submit) submit.disabled = true;
|
||||
try {
|
||||
const res = await fetch(`/api/cases/${encodeURIComponent(name)}/rename`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await res.json().catch(() => ({ success: false, error: 'Unexpected response' }));
|
||||
if (data.requiresStop) {
|
||||
const list = (data.sessions || []).map((s) => `${s.name}`).join(', ');
|
||||
state.stop = true;
|
||||
this._renameCaseNotice(`These sessions have the folder open: ${list}. They will be closed (their tabs go away and nothing is restarted) before the folder is renamed.`);
|
||||
if (submit) submit.textContent = 'Stop sessions and rename';
|
||||
return;
|
||||
}
|
||||
if (!data.success) {
|
||||
// A rename that failed after closing sessions says so: their tabs are gone either way.
|
||||
const closed = Number(data.stoppedSessions) || 0;
|
||||
const message = data.error || 'Failed to rename the case.';
|
||||
this._renameCaseNotice(
|
||||
closed > 0 ? `${message} (${closed} session${closed === 1 ? ' was' : 's were'} already closed.)` : message
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.closeRenameCase();
|
||||
this.showToast(`Case renamed to "${newName}"`, 'success');
|
||||
// The per-case run options are stored under the case name in this browser.
|
||||
try {
|
||||
const stored = localStorage.getItem('caseSettings_' + name);
|
||||
if (stored !== null) {
|
||||
localStorage.setItem('caseSettings_' + newName, stored);
|
||||
localStorage.removeItem('caseSettings_' + name);
|
||||
}
|
||||
} catch { /* storage blocked: the options fall back to their defaults */ }
|
||||
const select = document.getElementById('quickStartCase');
|
||||
const current = select?.value;
|
||||
await this.loadQuickStartCases(current === name ? newName : current);
|
||||
this.renderCaseManageList();
|
||||
} catch (err) {
|
||||
this._renameCaseNotice('Failed to rename the case: ' + err.message);
|
||||
} finally {
|
||||
state.busy = false;
|
||||
if (submit) submit.disabled = false;
|
||||
}
|
||||
},
|
||||
|
||||
async deleteCase(name) {
|
||||
if (!confirm(`Delete case "${name}"? Linked cases will only be unlinked (folder preserved). Created cases will be permanently deleted.`)) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user