Skip to content

Commit bca5fce

Browse files
committed
refactor: tighten screenshot renderable guard
1 parent 600a371 commit bca5fce

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

spx-gui/src/components/editor/preview/stage-viewer/StageViewer.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,18 @@ function handleWheel(e: KonvaEventObject<WheelEvent>) {
471471
472472
// TODO: implement a standalone screenshot taker which does not depend on StageViewer
473473
// See details in https://github.com/goplus/builder/issues/1807 .
474-
const canTakeScreenshot = computed(() => {
475-
const size = containerSizeRef.value
476-
return container.value != null && container.value.isConnected && size != null && size.width > 0 && size.height > 0
477-
})
474+
const canTakeScreenshot = computed(() => stageConfig.value != null)
478475
479-
async function takeScreenshot(name: string, signal?: AbortSignal) {
476+
function ensureCanTakeScreenshot() {
480477
if (!canTakeScreenshot.value) throw new Error('stage viewer is not renderable for screenshot')
478+
}
479+
480+
async function takeScreenshot(name: string, signal?: AbortSignal) {
481+
ensureCanTakeScreenshot()
481482
const stage = await untilNotNull(stageRef, signal)
482483
const nodeTransformer = await untilNotNull(nodeTransformerRef, signal)
483484
await until(() => !loading.value, signal)
484-
if (!canTakeScreenshot.value) throw new Error('stage viewer is not renderable for screenshot')
485+
ensureCanTakeScreenshot()
485486
// Omit transform control when taking screenshot
486487
const blob = await nodeTransformer.withHidden(
487488
() =>

spx-gui/src/models/spx/project.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { fromText, toConfig, type Files } from '../common/file'
77
import * as hashHelper from '../common/hash'
88
import { Backdrop } from './backdrop'
99
import { Monitor } from './widget/monitor'
10-
import { SpxProject, projectConfigFilePath, type RawProjectConfig } from './project'
10+
import { SpxProject, projectConfigFilePath, type RawProjectConfig, type ScreenshotTaker } from './project'
1111

1212
function mockFile(name = 'mocked') {
1313
return fromText(name, Math.random() + '')
1414
}
1515

16-
function makeProject(screenshotTaker: (() => Promise<ReturnType<typeof mockFile>>) | null = async () => mockFile()) {
16+
function makeProject(screenshotTaker: ScreenshotTaker | null = async () => mockFile()) {
1717
const project = new SpxProject()
1818
const sound = new Sound('sound', mockFile())
1919
project.addSound(sound)
@@ -104,6 +104,7 @@ describe('Project', () => {
104104
const thumbnail = mockFile('thumbnail')
105105
const unbindScreenshotTaker = project.bindScreenshotTaker(async () => thumbnail)
106106

107+
// Schedule the debounced update first, then flush it immediately for a deterministic assertion.
107108
project['updateThumbnail']()
108109
await project['updateThumbnail'].flush()
109110
unbindScreenshotTaker()

0 commit comments

Comments
 (0)