dsh-gufa-code 1.3.117 → 1.3.133

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,30 +81,106 @@ function selectSessionCwd(state) {
81
81
  return picked?.projectionValues?.workingDirectory ?? picked?.cwd ?? null
82
82
  }
83
83
 
84
- /** 可拖拽尺寸 hook:返回 [size, onMouseDown, setSize]。 */
84
+ /** 可拖拽尺寸 hook:返回 [size, onMouseDown, setSize]。
85
+ * 拖拽过程只改根节点 CSS 变量(rAF 合帧),不触发 GufaApp 整棵树重渲染;
86
+ * 松手时才 setSize 一次,避免拖边框卡顿。 */
85
87
  G.useSplitter = function useSplitter(options) {
86
- const { initial, min, max, axis = 'x', invert = false, storageKey } = options
88
+ const { initial, min, max, axis = 'x', invert = false, storageKey, cssVar, targetSelector } = options
87
89
  const [size, setSize] = useState(() => {
88
90
  const stored = storageKey === undefined ? null : Number(localStorage.getItem(storageKey))
89
91
  return Number.isFinite(stored) && stored > 0 ? Math.min(max, Math.max(min, stored)) : initial
90
92
  })
93
+ const findTarget = () => {
94
+ if (typeof targetSelector !== 'string' || targetSelector === '') return null
95
+ try {
96
+ return typeof document !== 'undefined' ? document.querySelector(targetSelector) : null
97
+ } catch {
98
+ return null
99
+ }
100
+ }
101
+ const applyToTarget = (target, value) => {
102
+ if (target === null || target === undefined) return false
103
+ try {
104
+ if (axis === 'x') target.style.width = `${value}px`
105
+ else target.style.height = `${value}px`
106
+ return true
107
+ } catch {
108
+ return false
109
+ }
110
+ }
111
+ /* 初始/恢复持久化尺寸:直接写目标元素,不往 documentElement 挂 CSS 变量
112
+ (根节点自定义属性变化会让整棵文档(含 Monaco/DSH)样式重算,拖拽时非常伤) */
113
+ React.useLayoutEffect(() => {
114
+ const target = findTarget()
115
+ if (!applyToTarget(target, size)) {
116
+ if (typeof cssVar !== 'string' || cssVar === '') return
117
+ try {
118
+ document.documentElement.style.setProperty(cssVar, `${size}px`)
119
+ } catch {
120
+ /* ignore */
121
+ }
122
+ }
123
+ return undefined
124
+ }, [cssVar, size])
91
125
  const onMouseDown = useCallback((event) => {
92
126
  if (event.button !== 0) return
93
127
  event.preventDefault()
128
+ const splitter = event.currentTarget
129
+ const target = findTarget()
94
130
  const startPosition = axis === 'x' ? event.clientX : event.clientY
95
131
  const startSize = size
96
132
  let latest = size
97
- const onMove = (moveEvent) => {
98
- const position = axis === 'x' ? moveEvent.clientX : moveEvent.clientY
99
- const delta = (position - startPosition) * (invert ? -1 : 1)
133
+ let frame = 0
134
+ let pendingPosition = null
135
+ const applySize = () => {
136
+ frame = 0
137
+ if (pendingPosition === null) return
138
+ const delta = (pendingPosition - startPosition) * (invert ? -1 : 1)
100
139
  latest = Math.min(max, Math.max(min, startSize + delta))
101
- setSize(latest)
140
+ pendingPosition = null
141
+ /* 只改目标元素自身的 width/height:不发散到整个 root 的 CSS 变量 */
142
+ if (!applyToTarget(target, latest)) {
143
+ try {
144
+ if (typeof cssVar === 'string' && cssVar !== '') document.documentElement.style.setProperty(cssVar, `${latest}px`)
145
+ } catch {
146
+ /* ignore */
147
+ }
148
+ }
149
+ /* 聊天栏是 DSH 原生浮层,需要跟着宿主矩形重新定位;终端/Monaco 各自有 ResizeObserver */
150
+ try {
151
+ G.bus.emit('layout:resize', { varName: cssVar ?? '', size: latest })
152
+ } catch {
153
+ /* ignore */
154
+ }
155
+ }
156
+ const onMove = (moveEvent) => {
157
+ pendingPosition = axis === 'x' ? moveEvent.clientX : moveEvent.clientY
158
+ if (frame === 0) {
159
+ frame = typeof requestAnimationFrame === 'function' ? requestAnimationFrame(applySize) : window.setTimeout(applySize, 16)
160
+ }
102
161
  }
103
162
  const onUp = () => {
104
163
  window.removeEventListener('mousemove', onMove)
105
164
  window.removeEventListener('mouseup', onUp)
165
+ window.removeEventListener('blur', onUp)
166
+ if (frame !== 0) {
167
+ try {
168
+ if (typeof cancelAnimationFrame === 'function') cancelAnimationFrame(frame)
169
+ else window.clearTimeout(frame)
170
+ } catch {
171
+ /* ignore */
172
+ }
173
+ frame = 0
174
+ }
175
+ if (pendingPosition !== null) applySize()
176
+ setSize(latest)
106
177
  document.body.style.cursor = ''
107
178
  document.body.style.userSelect = ''
179
+ try {
180
+ if (splitter !== null && splitter !== undefined && typeof splitter.removeAttribute === 'function') splitter.removeAttribute('data-dragging')
181
+ } catch {
182
+ /* ignore */
183
+ }
108
184
  if (storageKey !== undefined) {
109
185
  try {
110
186
  localStorage.setItem(storageKey, String(latest))
@@ -112,12 +188,30 @@ G.useSplitter = function useSplitter(options) {
112
188
  /* ignore */
113
189
  }
114
190
  }
191
+ G.__gufaSplitterDragging = false
192
+ try {
193
+ G.bus.emit('layout:resize-end', { varName: cssVar ?? '', size: latest })
194
+ } catch {
195
+ /* ignore */
196
+ }
197
+ }
198
+ try {
199
+ if (splitter !== null && splitter !== undefined && typeof splitter.setAttribute === 'function') splitter.setAttribute('data-dragging', '')
200
+ } catch {
201
+ /* ignore */
115
202
  }
116
203
  document.body.style.cursor = axis === 'x' ? 'col-resize' : 'row-resize'
117
204
  document.body.style.userSelect = 'none'
205
+ G.__gufaSplitterDragging = true
206
+ try {
207
+ G.bus.emit('layout:resize-start', { varName: cssVar ?? '' })
208
+ } catch {
209
+ /* ignore */
210
+ }
118
211
  window.addEventListener('mousemove', onMove)
119
212
  window.addEventListener('mouseup', onUp)
120
- }, [axis, invert, max, min, size, storageKey])
213
+ window.addEventListener('blur', onUp)
214
+ }, [axis, cssVar, invert, max, min, size, storageKey])
121
215
  return [size, onMouseDown, setSize]
122
216
  }
123
217
 
@@ -281,9 +375,9 @@ G.GufaApp = function GufaApp(props) {
281
375
  const [modalPending, setModalPending] = useState(false)
282
376
  const [modalError, setModalError] = useState('')
283
377
  const bootedRef = useRef(false)
284
- const [explorerWidth, explorerDrag] = G.useSplitter({ initial: 260, min: 190, max: 520, storageKey: 'gufa-code:w:explorer' })
285
- const [chatWidth, chatDrag] = G.useSplitter({ initial: 430, min: 320, max: 780, axis: 'x', invert: true, storageKey: 'gufa-code:w:chat' })
286
- const [terminalHeight, terminalDrag] = G.useSplitter({ initial: 230, min: 120, max: 560, axis: 'y', invert: true, storageKey: 'gufa-code:h:terminal' })
378
+ const [explorerWidth, explorerDrag] = G.useSplitter({ initial: 260, min: 190, max: 520, storageKey: 'gufa-code:w:explorer', cssVar: '--gufa-w-explorer', targetSelector: '.gufa-sidepanel' })
379
+ const [chatWidth, chatDrag] = G.useSplitter({ initial: 430, min: 320, max: 780, axis: 'x', invert: true, storageKey: 'gufa-code:w:chat', cssVar: '--gufa-w-chat', targetSelector: '.gufa-chatpane' })
380
+ const [terminalHeight, terminalDrag] = G.useSplitter({ initial: 230, min: 120, max: 560, axis: 'y', invert: true, storageKey: 'gufa-code:h:terminal', cssVar: '--gufa-h-terminal', targetSelector: '.gufa-termpane' })
287
381
  useEffect(() => {
288
382
  tabsRef.current = tabs
289
383
  }, [tabs])
@@ -710,6 +804,7 @@ G.GufaApp = function GufaApp(props) {
710
804
  if (project === null) return
711
805
  const tab = tabsRef.current.find((item) => item !== null && item.path === path)
712
806
  if (tab === undefined) return
807
+ if (tab.external === true) return
713
808
  /* 缓冲里可能有比 tabs 更新的内容(按键后 300ms 内保存/自动保存) */
714
809
  const content = editorBuffersRef.current.has(path) ? editorBuffersRef.current.get(path) : tab.content
715
810
  if (content === tab.savedContent) {
@@ -1161,6 +1256,65 @@ G.GufaApp = function GufaApp(props) {
1161
1256
  }
1162
1257
  }, [project, tabs, patchTab, refreshGit, t])
1163
1258
 
1259
+ /* TS 内置 lib 定义(parseInt/Promise 等):以只读标签打开 */
1260
+ const openExternalTab = useCallback(async (detail) => {
1261
+ if (detail === null || detail === undefined) return
1262
+ const absolutePath = typeof detail.absolutePath === 'string' ? detail.absolutePath : ''
1263
+ const libName = typeof detail.libName === 'string' ? detail.libName : ''
1264
+ if (absolutePath === '' && libName === '') return
1265
+ const relative = libName !== ''
1266
+ ? libName
1267
+ : (typeof detail.path === 'string' && detail.path !== '' ? detail.path.replace(/^\/+/, '') : 'lib.d.ts')
1268
+ const pseudoPath = `~typescript/${relative}`
1269
+ flushBuffersRef.current?.()
1270
+ const existing = (tabsRef.current ?? []).find((tab) => tab !== null && tab !== undefined && tab.path === pseudoPath)
1271
+ if (existing !== undefined) {
1272
+ setActivePath(pseudoPath)
1273
+ window.setTimeout(() => {
1274
+ try {
1275
+ G.bus.emit('editor:reveal', { path: pseudoPath, line: Number(detail.line) || 1, column: Number(detail.column) || 1 })
1276
+ } catch {
1277
+ /* ignore */
1278
+ }
1279
+ }, 120)
1280
+ return
1281
+ }
1282
+ const doc = await G.api('code.readTsLib', libName !== '' ? { libName } : { absolutePath })
1283
+ const content = doc !== null && doc !== undefined && typeof doc.content === 'string' ? doc.content : ''
1284
+ const tab = {
1285
+ path: pseudoPath,
1286
+ content,
1287
+ savedContent: content,
1288
+ revision: 'external',
1289
+ savedRevision: 'external',
1290
+ dirty: false,
1291
+ size: content.length,
1292
+ language: doc !== null && doc !== undefined && typeof doc.language === 'string' ? doc.language : 'typescript',
1293
+ external: true,
1294
+ absolutePath: detail.absolutePath,
1295
+ name: doc !== null && doc !== undefined && typeof doc.name === 'string' ? doc.name : pseudoPath,
1296
+ }
1297
+ setTabs((current) => (current.some((item) => item !== null && item !== undefined && item.path === pseudoPath) ? current : [...current, tab]))
1298
+ setActivePath(pseudoPath)
1299
+ window.setTimeout(() => {
1300
+ try {
1301
+ G.bus.emit('editor:reveal', { path: pseudoPath, line: Number(detail.line) || 1, column: Number(detail.column) || 1 })
1302
+ } catch {
1303
+ /* ignore */
1304
+ }
1305
+ }, 120)
1306
+ }, [])
1307
+
1308
+ useEffect(() => {
1309
+ if (G.bus === null || G.bus === undefined || typeof G.bus.on !== 'function') return undefined
1310
+ const offExternal = G.bus.on('editor:open-external', (detail) => {
1311
+ void openExternalTab(detail).catch((error) => setBanner(G.errorText(error)))
1312
+ })
1313
+ return () => {
1314
+ offExternal?.()
1315
+ }
1316
+ }, [openExternalTab])
1317
+
1164
1318
  /* 自动保存:dirty 后 700ms 静默保存(普通文件 / 工作树 diff / 两文件对比) */
1165
1319
  const saveFileRef = useRef(null)
1166
1320
  const saveCompareRef = useRef(null)
@@ -2460,7 +2614,7 @@ G.GufaApp = function GufaApp(props) {
2460
2614
  {
2461
2615
  className: 'gufa-root',
2462
2616
  'data-backdrop': backdropActive ? 'on' : undefined,
2463
- style: { '--gufa-dock-width': ui.chatOpen ? `${Math.round(chatWidth)}px` : '0px' },
2617
+ style: { '--gufa-dock-width': ui.chatOpen ? `var(--gufa-w-chat, ${Math.round(chatWidth)}px)` : '0px' },
2464
2618
  },
2465
2619
  backdropActive ? h(G.SkinBackdrop, { meta: skinBackdrop, enabled: true, strength: backdropStrength }) : null,
2466
2620
  h(
@@ -2595,7 +2749,7 @@ G.GufaApp = function GufaApp(props) {
2595
2749
  ? null
2596
2750
  : h(
2597
2751
  'div',
2598
- { className: 'gufa-sidepanel', style: { width: explorerWidth } },
2752
+ { className: 'gufa-sidepanel', style: { width: `var(--gufa-w-explorer, ${explorerWidth}px)` } },
2599
2753
  h('div', { style: { flex: '1 1 auto', minHeight: 0, display: 'flex', flexDirection: 'column' } }, leftPanel),
2600
2754
  ),
2601
2755
  ui.sidebarOpen === false ? null : h('div', { className: 'gufa-splitter', onMouseDown: explorerDrag, title: '' }),
@@ -2677,7 +2831,7 @@ G.GufaApp = function GufaApp(props) {
2677
2831
  h('div', { className: 'gufa-splitter', onMouseDown: chatDrag }),
2678
2832
  h(
2679
2833
  'div',
2680
- { className: 'gufa-chatpane', style: { width: chatWidth } },
2834
+ { className: 'gufa-chatpane', style: { width: `var(--gufa-w-chat, ${chatWidth}px)` } },
2681
2835
  h(G.ChatDock, {
2682
2836
  project,
2683
2837
  activeFile: activeFileForChat,