|
1 | | -import { extractDirectiveReferences, buildMultiFileContext } from './multiFileHelper.js'; |
| 1 | +import { extractDirectiveReferences, buildMultiFileContext } from './multiFileHelper.js'; |
| 2 | + |
| 3 | +const COLLAPSED_FILE_LIST_OFFSET = 48; |
2 | 4 |
|
3 | 5 | export const PROJECT_TYPE_CHANGE_EVENT = 'sharppad:projectTypeChanged'; |
4 | 6 |
|
@@ -200,45 +202,73 @@ export function getResponsiveSize(defaultSize, mobileSize) { |
200 | 202 | } |
201 | 203 |
|
202 | 204 | // 响应式设置container宽度 |
203 | | -export function setContainerWidth(container, fileListWidth, chatPanelWidth, chatPanelVisible) { |
204 | | - // 使用媒体查询检测移动设备 |
205 | | - const isMobile = window.matchMedia('(max-width: 768px) and (pointer: coarse), (max-width: 480px)').matches; |
206 | | - |
207 | | - if (isMobile) { |
208 | | - // 移动设备适配 |
209 | | - if (chatPanelVisible) { |
210 | | - // 移动设备上聊天面板设置为全屏宽度,主容器隐藏或调整到最小 |
211 | | - container.style.width = '100%'; |
212 | | - container.style.marginRight = '0'; |
213 | | - |
214 | | - // 根据聊天面板的位置决定编辑器的样式 |
215 | | - // 如果聊天面板在屏幕下半部分 |
216 | | - container.style.height = 'calc(50vh - 1px)'; |
217 | | - container.style.marginBottom = '0'; |
218 | | - } else { |
219 | | - // 聊天面板不可见时,编辑器占满全屏 |
220 | | - container.style.width = '100%'; |
221 | | - container.style.height = '100vh'; |
222 | | - container.style.marginRight = '0'; |
223 | | - container.style.marginBottom = '0'; |
224 | | - } |
225 | | - |
226 | | - // 在移动设备上,文件列表可能会是覆盖式的,而不是并排 |
227 | | - container.style.marginLeft = '0'; |
228 | | - } else { |
229 | | - // 桌面设备使用原来的布局逻辑 |
230 | | - if (chatPanelVisible) { |
231 | | - container.style.width = `calc(100% - ${fileListWidth}px - ${chatPanelWidth}px)`; |
232 | | - container.style.marginRight = `${chatPanelWidth}px`; |
233 | | - } else { |
234 | | - container.style.width = `calc(100% - ${fileListWidth}px)`; |
235 | | - container.style.marginRight = '0'; |
236 | | - } |
237 | | - container.style.marginLeft = `${fileListWidth}px`; |
238 | | - container.style.height = '100vh'; |
239 | | - } |
240 | | - |
241 | | - // 重新布局编辑器 |
| 205 | +export function setContainerWidth(container, fileListWidth, chatPanelWidth, chatPanelVisible) { |
| 206 | + if (!container) { |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + const fileListElement = document.getElementById('fileList'); |
| 211 | + const isFileListCollapsed = fileListElement?.classList.contains('collapsed'); |
| 212 | + const normalizedFileListWidth = Number.isFinite(fileListWidth) ? fileListWidth : 0; |
| 213 | + const effectiveFileListWidth = isFileListCollapsed ? 0 : normalizedFileListWidth; |
| 214 | + |
| 215 | + // 使用媒体查询检测移动设备 |
| 216 | + const isMobile = window.matchMedia('(max-width: 768px) and (pointer: coarse), (max-width: 480px)').matches; |
| 217 | + const collapsedOffset = isFileListCollapsed && !isMobile ? COLLAPSED_FILE_LIST_OFFSET : 0; |
| 218 | + |
| 219 | + if (isMobile) { |
| 220 | + // 移动设备适配 |
| 221 | + if (chatPanelVisible) { |
| 222 | + // 移动设备上聊天面板设置为全屏宽度,主容器隐藏或调整到最小 |
| 223 | + container.style.width = '100%'; |
| 224 | + container.style.marginRight = '0'; |
| 225 | + |
| 226 | + // 根据聊天面板的位置决定编辑器的样式 |
| 227 | + // 如果聊天面板在屏幕下半部分 |
| 228 | + container.style.height = 'calc(50vh - 1px)'; |
| 229 | + container.style.marginBottom = '0'; |
| 230 | + } else { |
| 231 | + // 聊天面板不可见时,编辑器占满全屏 |
| 232 | + container.style.width = '100%'; |
| 233 | + container.style.height = '100vh'; |
| 234 | + container.style.marginRight = '0'; |
| 235 | + container.style.marginBottom = '0'; |
| 236 | + } |
| 237 | + |
| 238 | + // 在移动设备上,文件列表可能会是覆盖式的,而不是并排 |
| 239 | + container.style.marginLeft = '0'; |
| 240 | + container.style.marginRight = '0'; |
| 241 | + } else { |
| 242 | + // 桌面设备使用原来的布局逻辑 |
| 243 | + const totalFileListOffset = effectiveFileListWidth + collapsedOffset; |
| 244 | + const normalizedChatWidth = chatPanelVisible && Number.isFinite(chatPanelWidth) |
| 245 | + ? chatPanelWidth |
| 246 | + : 0; |
| 247 | + |
| 248 | + let outputPanelWidth = 0; |
| 249 | + const outputPanel = document.getElementById('outputPanel'); |
| 250 | + if (outputPanel) { |
| 251 | + const outputStyles = window.getComputedStyle(outputPanel); |
| 252 | + const isVerticalOutput = outputPanel.classList.contains('vertical') && outputStyles.display !== 'none'; |
| 253 | + if (isVerticalOutput) { |
| 254 | + const parsedWidth = parseInt(outputStyles.width, 10); |
| 255 | + if (Number.isFinite(parsedWidth)) { |
| 256 | + outputPanelWidth = parsedWidth; |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + // 考虑垂直输出面板的宽度,避免折叠文件列表时挤压输出区域 |
| 262 | + const totalRightOffset = normalizedChatWidth + outputPanelWidth; |
| 263 | + const rightOffsetTerm = totalRightOffset > 0 ? ` - ${totalRightOffset}px` : ''; |
| 264 | + |
| 265 | + container.style.width = `calc(100% - ${totalFileListOffset}px${rightOffsetTerm})`; |
| 266 | + container.style.marginRight = `${totalRightOffset}px`; |
| 267 | + container.style.marginLeft = `${totalFileListOffset}px`; |
| 268 | + container.style.height = '100vh'; |
| 269 | + } |
| 270 | + |
| 271 | + // 重新布局编辑器 |
242 | 272 | layoutEditor(); |
243 | 273 | } |
244 | 274 |
|
|
0 commit comments