feat: 使命令输入框内容与会话绑定

为每个会话引入独立的命令输入状态。不再共享命令输入框内容。
This commit is contained in:
Baobhan Sith
2025-05-02 23:24:45 +08:00
parent dd2836f44d
commit 68d573196a
2 changed files with 61 additions and 17 deletions
@@ -1,18 +1,20 @@
<script setup lang="ts">
import { ref, watch, nextTick, onMounted, onBeforeUnmount, defineExpose, computed } from 'vue';
import { ref, watch, nextTick, onMounted, onBeforeUnmount, defineExpose, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import { useFocusSwitcherStore } from '../stores/focusSwitcher.store';
import { useSettingsStore } from '../stores/settings.store';
import { useQuickCommandsStore } from '../stores/quickCommands.store';
import { useCommandHistoryStore } from '../stores/commandHistory.store';
import { storeToRefs } from 'pinia';
import { useSessionStore } from '../stores/session.store'; // +++ 导入 Session Store +++
import { useFocusSwitcherStore } from '../stores/focusSwitcher.store';
import { useSettingsStore } from '../stores/settings.store';
import { useQuickCommandsStore } from '../stores/quickCommands.store';
import { useCommandHistoryStore } from '../stores/commandHistory.store';
const emit = defineEmits(['send-command', 'search', 'find-next', 'find-previous', 'close-search', 'clear-terminal']); // 添加 clear-terminal 事件
const { t } = useI18n();
const focusSwitcherStore = useFocusSwitcherStore();
const settingsStore = useSettingsStore();
const quickCommandsStore = useQuickCommandsStore();
const commandHistoryStore = useCommandHistoryStore();
const settingsStore = useSettingsStore();
const quickCommandsStore = useQuickCommandsStore();
const commandHistoryStore = useCommandHistoryStore();
const sessionStore = useSessionStore(); // +++ 初始化 Session Store +++
// Get reactive setting from store
const { commandInputSyncTarget } = storeToRefs(settingsStore);
@@ -22,23 +24,44 @@ const { resetSelection: resetQuickCommandsSelection } = quickCommandsStore;
// Get reactive state and actions from command history store
const { selectedIndex: historySelectedIndex, filteredHistory: historyFiltered } = storeToRefs(commandHistoryStore);
const { resetSelection: resetHistorySelection } = commandHistoryStore;
// +++ Get active session ID from session store +++
const { activeSessionId } = storeToRefs(sessionStore);
const { updateSessionCommandInput } = sessionStore;
// Props definition is now empty as search results are no longer handled here
const props = defineProps<{
// No props defined here currently
}>();
const commandInput = ref('');
// --- 移除本地 commandInput ref ---
// const commandInput = ref('');
const isSearching = ref(false);
const searchTerm = ref('');
// *** 移除本地的搜索结果 ref ***
// const searchResultCount = ref(0);
// const currentSearchResultIndex = ref(0);
// +++ 计算属性,用于获取和设置当前活动会话的命令输入 +++
const currentSessionCommandInput = computed({
get: () => {
if (!activeSessionId.value) return '';
const session = sessionStore.sessions.get(activeSessionId.value);
return session ? session.commandInputContent.value : '';
},
set: (newValue) => {
if (activeSessionId.value) {
updateSessionCommandInput(activeSessionId.value, newValue);
}
}
});
const sendCommand = () => {
const command = commandInput.value;
const command = currentSessionCommandInput.value; // 使用计算属性获取值
console.log(`[CommandInputBar] Sending command: ${command || '<Enter>'} `);
emit('send-command', command);
commandInput.value = '';
// 清空 store 中的值
if (activeSessionId.value) {
updateSessionCommandInput(activeSessionId.value, '');
}
};
const toggleSearch = () => {
@@ -72,8 +95,8 @@ watch(searchTerm, (newValue) => {
}
});
// NEW: Watch commandInput and sync searchTerm based on settings
watch(commandInput, (newValue) => {
// NEW: Watch currentSessionCommandInput and sync searchTerm based on settings
watch(currentSessionCommandInput, (newValue) => { // 监听计算属性
const target = commandInputSyncTarget.value;
if (target === 'quickCommands') {
quickCommandsStore.setSearchTerm(newValue);
@@ -138,9 +161,12 @@ const handleCommandInputKeydown = (event: KeyboardEvent) => {
event.preventDefault();
console.log(`[CommandInputBar] Alt+Enter detected. Sending selected command: ${selectedCommand}`);
emit('send-command', selectedCommand + '\n');
commandInput.value = ''; // Clear input after sending selected command
// 清空 store 中的值
if (activeSessionId.value) {
updateSessionCommandInput(activeSessionId.value, '');
}
}
} else if (event.ctrlKey && event.key === 'c' && commandInput.value === '') {
} else if (event.ctrlKey && event.key === 'c' && currentSessionCommandInput.value === '') { // 检查计算属性的值
// Handle Ctrl+C when input is empty
event.preventDefault();
console.log('[CommandInputBar] Ctrl+C detected with empty input. Sending SIGINT.');
@@ -242,7 +268,7 @@ onBeforeUnmount(() => {
<!-- Command Input -->
<input
type="text"
v-model="commandInput"
v-model="currentSessionCommandInput"
:placeholder="t('commandInputBar.placeholder')"
class="flex-grow min-w-0 px-4 py-1.5 border border-border/50 rounded-lg bg-input text-foreground text-sm shadow-sm focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all duration-300 ease-in-out"
:class="{ 'basis-3/4': isSearching, 'basis-full': !isSearching }"
@@ -73,6 +73,8 @@ export interface SessionState {
// --- 新增:独立编辑器状态 ---
editorTabs: Ref<FileTab[]>; // 编辑器标签页列表
activeEditorTabId: Ref<string | null>; // 当前活动的编辑器标签页 ID
// --- 新增:命令输入框内容 ---
commandInputContent: Ref<string>; // 当前会话的命令输入框内容
}
// 为标签栏定义包含状态的类型
@@ -185,6 +187,8 @@ export const useSessionStore = defineStore('session', () => {
// --- 初始化编辑器状态 ---
editorTabs: ref([]), // 初始化为空数组
activeEditorTabId: ref(null), // 初始化为 null
// --- 初始化命令输入框内容 ---
commandInputContent: ref(''), // 初始化为空字符串
};
// 3. 添加到 Map 并激活 (需要创建 Map 的新实例以触发 shallowRef 更新)
@@ -652,6 +656,18 @@ export const useSessionStore = defineStore('session', () => {
rdpConnectionInfo.value = null; // 清除连接信息
};
/**
* 更新指定会话的命令输入框内容
*/
const updateSessionCommandInput = (sessionId: string, content: string) => {
const session = sessions.value.get(sessionId);
if (session) {
session.commandInputContent.value = content;
} else {
console.warn(`[SessionStore] 尝试更新不存在的会话 ${sessionId} 的命令输入内容`);
}
};
return {
// State
@@ -681,5 +697,7 @@ export const useSessionStore = defineStore('session', () => {
// --- RDP Modal Actions ---
openRdpModal, // 导出打开 RDP 模态框 Action
closeRdpModal, // 导出关闭 RDP 模态框 Action
// --- 命令输入框 Action ---
updateSessionCommandInput, // 导出更新命令输入 Action
};
});