diff --git a/frontend/src/views/CamerasView.vue b/frontend/src/views/CamerasView.vue index 32b0c89..725f2a8 100644 --- a/frontend/src/views/CamerasView.vue +++ b/frontend/src/views/CamerasView.vue @@ -4,7 +4,8 @@ * * 摄像头只能在 NVR 页「拉取通道 → 勾选导入」产生;这里负责查看 / 改名 / 启停 / 删除。 */ -import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'; +import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref } from 'vue'; +import type { ComponentPublicInstance } from 'vue'; import AppLayout from '@/components/AppLayout.vue'; import DataPager from '@/components/DataPager.vue'; import ModalBox from '@/components/ModalBox.vue'; @@ -156,6 +157,68 @@ async function toggleStatus(camera: Camera) { } } +// ---------------- 列表内双击改名 ---------------- +/** 正在改名的摄像头 id(null = 没有在改名) */ +const inlineId = ref(null); +const inlineName = ref(''); +const inlineSaving = ref(false); +const inlineInput = ref(null); + +/** v-for 里的 ref 会变成数组,这里用函数 ref 只留当前那个 input */ +function bindInlineInput(el: Element | ComponentPublicInstance | null) { + inlineInput.value = el instanceof HTMLInputElement ? el : null; +} + +function startRename(camera: Camera) { + if (inlineSaving.value) return; + inlineId.value = camera.id; + inlineName.value = camera.name || ''; + void nextTick(() => { + inlineInput.value?.focus(); + inlineInput.value?.select(); + }); +} + +function cancelRename() { + if (inlineSaving.value) return; + inlineId.value = null; + inlineName.value = ''; +} + +/** 提交改名:回车 / 失焦都走这里;名称没变或为空就只是取消 */ +async function commitRename(camera: Camera) { + if (inlineId.value !== camera.id || inlineSaving.value) return; + + const next = inlineName.value.trim(); + const previous = (camera.name || '').trim(); + + if (!next) { + error.value = '摄像头名称不能为空'; + cancelRename(); + return; + } + if (next === previous) { + cancelRename(); + return; + } + + inlineSaving.value = true; + error.value = ''; + okMsg.value = ''; + try { + await updateCamera(camera.id, { name: next }); + // 就地更新,避免为了一个名字整表重载(会丢滚动位置和当前页) + camera.name = next; + okMsg.value = `已改名:${next}`; + inlineId.value = null; + inlineName.value = ''; + } catch (err) { + error.value = err instanceof ApiError ? err.message : '改名失败'; + } finally { + inlineSaving.value = false; + } +} + /** * 页面关闭/刷新/切走时释放预览 * @@ -561,7 +624,7 @@ async function doPreset(action: 'set' | 'goto' | 'clear') { - 新增请到「NVR 录像机 → 拉取通道」 + 新增请到「NVR 录像机 → 拉取通道」;双击名称可直接改名
@@ -586,8 +649,18 @@ async function doPreset(action: 'set' | 'goto' | 'clear') { - - {{ camera.name || '—' }} + + + {{ camera.name || '—' }} {{ camera.channelName || '未命名' }} {{ camera.nvr ? (camera.nvr.name || camera.nvr.host) : '—' }} @@ -781,6 +854,24 @@ async function doPreset(action: 'set' | 'goto' | 'clear') { color: #cbd5e1; } +/* 列表内改名:名称列双击变成输入框(同一行的 td 高度稳定,不跳动) */ +.name-cell { + cursor: text; + min-width: 140px; +} + +.inline-name-input { + width: 100%; + min-width: 120px; + padding: 3px 6px; + font-size: 13px; + line-height: 1.4; + border: 1px solid #2563eb; + border-radius: 4px; + outline: none; + box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15); +} + .preview-head { display: flex; align-items: baseline;