@adecore/terminal 0.20.0 → 0.21.0

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.
@@ -1,5 +1,6 @@
1
1
  import { type Ref } from 'react';
2
2
  import { Terminal } from '@xterm/xterm';
3
+ import { type TerminalLinkBounds } from './links.ts';
3
4
  export interface TerminalSize {
4
5
  cols: number;
5
6
  rows: number;
@@ -21,7 +22,8 @@ export interface TerminalViewProps {
21
22
  ref?: Ref<TerminalViewHandle>;
22
23
  onData?(data: string): void;
23
24
  onResize?(cols: number, rows: number): void;
24
- onOpenLink?(uri: string): void;
25
+ onOpenLink?(uri: string, event: MouseEvent): void;
26
+ onLinkHover?(uri: string | null, bounds: TerminalLinkBounds | null): void;
25
27
  fontSize?: number;
26
28
  lineHeight?: number;
27
29
  readOnly?: boolean;
@@ -31,4 +33,4 @@ export interface TerminalViewProps {
31
33
  label?: string;
32
34
  className?: string;
33
35
  }
34
- export declare function TerminalView({ ref, onData, onResize, onOpenLink, fontSize, lineHeight, readOnly, convertEol, webgl, scaledByAncestor, label, className }: TerminalViewProps): import("react").JSX.Element;
36
+ export declare function TerminalView({ ref, onData, onResize, onOpenLink, onLinkHover, fontSize, lineHeight, readOnly, convertEol, webgl, scaledByAncestor, label, className }: TerminalViewProps): import("react").JSX.Element;
@@ -2,9 +2,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useImperativeHandle, useRef } from 'react';
3
3
  import clsx from 'clsx';
4
4
  import { FitAddon } from '@xterm/addon-fit';
5
- import { WebLinksAddon } from '@xterm/addon-web-links';
6
5
  import { Terminal } from '@xterm/xterm';
7
6
  import { readTerminalFont, readTerminalTheme } from './theme.js';
7
+ import { bindTerminalLinks } from './links.js';
8
8
  import { webglBudget } from './webgl-budget.js';
9
9
  import { fitToHost, followAncestorScale } from './xterm-internals.js';
10
10
  const RESIZE_DEBOUNCE_MS = 50;
@@ -12,13 +12,13 @@ const RESIZE_DEBOUNCE_MS = 50;
12
12
  const ROOT_ATTRIBUTES = ['data-theme', 'style', 'class'];
13
13
  let terminals = 0;
14
14
  /* A terminal on xterm.js that fits its container and follows the theme. The app feeds it through the handle. */
15
- export function TerminalView({ ref, onData, onResize, onOpenLink, fontSize = 13, lineHeight = 1, readOnly = false, convertEol = false, webgl = false, scaledByAncestor = false, label, className }) {
15
+ export function TerminalView({ ref, onData, onResize, onOpenLink, onLinkHover, fontSize = 13, lineHeight = 1, readOnly = false, convertEol = false, webgl = false, scaledByAncestor = false, label, className }) {
16
16
  const hostRef = useRef(null);
17
17
  const live = useRef(null);
18
18
  // Filled with the first props, so the mount reads them before the effect below brings them up to date.
19
- const latest = useRef({ onData, onResize, onOpenLink, fontSize, lineHeight, scaledByAncestor });
19
+ const latest = useRef({ onData, onResize, onOpenLink, onLinkHover, fontSize, lineHeight, scaledByAncestor });
20
20
  useEffect(() => {
21
- latest.current = { onData, onResize, onOpenLink, fontSize, lineHeight, scaledByAncestor };
21
+ latest.current = { onData, onResize, onOpenLink, onLinkHover, fontSize, lineHeight, scaledByAncestor };
22
22
  });
23
23
  useImperativeHandle(ref, () => ({
24
24
  get terminal() {
@@ -84,8 +84,8 @@ export function TerminalView({ ref, onData, onResize, onOpenLink, fontSize = 13,
84
84
  });
85
85
  const fit = new FitAddon();
86
86
  term.loadAddon(fit);
87
- term.loadAddon(new WebLinksAddon(latest.current.onOpenLink ? (_event, uri) => latest.current.onOpenLink?.(uri) : undefined));
88
87
  term.open(host);
88
+ const disposeLinks = bindTerminalLinks(term, () => latest.current);
89
89
  if (initial.scaledByAncestor) {
90
90
  followAncestorScale(term);
91
91
  }
@@ -179,6 +179,7 @@ export function TerminalView({ ref, onData, onResize, onOpenLink, fontSize = 13,
179
179
  data.dispose();
180
180
  // The context goes back before the terminal is gone, so the next one can take it.
181
181
  current.setWebgl(false);
182
+ disposeLinks();
182
183
  term.dispose();
183
184
  live.current = null;
184
185
  };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { TerminalView, type TerminalSize, type TerminalViewHandle, type TerminalViewProps } from './TerminalView.tsx';
2
2
  export { webglTerminals } from './webgl-budget.ts';
3
+ export { linkLineBounds, type TerminalLinkBounds } from './links.ts';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { TerminalView } from './TerminalView.js';
2
2
  export { webglTerminals } from './webgl-budget.js';
3
+ export { linkLineBounds } from './links.js';
@@ -0,0 +1,18 @@
1
+ import type { IBufferRange, Terminal } from '@xterm/xterm';
2
+ export interface TerminalLinkBounds {
3
+ x: number;
4
+ y: number;
5
+ width: number;
6
+ height: number;
7
+ }
8
+ export declare function linkLineBounds(range: IBufferRange, grid: {
9
+ cols: number;
10
+ rows: number;
11
+ viewportY: number;
12
+ }, screen: Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>, pointerY: number): TerminalLinkBounds | null;
13
+ interface TerminalLinks {
14
+ onOpenLink?(uri: string, event: MouseEvent): void;
15
+ onLinkHover?(uri: string | null, bounds: TerminalLinkBounds | null): void;
16
+ }
17
+ export declare function bindTerminalLinks(term: Terminal, callbacks: () => TerminalLinks): () => void;
18
+ export {};
package/dist/links.js ADDED
@@ -0,0 +1,70 @@
1
+ import { WebLinksAddon } from '@xterm/addon-web-links';
2
+ export function linkLineBounds(range, grid, screen, pointerY) {
3
+ if (screen.width <= 0 || screen.height <= 0) {
4
+ return null;
5
+ }
6
+ const cellWidth = screen.width / grid.cols;
7
+ const cellHeight = screen.height / grid.rows;
8
+ const row = Math.max(range.start.y, Math.min(range.end.y, grid.viewportY + Math.floor((pointerY - screen.top) / cellHeight) + 1));
9
+ const start = row === range.start.y ? range.start.x : 1;
10
+ const end = row === range.end.y ? range.end.x : grid.cols;
11
+ return {
12
+ x: screen.left + (start - 1) * cellWidth,
13
+ y: screen.top + (row - grid.viewportY - 1) * cellHeight,
14
+ width: (end - start + 1) * cellWidth,
15
+ height: cellHeight
16
+ };
17
+ }
18
+ export function bindTerminalLinks(term, callbacks) {
19
+ const element = term.element;
20
+ let press = null;
21
+ const rememberPress = (event) => {
22
+ press = event.button === 0 ? { x: event.clientX, y: event.clientY } : null;
23
+ };
24
+ const cancelPress = () => {
25
+ press = null;
26
+ };
27
+ const activate = (event, uri) => {
28
+ const start = press;
29
+ press = null;
30
+ // xterm 6 activates on mouseup, including selection drags and non-primary buttons.
31
+ if (!start || event.button !== 0 || event.defaultPrevented || term.hasSelection() || Math.hypot(event.clientX - start.x, event.clientY - start.y) > 3) {
32
+ return;
33
+ }
34
+ // OSC 8 targets and detected URLs share the same web-only boundary.
35
+ if (!/^https?:\/\//i.test(uri) || !URL.canParse(uri)) {
36
+ return;
37
+ }
38
+ const open = callbacks().onOpenLink;
39
+ if (open) {
40
+ open(uri, event);
41
+ }
42
+ else {
43
+ // Shells that deny new windows need the target in the initial request.
44
+ window.open(uri, '_blank', 'noopener,noreferrer');
45
+ }
46
+ };
47
+ const hover = (event, uri, range) => {
48
+ const screen = element.querySelector('.xterm-screen')?.getBoundingClientRect();
49
+ // Screen bounds include canvas zoom; xterm ranges are one-based buffer coordinates, including scrollback.
50
+ const bounds = screen
51
+ ? linkLineBounds(range, { cols: term.cols, rows: term.rows, viewportY: term.buffer.active.viewportY }, screen, event.clientY)
52
+ : null;
53
+ callbacks().onLinkHover?.(bounds ? uri : null, bounds);
54
+ };
55
+ const leave = () => callbacks().onLinkHover?.(null, null);
56
+ const previous = term.options.linkHandler;
57
+ term.options.linkHandler = { activate, hover, leave, allowNonHttpProtocols: false };
58
+ const addon = new WebLinksAddon(activate, { hover, leave });
59
+ term.loadAddon(addon);
60
+ element.addEventListener('mousedown', rememberPress, true);
61
+ element.addEventListener('pointercancel', cancelPress);
62
+ element.addEventListener('mouseleave', cancelPress);
63
+ return () => {
64
+ element.removeEventListener('mousedown', rememberPress, true);
65
+ element.removeEventListener('pointercancel', cancelPress);
66
+ element.removeEventListener('mouseleave', cancelPress);
67
+ term.options.linkHandler = previous;
68
+ addon.dispose();
69
+ };
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/terminal",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "A themed terminal pane on xterm.js that fits its container and is fed by the app.",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "author": "Bas Milius",
@@ -1,9 +1,9 @@
1
1
  import { useEffect, useImperativeHandle, useRef, type Ref } from 'react';
2
2
  import clsx from 'clsx';
3
3
  import { FitAddon } from '@xterm/addon-fit';
4
- import { WebLinksAddon } from '@xterm/addon-web-links';
5
4
  import { Terminal } from '@xterm/xterm';
6
5
  import { readTerminalFont, readTerminalTheme } from './theme.ts';
6
+ import { bindTerminalLinks, type TerminalLinkBounds } from './links.ts';
7
7
  import { webglBudget } from './webgl-budget.ts';
8
8
  import { fitToHost, followAncestorScale } from './xterm-internals.ts';
9
9
 
@@ -46,7 +46,9 @@ export interface TerminalViewProps {
46
46
  Not called for the grid at mount: `size()` has that one. */
47
47
  onResize?(cols: number, rows: number): void;
48
48
  /* Where a link in the output goes. Without it, a new window opens on the link. */
49
- onOpenLink?(uri: string): void;
49
+ onOpenLink?(uri: string, event: MouseEvent): void;
50
+ /* The complete target and its segment on the hovered row in viewport pixels; both null on leave. */
51
+ onLinkHover?(uri: string | null, bounds: TerminalLinkBounds | null): void;
50
52
  fontSize?: number;
51
53
  lineHeight?: number;
52
54
  readOnly?: boolean;
@@ -80,6 +82,7 @@ export function TerminalView({
80
82
  onData,
81
83
  onResize,
82
84
  onOpenLink,
85
+ onLinkHover,
83
86
  fontSize = 13,
84
87
  lineHeight = 1,
85
88
  readOnly = false,
@@ -92,10 +95,10 @@ export function TerminalView({
92
95
  const hostRef = useRef<HTMLDivElement>(null);
93
96
  const live = useRef<Live | null>(null);
94
97
  // Filled with the first props, so the mount reads them before the effect below brings them up to date.
95
- const latest = useRef({ onData, onResize, onOpenLink, fontSize, lineHeight, scaledByAncestor });
98
+ const latest = useRef({ onData, onResize, onOpenLink, onLinkHover, fontSize, lineHeight, scaledByAncestor });
96
99
 
97
100
  useEffect(() => {
98
- latest.current = { onData, onResize, onOpenLink, fontSize, lineHeight, scaledByAncestor };
101
+ latest.current = { onData, onResize, onOpenLink, onLinkHover, fontSize, lineHeight, scaledByAncestor };
99
102
  });
100
103
 
101
104
  useImperativeHandle(
@@ -167,8 +170,8 @@ export function TerminalView({
167
170
  });
168
171
  const fit = new FitAddon();
169
172
  term.loadAddon(fit);
170
- term.loadAddon(new WebLinksAddon(latest.current.onOpenLink ? (_event, uri) => latest.current.onOpenLink?.(uri) : undefined));
171
173
  term.open(host);
174
+ const disposeLinks = bindTerminalLinks(term, () => latest.current);
172
175
  if (initial.scaledByAncestor) {
173
176
  followAncestorScale(term);
174
177
  }
@@ -268,6 +271,7 @@ export function TerminalView({
268
271
  data.dispose();
269
272
  // The context goes back before the terminal is gone, so the next one can take it.
270
273
  current.setWebgl(false);
274
+ disposeLinks();
271
275
  term.dispose();
272
276
  live.current = null;
273
277
  };
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { TerminalView, type TerminalSize, type TerminalViewHandle, type TerminalViewProps } from './TerminalView.tsx';
2
2
  export { webglTerminals } from './webgl-budget.ts';
3
+ export { linkLineBounds, type TerminalLinkBounds } from './links.ts';
package/src/links.ts ADDED
@@ -0,0 +1,89 @@
1
+ import { WebLinksAddon } from '@xterm/addon-web-links';
2
+ import type { IBufferRange, Terminal } from '@xterm/xterm';
3
+
4
+ export interface TerminalLinkBounds {
5
+ x: number;
6
+ y: number;
7
+ width: number;
8
+ height: number;
9
+ }
10
+
11
+ export function linkLineBounds(
12
+ range: IBufferRange,
13
+ grid: { cols: number; rows: number; viewportY: number },
14
+ screen: Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>,
15
+ pointerY: number
16
+ ): TerminalLinkBounds | null {
17
+ if (screen.width <= 0 || screen.height <= 0) {
18
+ return null;
19
+ }
20
+ const cellWidth = screen.width / grid.cols;
21
+ const cellHeight = screen.height / grid.rows;
22
+ const row = Math.max(range.start.y, Math.min(range.end.y, grid.viewportY + Math.floor((pointerY - screen.top) / cellHeight) + 1));
23
+ const start = row === range.start.y ? range.start.x : 1;
24
+ const end = row === range.end.y ? range.end.x : grid.cols;
25
+ return {
26
+ x: screen.left + (start - 1) * cellWidth,
27
+ y: screen.top + (row - grid.viewportY - 1) * cellHeight,
28
+ width: (end - start + 1) * cellWidth,
29
+ height: cellHeight
30
+ };
31
+ }
32
+
33
+ interface TerminalLinks {
34
+ onOpenLink?(uri: string, event: MouseEvent): void;
35
+ onLinkHover?(uri: string | null, bounds: TerminalLinkBounds | null): void;
36
+ }
37
+
38
+ export function bindTerminalLinks(term: Terminal, callbacks: () => TerminalLinks): () => void {
39
+ const element = term.element!;
40
+ let press: { x: number; y: number } | null = null;
41
+ const rememberPress = (event: MouseEvent): void => {
42
+ press = event.button === 0 ? { x: event.clientX, y: event.clientY } : null;
43
+ };
44
+ const cancelPress = (): void => {
45
+ press = null;
46
+ };
47
+ const activate = (event: MouseEvent, uri: string): void => {
48
+ const start = press;
49
+ press = null;
50
+ // xterm 6 activates on mouseup, including selection drags and non-primary buttons.
51
+ if (!start || event.button !== 0 || event.defaultPrevented || term.hasSelection() || Math.hypot(event.clientX - start.x, event.clientY - start.y) > 3) {
52
+ return;
53
+ }
54
+ // OSC 8 targets and detected URLs share the same web-only boundary.
55
+ if (!/^https?:\/\//i.test(uri) || !URL.canParse(uri)) {
56
+ return;
57
+ }
58
+ const open = callbacks().onOpenLink;
59
+ if (open) {
60
+ open(uri, event);
61
+ } else {
62
+ // Shells that deny new windows need the target in the initial request.
63
+ window.open(uri, '_blank', 'noopener,noreferrer');
64
+ }
65
+ };
66
+ const hover = (event: MouseEvent, uri: string, range: IBufferRange): void => {
67
+ const screen = element.querySelector('.xterm-screen')?.getBoundingClientRect();
68
+ // Screen bounds include canvas zoom; xterm ranges are one-based buffer coordinates, including scrollback.
69
+ const bounds = screen
70
+ ? linkLineBounds(range, { cols: term.cols, rows: term.rows, viewportY: term.buffer.active.viewportY }, screen, event.clientY)
71
+ : null;
72
+ callbacks().onLinkHover?.(bounds ? uri : null, bounds);
73
+ };
74
+ const leave = (): void => callbacks().onLinkHover?.(null, null);
75
+ const previous = term.options.linkHandler;
76
+ term.options.linkHandler = { activate, hover, leave, allowNonHttpProtocols: false };
77
+ const addon = new WebLinksAddon(activate, { hover, leave });
78
+ term.loadAddon(addon);
79
+ element.addEventListener('mousedown', rememberPress, true);
80
+ element.addEventListener('pointercancel', cancelPress);
81
+ element.addEventListener('mouseleave', cancelPress);
82
+ return () => {
83
+ element.removeEventListener('mousedown', rememberPress, true);
84
+ element.removeEventListener('pointercancel', cancelPress);
85
+ element.removeEventListener('mouseleave', cancelPress);
86
+ term.options.linkHandler = previous;
87
+ addon.dispose();
88
+ };
89
+ }