review code.

This commit is contained in:
lijiahang
2024-02-01 16:15:51 +08:00
parent 3abd273f88
commit 0263231fdb
59 changed files with 101 additions and 4186 deletions

View File

@@ -6,7 +6,6 @@ import useTabBarStore from './modules/tab-bar';
import useCacheStore from './modules/cache';
import useTipsStore from './modules/tips';
import useDictStore from './modules/dict';
import useHostSpaceStore from './modules/host-space';
import useTerminalStore from './modules/terminal';
const pinia = createPinia();
@@ -20,7 +19,6 @@ export {
useTipsStore,
useDictStore,
useTerminalStore,
useHostSpaceStore,
};
export default pinia;

View File

@@ -1,179 +0,0 @@
import type {
TerminalActionBarSetting,
TerminalDisplaySetting,
TerminalInteractSetting,
TerminalPluginsSetting,
TerminalPreference,
TerminalSessionSetting,
TerminalShortcutSetting,
TerminalState
} from './types';
import type { AuthorizedHostQueryResponse } from '@/api/asset/asset-authorized-data';
import type { HostQueryResponse } from '@/api/asset/host';
import type { TerminalTheme, TerminalThemeSchema } from '@/api/asset/host-terminal';
import { getCurrentAuthorizedHost } from '@/api/asset/asset-authorized-data';
import { getTerminalThemes } from '@/api/asset/host-terminal';
import { defineStore } from 'pinia';
import { getPreference, updatePreference } from '@/api/user/preference';
import { nextSessionId } from '@/utils';
import { Message } from '@arco-design/web-vue';
import { TerminalTabType } from '@/views/host/terminal/types/terminal.const';
import TerminalTabManager from '@/views/host/terminal/handler/terminal-tab-manager';
import TerminalSessionManager from '@/views/host/terminal/handler/terminal-session-manager';
// 终端偏好项
export const TerminalPreferenceItem = {
// 新建连接类型
NEW_CONNECTION_TYPE: 'newConnectionType',
// 终端主题
THEME: 'theme',
// 显示设置
DISPLAY_SETTING: 'displaySetting',
// 操作栏设置
ACTION_BAR_SETTING: 'actionBarSetting',
// 右键菜单设置
RIGHT_MENU_SETTING: 'rightMenuSetting',
// 交互设置
INTERACT_SETTING: 'interactSetting',
// 插件设置
PLUGINS_SETTING: 'pluginsSetting',
// 会话设置
SESSION_SETTING: 'sessionSetting',
// 快捷键设置
SHORTCUT_SETTING: 'shortcutSetting',
};
export default defineStore('hostSpace', {
state: (): TerminalState => ({
preference: {
newConnectionType: 'group',
theme: {
schema: {} as TerminalThemeSchema
} as TerminalTheme,
displaySetting: {} as TerminalDisplaySetting,
actionBarSetting: {} as TerminalActionBarSetting,
rightMenuSetting: [],
interactSetting: {} as TerminalInteractSetting,
pluginsSetting: {} as TerminalPluginsSetting,
sessionSetting: {} as TerminalSessionSetting,
shortcutSetting: {
enabled: false,
keys: []
} as TerminalShortcutSetting,
},
hosts: {} as AuthorizedHostQueryResponse,
tabManager: new TerminalTabManager(),
sessionManager: new TerminalSessionManager()
}),
actions: {
// 加载终端偏好
async fetchPreference() {
try {
// 加载偏好
const { data } = await getPreference<TerminalPreference>('TERMINAL');
// theme 不存在则默认加载第一个
if (!data.theme?.name) {
const { data: themes } = await getTerminalThemes();
data.theme = themes[0];
// 更新默认主题偏好
await this.updateTerminalPreference(TerminalPreferenceItem.THEME, data.theme);
}
// 移除禁用的快捷键
if (data.shortcutSetting?.enabled) {
data.shortcutSetting.keys = data.shortcutSetting.keys.filter(s => s.enabled);
} else {
data.shortcutSetting = {
enabled: false,
keys: []
};
}
// 选择赋值 (不能修改引用)
const keys = Object.keys(this.preference);
keys.forEach(key => {
const item = data[key as keyof TerminalPreference];
if (item) {
this.preference[key as keyof TerminalPreference] = item as any;
}
});
} catch (e) {
Message.error('配置加载失败');
}
},
// 更新终端偏好
async updateTerminalPreference(item: string, value: any, setLocal = false) {
if (setLocal) {
this.preference[item as keyof TerminalPreference] = value;
}
try {
// 修改配置
await updatePreference({
type: 'TERMINAL',
item,
value
});
} catch (e) {
Message.error('同步失败');
}
},
// 加载主机列表
async loadHosts() {
if (this.hosts.hostList?.length) {
return;
}
const { data } = await getCurrentAuthorizedHost();
Object.keys(data).forEach(k => {
this.hosts[k as keyof AuthorizedHostQueryResponse] = data[k as keyof AuthorizedHostQueryResponse] as any;
});
},
// 打开终端
openTerminal(record: HostQueryResponse) {
// 添加到最近连接
this.hosts.latestHosts = [...new Set([record.id, ...this.hosts.latestHosts])];
// 获取 seq
const tabSeqArr = this.tabManager.items
.map(s => s.seq)
.filter(Boolean)
.map(Number);
const nextSeq = tabSeqArr.length
? Math.max(...tabSeqArr) + 1
: 1;
// 打开 tab
this.tabManager.openTab({
type: TerminalTabType.TERMINAL,
key: nextSessionId(10),
seq: nextSeq,
title: `(${nextSeq}) ${record.alias || record.name}`,
hostId: record.id,
address: record.address
});
},
// 复制并且打开终端
openCopyTerminal(hostId: number) {
const host = this.hosts.hostList
.find(s => s.id === hostId);
if (host) {
this.openTerminal(host);
}
},
// 获取当前终端会话
getCurrentTerminalSession(tips: boolean = true) {
const tab = this.tabManager.getCurrentTab();
if (!tab || tab.type !== TerminalTabType.TERMINAL) {
if (tips) {
Message.warning('请切换到终端标签页');
}
return;
}
// 获取处理器并截图
return this.sessionManager.getSession(tab.key);
},
},
});

View File

@@ -1,97 +0,0 @@
import type { ITerminalSessionManager, ITerminalTabManager } from '@/views/host/terminal/types/terminal.type';
import type { AuthorizedHostQueryResponse } from '@/api/asset/asset-authorized-data';
import type { TerminalTheme } from '@/api/asset/host-terminal';
export interface TerminalState {
preference: TerminalPreference;
hosts: AuthorizedHostQueryResponse;
tabManager: ITerminalTabManager;
sessionManager: ITerminalSessionManager;
}
// 终端配置
export interface TerminalPreference {
newConnectionType: string;
theme: TerminalTheme;
displaySetting: TerminalDisplaySetting;
actionBarSetting: TerminalActionBarSetting;
rightMenuSetting: Array<string>,
interactSetting: TerminalInteractSetting;
pluginsSetting: TerminalPluginsSetting;
sessionSetting: TerminalSessionSetting;
shortcutSetting: TerminalShortcutSetting;
}
// 显示设置
export interface TerminalDisplaySetting {
fontFamily?: string;
fontSize?: number;
lineHeight?: number;
fontWeight?: string | number;
fontWeightBold?: string | number;
cursorStyle?: string;
cursorBlink?: boolean;
}
// 操作栏设置
export interface TerminalActionBarSetting {
commandInput?: boolean;
connectStatus?: boolean;
[key: string]: unknown;
}
// 交互设置
export interface TerminalInteractSetting {
fastScrollModifier: boolean;
altClickMovesCursor: boolean;
rightClickSelectsWord: boolean;
selectionChangeCopy: boolean;
copyAutoTrim: boolean;
pasteAutoTrim: boolean;
rightClickPaste: boolean;
enableRightClickMenu: boolean;
enableBell: boolean;
wordSeparator: string;
}
// 插件设置
export interface TerminalPluginsSetting {
enableWeblinkPlugin: boolean;
enableWebglPlugin: boolean;
enableImagePlugin: boolean;
}
// 会话设置
export interface TerminalSessionSetting {
terminalEmulationType: string;
scrollBackLine: number;
}
// 终端快捷键设置
export interface TerminalShortcutSetting {
enabled: boolean;
keys: Array<TerminalShortcutKey>;
}
// 终端快捷键
export interface ShortcutKey {
ctrlKey: boolean;
shiftKey: boolean;
altKey: boolean;
code: string;
}
// 终端快捷键
export interface TerminalShortcutKey extends ShortcutKey {
item: string;
enabled: boolean;
}
// 终端快捷键编辑
export interface TerminalShortcutKeyEditable extends TerminalShortcutKey {
editable: boolean;
content: string;
type: number;
shortcutKey?: string;
}

View File

@@ -170,7 +170,6 @@ export default defineStore('terminal', {
}
return;
}
// 获取处理器并截图
return this.sessionManager.getSession(tab.key);
},