diff --git a/packages/independent/snapshot/package.json b/packages/independent/snapshot/package.json index 54d71409ee..c6ddcc51b5 100644 --- a/packages/independent/snapshot/package.json +++ b/packages/independent/snapshot/package.json @@ -1,6 +1,6 @@ { "name": "@jsenv/snapshot", - "version": "2.9.10", + "version": "2.9.11", "description": "Snapshot testing", "license": "MIT", "author": { @@ -38,7 +38,7 @@ "@jsenv/exception": "1.1.2", "@jsenv/humanize": "1.2.8", "@jsenv/filesystem": "4.10.2", - "@jsenv/terminal-recorder": "1.4.5", + "@jsenv/terminal-recorder": "1.4.6", "@jsenv/urls": "2.5.2", "@jsenv/utils": "2.1.2", "ansi-regex": "6.0.1", diff --git a/packages/independent/terminal-recorder/dist/js/terminal_recorder.js b/packages/independent/terminal-recorder/dist/js/terminal_recorder.js index ba9377ef89..e70ac60460 100644 --- a/packages/independent/terminal-recorder/dist/js/terminal_recorder.js +++ b/packages/independent/terminal-recorder/dist/js/terminal_recorder.js @@ -1987,59 +1987,77 @@ const initTerminal = ({ const terminalElement = document.getElementById("terminal"); term.open(terminalElement); - const canvas = document.querySelector("#canvas"); - const context = canvas.getContext("2d", { willReadFrequently: true }); - context.imageSmoothingEnabled = true; - const xtermCanvas = document.querySelector( - "#terminal canvas.xterm-link-layer + canvas", - ); - const headerHeight = 40; - canvas.width = paddingLeft + xtermCanvas.width + paddingRight; - canvas.height = headerHeight + xtermCanvas.height; - - { - drawRectangle(context, { - x: 0, - y: 0, - width: canvas.width, - height: canvas.height, - fill: "#282c34", - stroke: "rgba(255,255,255,0.35)", - strokeWidth: 10, - radius: 8, - }); - drawCircle(context, { - x: 20, - y: headerHeight / 2, - radius: 6, - fill: "#ff5f57", - }); - drawCircle(context, { - x: 40, - y: headerHeight / 2, - radius: 6, - fill: "#febc2e", - }); - drawCircle(context, { - x: 60, - y: headerHeight / 2, - radius: 6, - fill: "#28c840", - }); - - // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text - const text = "Terminal"; - context.font = `${fontSize}px ${fontFamily}`; - context.textBaseline = "middle"; - context.textAlign = "center"; - // const textSize = context.measureText(text); - context.fillStyle = "#abb2bf"; - context.fillText(text, canvas.width / 2, headerHeight / 2); - } + const canvasPromise = (async () => { + const canvas = document.querySelector("#canvas"); + if (!canvas) { + throw new Error('Cannot find in the document'); + } + let xtermCanvas = document.querySelector( + "#terminal canvas.xterm-link-layer + canvas", + ); + if (!xtermCanvas) { + await new Promise((resolve) => setTimeout(resolve, 100)); + xtermCanvas = document.querySelector( + "#terminal canvas.xterm-link-layer + canvas", + ); + if (!xtermCanvas) { + throw new Error( + "Cannot find xterm canvas (canvas.xterm-link-layer + canvas)", + ); + } + } + return { canvas, xtermCanvas }; + })(); return { term, startRecording: async () => { + const { canvas, xtermCanvas } = await canvasPromise; + const context = canvas.getContext("2d", { willReadFrequently: true }); + context.imageSmoothingEnabled = true; + const headerHeight = 40; + canvas.width = paddingLeft + xtermCanvas.width + paddingRight; + canvas.height = headerHeight + xtermCanvas.height; + { + drawRectangle(context, { + x: 0, + y: 0, + width: canvas.width, + height: canvas.height, + fill: "#282c34", + stroke: "rgba(255,255,255,0.35)", + strokeWidth: 10, + radius: 8, + }); + drawCircle(context, { + x: 20, + y: headerHeight / 2, + radius: 6, + fill: "#ff5f57", + }); + drawCircle(context, { + x: 40, + y: headerHeight / 2, + radius: 6, + fill: "#febc2e", + }); + drawCircle(context, { + x: 60, + y: headerHeight / 2, + radius: 6, + fill: "#28c840", + }); + + // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text + const text = "Terminal"; + context.font = `${fontSize}px ${fontFamily}`; + context.textBaseline = "middle"; + context.textAlign = "center"; + // const textSize = context.measureText(text); + context.fillStyle = "#abb2bf"; + context.fillText(text, canvas.width / 2, headerHeight / 2); + } + const records = {}; let writePromise = Promise.resolve(); const frameCallbackSet = new Set(); diff --git a/packages/independent/terminal-recorder/package.json b/packages/independent/terminal-recorder/package.json index 46424a7b6d..f0c0b3a56e 100644 --- a/packages/independent/terminal-recorder/package.json +++ b/packages/independent/terminal-recorder/package.json @@ -1,6 +1,6 @@ { "name": "@jsenv/terminal-recorder", - "version": "1.4.5", + "version": "1.4.6", "description": "Record terminal output as .svg, .gif, .webm, .mp4", "license": "MIT", "repository": { diff --git a/packages/independent/terminal-recorder/scripts/build.mjs b/packages/independent/terminal-recorder/scripts/build.mjs index 5d4db43218..39c44926a6 100644 --- a/packages/independent/terminal-recorder/scripts/build.mjs +++ b/packages/independent/terminal-recorder/scripts/build.mjs @@ -4,12 +4,8 @@ import { jsenvPluginCommonJs } from "@jsenv/plugin-commonjs"; await build({ sourceDirectoryUrl: new URL("../src/", import.meta.url), buildDirectoryUrl: new URL("../dist/", import.meta.url), - entryPoints: { - "./client/xterm.html": "xterm.html", - }, - runtimeCompat: { - chrome: "100", - }, + entryPoints: { "./client/xterm.html": "xterm.html" }, + runtimeCompat: { chrome: "100" }, minification: false, versioning: false, }); @@ -17,12 +13,8 @@ await build({ await build({ sourceDirectoryUrl: new URL("../src/", import.meta.url), buildDirectoryUrl: new URL("../dist/", import.meta.url), - entryPoints: { - "./main_browser.js": "terminal_recorder_browser.js", - }, - runtimeCompat: { - chrome: "100", - }, + entryPoints: { "./main_browser.js": "terminal_recorder_browser.js" }, + runtimeCompat: { chrome: "100" }, plugins: [ jsenvPluginCommonJs({ include: { diff --git a/packages/independent/terminal-recorder/src/client/terminal_recorder.js b/packages/independent/terminal-recorder/src/client/terminal_recorder.js index 27b22040d0..1d95f4ffae 100644 --- a/packages/independent/terminal-recorder/src/client/terminal_recorder.js +++ b/packages/independent/terminal-recorder/src/client/terminal_recorder.js @@ -89,59 +89,77 @@ export const initTerminal = ({ const terminalElement = document.getElementById("terminal"); term.open(terminalElement); - const canvas = document.querySelector("#canvas"); - const context = canvas.getContext("2d", { willReadFrequently: true }); - context.imageSmoothingEnabled = true; - const xtermCanvas = document.querySelector( - "#terminal canvas.xterm-link-layer + canvas", - ); - const headerHeight = 40; - canvas.width = paddingLeft + xtermCanvas.width + paddingRight; - canvas.height = headerHeight + xtermCanvas.height; - - draw_header: { - drawRectangle(context, { - x: 0, - y: 0, - width: canvas.width, - height: canvas.height, - fill: "#282c34", - stroke: "rgba(255,255,255,0.35)", - strokeWidth: 10, - radius: 8, - }); - drawCircle(context, { - x: 20, - y: headerHeight / 2, - radius: 6, - fill: "#ff5f57", - }); - drawCircle(context, { - x: 40, - y: headerHeight / 2, - radius: 6, - fill: "#febc2e", - }); - drawCircle(context, { - x: 60, - y: headerHeight / 2, - radius: 6, - fill: "#28c840", - }); - - // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text - const text = "Terminal"; - context.font = `${fontSize}px ${fontFamily}`; - context.textBaseline = "middle"; - context.textAlign = "center"; - // const textSize = context.measureText(text); - context.fillStyle = "#abb2bf"; - context.fillText(text, canvas.width / 2, headerHeight / 2); - } + const canvasPromise = (async () => { + const canvas = document.querySelector("#canvas"); + if (!canvas) { + throw new Error('Cannot find in the document'); + } + let xtermCanvas = document.querySelector( + "#terminal canvas.xterm-link-layer + canvas", + ); + if (!xtermCanvas) { + await new Promise((resolve) => setTimeout(resolve, 100)); + xtermCanvas = document.querySelector( + "#terminal canvas.xterm-link-layer + canvas", + ); + if (!xtermCanvas) { + throw new Error( + "Cannot find xterm canvas (canvas.xterm-link-layer + canvas)", + ); + } + } + return { canvas, xtermCanvas }; + })(); return { term, startRecording: async () => { + const { canvas, xtermCanvas } = await canvasPromise; + const context = canvas.getContext("2d", { willReadFrequently: true }); + context.imageSmoothingEnabled = true; + const headerHeight = 40; + canvas.width = paddingLeft + xtermCanvas.width + paddingRight; + canvas.height = headerHeight + xtermCanvas.height; + draw_header: { + drawRectangle(context, { + x: 0, + y: 0, + width: canvas.width, + height: canvas.height, + fill: "#282c34", + stroke: "rgba(255,255,255,0.35)", + strokeWidth: 10, + radius: 8, + }); + drawCircle(context, { + x: 20, + y: headerHeight / 2, + radius: 6, + fill: "#ff5f57", + }); + drawCircle(context, { + x: 40, + y: headerHeight / 2, + radius: 6, + fill: "#febc2e", + }); + drawCircle(context, { + x: 60, + y: headerHeight / 2, + radius: 6, + fill: "#28c840", + }); + + // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text + const text = "Terminal"; + context.font = `${fontSize}px ${fontFamily}`; + context.textBaseline = "middle"; + context.textAlign = "center"; + // const textSize = context.measureText(text); + context.fillStyle = "#abb2bf"; + context.fillText(text, canvas.width / 2, headerHeight / 2); + } + const records = {}; let writePromise = Promise.resolve(); const frameCallbackSet = new Set();