mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
Street Level photo viewer: fresh elements per mount, and one viewer drawn
Fixes from review of the move to StreetLevelPhotoViewer: - Each mount and each panorama canvas now takes a key no viewer reuses, and the view keys its root by the mount. Before, switching KartaView and Panoramax in one tick let Vue patch the old viewer's canvas, whose destroyed renderer had lost its context, into the new one, so 360° photos fell back to their front view. - Of the viewers mounting in the host, the view draws the one mounted last, by mount key (photoViewers.js, which subscribePhotoViewer() now uses), whatever order they present in. A viewer still mounting when a later one is drawn is superseded: it unmounts and its mount fails as closed, rather than staying pending and being drawn once the later one goes. - A panorama's first image is uploaded once the view has drawn its canvas shown (the view hands the canvas back on every draw), so it is never sized into a hidden canvas. - The tests' fake view now draws as Vue does: once per tick with the final value, keeping elements while their keys are unchanged and handing every element back on each draw. StreetLevelPhotoViewer.test.mjs covers the switch in one tick and the overtaken mount with the real view. - The guard test's check that the photo viewer writes no DOM covers inserting, moving, removing and replacing elements, attributes, classes, styles and the hidden, disabled and text properties. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
766364f691
commit
2a0e21774b
17 files changed
+340
-107
No files matched your search
@@ -7,6 +7,7 @@ import assert from 'node:assert/strict';
|
||||
import { createApp, nextTick } from 'vue';
|
||||
import { createPinia } from 'pinia';
|
||||
import { createPhotoViewer } from '../../../layers/streetLevel/photoViewer.js';
|
||||
import { subscribePhotoViewers } from '../../../layers/streetLevel/photoViewers.js';
|
||||
import { useStreetLevelPhotoStore } from '../../stores/streetLevelPhoto.js';
|
||||
import StreetLevelPhotoViewer from './StreetLevelPhotoViewer.vue';
|
||||
|
||||
@@ -24,8 +25,11 @@ const SOURCES = {
|
||||
describe: () => ({ externalUrl: 'https://example.org' }),
|
||||
};
|
||||
|
||||
/** The view in an app, a viewer published to its store, and a panel host. */
|
||||
function mount(t, options = {}) {
|
||||
/**
|
||||
* The view in an app, a viewer per provider published to its store as the
|
||||
* street-level layer does, and a panel host.
|
||||
*/
|
||||
function mount(t, options = {}, providerIds = ['panoramax']) {
|
||||
document.body.replaceChildren();
|
||||
const wrap = document.body.appendChild(document.createElement('div'));
|
||||
wrap.className = 'sl-viewer-wrap';
|
||||
@@ -37,9 +41,10 @@ function mount(t, options = {}) {
|
||||
const store = useStreetLevelPhotoStore(pinia);
|
||||
const renderers = [];
|
||||
const stepped = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'panoramax',
|
||||
name: 'Panoramax',
|
||||
const create = (providerId) =>
|
||||
createPhotoViewer({
|
||||
providerId,
|
||||
name: providerId === 'panoramax' ? 'Panoramax' : 'KartaView',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
// f, then g: the whole sequence.
|
||||
@@ -63,13 +68,16 @@ function mount(t, options = {}) {
|
||||
},
|
||||
...options,
|
||||
});
|
||||
const unsubscribe = viewer.subscribeView((view) => store.publish(view));
|
||||
const viewers = providerIds.map(create);
|
||||
const unsubscribe = subscribePhotoViewers(viewers, (view) =>
|
||||
store.publish(view),
|
||||
);
|
||||
t.after(() => {
|
||||
viewer.unmount();
|
||||
for (const viewer of viewers) viewer.unmount();
|
||||
unsubscribe();
|
||||
app.unmount();
|
||||
});
|
||||
return { viewer, host, wrap, renderers, stepped };
|
||||
return { viewer: viewers[0], viewers, host, wrap, renderers, stepped };
|
||||
}
|
||||
|
||||
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
@@ -246,3 +254,51 @@ test('without a view, the viewer cannot mount', async () => {
|
||||
/no view to draw it/,
|
||||
);
|
||||
});
|
||||
|
||||
test('a viewer mounted in the tick another unmounts gets elements of its own', async (t) => {
|
||||
const { viewers, host, renderers } = mount(t, {}, ['kartaview', 'panoramax']);
|
||||
const [kartaView, panoramax] = viewers;
|
||||
await kartaView.mount(host);
|
||||
await kartaView.open('p');
|
||||
const root = host.querySelector('.sl-photo-viewer');
|
||||
const canvas = root.children[1];
|
||||
assert.ok(renderers[0].canvas === canvas);
|
||||
// The core switches source: one unmounts and the other mounts at once.
|
||||
kartaView.unmount();
|
||||
const mounting = panoramax.mount(host);
|
||||
await mounting;
|
||||
await panoramax.open('p');
|
||||
const fresh = host.querySelector('.sl-photo-viewer');
|
||||
assert.equal(host.children.length, 1);
|
||||
assert.ok(fresh !== root, 'a root of its own');
|
||||
assert.equal(fresh.dataset.provider, 'panoramax');
|
||||
assert.ok(fresh.children[1] !== canvas, 'not the destroyed renderer’s canvas');
|
||||
assert.ok(renderers[1].canvas === fresh.children[1]);
|
||||
assert.equal(fresh.dataset.kind, 'pano', 'not the front view');
|
||||
});
|
||||
|
||||
test('a mount overtaken before it is drawn closes, and never shows later', async (t) => {
|
||||
const { viewers, host } = mount(t, {}, ['kartaview', 'panoramax']);
|
||||
const [kartaView, panoramax] = viewers;
|
||||
// KartaView starts mounting; Panoramax mounts before the view draws it.
|
||||
const overtaken = kartaView.mount(host);
|
||||
const current = panoramax.mount(host);
|
||||
// It settles, rather than wait for a draw that never comes.
|
||||
const outcome = await Promise.race([
|
||||
overtaken.then(
|
||||
() => 'mounted',
|
||||
(error) => error.message,
|
||||
),
|
||||
new Promise((resolve) => setTimeout(() => resolve('still pending'), 100)),
|
||||
]);
|
||||
assert.match(outcome, /viewer was closed/);
|
||||
await current;
|
||||
await nextTick();
|
||||
assert.equal(host.children.length, 1);
|
||||
assert.equal(host.children[0].dataset.provider, 'panoramax');
|
||||
await assert.rejects(kartaView.open('f'), /not mounted/);
|
||||
// Once Panoramax goes, nothing is left in the host.
|
||||
panoramax.unmount();
|
||||
await nextTick();
|
||||
assert.equal(host.children.length, 0);
|
||||
});
|
||||
@@ -9,7 +9,8 @@ import { useStreetLevelPhotoStore } from '../../stores/streetLevelPhoto.js';
|
||||
// which photo shows and how, its loading, the panorama it draws, the keys,
|
||||
// drag and zoom. This view draws what it presents, forwards the events and
|
||||
// hands each element back: the canvas it draws on, the frame and picture
|
||||
// it measures, the root and buttons it keeps focus with.
|
||||
// it measures, the root and buttons it keeps focus with. Each mount and each
|
||||
// canvas has its own key, so a viewer never gets another one's elements.
|
||||
const { photo } = storeToRefs(useStreetLevelPhotoStore());
|
||||
|
||||
/** Hand `name`'s element back to the viewer that presented it. */
|
||||
@@ -30,6 +31,7 @@ const refs = {
|
||||
<template>
|
||||
<Teleport v-if="photo" :to="photo.host"
|
||||
><div
|
||||
:key="photo.mountKey"
|
||||
:ref="refs.root"
|
||||
class="sl-photo-viewer"
|
||||
:data-provider="photo.providerId"
|
||||
|
||||
Reference in new issue
Block a user