Repository navigation
fix: file preview width too narrow #8303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,8 @@ | ||||||||||||||||||
| import { inject, ref } from 'vue' | ||||||||||||||||||
| import { computed, inject, ref, toValue, watch } from 'vue' | ||||||||||||||||||
| import type { ChatRecordMeta } from '../core/types' | ||||||||||||||||||
| import type { PreviewFile } from './file-preview/index' | ||||||||||||||||||
| import type { ChatParagraphSource } from '@/api/types' | ||||||||||||||||||
| import type { InjectionKey } from 'vue' | ||||||||||||||||||
| import type { InjectionKey, MaybeRefOrGetter, Ref } from 'vue' | ||||||||||||||||||
| import { createExecutionDetailStore } from './execution-detail/index' | ||||||||||||||||||
| import { createFilePreviewStore } from './file-preview/index' | ||||||||||||||||||
| import { createKnowledgeSourceStore } from './knowledge-source/index' | ||||||||||||||||||
|
|
@@ -16,6 +16,35 @@ export const RIGHT_SIDEBAR_PANEL = { | |||||||||||||||||
|
|
||||||||||||||||||
| export type RightSidebarPanel = (typeof RIGHT_SIDEBAR_PANEL)[keyof typeof RIGHT_SIDEBAR_PANEL] | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * 右侧栏宽度:执行详情、知识来源固定 420px;文件预览默认取对话区一半(420px–960px),可拖动调整并记住。 | ||||||||||||||||||
| * 拖动后的宽度至少 420px,并为对话区保留至少 360px;窗口变窄时按当前上限显示,不改写记住的宽度。cqw 以对话布局宽度为基准。 | ||||||||||||||||||
| */ | ||||||||||||||||||
| const RIGHT_SIDEBAR_WIDTH = 420 | ||||||||||||||||||
| const FILE_PREVIEW_DEFAULT_WIDTH = 'clamp(420px, 50cqw, 960px)' | ||||||||||||||||||
| const FILE_PREVIEW_MIN_WIDTH = 420 | ||||||||||||||||||
| const CONVERSATION_MIN_WIDTH = 360 | ||||||||||||||||||
| const FILE_PREVIEW_WIDTH_STORAGE_KEY = 'conversation-file-preview-width' | ||||||||||||||||||
|
|
||||||||||||||||||
| // 读取记住的文件预览宽度;嵌入页禁用存储等读取失败时按未调整处理 | ||||||||||||||||||
| function readFilePreviewWidth(): number | null { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const width = Number(localStorage.getItem(FILE_PREVIEW_WIDTH_STORAGE_KEY)) | ||||||||||||||||||
| return width >= FILE_PREVIEW_MIN_WIDTH ? width : null | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| return null | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| function writeFilePreviewWidth(width: number | null) { | ||||||||||||||||||
| try { | ||||||||||||||||||
| if (width == null) localStorage.removeItem(FILE_PREVIEW_WIDTH_STORAGE_KEY) | ||||||||||||||||||
| else localStorage.setItem(FILE_PREVIEW_WIDTH_STORAGE_KEY, String(width)) | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| // 存储不可用时只在本次页面内生效 | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * 右侧栏 store:当前面板与右侧开合,组合各面板自己的 store。 | ||||||||||||||||||
| * 由 view 创建并 provide;消息底部点击「执行详情」「知识来源」、回答中点击可预览文件时写入对应面板数据并打开右侧。 | ||||||||||||||||||
|
|
@@ -26,6 +55,28 @@ export function createRightSidebarStore() { | |||||||||||||||||
| const executionDetail = createExecutionDetailStore() | ||||||||||||||||||
| const knowledgeSource = createKnowledgeSourceStore() | ||||||||||||||||||
| const filePreview = createFilePreviewStore() | ||||||||||||||||||
| const filePreviewWidth = ref(readFilePreviewWidth()) | ||||||||||||||||||
| /** 只有文件预览可拖动调整宽度 */ | ||||||||||||||||||
| const rightResizable = computed(() => panel.value === RIGHT_SIDEBAR_PANEL.FILE_PREVIEW) | ||||||||||||||||||
| const rightWidth = computed(() => { | ||||||||||||||||||
| if (!rightResizable.value) return RIGHT_SIDEBAR_WIDTH | ||||||||||||||||||
| if (filePreviewWidth.value == null) return FILE_PREVIEW_DEFAULT_WIDTH | ||||||||||||||||||
| return `clamp(${FILE_PREVIEW_MIN_WIDTH}px, ${filePreviewWidth.value}px, calc(100cqw - ${CONVERSATION_MIN_WIDTH}px))` | ||||||||||||||||||
| }) | ||||||||||||||||||
|
|
||||||||||||||||||
| // 拖动调整文件预览宽度,按当前布局宽度限定范围后记住 | ||||||||||||||||||
| const resizeRight = (width: number, layoutWidth: number) => { | ||||||||||||||||||
| if (!rightResizable.value) return | ||||||||||||||||||
| const maxWidth = Math.max(FILE_PREVIEW_MIN_WIDTH, layoutWidth - CONVERSATION_MIN_WIDTH) | ||||||||||||||||||
| filePreviewWidth.value = Math.round(Math.min(Math.max(width, FILE_PREVIEW_MIN_WIDTH), maxWidth)) | ||||||||||||||||||
| writeFilePreviewWidth(filePreviewWidth.value) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // 恢复文件预览默认宽度 | ||||||||||||||||||
| const resetRightWidth = () => { | ||||||||||||||||||
| filePreviewWidth.value = null | ||||||||||||||||||
| writeFilePreviewWidth(null) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const openPanel = (target: RightSidebarPanel) => { | ||||||||||||||||||
| panel.value = target | ||||||||||||||||||
|
|
@@ -53,6 +104,10 @@ export function createRightSidebarStore() { | |||||||||||||||||
| return { | ||||||||||||||||||
| panel, | ||||||||||||||||||
| rightSideOpen, | ||||||||||||||||||
| rightWidth, | ||||||||||||||||||
| rightResizable, | ||||||||||||||||||
| resizeRight, | ||||||||||||||||||
| resetRightWidth, | ||||||||||||||||||
| executionDetail, | ||||||||||||||||||
| knowledgeSource, | ||||||||||||||||||
| filePreview, | ||||||||||||||||||
|
|
@@ -65,6 +120,35 @@ export function createRightSidebarStore() { | |||||||||||||||||
|
|
||||||||||||||||||
| export type RightSidebarStore = ReturnType<typeof createRightSidebarStore> | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * 挤压态下文件预览与左侧栏互相让位,避免对话区被两侧挤窄: | ||||||||||||||||||
| * 打开文件预览时收起左侧栏,预览关闭或切到其他面板后恢复由预览收起的左侧栏;预览中展开左侧栏则关闭预览。 | ||||||||||||||||||
| * 抽屉态两侧都覆盖在对话区上,不处理。 | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function bindFilePreviewLeftSide(rightSidebar: RightSidebarStore, leftSideOpen: Ref<boolean>, pushMode: MaybeRefOrGetter<boolean>) { | ||||||||||||||||||
| const previewOpen = computed(() => rightSidebar.rightSideOpen.value && rightSidebar.panel.value === RIGHT_SIDEBAR_PANEL.FILE_PREVIEW) | ||||||||||||||||||
| let collapsedByPreview = false | ||||||||||||||||||
|
|
||||||||||||||||||
| watch(previewOpen, (open) => { | ||||||||||||||||||
| if (!toValue(pushMode)) { | ||||||||||||||||||
| collapsedByPreview = false | ||||||||||||||||||
| return | ||||||||||||||||||
| } | ||||||||||||||||||
| if (open && leftSideOpen.value) { | ||||||||||||||||||
| collapsedByPreview = true | ||||||||||||||||||
| leftSideOpen.value = false | ||||||||||||||||||
| } else if (!open && collapsedByPreview) { | ||||||||||||||||||
| collapsedByPreview = false | ||||||||||||||||||
| leftSideOpen.value = true | ||||||||||||||||||
| } | ||||||||||||||||||
| }) | ||||||||||||||||||
| watch(leftSideOpen, (open) => { | ||||||||||||||||||
| if (!open || !previewOpen.value || !toValue(pushMode)) return | ||||||||||||||||||
| collapsedByPreview = false | ||||||||||||||||||
| rightSidebar.closeRightSide() | ||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export const RIGHT_SIDEBAR_KEY: InjectionKey<RightSidebarStore> = Symbol('right-sidebar') | ||||||||||||||||||
|
|
||||||||||||||||||
| export function useRightSidebarStore(): RightSidebarStore { | ||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Review: Right Sidebar Store ChangesOverall AssessmentThe changes are generally well-structured, but there are several issues worth addressing. Issues & Potential Bugs1.
|
||||||||||||||||||
| Suggestion | Benefit |
|---|---|
Always return CSS-string rightWidth |
Simpler consumer code |
Use a STORAGE_KEY constant shared with tests |
Avoid magic strings |
Debounce writeFilePreviewWidth during drag |
Avoid excessive localStorage.setItem calls (storage API is synchronous) |
Use Number.isFinite guard in readFilePreviewWidth |
Robustness |
Combine two watchers into a single watch([previewOpen, leftSideOpen], ...) with clearer state transitions |
Easier reasoning |
Add explicit type: const filePreviewWidth = ref<number | null>(readFilePreviewWidth()) |
TS strict inference |
Export constants (RIGHT_SIDEBAR_WIDTH, etc.) for reuse/testing |
Testability |
Style Notes
- Consider extracting inline Chinese comments into English or maintain bilingual docs if this is a shared repo (review request is in English).
- The
@ -1,8 +1,8 @@diff header hints at a truncated diff; ensure the full file compiles (importstoValueandwatchare used only in the new helper — good).
Summary
Functional logic is sound, but I recommend: (1) fix the pushMode toggle edge case in bindFilePreviewLeftSide, (2) harden readFilePreviewWidth with Number.isFinite, (3) unify clamp behavior between default and remembered widths, and (4) debounce persistence writes during drag.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import ConversationLayout from '../../core/layout/index.vue' | |
| import { CONVERSATION_LIST_KEY } from '../../left-sidebar/conversation-list/index' | ||
| import ConversationList from '../../left-sidebar/conversation-list/index.vue' | ||
| import ChatPanel from '../../main/chat-panel/index.vue' | ||
| import { RIGHT_SIDEBAR_KEY } from '../../right-sidebar/index' | ||
| import { RIGHT_SIDEBAR_KEY, bindFilePreviewLeftSide } from '../../right-sidebar/index' | ||
| import RightSidebar from '../../right-sidebar/index.vue' | ||
| import { CHAT_USER_PROFILE_KEY, DEVICE_KEY, SHOW_SHARE_KEY, VIEW_KEY, VIEW_TYPE, createDeviceType } from '../index.ts' | ||
| import { createChatConversation, createChatUserProfileGetter } from './index' | ||
|
|
@@ -55,6 +55,8 @@ bundle.list.bindLeftSideMode(leftMode) | |
| const rightMode = computed(() => (isMobile.value ? 'drawer' : 'push')) | ||
| // 左栏为抽屉即手机端 | ||
| provide(DEVICE_KEY, createDeviceType(leftMode)) | ||
| // 挤压态下文件预览与左侧栏互相让位 | ||
| bindFilePreviewLeftSide(bundle.rightSidebar, bundle.list.leftSideOpen, () => leftMode.value === 'push') | ||
|
|
||
| const closeDrawers = () => { | ||
| if (leftMode.value === 'drawer') bundle.list.leftSideOpen.value = false | ||
|
|
@@ -83,9 +85,13 @@ watch([() => bundle.list.currentChatId.value, () => bundle.list.composerResetSig | |
| <ConversationLayout | ||
| :left-open="bundle.list.leftSideOpen.value" | ||
| :right-open="bundle.rightSidebar.rightSideOpen.value" | ||
| :right-width="bundle.rightSidebar.rightWidth.value" | ||
| :right-resizable="bundle.rightSidebar.rightResizable.value" | ||
| :left-mode="leftMode" | ||
| :right-mode="rightMode" | ||
| @mask-click="closeDrawers" | ||
| @right-resize="bundle.rightSidebar.resizeRight" | ||
| @right-resize-reset="bundle.rightSidebar.resetRightWidth" | ||
| > | ||
| <template #left><ConversationList /></template> | ||
| <template #main><ChatPanel /></template> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review of the diff: 1. Potential circular/tight coupling 2. Reactivity in the predicate 3. Missing cleanup / unbind 4. Manual This forces Vue to touch a ref inside the render function rather than letting auto-unwrapping handle it, and is inconsistent with idiomatic usage. If these are plain refs, passing them in a way that preserves reactivity (e.g. 5. Event naming consistency 6. Width state ownership duplication 7. Minor style suggestion No functional bugs are evident from the diff itself, but items 1–3 (binding lifecycle/reactivity/cleanup) are the highest-risk areas to verify. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a review of the patch.
Bugs / irregularities
&::afterselector is corrupted —[IP]ter.In the SCSS block the pseudo-element is written as
&[IP]ter(and in the hover rules). This is invalid and the resize line will never render. It should be:The
.resizing &::afterrule also requires an ancestor with.resizing, butright-resizingclass is applied to the<aside>, not the wrapper.mk-conversation-side-panel__inner. Verify the selector really matches; otherwise use.mk-conversation-side-panel.resizing .mk-conversation-resize-handle::after.@containeradded on the layout root, but no consumer. You referencecqwin the docs forrightWidth, but nothing declares a container query context name nor usescqwin this file. Either document how--mk-chat-right-widthconsumes it, or drop@containerhere (the class only establishes a container if something queries it).pointermoveon the handle without pointer capture flow verification.setPointerCaptureis called instartRightResize, sopointermove/pointerupwill fire on the handle element even outside it — good. But ifevent.currentTargetis typed loosely, guard against null. Also consider handlingpointerleaveonly when not captured (currently you rely solely on capture — fine, but note it).endRightResizeis attached to bothpointercancelandlostpointercapture— that's redundant but harmless; it just sets a boolean twice.No cleanup of capture on unmount. If the component unmounts mid-drag, the boolean stays true and the
select-noneclass relies on state that's gone. Minor, but aonBeforeUnmount(() => rightResizing.value = false)is cheap insurance.rightWidthStylecan returnundefined. WhenrightWidthisundefined, the computed returnsundefined, producing'--mk-chat-right-width': undefined. Confirm the CSS has a fallback/default, otherwise the variable may be set to empty unexpectedly. Prefer:layoutRefwithuseTemplateRef<HTMLElement>('layoutRef')— the generic should match the ref'd element;HTMLElementis fine for a<div>, just make sure the Vue version supportsuseTemplateRef(Vue 3.5+). If you're on <3.5, this breaks the build.emittype signature:'right-resize-reset': []is correct, but in the template you callemit('right-resize-reset')— fine. However the mask still uses$emitwhile others useemit; consistent enough but worth standardizing.Potential issues
rect.right - clientXcan go negative). Note this in the contract or clamp defensively.pointer-events-noneon the slot wrapper during resize prevents accidental iframe/text interaction — good, but users can't scroll the panel while dragging. Acceptable, just a UX note.getBoundingClientRect()on everypointermovecauses layout read each move. Pointer events can fire at high frequency; consider throttling viarequestAnimationFrame, or cacherect.right/rect.widthat drag start (only stale if the window resizes mid-drag, which is rare). This is the main perf optimization.rightResizablecombined withrightMode === 'push'only shows the handle in push mode. If drawer mode should also be resizable, the condition excludes it — verify that's intended.pointer-events-nonewrapper — same caveat as above.Optimization suggestions
[IP]tercorruption first (functional bug).pointerdown; recompute lazily (e.g., onresize) instead of every move.emit('right-resize', ...)to reduce downstream re-renders.touch-action: noneon.mk-conversation-resize-handleso touch pointers don't scroll while resizing.user-select: nonedirectly on the handle during drag rather than on the whole layout (select-noneon root is broad).emit(...)everywhere vs$emit(...)) and add explicit default forrightResizableinwithDefaultsif it isn'tfalseby convention.