opencode-scroll-mode 0.3.2 → 0.3.4

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -5
  3. package/package.json +2 -1
  4. package/tui.ts +110 -10
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 qufeiyan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,12 +2,48 @@
2
2
 
3
3
  OpenCode TUI 的「对话滚动模式」插件:一键切换编辑 / 滚动两种状态,滚动时支持 vim 键位、消息搜索与 visual 选区复制。
4
4
 
5
+ ## 安装
6
+
7
+ ### npm(推荐)
8
+
9
+ ```sh
10
+ opencode plugin add opencode-scroll-mode
11
+ ```
12
+
13
+ `opencode plugin add` 会从 npm 安装并写入全局 `opencode.json(c)` 的 `plugins`。也可以锁定版本:
14
+
15
+ ```sh
16
+ opencode plugin add opencode-scroll-mode@0.3.3
17
+ ```
18
+
19
+ 升级 / 移除:
20
+
21
+ ```sh
22
+ opencode plugin update opencode-scroll-mode
23
+ opencode plugin remove opencode-scroll-mode
24
+ ```
25
+
26
+ ### 手动配置
27
+
28
+ 在 `opencode.json(c)` 的 `plugins` 中加入包名,OpenCode 会在启动时自动安装缺失的 npm 包:
29
+
30
+ ```jsonc
31
+ {
32
+ "$schema": "https://opencode.ai/config.json",
33
+ "plugins": ["opencode-scroll-mode"]
34
+ }
35
+ ```
36
+
37
+ > 本插件同时包含 server 与 TUI 入口,因此配置在 `opencode.json(c)`;只含 TUI 的插件才放在 `cli.json`。
38
+ >
39
+ > 如果不使用 npm 包,也可以把插件目录放在自动加载路径(如 `~/.config/opencode/plugins/scroll-mode/`),无需再写进 `plugins`。
40
+
5
41
  ## 功能
6
42
 
7
43
  - `ctrl+[`(默认,可用 `toggleKey` 配置)在「编辑模式」与「滚动模式」之间切换。
8
44
  - 编辑模式:输入框保留原生 readline 键位。
9
45
  - 滚动模式:导航键位移动 transcript 中的行 / 字符游标;只有游标到达视口边缘时才让宿主滚动,并启用 vim 键位与搜索。
10
- - 滚动模式会**锁定输入框**:进入时让编辑器失焦、隐藏编辑光标,并全局吞掉按键,避免滚动时误把字符打进 prompt;退出时恢复焦点、光标与输入。对话框(搜索、选择等)获得焦点时自动放行。
46
+ - 滚动模式会**锁定输入框**:进入时让编辑器失焦、隐藏编辑光标,并全局吞掉按键,避免滚动时误把字符打进 prompt;退出时恢复焦点、光标与输入。当其他组件(命令面板、搜索、选择等对话框)获得焦点时,滚动键位会**整体挂起**,把键盘交给对话框,避免在面板里打字时后台游标乱跳。
11
47
  - 滚动全部委托给 OpenCode 内置的 `session.*` 命令,不自行计算偏移,避免与 transcript 分页冲突。
12
48
  - 状态存于内存:每次 TUI 启动重置,插件热重载时保留。
13
49
 
@@ -80,13 +116,13 @@ yank 后写入系统剪贴板:优先 **OSC52**(iTerm2 需在设置里允许
80
116
 
81
117
  ## 配置
82
118
 
83
- 放在自动加载目录(如 `~/.config/opencode/plugins/scroll-mode/`)即生效。若需传参数,在 `opencode.json` 的 `plugins` 中显式列出:
119
+ 传参数时使用 `plugins` 的对象形式;`package` 可以是 npm 包名,也可以是本地插件目录(本地目录若已在自动加载路径中,则二选一,避免重复加载):
84
120
 
85
121
  ```jsonc
86
122
  {
87
123
  "plugins": [
88
124
  {
89
- "package": "./plugins-src/scroll-mode",
125
+ "package": "opencode-scroll-mode",
90
126
  "options": {
91
127
  "vim": true,
92
128
  "search": true,
@@ -120,8 +156,6 @@ yank 后写入系统剪贴板:优先 **OSC52**(iTerm2 需在设置里允许
120
156
  > - `ctrl+[` 在多数终端里和 `Esc` 发送同一个字节(`0x1b`):只有终端开启 **kitty keyboard 协议**(kitty / WezTerm / Ghostty / 新版 iTerm2)时才能被识别成独立的 `Ctrl+[`;跑在 Neovim `:terminal` / sidekick.nvim 里时它就是纯 `Esc`,不会触发切换。
121
157
  > - 如果在你的环境里 `ctrl+[` 没反应,改用 `"toggleKey": "f9"`(nvim / 终端都不拦截)最稳。
122
158
 
123
- > 提示:同一目录若既位于自动加载的 `plugins/` 下、又在 `plugins` 中显式列出,可能被加载两次。二选一。
124
-
125
159
  ## 结构
126
160
 
127
161
  - `index.ts` — 服务端入口(仅注册 id)。
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "opencode-scroll-mode",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
+ "license": "MIT",
4
5
  "description": "OpenCode TUI scroll mode plugin — toggle edit/scroll states with vim keys, message search, and line-precise visual selection/copy",
5
6
  "type": "module",
6
7
  "exports": {
package/tui.ts CHANGED
@@ -496,6 +496,12 @@ export default Plugin.define({
496
496
  cursorCol: 0,
497
497
  },
498
498
  })
499
+ // True while a widget other than the composer owns focus (the command
500
+ // palette, search, selects, …). While true the scroll keymap is suspended
501
+ // so typing in that dialog doesn't move the background cursor.
502
+ const [focusState, setFocusState] = context.storage.memory("scroll-mode.focus", {
503
+ initial: { foreign: false },
504
+ })
499
505
 
500
506
  // ---- editor lock while scroll mode is on -------------------------------
501
507
  // Scroll mode owns the keyboard: the composer must not receive text, or
@@ -515,6 +521,7 @@ export default Plugin.define({
515
521
  let savedFocus: any = null
516
522
  let detachGuard: (() => void) | undefined
517
523
  let detachCursorGuard: (() => void) | undefined
524
+ let detachFocusGuard: (() => void) | undefined
518
525
 
519
526
  const focusedRenderable = (): any => {
520
527
  try {
@@ -576,6 +583,9 @@ export default Plugin.define({
576
583
  const exitScroll = () => {
577
584
  stopPoll()
578
585
  clearLineSelection()
586
+ setFocusState((draft: any) => {
587
+ if (draft.foreign) draft.foreign = false
588
+ })
579
589
  const target = savedFocus
580
590
  savedFocus = null
581
591
  restoreEditorCursor(target)
@@ -594,11 +604,66 @@ export default Plugin.define({
594
604
  }
595
605
  }
596
606
 
607
+ // Suspend everything scroll mode paints, without leaving scroll mode.
608
+ //
609
+ // The user-visible bug: while a dialog (command palette, prompt, select)
610
+ // owns focus, the transcript highlight could still be re-resolved against
611
+ // the live layout and repaint a different line, which reads as "the
612
+ // background cursor jumps". Exiting scroll mode entirely is not an option:
613
+ // exitScroll() calls focus() on the saved composer and would steal focus
614
+ // back from the dialog. So on dialog focus we tear down the visual layer
615
+ // and stop the poll, and on focus return we rebuild it.
616
+ //
617
+ // This also covers our own dialogs (search prompt, selects): typing there
618
+ // no longer has any chance of moving the transcript cursor.
619
+ const applyFocusState = (foreign: boolean) => {
620
+ if (!state.active) return
621
+ const changed = focusState.foreign !== foreign
622
+ if (changed) {
623
+ setFocusState((draft: any) => {
624
+ draft.foreign = foreign
625
+ })
626
+ }
627
+ if (!changed) return
628
+ if (foreign) {
629
+ // Dialog took focus: no poll, no selection, no cursor bar.
630
+ stopPoll()
631
+ lastKey = null
632
+ barPos = null
633
+ clearLineSelection()
634
+ try {
635
+ renderer?.requestRender?.()
636
+ } catch {
637
+ /* ignore */
638
+ }
639
+ } else {
640
+ // Dialog closed: rebuild the highlight from the current layout.
641
+ lastKey = null
642
+ startPoll()
643
+ }
644
+ }
645
+
646
+ // Imperative focus check, read by each command's run() at execution time.
647
+ // The layer's reactive suspension (focusState.foreign) does not reliably
648
+ // re-evaluate on every host, so we do not trust a stored flag here: the
649
+ // command itself re-reads the live focus the moment it fires. Returns true
650
+ // when a dialog (or any widget other than the saved composer) owns focus.
651
+ const dialogHasFocus = (): boolean => {
652
+ if (!state.active) return false
653
+ const focused = focusedRenderable()
654
+ return !!focused && focused !== savedFocus
655
+ }
656
+
597
657
  const onKey = (key: any) => {
598
658
  if (!state.active) return
599
659
  const focused = focusedRenderable()
600
- // A dialog or other widget has taken focus: let it receive input.
601
- if (focused && focused !== savedFocus) return
660
+ // A dialog or other widget has taken focus: suspend the scroll keymap and
661
+ // let it receive input. Sync here too (not only via focused_renderable)
662
+ // so the suspension is in place before the next keystroke reaches the
663
+ // dispatcher, even if the host doesn't emit the focus event.
664
+ const foreign = !!focused && focused !== savedFocus
665
+ applyFocusState(foreign)
666
+ if (foreign) return
602
667
  if (focused) hideEditorCursor(focused)
603
668
  try {
604
669
  key.preventDefault()
@@ -622,6 +687,16 @@ export default Plugin.define({
622
687
  }
623
688
  }
624
689
 
690
+ // Track whether a widget other than the composer owns focus. While scroll
691
+ // mode is active and a dialog (palette, search, select) has focus, the
692
+ // scroll keymap is suspended so the dialog receives every key and the
693
+ // background cursor doesn't jump while typing.
694
+ const onFocusChange = (current: any) => {
695
+ if (!state.active) return
696
+ const foreign = !!current && current !== savedFocus
697
+ applyFocusState(foreign)
698
+ }
699
+
625
700
  const toast = (message: string, variant?: "info" | "success" | "warning" | "error") => {
626
701
  try {
627
702
  context.ui.toast.show(variant ? { message, variant } : { message })
@@ -1446,7 +1521,9 @@ export default Plugin.define({
1446
1521
 
1447
1522
  const drawCursorBar = (buffer: any) => {
1448
1523
  try {
1449
- if (!state.active || !options.highlight) return
1524
+ // While a dialog owns focus the whole scroll-mode overlay is suspended
1525
+ // (see applyFocusState): no bar, no selection, no poll.
1526
+ if (!state.active || !options.highlight || focusState.foreign) return
1450
1527
  refreshBar()
1451
1528
  if (!barPos || !barRgba) return
1452
1529
  if (barPos.y < 0) return
@@ -1830,9 +1907,11 @@ export default Plugin.define({
1830
1907
 
1831
1908
  keymap.layer(() => {
1832
1909
  const active = state.active
1910
+ const suspended = focusState.foreign
1833
1911
 
1834
- const commands: any[] = [
1835
- {
1912
+ const commands: any[] = []
1913
+ if (!suspended) {
1914
+ commands.push({
1836
1915
  id: "scroll-mode.toggle",
1837
1916
  title: "Toggle scroll mode",
1838
1917
  group: "Scroll mode",
@@ -1858,10 +1937,10 @@ export default Plugin.define({
1858
1937
  toast(`Scroll mode: editor disabled · ctrl+u / ctrl+d scroll, ${options.toggleKey} back to edit`)
1859
1938
  }
1860
1939
  },
1861
- },
1862
- ]
1940
+ })
1941
+ }
1863
1942
 
1864
- if (active) {
1943
+ if (active && !suspended) {
1865
1944
  // Half-page scrolling. The cursor keeps its screen row while the text
1866
1945
  // scrolls under it, in both normal and visual mode.
1867
1946
  commands.push(
@@ -2132,11 +2211,23 @@ export default Plugin.define({
2132
2211
  }
2133
2212
  }
2134
2213
 
2214
+ // Wrap every command's run() so it re-reads live focus at execution time.
2215
+ // If a dialog owns focus we return false (continue keyboard dispatch) so
2216
+ // the key reaches the dialog untouched, and the scroll action never runs.
2217
+ // This bypasses the layer's reactive suspension, which is unreliable.
2218
+ const guarded = commands.map((c: any) => ({
2219
+ ...c,
2220
+ run: (...args: any[]) => {
2221
+ if (dialogHasFocus()) return false
2222
+ return c.run(...args)
2223
+ },
2224
+ }))
2225
+
2135
2226
  return {
2136
2227
  mode: "global",
2137
2228
  priority: 100,
2138
- commands,
2139
- bindings: commands.map((c) => c.id),
2229
+ commands: guarded,
2230
+ bindings: guarded.map((c) => c.id),
2140
2231
  }
2141
2232
  })
2142
2233
 
@@ -2169,6 +2260,14 @@ export default Plugin.define({
2169
2260
  /* ignore */
2170
2261
  }
2171
2262
  }
2263
+ renderer.on("focused_renderable", onFocusChange)
2264
+ detachFocusGuard = () => {
2265
+ try {
2266
+ renderer.off("focused_renderable", onFocusChange)
2267
+ } catch {
2268
+ /* ignore */
2269
+ }
2270
+ }
2172
2271
  }
2173
2272
  } catch {
2174
2273
  /* ignore */
@@ -2184,6 +2283,7 @@ export default Plugin.define({
2184
2283
  clearTimeout(restoreIfActive)
2185
2284
  detachGuard?.()
2186
2285
  detachCursorGuard?.()
2286
+ detachFocusGuard?.()
2187
2287
  try {
2188
2288
  renderer?.removePostProcessFn?.(drawCursorBar)
2189
2289
  } catch {