|
| 1 | +import { FitAddon } from "@xterm/addon-fit"; |
| 2 | +import { Terminal } from "@xterm/xterm"; |
| 3 | +import "@xterm/xterm/css/xterm.css"; |
| 4 | +import * as React from "react"; |
| 5 | + |
| 6 | +import { base64ToArray } from "@/util/base64"; |
| 7 | + |
| 8 | +export type TsunamiTermElem = HTMLDivElement & { |
| 9 | + __termWrite: (data64: string) => void; |
| 10 | + __termFocus: () => void; |
| 11 | + __termSize: () => VDomTermSize | null; |
| 12 | +}; |
| 13 | + |
| 14 | +type TsunamiTermProps = React.HTMLAttributes<HTMLDivElement> & { |
| 15 | + onData?: (data: string | null, termsize: VDomTermSize | null) => void; |
| 16 | + termFontSize?: number; |
| 17 | + termFontFamily?: string; |
| 18 | + termScrollback?: number; |
| 19 | +}; |
| 20 | + |
| 21 | +const TsunamiTerm = React.forwardRef<HTMLDivElement, TsunamiTermProps>(function TsunamiTerm(props, ref) { |
| 22 | + const { onData, termFontSize, termFontFamily, termScrollback, ...outerProps } = props; |
| 23 | + const outerRef = React.useRef<TsunamiTermElem>(null); |
| 24 | + const termRef = React.useRef<HTMLDivElement>(null); |
| 25 | + const terminalRef = React.useRef<Terminal | null>(null); |
| 26 | + const onDataRef = React.useRef(onData); |
| 27 | + onDataRef.current = onData; |
| 28 | + |
| 29 | + const setOuterRef = React.useCallback( |
| 30 | + (elem: TsunamiTermElem) => { |
| 31 | + outerRef.current = elem; |
| 32 | + if (elem != null) { |
| 33 | + elem.__termWrite = (data64: string) => { |
| 34 | + if (data64 == null || data64 === "") { |
| 35 | + return; |
| 36 | + } |
| 37 | + try { |
| 38 | + terminalRef.current?.write(base64ToArray(data64)); |
| 39 | + } catch (error) { |
| 40 | + console.error("Failed to write to terminal:", error); |
| 41 | + } |
| 42 | + }; |
| 43 | + elem.__termFocus = () => { |
| 44 | + terminalRef.current?.focus(); |
| 45 | + }; |
| 46 | + elem.__termSize = () => { |
| 47 | + const terminal = terminalRef.current; |
| 48 | + if (terminal == null) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + return { rows: terminal.rows, cols: terminal.cols }; |
| 52 | + }; |
| 53 | + } |
| 54 | + if (typeof ref === "function") { |
| 55 | + ref(elem); |
| 56 | + return; |
| 57 | + } |
| 58 | + if (ref != null) { |
| 59 | + ref.current = elem; |
| 60 | + } |
| 61 | + }, |
| 62 | + [ref] |
| 63 | + ); |
| 64 | + |
| 65 | + React.useEffect(() => { |
| 66 | + if (termRef.current == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + const terminal = new Terminal({ |
| 70 | + convertEol: false, |
| 71 | + ...(termFontSize != null ? { fontSize: termFontSize } : {}), |
| 72 | + ...(termFontFamily != null ? { fontFamily: termFontFamily } : {}), |
| 73 | + ...(termScrollback != null ? { scrollback: termScrollback } : {}), |
| 74 | + }); |
| 75 | + const fitAddon = new FitAddon(); |
| 76 | + terminal.loadAddon(fitAddon); |
| 77 | + terminal.open(termRef.current); |
| 78 | + fitAddon.fit(); |
| 79 | + terminalRef.current = terminal; |
| 80 | + |
| 81 | + const onDataDisposable = terminal.onData((data) => { |
| 82 | + if (onDataRef.current == null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + onDataRef.current(data, null); |
| 86 | + }); |
| 87 | + const onResizeDisposable = terminal.onResize((size) => { |
| 88 | + if (onDataRef.current == null) { |
| 89 | + return; |
| 90 | + } |
| 91 | + onDataRef.current(null, { rows: size.rows, cols: size.cols }); |
| 92 | + }); |
| 93 | + if (onDataRef.current != null) { |
| 94 | + onDataRef.current(null, { rows: terminal.rows, cols: terminal.cols }); |
| 95 | + } |
| 96 | + |
| 97 | + const resizeObserver = new ResizeObserver(() => { |
| 98 | + fitAddon.fit(); |
| 99 | + }); |
| 100 | + if (outerRef.current != null) { |
| 101 | + resizeObserver.observe(outerRef.current); |
| 102 | + } |
| 103 | + |
| 104 | + return () => { |
| 105 | + resizeObserver.disconnect(); |
| 106 | + onResizeDisposable.dispose(); |
| 107 | + onDataDisposable.dispose(); |
| 108 | + terminal.dispose(); |
| 109 | + terminalRef.current = null; |
| 110 | + }; |
| 111 | + }, []); |
| 112 | + |
| 113 | + React.useEffect(() => { |
| 114 | + const terminal = terminalRef.current; |
| 115 | + if (terminal == null) { |
| 116 | + return; |
| 117 | + } |
| 118 | + if (termFontSize != null) { |
| 119 | + terminal.options.fontSize = termFontSize; |
| 120 | + } |
| 121 | + if (termFontFamily != null) { |
| 122 | + terminal.options.fontFamily = termFontFamily; |
| 123 | + } |
| 124 | + if (termScrollback != null) { |
| 125 | + terminal.options.scrollback = termScrollback; |
| 126 | + } |
| 127 | + }, [termFontSize, termFontFamily, termScrollback]); |
| 128 | + |
| 129 | + const handleFocus = React.useCallback( |
| 130 | + (e: React.FocusEvent<HTMLDivElement>) => { |
| 131 | + terminalRef.current?.focus(); |
| 132 | + outerProps.onFocus?.(e); |
| 133 | + }, |
| 134 | + [outerProps.onFocus] |
| 135 | + ); |
| 136 | + |
| 137 | + const handleBlur = React.useCallback( |
| 138 | + (e: React.FocusEvent<HTMLDivElement>) => { |
| 139 | + terminalRef.current?.blur(); |
| 140 | + outerProps.onBlur?.(e); |
| 141 | + }, |
| 142 | + [outerProps.onBlur] |
| 143 | + ); |
| 144 | + |
| 145 | + return ( |
| 146 | + <div |
| 147 | + {...outerProps} |
| 148 | + ref={setOuterRef as React.RefCallback<HTMLDivElement>} |
| 149 | + onFocus={handleFocus} |
| 150 | + onBlur={handleBlur} |
| 151 | + > |
| 152 | + <div ref={termRef} className="w-full h-full" /> |
| 153 | + </div> |
| 154 | + ); |
| 155 | +}); |
| 156 | + |
| 157 | +export { TsunamiTerm }; |
0 commit comments