import { build_link_handler } from "./links.js"; // drawImage on a composited WebGL canvas returns blank pixels unless // preserveDrawingBuffer is set (needed by the pinch overlay snapshot); this must // run before xterm.js's WebglAddon creates its context. function ensurePreserveDrawingBuffer() { if (window.__zjPreserveDrawingBuffer) return; window.__zjPreserveDrawingBuffer = true; const orig = HTMLCanvasElement.prototype.getContext; HTMLCanvasElement.prototype.getContext = function (type, options) { if (type === "webgl" || type === "webgl2") { options = Object.assign({}, options || {}, { preserveDrawingBuffer: true, }); } return orig.call(this, type, options); }; } export function initTerminal() { ensurePreserveDrawingBuffer(); const term = new Terminal({ fontFamily: "Monospace", allowProposedApi: true, scrollback: 0, }); window.term = term; const fitAddon = new FitAddon.FitAddon(); const clipboardAddon = new ClipboardAddon.ClipboardAddon(); const { linkHandler, activateLink } = build_link_handler(); const webLinksAddon = new WebLinksAddon.WebLinksAddon( activateLink, linkHandler ); term.options.linkHandler = linkHandler; const webglAddon = new WebglAddon.WebglAddon(); term.loadAddon(fitAddon); term.loadAddon(clipboardAddon); term.loadAddon(webLinksAddon); webglAddon.onContextLoss((e) => { webglAddon.dispose(); }); term.loadAddon(webglAddon); term.open(document.getElementById("terminal")); fitAddon.fit(); term.focus(); return { term, fitAddon }; } export const MIN_FONT_SIZE_PX = 6; export const MAX_FONT_SIZE_PX = 96; export function applyFontSize(term, fitAddon, requestedPx) { const requested = typeof requestedPx === "number" && requestedPx > 0 ? requestedPx : term.options.fontSize || 12; const effective = clampFontSize(requested); if (term.options.fontSize !== effective) { term.options.fontSize = effective; } try { fitAddon.fit(); } catch (e) { } } export function clampFontSize(px) { const n = Math.round(Number(px) || 0); if (n < MIN_FONT_SIZE_PX) return MIN_FONT_SIZE_PX; if (n > MAX_FONT_SIZE_PX) return MAX_FONT_SIZE_PX; return n; }