diff --git a/frontend/src/components/document-preview.focus.test.mjs b/frontend/src/components/document-preview.focus.test.mjs new file mode 100644 index 00000000..312cd529 --- /dev/null +++ b/frontend/src/components/document-preview.focus.test.mjs @@ -0,0 +1,144 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import vm from 'node:vm' +import ts from 'typescript' +import { effectScope, nextTick, reactive, shallowRef as ref, watch } from 'vue' +import { parse, compileTemplate } from '@vue/compiler-sfc' + +const source = readFileSync(new URL('./document-preview.vue', import.meta.url), 'utf8') +const start = source.indexOf('const previewRoot =') +const end = source.indexOf('\nfunction toggleFullscreen()', start) +assert.ok(start >= 0 && end > start) +const focusSource = ts.transpile(source.slice(start, end)) + +async function flush() { + await nextTick() + await nextTick() +} + +function setup(t) { + const scope = effectScope() + t.after(() => scope.stop()) + const document = { body: {}, activeElement: null } + document.activeElement = document.body + const calls = [] + const element = name => ({ + isConnected: true, + focus(options) { + assert.equal(options.preventScroll, true) + document.activeElement = this + calls.push(name) + }, + }) + const props = reactive({ active: true, sourceKey: 'artifact:one' }) + const docxContainer = ref(null) + const htmlViewMode = ref('render') + const isFullscreen = ref(false) + const state = scope.run(() => vm.runInNewContext(`${focusSource}; ({ + previewRoot, previewContent, focusPreviewContent, onPreviewFrameLoad, + })`, { + ref, watch, nextTick, props, document, docxContainer, htmlViewMode, isFullscreen, + getPreviewSourceKey: () => props.sourceKey, + })) + return { ...state, props, document, docxContainer, htmlViewMode, isFullscreen, calls, element } +} + +test('opening a preview focuses its scroll container without scrolling ancestors', async t => { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + ctx.previewContent.value = ctx.element('markdown') + await flush() + assert.deepEqual(ctx.calls, ['markdown']) +}) + +test('delayed content inherits focus from the loading preview', async t => { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + await flush() + assert.deepEqual(ctx.calls, ['root']) + ctx.previewContent.value = ctx.element('code') + await flush() + assert.deepEqual(ctx.calls, ['root', 'code']) +}) + +test('loading does not steal focus from the composer or preview toolbar', async t => { + for (const target of ['composer', 'toolbar']) { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + await flush() + const control = ctx.element(target) + ctx.document.activeElement = control + ctx.previewContent.value = ctx.element('excel') + await flush() + assert.equal(ctx.document.activeElement, control) + assert.deepEqual(ctx.calls, ['root']) + } +}) + +test('hidden previews never take focus and reactivation focuses cached content', async t => { + const ctx = setup(t) + ctx.props.active = false + ctx.previewRoot.value = ctx.element('root') + ctx.previewContent.value = ctx.element('cached') + await flush() + ctx.focusPreviewContent() + assert.deepEqual(ctx.calls, []) + ctx.props.active = true + await flush() + assert.deepEqual(ctx.calls, ['cached']) +}) + +test('switching files, source mode and fullscreen restores content focus', async t => { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + ctx.previewContent.value = ctx.element('content') + await flush() + for (const update of [ + () => { ctx.props.sourceKey = 'artifact:two' }, + () => { ctx.htmlViewMode.value = 'source' }, + () => { ctx.isFullscreen.value = true }, + () => { ctx.isFullscreen.value = false }, + ]) { + ctx.document.activeElement = ctx.element('button') + update() + await flush() + assert.equal(ctx.document.activeElement, ctx.previewContent.value) + } +}) + +test('DOCX focuses the renderer container and detached previews do not focus', async t => { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + await flush() + ctx.docxContainer.value = ctx.element('docx') + await flush() + assert.deepEqual(ctx.calls, ['root', 'docx']) + ctx.previewRoot.value.isConnected = false + ctx.focusPreviewContent() + assert.deepEqual(ctx.calls, ['root', 'docx']) +}) + +test('iframe load retains preview focus without taking it from another input', async t => { + const ctx = setup(t) + ctx.previewRoot.value = ctx.element('root') + ctx.previewContent.value = ctx.element('iframe') + await flush() + ctx.onPreviewFrameLoad() + assert.deepEqual(ctx.calls, ['iframe', 'iframe']) + const input = ctx.element('input') + ctx.document.activeElement = input + ctx.onPreviewFrameLoad() + assert.equal(ctx.document.activeElement, input) +}) + +test('every document scroll target is tabbable and key events keep native behavior', () => { + const { descriptor } = parse(source) + const template = descriptor.template.content + for (const tag of template.matchAll(/<[^>]+ref="(?:previewContent|docxContainer)"[^>]*>/g)) { + assert.ok(/tabindex="0"|\bcontrols\b/.test(tag[0]), tag[0]) + } + assert.doesNotMatch(template, /@key(?:down|up)/) + assert.match(template, /sandbox="allow-scripts"/) + assert.deepEqual(compileTemplate({ source: template, filename: 'document-preview.vue', id: 'preview' }).errors, []) +}) diff --git a/frontend/src/components/document-preview.fullscreen.test.mjs b/frontend/src/components/document-preview.fullscreen.test.mjs new file mode 100644 index 00000000..98e8bbb5 --- /dev/null +++ b/frontend/src/components/document-preview.fullscreen.test.mjs @@ -0,0 +1,161 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import vm from 'node:vm' +import ts from 'typescript' +import { effectScope, nextTick, reactive, shallowRef as ref, watch } from 'vue' +import { parse, compileStyleAsync } from '@vue/compiler-sfc' + +const source = readFileSync(new URL('./document-preview.vue', import.meta.url), 'utf8') +const start = source.indexOf('function toggleFullscreen()') +const end = source.indexOf('function ensureBlobType(', start) +assert.ok(start >= 0 && end > start) +const fullscreenSource = ts.transpile(source.slice(start, end)) + +function setup(t, native = true) { + const scope = effectScope() + const mounted = [], unmounted = [], listeners = new Map() + const events = { + addEventListener(name, fn, capture) { listeners.set(name, { fn, capture }) }, + removeEventListener(name, fn) { + if (listeners.get(name)?.fn === fn) listeners.delete(name) + }, + } + const document = { ...events, body: { style: { overflow: 'clip' } }, fullscreenElement: null } + const isFullscreen = ref(false) + const props = reactive({ active: true }) + const root = { isConnected: true } + if (native) { + root.requestFullscreen = async () => { + document.fullscreenElement = root + listeners.get('fullscreenchange').fn() + } + } + document.exitFullscreen = async () => { + document.fullscreenElement = null + listeners.get('fullscreenchange')?.fn() + } + const state = scope.run(() => vm.runInNewContext(`${fullscreenSource}; ({ + enterPreviewFullscreen, exitPreviewFullscreen, toggleFullscreen, onFullscreenEscape, + })`, { + props, isFullscreen, previewRoot: ref(root), document, window: events, watch, + onMounted: fn => mounted.push(fn), onUnmounted: fn => unmounted.push(fn), + })) + mounted.forEach(fn => fn()) + const dispose = () => { unmounted.splice(0).forEach(fn => fn()); scope.stop() } + t.after(dispose) + return { ...state, props, root, document, isFullscreen, listeners, dispose } +} + +test('native fullscreen synchronizes browser Escape and button exit without changing body scroll', async t => { + const ctx = setup(t) + await ctx.enterPreviewFullscreen() + assert.equal(ctx.document.fullscreenElement, ctx.root) + assert.equal(ctx.isFullscreen.value, true) + assert.equal(ctx.document.body.style.overflow, 'clip') + // Browser Escape works even when the iframe consumes all keyboard events. + ctx.document.fullscreenElement = null + ctx.listeners.get('fullscreenchange').fn() + assert.equal(ctx.isFullscreen.value, false) + await ctx.enterPreviewFullscreen() + await ctx.exitPreviewFullscreen() + assert.equal(ctx.isFullscreen.value, false) + assert.equal(ctx.document.fullscreenElement, null) +}) + +test('fallback Escape exits only fullscreen and restores the existing scroll lock', async t => { + const ctx = setup(t, false) + await ctx.enterPreviewFullscreen() + assert.equal(ctx.isFullscreen.value, true) + assert.equal(ctx.document.body.style.overflow, 'hidden') + assert.equal(ctx.listeners.get('keydown').capture, true) + const calls = [] + ctx.onFullscreenEscape({ key: 'Escape', preventDefault: () => calls.push('prevent'), stopImmediatePropagation: () => calls.push('stop') }) + assert.deepEqual(calls, ['prevent', 'stop']) + assert.equal(ctx.isFullscreen.value, false) + assert.equal(ctx.document.body.style.overflow, 'clip') + ctx.onFullscreenEscape({ key: 'Escape', preventDefault: () => assert.fail('regular Escape belongs to the drawer') }) +}) + +test('ordinary keys and IME Escape are left alone', async t => { + const ctx = setup(t, false) + await ctx.enterPreviewFullscreen() + for (const event of [{ key: 'PageDown' }, { key: 'Escape', isComposing: true }]) { + ctx.onFullscreenEscape({ ...event, preventDefault: () => assert.fail('key must remain native') }) + } + assert.equal(ctx.isFullscreen.value, true) +}) + +test('a denied native request falls back and deactivation exits fullscreen', async t => { + const ctx = setup(t) + ctx.root.requestFullscreen = async () => { throw new Error('Permissions policy') } + await ctx.enterPreviewFullscreen() + assert.equal(ctx.isFullscreen.value, true) + ctx.props.active = false + await nextTick() + assert.equal(ctx.isFullscreen.value, false) + assert.equal(ctx.document.body.style.overflow, 'clip') +}) + +test('native fullscreen is exited when the preview is hidden', async t => { + const ctx = setup(t) + await ctx.enterPreviewFullscreen() + ctx.props.active = false + await nextTick() + assert.equal(ctx.document.fullscreenElement, null) + assert.equal(ctx.isFullscreen.value, false) +}) + +test('teardown restores fallback styles and unregisters both listeners', async t => { + const ctx = setup(t, false) + await ctx.enterPreviewFullscreen() + ctx.dispose() + assert.equal(ctx.document.body.style.overflow, 'clip') + assert.equal(ctx.listeners.size, 0) + await ctx.enterPreviewFullscreen() + assert.equal(ctx.isFullscreen.value, false) +}) + +test('a late native request cannot leave a disposed preview fullscreen', async t => { + const ctx = setup(t) + let complete + ctx.root.requestFullscreen = () => new Promise(resolve => { + complete = () => { ctx.document.fullscreenElement = ctx.root; resolve() } + }) + const pending = ctx.enterPreviewFullscreen() + ctx.dispose() + complete() + await pending + assert.equal(ctx.document.fullscreenElement, null) + assert.equal(ctx.isFullscreen.value, false) +}) + +test('diagram zoom exits native fullscreen before opening its body-mounted viewer', async () => { + const code = source.slice(source.indexOf('async function openMermaid()'), source.indexOf('async function loadPreview()')) + const calls = [] + let finishExit + const open = vm.runInNewContext(`${code}; openMermaid`, { + mermaidSvg: { value: '' }, props: { active: true }, previewRoot: { value: { isConnected: true } }, + exitPreviewFullscreen: () => new Promise(resolve => { finishExit = resolve }), + openMermaidFullscreen: svg => calls.push(svg), + }) + const pending = open() + assert.deepEqual(calls, []) + finishExit() + await pending + assert.deepEqual(calls, ['']) +}) + +test('toolbar reserves layout space in normal and fullscreen modes and keeps exit visible on errors', async () => { + const { descriptor } = parse(source) + assert.match(descriptor.template.content, /v-if="isFullscreen \|\|/) + assert.match(descriptor.template.content, /\{\{ isFullscreen \? \$t\('preview.exitFullscreen'\)/) + const result = await compileStyleAsync({ + source: descriptor.styles[0].content, filename: 'document-preview.vue', id: 'preview', preprocessLang: 'less', + }) + assert.deepEqual(result.errors, []) + const toolbars = [...result.code.matchAll(/[^{}]*\.preview-toolbar\s*\{([^}]+)\}/g)] + assert.ok(toolbars.length >= 2) + for (const [, css] of toolbars) assert.doesNotMatch(css, /position:\s*(absolute|fixed)|opacity:/) + assert.ok(toolbars.some(([, css]) => /flex-shrink:\s*0/.test(css))) +}) diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue index e6be0142..f5ccf0a4 100644 --- a/frontend/src/components/document-preview.vue +++ b/frontend/src/components/document-preview.vue @@ -1,6 +1,6 @@ // @ts-nocheck