refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* features/chat/composables/useChatBrowserPanel.ts
|
||||
* 内置浏览器侧栏:URL 规范化、iframe 加载与桌面端宽度拖拽。
|
||||
*/
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
const MIN_BROWSER_PANEL_WIDTH = 320;
|
||||
const MAX_BROWSER_PANEL_WIDTH = 960;
|
||||
|
||||
export interface UseChatBrowserPanelOptions {
|
||||
/** 无效 URL 等提示 */
|
||||
warn: (message: string) => void;
|
||||
invalidUrlMessage: string;
|
||||
enterUrlMessage: string;
|
||||
}
|
||||
|
||||
const normalizeBrowserInput = (value: string) => {
|
||||
const raw = (value || '').trim();
|
||||
if (!raw) return '';
|
||||
if (/^https?:\/\//i.test(raw)) return raw;
|
||||
if (/^localhost(?::\d+)?(\/.*)?$/i.test(raw)) return `http://${raw}`;
|
||||
if (/^(?:\d{1,3}\.){3}\d{1,3}(?::\d+)?(\/.*)?$/.test(raw)) return `http://${raw}`;
|
||||
if (/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/.*)?$/.test(raw)) return `https://${raw}`;
|
||||
return '';
|
||||
};
|
||||
|
||||
export function useChatBrowserPanel(options: UseChatBrowserPanelOptions) {
|
||||
const open = ref(false);
|
||||
const draftUrl = ref('');
|
||||
const currentUrl = ref('');
|
||||
const frameKey = ref(0);
|
||||
const panelWidth = ref(420);
|
||||
const isResizing = ref(false);
|
||||
const viewportWidth = ref(typeof window === 'undefined' ? 1440 : window.innerWidth);
|
||||
|
||||
const clampPanelWidth = (width: number) => {
|
||||
const maxAllowed = Math.min(
|
||||
MAX_BROWSER_PANEL_WIDTH,
|
||||
Math.max(MIN_BROWSER_PANEL_WIDTH, Math.floor(window.innerWidth * 0.7)),
|
||||
);
|
||||
return Math.min(Math.max(width, MIN_BROWSER_PANEL_WIDTH), maxAllowed);
|
||||
};
|
||||
|
||||
const panelStyle = computed(() => {
|
||||
if (viewportWidth.value < 1024) return undefined;
|
||||
return { width: `${panelWidth.value}px` };
|
||||
});
|
||||
|
||||
const loadPage = () => {
|
||||
const normalized = normalizeBrowserInput(draftUrl.value);
|
||||
if (!normalized) {
|
||||
options.warn(options.invalidUrlMessage);
|
||||
return;
|
||||
}
|
||||
currentUrl.value = normalized;
|
||||
draftUrl.value = normalized;
|
||||
frameKey.value += 1;
|
||||
open.value = true;
|
||||
};
|
||||
|
||||
const refreshPage = () => {
|
||||
if (!currentUrl.value) {
|
||||
loadPage();
|
||||
return;
|
||||
}
|
||||
frameKey.value += 1;
|
||||
};
|
||||
|
||||
const openInNewTab = () => {
|
||||
const normalized = currentUrl.value || normalizeBrowserInput(draftUrl.value);
|
||||
if (!normalized) {
|
||||
options.warn(options.enterUrlMessage);
|
||||
return;
|
||||
}
|
||||
window.open(normalized, '_blank', 'noopener,noreferrer');
|
||||
};
|
||||
|
||||
const onUrlEnter = (event: KeyboardEvent) => {
|
||||
if (event.key !== 'Enter') return;
|
||||
event.preventDefault();
|
||||
loadPage();
|
||||
};
|
||||
|
||||
const handleResizeMove = (event: MouseEvent) => {
|
||||
if (!isResizing.value) return;
|
||||
panelWidth.value = clampPanelWidth(window.innerWidth - event.clientX);
|
||||
};
|
||||
|
||||
const stopResize = () => {
|
||||
if (!isResizing.value) return;
|
||||
isResizing.value = false;
|
||||
document.body.style.cursor = '';
|
||||
document.body.style.userSelect = '';
|
||||
window.removeEventListener('mousemove', handleResizeMove);
|
||||
window.removeEventListener('mouseup', stopResize);
|
||||
};
|
||||
|
||||
const startResize = (event: MouseEvent) => {
|
||||
if (viewportWidth.value < 1024) return;
|
||||
event.preventDefault();
|
||||
isResizing.value = true;
|
||||
document.body.style.cursor = 'col-resize';
|
||||
document.body.style.userSelect = 'none';
|
||||
window.addEventListener('mousemove', handleResizeMove);
|
||||
window.addEventListener('mouseup', stopResize);
|
||||
};
|
||||
|
||||
const syncViewportWidth = () => {
|
||||
viewportWidth.value = window.innerWidth;
|
||||
if (viewportWidth.value >= 1024) {
|
||||
panelWidth.value = clampPanelWidth(panelWidth.value);
|
||||
}
|
||||
};
|
||||
|
||||
const close = () => {
|
||||
open.value = false;
|
||||
};
|
||||
|
||||
const toggle = () => {
|
||||
open.value = !open.value;
|
||||
};
|
||||
|
||||
return {
|
||||
open,
|
||||
draftUrl,
|
||||
currentUrl,
|
||||
frameKey,
|
||||
panelStyle,
|
||||
isResizing,
|
||||
loadPage,
|
||||
refreshPage,
|
||||
openInNewTab,
|
||||
onUrlEnter,
|
||||
startResize,
|
||||
stopResize,
|
||||
syncViewportWidth,
|
||||
close,
|
||||
toggle,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user