mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 02:02:21 +00:00
Merge pull request #1868 from bilawalsidhu/samehkhamis/kit-sweep-photo-globe
Vue kit: Street Level's photo viewer is a Vue view
This commit is contained in:
23 files changed
+1152
-117
No files matched your search
@@ -0,0 +1,3 @@
|
||||
- Street Level's photo viewer is a Vue view with kit step buttons, and a
|
||||
provider switch always gets a fresh panorama canvas (manjunath22466,
|
||||
samehkhamis, #1868).
|
||||
+10
-1
@@ -6336,7 +6336,16 @@ caption has no place name (KartaView's is where the drive began) and links to
|
||||
the full-size photo through the image proxy, since kartaview.org's photo
|
||||
pages fail for signed-out visitors. The viewer itself is `photoViewer.js`,
|
||||
shared with Panoramax: each provider hands it a `PhotoSources` table (first
|
||||
and sharper flat and panorama URLs, and the caption fields).
|
||||
and sharper flat and panorama URLs, and the caption fields). It builds no
|
||||
element: it presents the viewer through the layer's
|
||||
`subscribePhotoViewer()`, which the application shell publishes to the
|
||||
`gev-street-level-photo` store, and `StreetLevelPhotoViewer` draws it in the
|
||||
viewer host and hands back the canvas the panorama is drawn on (a fresh one
|
||||
after a lost context). Each mount and each canvas has a key of its own, so
|
||||
a viewer never draws on another one's elements, and of two viewers
|
||||
mounting at once only the later one is drawn (`photoViewers.js`): the
|
||||
earlier one's mount closes. Without a subscribed view these photos cannot
|
||||
open.
|
||||
|
||||
Panoramax (`providers/panoramax/`) is keyless and browser-side: the
|
||||
meta-catalogue `api.panoramax.xyz` (CORS open) federates every instance, and
|
||||
|
||||
+24
-1
@@ -260,6 +260,16 @@ targets in its template, as many as `view.subRings` says, rather than
|
||||
building them in script; the Omnibar's one other `material-symbols-outlined`
|
||||
is a stylesheet selector, and the layer disclosure only names a button in a
|
||||
comment (`src/ui/kit/smallSurfacesControls.test.mjs` pins it).
|
||||
The Street Level photo viewer of KartaView and Panoramax (the photo, the
|
||||
panorama canvas and the ‹ › buttons) is `StreetLevelPhotoViewer`, drawn
|
||||
from what the photo viewer presents rather than built in script. Its ‹ and
|
||||
› are ghost `UiButton`s, not pressed toggles, whose glyphs are text
|
||||
characters; the viewer builds no element, and the street-level layer it
|
||||
belongs to needs no Vue (`src/ui/kit/streetLevelPhotoControls.test.mjs`
|
||||
pins it). The notice the God's Eye View panel page shows when it loses its
|
||||
3D graphics (`#status`, in `src/app/globePanelRuntime.js`) stays built in
|
||||
script: that script is sent to the host as source text and runs in the
|
||||
host's panel page before the app loads, so it can import nothing.
|
||||
The Bhote Koshi reconstruction panel's buttons (previous and next beat,
|
||||
Play, Play scene, Cinematic, Full story, Open original, Lower gorge, the
|
||||
field reports and Open public map) are ghost `UiButton`s that keep their
|
||||
@@ -460,7 +470,20 @@ slot whenever its view is drawn.
|
||||
(done: `RecentImageryReadout` and its day cards, the same way),
|
||||
Street level (done: `StreetLevelPanel` renders the `gev-street-level`
|
||||
store that `StreetLevelControls` publishes to; the controls keep their
|
||||
listeners, EXPAND, focus and resizing), CCTV (done: `CctvPanel` renders
|
||||
listeners, EXPAND, focus and resizing; `StreetLevelPhotoViewer`, beside
|
||||
it in the layer panels, teleports into the viewer host the photo viewer
|
||||
KartaView and Panoramax mount there, which `createPhotoViewer()`
|
||||
presents through the layer's `subscribePhotoViewer()` and the
|
||||
application shell publishes to the `gev-street-level-photo` store, so
|
||||
the portable `./layers/street-level` entries need no Vue; the viewer
|
||||
keeps every decision, the photo loading, keys, drag and zoom, and draws
|
||||
its panorama on the canvas the view hands back, a fresh one after a lost
|
||||
WebGL context; each mount and canvas is keyed, so no viewer reuses
|
||||
another's elements, and of two viewers mounting at once only the later
|
||||
is drawn while the earlier one's mount closes; with no view
|
||||
subscribed, as when the layer runs without the application shell, a
|
||||
KartaView or Panoramax photo cannot mount),
|
||||
CCTV (done: `CctvPanel` renders
|
||||
the whole panel from the `gev-cctv` store, including the camera picker,
|
||||
the inline pose editor, the frame preview, source badge and typed
|
||||
summary; `CctvControls` keeps its listeners, the frame requests and the
|
||||
|
||||
@@ -1155,6 +1155,8 @@
|
||||
"src/layers/streetLevel/providers/panoramax/decode.js",
|
||||
"src/layers/streetLevel/providers/panoramax/viewer.js",
|
||||
"src/layers/streetLevel/photoViewer.js",
|
||||
"src/layers/streetLevel/photoViewers.js",
|
||||
"src/layers/streetLevel/photoViewers.test.mjs",
|
||||
"src/layers/streetLevel/limiter.js",
|
||||
"src/layers/streetLevel/imageSize.js",
|
||||
"src/layers/streetLevel/tileCache.js",
|
||||
@@ -1362,6 +1364,8 @@
|
||||
"src/ui/kit/cockpitControls.test.mjs",
|
||||
"src/ui/kit/smallSurfacesControls.test.mjs",
|
||||
"src/ui/kit/commandDockControls.test.mjs",
|
||||
"src/ui/kit/streetLevelPhotoControls.test.mjs",
|
||||
"src/ui/stores/streetLevelPhoto.js",
|
||||
"src/ui/kit/bhoteKoshiControls.test.mjs",
|
||||
"src/ui/bhoteKoshi/BhoteKoshiMediaCards.vue",
|
||||
"src/ui/kit/missionsRadioControls.test.mjs",
|
||||
|
||||
@@ -211,7 +211,9 @@
|
||||
"src/ui/stores/windInspection.js",
|
||||
"src/ui/views/panels/WindInspectionMarker.vue",
|
||||
"src/ui/stores/missionReplay.js",
|
||||
"src/ui/stores/streetLevelPhoto.js",
|
||||
"src/ui/views/panels/MissionReplayOverlay.vue",
|
||||
"src/ui/views/panels/StreetLevelPhotoViewer.vue",
|
||||
"src/ui/stores/bhoteKoshiPanel.js",
|
||||
"src/ui/views/panels/BhoteKoshiPanel.vue",
|
||||
"src/ui/stores/celestialRing.js",
|
||||
@@ -1604,6 +1606,7 @@
|
||||
"src/ui/stores/status.js",
|
||||
"src/ui/toast.js",
|
||||
"src/ui/stores/missionReplay.js",
|
||||
"src/ui/stores/streetLevelPhoto.js",
|
||||
"src/ui/stores/celestialRing.js",
|
||||
"src/ui/stores/worldOverlayActions.js",
|
||||
"src/ui/stores/awarenessDirection.js",
|
||||
@@ -3559,6 +3562,7 @@
|
||||
"src/layers/streetLevel/providers/panoramax/decode.js",
|
||||
"src/layers/streetLevel/providers/panoramax/viewer.js",
|
||||
"src/layers/streetLevel/photoViewer.js",
|
||||
"src/layers/streetLevel/photoViewers.js",
|
||||
"src/layers/streetLevel/limiter.js",
|
||||
"src/layers/streetLevel/imageSize.js",
|
||||
"src/layers/streetLevel/tileCache.js",
|
||||
@@ -5156,6 +5160,7 @@
|
||||
"src/layers/streetLevel/providers/panoramax/decode.js",
|
||||
"src/layers/streetLevel/providers/panoramax/viewer.js",
|
||||
"src/layers/streetLevel/photoViewer.js",
|
||||
"src/layers/streetLevel/photoViewers.js",
|
||||
"src/layers/streetLevel/limiter.js",
|
||||
"src/layers/streetLevel/imageSize.js",
|
||||
"src/layers/streetLevel/tileCache.js",
|
||||
@@ -5374,6 +5379,7 @@
|
||||
"src/ui/stores/windInspection.js",
|
||||
"src/layers/wind/inspectionMarker.js",
|
||||
"src/ui/stores/missionReplay.js",
|
||||
"src/ui/stores/streetLevelPhoto.js",
|
||||
"src/ui/stores/bhoteKoshiPanel.js",
|
||||
"src/ui/stores/celestialRing.js",
|
||||
"src/ui/stores/screenAnnotations.js",
|
||||
@@ -6222,6 +6228,7 @@
|
||||
"src/layers/streetLevel/providers/panoramax/decode.js",
|
||||
"src/layers/streetLevel/providers/panoramax/viewer.js",
|
||||
"src/layers/streetLevel/photoViewer.js",
|
||||
"src/layers/streetLevel/photoViewers.js",
|
||||
"src/layers/streetLevel/limiter.js",
|
||||
"src/layers/streetLevel/imageSize.js",
|
||||
"src/layers/streetLevel/tileCache.js",
|
||||
|
||||
@@ -5,6 +5,7 @@ import { createCredits } from './credits.js';
|
||||
import { createPickRouter } from './pickRouter.js';
|
||||
import { createSelection } from './selection.js';
|
||||
import { createViewerHost } from './viewerHost.js';
|
||||
import { subscribePhotoViewers } from './photoViewers.js';
|
||||
import { filtersOf, requiresKeyIdFor, validateProviders } from './registry.js';
|
||||
import { normalizeFilter, resolveFilter, sameFilter } from './filter.js';
|
||||
import { worldTimeFor } from '../../time/worldClock.js';
|
||||
@@ -703,6 +704,22 @@ export function createStreetLevelLayer({
|
||||
attachViewerHost(element) {
|
||||
parts.viewerHost.attach(element);
|
||||
},
|
||||
/**
|
||||
* Receive every presentation of the photo viewer the providers whose
|
||||
* photos are plain files (KartaView, Panoramax) mount in the viewer
|
||||
* host, the one mounted last (see photoViewers.js), starting with the
|
||||
* current one; null while none is mounted. A view draws it (see
|
||||
* photoViewer.js); with nobody subscribed, those providers' photos
|
||||
* cannot open.
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
subscribePhotoViewer(listener) {
|
||||
return subscribePhotoViewers(
|
||||
[...state.providers.values()].map((entry) => entry.instance.viewer),
|
||||
listener,
|
||||
);
|
||||
},
|
||||
setProviderEnabled,
|
||||
openImage,
|
||||
/**
|
||||
|
||||
@@ -2176,3 +2176,41 @@ test('proceed() is asked after the lookup: false loads nothing and clears the sp
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('subscribePhotoViewer hands on the photo viewer a provider presents, the later mount’s while two overlap', async (t) => {
|
||||
const presenting = (id, pickPrefix) => {
|
||||
const provider = fakeProvider({ id, pickPrefix });
|
||||
const create = provider.create;
|
||||
const listeners = new Set();
|
||||
provider.present = (view) => {
|
||||
for (const listener of [...listeners]) listener(view);
|
||||
};
|
||||
provider.create = (context) => {
|
||||
const instance = create(context);
|
||||
instance.viewer.subscribeView = (listener) => {
|
||||
listeners.add(listener);
|
||||
listener(null);
|
||||
return () => listeners.delete(listener);
|
||||
};
|
||||
return instance;
|
||||
};
|
||||
return provider;
|
||||
};
|
||||
const a = presenting('mapillary', 'mly:');
|
||||
const b = presenting('other', 'oth:');
|
||||
const { layer } = await enabledLayer(t, [a, b]);
|
||||
const shown = [];
|
||||
const unsubscribe = layer.subscribePhotoViewer((view) => shown.push(view));
|
||||
assert.deepEqual(shown, [null], 'the current one first');
|
||||
const supersede = () => {};
|
||||
const viewA = { host: 'a', mountKey: 1, actions: { supersede } };
|
||||
const viewB = { host: 'b', mountKey: 2, actions: { supersede } };
|
||||
a.present(viewA);
|
||||
b.present(viewB);
|
||||
a.present(null);
|
||||
b.present(null);
|
||||
assert.deepEqual(shown, [null, viewA, viewB, null]);
|
||||
unsubscribe();
|
||||
a.present(viewA);
|
||||
assert.equal(shown.length, 4, 'unsubscribed');
|
||||
});
|
||||
@@ -36,6 +36,14 @@ function tooManyPixels(width, height) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys for each mount's and each canvas's elements, never reused by any
|
||||
* viewer: a view that sees one viewer unmount and another mount in the same
|
||||
* tick still draws them fresh elements (a destroyed renderer leaves its
|
||||
* canvas's context lost).
|
||||
*/
|
||||
let viewKeys = 0;
|
||||
|
||||
/** Panorama texture widths: enough for a docked viewer, and for an expanded one. */
|
||||
const PANO_MID_SIZE = Object.freeze([2048, 1024]);
|
||||
export const PANO_FULL_SIZE = Object.freeze([4096, 2048]);
|
||||
@@ -213,6 +221,21 @@ export function panoSizeFor(canvasPixels, fovDeg) {
|
||||
* or pinch to zoom), and ‹ › to step along the sequence (also ← →). Poses
|
||||
* follow the view direction.
|
||||
*
|
||||
* The adapter draws nothing itself. It presents the viewer through
|
||||
* `subscribeView()` (the street-level layer's `subscribePhotoViewer()`): a
|
||||
* fresh `{host, mountKey, providerId, label, alt, kind, src, fit, canvasKey,
|
||||
* dragging, prevLabel, prevDisabled, nextLabel, nextDisabled, actions}` on
|
||||
* each change,
|
||||
* or null while unmounted. A view draws it into `host` (the application's is
|
||||
* `StreetLevelPhotoViewer`), forwards its events to `actions` and hands each
|
||||
* element back through `actions.attach(name, element)`: the panorama canvas
|
||||
* to draw on, the frame and picture to measure, the root and buttons to
|
||||
* keep focus. Each mount has its own `mountKey`, and a new `canvasKey` asks
|
||||
* for a fresh canvas (a lost WebGL context): neither is ever reused.
|
||||
* `actions.supersede()` tells a viewer another one is drawn in its place:
|
||||
* it unmounts, and a mount still waiting to be drawn fails as closed.
|
||||
* Mounting with no view subscribed fails.
|
||||
*
|
||||
* Photos are normalized by the provider: `{id, sequenceId, lon, lat,
|
||||
* heading, isPano, capturedAt}` plus whatever its `sources` read.
|
||||
*
|
||||
@@ -249,11 +272,18 @@ export function createPhotoViewer({
|
||||
}) {
|
||||
const LOAD_FAILED = `The ${name} photo could not be loaded`;
|
||||
const listeners = new Set();
|
||||
/** The elements the view handed back. */
|
||||
let root = null;
|
||||
let picture = null;
|
||||
let canvas = null;
|
||||
let prevButton = null;
|
||||
let nextButton = null;
|
||||
/** What the view draws; null while unmounted. */
|
||||
let presented = null;
|
||||
const viewListeners = new Set();
|
||||
/** Waiting for the view's root (a mount), or its canvas: a fresh one, or the next draw. */
|
||||
const rootWaiters = new Set();
|
||||
const canvasWaiters = new Set();
|
||||
let renderer = null;
|
||||
/** WebGL failed once: 360° photos show their front view instead. */
|
||||
let rendererFailed = false;
|
||||
@@ -265,8 +295,6 @@ export function createPhotoViewer({
|
||||
let drag = null;
|
||||
let poseFrame = 0;
|
||||
let removeListeners = [];
|
||||
/** The document the viewer was built in (new canvases come from it). */
|
||||
let documentOf = null;
|
||||
/** A context loss's look direction, for the reopen of that photo. */
|
||||
let restoreView = null;
|
||||
/** The panel's viewer box, where Esc is listened for (null: the root). */
|
||||
@@ -354,44 +382,71 @@ export function createPhotoViewer({
|
||||
});
|
||||
}
|
||||
|
||||
/** Present the viewer with `patch` applied (null: nothing to draw). */
|
||||
function present(patch) {
|
||||
if (patch === null) presented = null;
|
||||
else if (presented) presented = { ...presented, ...patch };
|
||||
else return;
|
||||
for (const listener of [...viewListeners]) listener(presented);
|
||||
}
|
||||
|
||||
/** Settle everything waiting on the view (it drew, or it went). */
|
||||
function wake(waiters) {
|
||||
for (const resolve of waiters) resolve();
|
||||
waiters.clear();
|
||||
}
|
||||
|
||||
/** The panorama canvas once the view has drawn it, or the open stopped. */
|
||||
function canvasReady(signal) {
|
||||
if (canvas || !presented || signal?.aborted) return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
canvasWaiters.add(resolve);
|
||||
signal?.addEventListener?.('abort', resolve, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
/** The view's next draw (it hands the canvas back each time), or none coming. */
|
||||
function nextDraw(signal) {
|
||||
if (!presented || signal?.aborted) return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
canvasWaiters.add(resolve);
|
||||
signal?.addEventListener?.('abort', resolve, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function syncButtons() {
|
||||
const enabled = Boolean(photo?.sequenceId) && showing !== null;
|
||||
if (prevButton) prevButton.disabled = !enabled;
|
||||
if (nextButton) nextButton.disabled = !enabled;
|
||||
present({ prevDisabled: !enabled, nextDisabled: !enabled });
|
||||
if (enabled) checkEnds(photo);
|
||||
}
|
||||
|
||||
/** Disable ‹ or › at once where the sequence ends (it also warms the neighbours). */
|
||||
function checkEnds(target) {
|
||||
for (const [delta, button] of [
|
||||
[-1, prevButton],
|
||||
[1, nextButton],
|
||||
])
|
||||
for (const delta of [-1, 1])
|
||||
step(target, delta).then(
|
||||
(id) => {
|
||||
if (photo === target && button && !id) disable(button);
|
||||
if (photo === target && presented && !id) disable(delta);
|
||||
},
|
||||
() => {},
|
||||
);
|
||||
}
|
||||
|
||||
/** Disable a step button, keeping keyboard focus in the viewer. */
|
||||
function disable(button) {
|
||||
if (button.ownerDocument?.activeElement === button) root?.focus?.();
|
||||
button.disabled = true;
|
||||
function disable(delta) {
|
||||
const button = delta < 0 ? prevButton : nextButton;
|
||||
if (button && button.ownerDocument?.activeElement === button)
|
||||
root?.focus?.();
|
||||
present(delta < 0 ? { prevDisabled: true } : { nextDisabled: true });
|
||||
}
|
||||
|
||||
function show(kind) {
|
||||
showing = kind;
|
||||
if (picture) picture.hidden = kind !== 'flat';
|
||||
if (canvas) canvas.hidden = kind !== 'pano';
|
||||
if (root) root.dataset.kind = kind || '';
|
||||
present({ kind: kind || '' });
|
||||
syncButtons();
|
||||
}
|
||||
|
||||
function applyRenderMode() {
|
||||
if (picture)
|
||||
picture.style.objectFit = renderMode === 'fill' ? 'cover' : 'contain';
|
||||
present({ fit: renderMode === 'fill' ? 'cover' : 'contain' });
|
||||
}
|
||||
|
||||
function ensureRenderer() {
|
||||
@@ -410,6 +465,9 @@ export function createPhotoViewer({
|
||||
* with it: drop the renderer, and show the photo again on a new one.
|
||||
*/
|
||||
function onContextLost(event) {
|
||||
// Only the canvas on screen: a replaced one has nothing left to lose.
|
||||
const from = event?.currentTarget ?? event?.target;
|
||||
if (from && from !== canvas) return;
|
||||
event.preventDefault?.();
|
||||
const lost = renderer;
|
||||
renderer = null;
|
||||
@@ -421,11 +479,10 @@ export function createPhotoViewer({
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
// A lost canvas hands back its lost context: draw on a new element.
|
||||
const fresh = makeCanvas(documentOf);
|
||||
fresh.hidden = canvas.hidden;
|
||||
canvas.replaceWith?.(fresh);
|
||||
canvas = fresh;
|
||||
// A lost canvas hands back its lost context: the view draws a new
|
||||
// element in its place and hands it back.
|
||||
canvas = null;
|
||||
present({ canvasKey: ++viewKeys });
|
||||
// Show the photo again through the core, unless a newer one is opening.
|
||||
if (showing === 'pano' && photo && openingId === null) {
|
||||
const again = photo.id;
|
||||
@@ -435,20 +492,6 @@ export function createPhotoViewer({
|
||||
}
|
||||
}
|
||||
|
||||
/** The panorama canvas, wired for drag, pinch, wheel and context loss. */
|
||||
function makeCanvas(doc) {
|
||||
const element = doc.createElement('canvas');
|
||||
element.className = 'sl-photo-pano';
|
||||
element.hidden = true;
|
||||
listen(element, 'pointerdown', onPointerDown);
|
||||
listen(element, 'pointermove', onPointerMove);
|
||||
listen(element, 'pointerup', onPointerUp);
|
||||
listen(element, 'pointercancel', onPointerUp);
|
||||
listen(element, 'wheel', onWheel, { passive: false });
|
||||
listen(element, 'webglcontextlost', onContextLost);
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Step along the sequence from the photo showing, or from the one on its
|
||||
* way after an earlier press, and open it through the core.
|
||||
@@ -469,8 +512,7 @@ export function createPhotoViewer({
|
||||
// Closed, or another photo was opened meanwhile.
|
||||
if ((openingId ?? photo?.id) !== fromId) return;
|
||||
if (!nextId) {
|
||||
const button = delta < 0 ? prevButton : nextButton;
|
||||
if (button && photo?.id === fromId) disable(button);
|
||||
if (presented && photo?.id === fromId) disable(delta);
|
||||
return;
|
||||
}
|
||||
openingId = nextId;
|
||||
@@ -504,7 +546,7 @@ export function createPhotoViewer({
|
||||
y: event.clientY,
|
||||
view: renderer.getView(),
|
||||
};
|
||||
canvas.classList.add('sl-photo-dragging');
|
||||
present({ dragging: true });
|
||||
}
|
||||
|
||||
function onPointerMove(event) {
|
||||
@@ -532,7 +574,7 @@ export function createPhotoViewer({
|
||||
if (pointers.size < 2) pinch = null;
|
||||
if (!drag || event.pointerId !== drag.id) return;
|
||||
drag = null;
|
||||
canvas.classList.remove('sl-photo-dragging');
|
||||
present({ dragging: false });
|
||||
}
|
||||
|
||||
function onWheel(event) {
|
||||
@@ -603,51 +645,67 @@ export function createPhotoViewer({
|
||||
);
|
||||
}
|
||||
|
||||
/** The elements the view hands back, by name. */
|
||||
const ELEMENTS = {
|
||||
root: (element) => (root = element),
|
||||
picture: (element) => (picture = element),
|
||||
canvas: (element) => (canvas = element),
|
||||
prev: (element) => (prevButton = element),
|
||||
next: (element) => (nextButton = element),
|
||||
};
|
||||
|
||||
/** Present the viewer in `host`; its view draws it and hands back its elements. */
|
||||
function build(host) {
|
||||
const doc = host.ownerDocument;
|
||||
documentOf = doc;
|
||||
root = doc.createElement('div');
|
||||
root.className = 'sl-photo-viewer';
|
||||
root.dataset.provider = providerId;
|
||||
root.tabIndex = 0;
|
||||
root.setAttribute('role', 'group');
|
||||
root.setAttribute(
|
||||
'aria-label',
|
||||
t(
|
||||
// Events from a view drawn for an earlier mount are not this one's.
|
||||
const current =
|
||||
(handler) =>
|
||||
(...args) => {
|
||||
if (presented?.actions === actions) handler(...args);
|
||||
};
|
||||
const actions = {
|
||||
attach(name, element) {
|
||||
if (presented?.actions !== actions || !element) return;
|
||||
ELEMENTS[name]?.(element);
|
||||
if (name === 'root') wake(rootWaiters);
|
||||
if (name === 'canvas') wake(canvasWaiters);
|
||||
},
|
||||
keydown: current(onKeyDown),
|
||||
pointerdown: current(onPointerDown),
|
||||
pointermove: current(onPointerMove),
|
||||
pointerup: current(onPointerUp),
|
||||
wheel: current(onWheel),
|
||||
contextLost: current(onContextLost),
|
||||
supersede: current(() => adapter.unmount()),
|
||||
step: current((delta, event) => {
|
||||
event?.stopPropagation?.();
|
||||
stepBy(delta);
|
||||
}),
|
||||
};
|
||||
presented = {
|
||||
host,
|
||||
mountKey: ++viewKeys,
|
||||
providerId,
|
||||
label: t(
|
||||
'{name} photo. ← and → step along the sequence. On a 360° photo, drag or use Shift+← →, ↑ ↓ to look around, and + − to zoom',
|
||||
{ name },
|
||||
),
|
||||
);
|
||||
picture = doc.createElement('img');
|
||||
picture.className = 'sl-photo-flat';
|
||||
picture.alt = t('{name} street-level photo', { name });
|
||||
picture.draggable = false;
|
||||
picture.hidden = true;
|
||||
canvas = makeCanvas(doc);
|
||||
const nav = (label, glyph, delta) => {
|
||||
const button = doc.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = `sl-photo-step sl-photo-step-${delta < 0 ? 'prev' : 'next'}`;
|
||||
button.textContent = glyph;
|
||||
button.title = label;
|
||||
button.setAttribute('aria-label', label);
|
||||
button.disabled = true;
|
||||
listen(button, 'click', (event) => {
|
||||
event.stopPropagation();
|
||||
stepBy(delta);
|
||||
});
|
||||
return button;
|
||||
alt: t('{name} street-level photo', { name }),
|
||||
kind: undefined,
|
||||
src: null,
|
||||
fit: renderMode === 'fill' ? 'cover' : 'contain',
|
||||
canvasKey: ++viewKeys,
|
||||
dragging: false,
|
||||
prevLabel: t('Previous photo in this sequence (←)'),
|
||||
prevDisabled: true,
|
||||
nextLabel: t('Next photo in this sequence (→)'),
|
||||
nextDisabled: true,
|
||||
actions,
|
||||
};
|
||||
prevButton = nav(t('Previous photo in this sequence (←)'), '‹', -1);
|
||||
nextButton = nav(t('Next photo in this sequence (→)'), '›', 1);
|
||||
root.append(picture, canvas, prevButton, nextButton);
|
||||
host.append(root);
|
||||
listen(root, 'keydown', onKeyDown);
|
||||
present({});
|
||||
// The viewer's box in the panel; without one, the root's own handler
|
||||
// takes Esc (one listener per element and type).
|
||||
escapeTarget = host.closest?.('.sl-viewer-wrap') ?? null;
|
||||
if (escapeTarget) listen(escapeTarget, 'keydown', onEscapeKey);
|
||||
applyRenderMode();
|
||||
}
|
||||
|
||||
function teardown() {
|
||||
@@ -655,13 +713,17 @@ export function createPhotoViewer({
|
||||
pinch = null;
|
||||
for (const remove of removeListeners) remove();
|
||||
removeListeners = [];
|
||||
escapeTarget = null;
|
||||
if (poseFrame) cancelAnimationFrame(poseFrame);
|
||||
poseFrame = 0;
|
||||
drag = null;
|
||||
renderer?.destroy();
|
||||
renderer = null;
|
||||
root?.remove();
|
||||
// The view takes its elements away.
|
||||
present(null);
|
||||
root = picture = canvas = prevButton = nextButton = null;
|
||||
wake(rootWaiters);
|
||||
wake(canvasWaiters);
|
||||
showing = null;
|
||||
photo = null;
|
||||
}
|
||||
@@ -738,7 +800,7 @@ export function createPhotoViewer({
|
||||
try {
|
||||
const image = await loadAs(url, { crossOrigin: false, signal });
|
||||
if (seq !== openSeq || photo !== target || showing !== 'flat') return;
|
||||
picture.src = image.src;
|
||||
present({ src: image.src });
|
||||
sharp.shown = url;
|
||||
requestRender();
|
||||
} catch {
|
||||
@@ -770,7 +832,8 @@ export function createPhotoViewer({
|
||||
/** The sharp panorama this viewer needs now: 2,048 docked, 4,096 expanded. */
|
||||
function panoSizeNow() {
|
||||
const ratio = Math.min(globalThis.devicePixelRatio || 1, 2);
|
||||
const width = (canvas?.clientWidth || 0) * ratio;
|
||||
// The canvas fills the frame; until the view shows it, it measures 0.
|
||||
const width = (canvas?.clientWidth || root?.clientWidth || 0) * ratio;
|
||||
return panoSizeFor(width, renderer?.getView().fov ?? PANO_FOV_DEFAULT_DEG);
|
||||
}
|
||||
|
||||
@@ -801,8 +864,15 @@ export function createPhotoViewer({
|
||||
else if (!keepView || showing !== 'pano')
|
||||
renderer.setView({ yaw: 0, pitch: 0, fov: PANO_FOV_DEFAULT_DEG });
|
||||
photo = target;
|
||||
// Shown first: a hidden canvas measures 0 and would be sized 1×1.
|
||||
// Shown first: a hidden canvas measures 0 and would be sized 1×1. The
|
||||
// view draws it shown on a microtask, and hands the canvas back then.
|
||||
show('pano');
|
||||
await nextDraw(signal);
|
||||
if (seq !== openSeq || !renderer) {
|
||||
image.close?.();
|
||||
if (seq !== openSeq) return false;
|
||||
throw new Error(LOAD_FAILED);
|
||||
}
|
||||
renderer.setImage(image);
|
||||
panoWidth = first.width || 0;
|
||||
// A sharper version of the photo before was dropped by `open`; one of
|
||||
@@ -827,7 +897,7 @@ export function createPhotoViewer({
|
||||
}
|
||||
if (!image) throw failure || new Error(LOAD_FAILED);
|
||||
if (seq !== openSeq) return false;
|
||||
picture.src = image.src;
|
||||
present({ src: image.src });
|
||||
flatSharp = { shown: null, loading: null };
|
||||
photo = target;
|
||||
show('flat');
|
||||
@@ -837,6 +907,11 @@ export function createPhotoViewer({
|
||||
async function openPhoto(id, seq, keepView, signal) {
|
||||
const target = await getPhoto(id, { signal });
|
||||
if (seq !== openSeq) return;
|
||||
// A canvas lost moments ago: the view is drawing its replacement.
|
||||
if (sources.hasPano(target) && !rendererFailed) {
|
||||
await canvasReady(signal);
|
||||
if (seq !== openSeq) return;
|
||||
}
|
||||
let shown = false;
|
||||
if (sources.hasPano(target) && ensureRenderer()) {
|
||||
try {
|
||||
@@ -857,10 +932,17 @@ export function createPhotoViewer({
|
||||
const adapter = {
|
||||
async mount(host) {
|
||||
if (!host) throw new Error('Street Level viewer has no host element');
|
||||
if (root && root.parentNode === host) return;
|
||||
if (root && presented?.host === host) return;
|
||||
teardown();
|
||||
rendererFailed = false;
|
||||
if (!viewListeners.size)
|
||||
throw new Error('Street Level photo viewer has no view to draw it');
|
||||
// Resolves once the view drew the viewer, or it was unmounted meanwhile.
|
||||
const drawn = new Promise((resolve) => rootWaiters.add(resolve));
|
||||
build(host);
|
||||
if (!root) await drawn;
|
||||
// Unmounted or superseded before the view drew it.
|
||||
if (!root) throw new Error('Street-level viewer was closed');
|
||||
},
|
||||
|
||||
/** Resolves once the photo is on screen and its pose was emitted. */
|
||||
@@ -896,7 +978,7 @@ export function createPhotoViewer({
|
||||
photo = null;
|
||||
flatSharp = { shown: null, loading: null };
|
||||
show(null);
|
||||
if (picture) picture.removeAttribute('src');
|
||||
present({ src: null });
|
||||
renderer?.clear?.();
|
||||
},
|
||||
|
||||
@@ -937,6 +1019,18 @@ export function createPhotoViewer({
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
|
||||
/**
|
||||
* Receive every presentation of the viewer, starting with the current
|
||||
* one (null while unmounted).
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
subscribeView(listener) {
|
||||
viewListeners.add(listener);
|
||||
listener(presented);
|
||||
return () => viewListeners.delete(listener);
|
||||
},
|
||||
};
|
||||
return adapter;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ function setup() {
|
||||
pending.get(url).reject(new Error('gone'));
|
||||
pending.delete(url);
|
||||
};
|
||||
return { viewer, loads, images, release, fail, host: fakeHost() };
|
||||
return { viewer, loads, images, release, fail, host: fakeHost(viewer) };
|
||||
}
|
||||
|
||||
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
@@ -146,7 +146,7 @@ test('Esc: the docked viewer offers it to the provider first; expanded, it is th
|
||||
onEscape: () => ++asked > 0,
|
||||
load: async (url) => ({ src: url }),
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
const root = host.children[0];
|
||||
const press = () => {
|
||||
@@ -195,7 +195,7 @@ test('a sharp panorama still downloading when another photo opens is aborted', a
|
||||
});
|
||||
t.after(() => delete globalThis.devicePixelRatio);
|
||||
globalThis.devicePixelRatio = 1;
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
parts(host).canvas.clientWidth = 1800;
|
||||
await viewer.open('a');
|
||||
@@ -235,7 +235,7 @@ test('a lost WebGL context while the first panorama loads releases its bitmap',
|
||||
};
|
||||
},
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
const opening = viewer.open('a');
|
||||
assert.ok(await until(() => land !== null), 'the panorama is loading');
|
||||
@@ -262,7 +262,7 @@ test('docked, Esc anywhere in the viewer’s box (toolbar, caption) is offered t
|
||||
onEscape: () => ++asked > 0,
|
||||
load: async (url) => ({ src: url }),
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
const wrap = fakeHost();
|
||||
host.closest = (selector) => (selector === '.sl-viewer-wrap' ? wrap : null);
|
||||
await viewer.mount(host);
|
||||
@@ -363,7 +363,7 @@ function heldViewer() {
|
||||
pending.get(url).resolve({ src: url });
|
||||
pending.delete(url);
|
||||
};
|
||||
return { viewer, pending, signals, loads, release, host: fakeHost() };
|
||||
return { viewer, pending, signals, loads, release, host: fakeHost(viewer) };
|
||||
}
|
||||
|
||||
test('a new open, a close or an unmount stops the downloads of the open before it', async () => {
|
||||
@@ -439,7 +439,6 @@ test('a resize while another photo opens does not upgrade the photo still shown'
|
||||
});
|
||||
|
||||
test('a panorama is always decoded at a bounded size', async () => {
|
||||
const env = heldViewer();
|
||||
const options = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
@@ -461,7 +460,7 @@ test('a panorama is always decoded at a bounded size', async () => {
|
||||
destroy() {},
|
||||
}),
|
||||
});
|
||||
await viewer.mount(env.host);
|
||||
await viewer.mount(fakeHost(viewer));
|
||||
await viewer.open('a');
|
||||
// SOURCES' panoFirst names no size: the viewer still bounds it.
|
||||
assert.deepEqual(options[0].resize, [2048, 1024]);
|
||||
@@ -549,7 +548,7 @@ test('a browser without :fullscreen / :popover-open (Safari < 16.4) still opens
|
||||
onEscape: () => ++asked > 0,
|
||||
load: async (url) => ({ src: url }),
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
const root = host.children[0];
|
||||
// Safari 16.0–16.3: neither pseudo-class parses, so closest() throws.
|
||||
@@ -571,3 +570,69 @@ test('a browser without :fullscreen / :popover-open (Safari < 16.4) still opens
|
||||
assert.doesNotThrow(() => root.fire('keydown', event), 'Esc does not throw');
|
||||
assert.equal(asked, 1, 'Esc reaches the provider');
|
||||
});
|
||||
|
||||
test('a panorama reopened after a lost context waits for the view’s fresh canvas', async () => {
|
||||
const canvases = [];
|
||||
const reopened = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
step: async () => null,
|
||||
onStep() {},
|
||||
onReopen: (id) => reopened.push(id),
|
||||
load: async (url) => ({ src: url }),
|
||||
createRenderer: (canvas) => {
|
||||
canvases.push(canvas);
|
||||
return {
|
||||
setImage() {},
|
||||
setView() {},
|
||||
getView: () => ({ yaw: 0, pitch: 0, fov: 80 }),
|
||||
resize() {},
|
||||
clear() {},
|
||||
destroy() {},
|
||||
};
|
||||
},
|
||||
});
|
||||
// A view that draws on a microtask, as Vue does, handing its canvas back
|
||||
// each time, but draws a replacement canvas only a while after it is
|
||||
// asked.
|
||||
let shown = null;
|
||||
let canvas = { name: 'canvas', clientWidth: 400 };
|
||||
let late = null;
|
||||
viewer.subscribeView((view) => {
|
||||
const first = view && !shown;
|
||||
const replaced = view && shown && view.canvasKey !== shown.canvasKey;
|
||||
shown = view;
|
||||
if (!view) return;
|
||||
queueMicrotask(() => {
|
||||
if (first)
|
||||
for (const name of ['root', 'picture', 'prev', 'next'])
|
||||
view.actions.attach(name, { name, clientWidth: 400 });
|
||||
if (replaced) {
|
||||
canvas = null;
|
||||
late = () => {
|
||||
canvas = { name: 'canvas again', clientWidth: 400 };
|
||||
shown.actions.attach('canvas', canvas);
|
||||
};
|
||||
}
|
||||
if (canvas) view.actions.attach('canvas', canvas);
|
||||
});
|
||||
});
|
||||
await viewer.mount({});
|
||||
await viewer.open('a');
|
||||
assert.equal(shown.kind, 'pano');
|
||||
shown.actions.contextLost({ preventDefault() {} });
|
||||
assert.deepEqual(reopened, ['a']);
|
||||
const opening = viewer.open('a');
|
||||
await tick();
|
||||
assert.equal(canvases.length, 1, 'no renderer without a canvas');
|
||||
late();
|
||||
await opening;
|
||||
assert.deepEqual(
|
||||
canvases.map((canvas) => canvas.name),
|
||||
['canvas', 'canvas again'],
|
||||
);
|
||||
assert.equal(shown.kind, 'pano', 'not the front view');
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Receive the photo viewer to draw, out of several photo viewers mounting
|
||||
* in one host (see photoViewer.js): the one mounted last, by its mount key,
|
||||
* whatever order they present in. One view draws one viewer, so a viewer
|
||||
* still mounting when a later one presents is superseded: it settles its
|
||||
* mount as closed and stops presenting. Starts with the current one (null
|
||||
* while none is mounted).
|
||||
* @param {Iterable<{subscribeView?: Function}>} viewers
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
export function subscribePhotoViewers(viewers, listener) {
|
||||
// Each viewer's current presentation.
|
||||
const shown = new Map();
|
||||
let ready = false;
|
||||
let last;
|
||||
const publish = () => {
|
||||
let next = null;
|
||||
for (const view of shown.values())
|
||||
if (!next || view.mountKey > next.mountKey) next = view;
|
||||
if (!ready || next === last) return;
|
||||
last = next;
|
||||
listener(next);
|
||||
// The others are not drawn: a mount still waiting on the view never
|
||||
// would be.
|
||||
if (next)
|
||||
for (const view of [...shown.values()])
|
||||
if (view.mountKey < next.mountKey) view.actions.supersede();
|
||||
};
|
||||
const unsubscribes = [];
|
||||
for (const viewer of viewers) {
|
||||
const unsubscribe = viewer?.subscribeView?.((view) => {
|
||||
if (view) shown.set(viewer, view);
|
||||
else shown.delete(viewer);
|
||||
publish();
|
||||
});
|
||||
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);
|
||||
}
|
||||
ready = true;
|
||||
publish();
|
||||
if (last === undefined) listener((last = null));
|
||||
return () => {
|
||||
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { subscribePhotoViewers } from './photoViewers.js';
|
||||
|
||||
/**
|
||||
* A photo viewer whose presentations the test sends; `superseded` counts.
|
||||
* Unlike photoViewer.js it keeps presenting once superseded, so the order
|
||||
* is seen to come from the mount keys alone.
|
||||
*/
|
||||
function fakeViewer() {
|
||||
const listeners = new Set();
|
||||
const viewer = {
|
||||
superseded: 0,
|
||||
view(mountKey) {
|
||||
return {
|
||||
mountKey,
|
||||
actions: { supersede: () => viewer.superseded++ },
|
||||
};
|
||||
},
|
||||
present(view) {
|
||||
for (const listener of [...listeners]) listener(view);
|
||||
},
|
||||
subscribeView(listener) {
|
||||
listeners.add(listener);
|
||||
listener(null);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
};
|
||||
return viewer;
|
||||
}
|
||||
|
||||
test('the later mount is drawn, whatever order the viewers present in', () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const shown = [];
|
||||
const unsubscribe = subscribePhotoViewers([a, b], (view) => shown.push(view));
|
||||
assert.deepEqual(shown, [null], 'the current one first');
|
||||
const first = a.view(1);
|
||||
a.present(first);
|
||||
const later = b.view(2);
|
||||
b.present(later);
|
||||
assert.equal(shown.at(-1), later);
|
||||
assert.equal(a.superseded, 1, 'the earlier mount is told it is not drawn');
|
||||
// The earlier viewer presents again: it does not take the view back.
|
||||
a.present({ ...first, kind: 'flat' });
|
||||
assert.equal(shown.at(-1), later);
|
||||
assert.equal(shown.length, 3);
|
||||
b.present(null);
|
||||
assert.equal(shown.at(-1).mountKey, 1, 'the only one presenting');
|
||||
a.present(null);
|
||||
assert.equal(shown.at(-1), null);
|
||||
unsubscribe();
|
||||
a.present(first);
|
||||
assert.equal(shown.length, 5, 'unsubscribed');
|
||||
});
|
||||
@@ -13,7 +13,11 @@ import {
|
||||
TOO_HIGH_HINT,
|
||||
} from './policy.js';
|
||||
import { validateProviders } from '../../registry.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
coverageViewer,
|
||||
@@ -190,7 +194,7 @@ test('the nearest photo is the closest within 50 m, and opening it asks nothing
|
||||
});
|
||||
const id = await instance.nearestImage({ lat: 42.3314, lon: -83.0458 });
|
||||
assert.equal(id, 'near');
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(instance.viewer);
|
||||
await instance.viewer.mount(host);
|
||||
await instance.viewer.open(id);
|
||||
assert.deepEqual(source.calls.photo, [], 'the lookup filled the cache');
|
||||
@@ -285,7 +289,7 @@ const press = async (button, opened) => {
|
||||
|
||||
async function mounted(sourceOptions, contextOptions) {
|
||||
const env = setup(sourceOptions, contextOptions);
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(env.instance.viewer);
|
||||
await env.instance.viewer.mount(host);
|
||||
const poses = [];
|
||||
env.instance.viewer.onPose((pose) => poses.push(pose.imageId));
|
||||
@@ -970,7 +974,7 @@ test('after a GPU reset the photo is drawn again by the viewer, not through the
|
||||
context.actions.reportError = (message) => errors.push(message);
|
||||
const instance = definition.create(context);
|
||||
actions.instance = instance;
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(instance.viewer);
|
||||
await instance.viewer.mount(host);
|
||||
const poses = [];
|
||||
instance.viewer.onPose((pose) => poses.push(pose.imageId));
|
||||
@@ -978,6 +982,7 @@ test('after a GPU reset the photo is drawn again by the viewer, not through the
|
||||
const canvas = () => host.children[0].children[1];
|
||||
const kind = () => host.children[0].dataset.kind;
|
||||
canvas().fire('webglcontextlost');
|
||||
await drawn();
|
||||
assert.equal(kind(), '', 'cleared while it is drawn again');
|
||||
assert.ok(await until(() => kind() === 'pano'), 'drawn again');
|
||||
assert.ok(poses.length >= 2);
|
||||
@@ -1010,7 +1015,7 @@ test('the marker is KartaView’s own cone, and it points along the drive, not a
|
||||
photos: page.photos.map((record) => ({ ...record, heading: '200' })),
|
||||
};
|
||||
};
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(env.instance.viewer);
|
||||
await env.instance.viewer.mount(host);
|
||||
const bearings = [];
|
||||
env.instance.viewer.onPose((pose) => bearings.push(pose.bearing));
|
||||
@@ -1101,7 +1106,7 @@ test('a sequence page that lands while a lost GPU context recovers still draws i
|
||||
const instance = definition.create(context);
|
||||
actions.instance = instance;
|
||||
const cones = await withCones(t, { instance });
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(instance.viewer);
|
||||
await instance.viewer.mount(host);
|
||||
await instance.viewer.open('p-0');
|
||||
// › to the next panorama; the GPU resets while it loads.
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createKartaViewViewer, loadImage, panoSizeFor } from './viewer.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
|
||||
const PHOTOS = {
|
||||
flat1: {
|
||||
@@ -148,7 +152,7 @@ function setup({
|
||||
});
|
||||
const poses = [];
|
||||
viewer.onPose((pose) => poses.push(pose));
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
return {
|
||||
viewer,
|
||||
poses,
|
||||
@@ -223,6 +227,7 @@ test('a flat photo shows its 1280 px version, then the sharp one, with a pose fa
|
||||
assert.equal(picture.src, loads[1].url, 'upgraded to the sharp version');
|
||||
assert.equal(viewer.supportsRenderModes(), true);
|
||||
viewer.setRenderMode('fill');
|
||||
await drawn();
|
||||
assert.equal(picture.style.objectFit, 'cover');
|
||||
});
|
||||
|
||||
@@ -357,12 +362,14 @@ test('a newer open wins; close hides the photo; unmount tears down the panorama'
|
||||
assert.equal(renderers.length, 0, 'nor built a WebGL context');
|
||||
await viewer.open('pano1');
|
||||
viewer.close();
|
||||
await drawn();
|
||||
const { picture, canvas, root } = parts(host);
|
||||
assert.equal(canvas.hidden, true);
|
||||
assert.equal(picture.hidden, true);
|
||||
assert.equal(root.dataset.kind, '');
|
||||
await assert.rejects(viewer.open('nope'), /no such photo/);
|
||||
viewer.unmount();
|
||||
await drawn();
|
||||
assert.equal(host.children.length, 0);
|
||||
assert.equal(renderers[0].destroyed, true);
|
||||
await assert.rejects(viewer.open('flat1'), /not mounted/);
|
||||
@@ -463,12 +470,14 @@ test('a lost WebGL context drops the panorama and shows the photo again on a new
|
||||
const lost = parts(host).canvas;
|
||||
lost.fire('webglcontextlost');
|
||||
assert.equal(renderers[0].destroyed, true);
|
||||
await drawn();
|
||||
// A lost canvas only ever gives back its lost context: a new element.
|
||||
assert.notEqual(parts(host).canvas, lost);
|
||||
assert.deepEqual(stepped, ['pano1'], 'reopened through the core');
|
||||
assert.ok(await until(() => renderers.length === 2), 'a new renderer');
|
||||
assert.equal(renderers[1].canvas, parts(host).canvas);
|
||||
assert.equal(poses.at(-1).imageId, 'pano1');
|
||||
await drawn();
|
||||
assert.equal(parts(host).canvas.hidden, false);
|
||||
});
|
||||
|
||||
@@ -498,7 +507,7 @@ test('a WebGL failure only lasts until the viewer is mounted again', async () =>
|
||||
return renderer;
|
||||
},
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('pano1');
|
||||
assert.equal(parts(host).picture.hidden, false, 'front view');
|
||||
@@ -589,7 +598,7 @@ function heldSetup({ slowSteps = false } = {}) {
|
||||
});
|
||||
const poses = [];
|
||||
viewer.onPose((pose) => poses.push(pose.imageId));
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
/** Let the load showing `what` (e.g. 'a@1280') finish. */
|
||||
const release = async (what) => {
|
||||
assert.ok(
|
||||
@@ -798,7 +807,7 @@ function heldPanoSetup(held) {
|
||||
});
|
||||
const poses = [];
|
||||
viewer.onPose((pose) => poses.push(pose));
|
||||
return { viewer, renderers, stepped, poses, host: fakeHost(), release };
|
||||
return { viewer, renderers, stepped, poses, host: fakeHost(viewer), release };
|
||||
}
|
||||
|
||||
const pano2Preview = Buffer.from(
|
||||
@@ -931,7 +940,7 @@ test('a docked viewer keeps the 1,280 px photo; expanding it brings the 2,560 px
|
||||
viewer.resize();
|
||||
assert.ok(await until(() => loads.length === 2));
|
||||
assert.match(loads[1].url, /rs:fit:2560:2560/);
|
||||
assert.equal(parts(host).picture.src, loads[1].url);
|
||||
assert.ok(await until(() => parts(host).picture.src === loads[1].url));
|
||||
viewer.resize();
|
||||
for (let i = 0; i < 4; i++) await settle();
|
||||
assert.equal(loads.length, 2, 'once only');
|
||||
|
||||
@@ -14,7 +14,11 @@ import {
|
||||
} from './policy.js';
|
||||
import { validateProviders } from '../../registry.js';
|
||||
import { tileBounds } from '../../tileMath.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
coverageViewer,
|
||||
@@ -284,7 +288,7 @@ async function mounted(sourceOptions, contextOptions) {
|
||||
const env = setup(sourceOptions, contextOptions);
|
||||
const poses = [];
|
||||
env.instance.viewer.onPose((pose) => poses.push(pose.imageId));
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(env.instance.viewer);
|
||||
await env.instance.viewer.mount(host);
|
||||
const [, , prev, next] = host.children[0].children;
|
||||
return { ...env, poses, prev, next };
|
||||
@@ -320,7 +324,7 @@ test('one lookup for a picture asked twice at once', async () => {
|
||||
await gate;
|
||||
return getItem(id);
|
||||
};
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(instance.viewer);
|
||||
await instance.viewer.mount(host);
|
||||
const first = instance.viewer.open('s-0');
|
||||
const second = instance.viewer.open('s-0');
|
||||
@@ -985,7 +989,7 @@ test('after a GPU reset the viewer draws the same picture itself, leaving the co
|
||||
const { context, actions } = fakeContext();
|
||||
const instance = definition.create(context);
|
||||
actions.instance = instance;
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(instance.viewer);
|
||||
await instance.viewer.mount(host);
|
||||
const poses = [];
|
||||
instance.viewer.onPose((pose) => poses.push(pose.imageId));
|
||||
@@ -994,6 +998,8 @@ test('after a GPU reset the viewer draws the same picture itself, leaving the co
|
||||
const kind = () => host.children[0].dataset.kind;
|
||||
assert.equal(kind(), 'pano');
|
||||
canvas().fire('webglcontextlost');
|
||||
await drawn();
|
||||
assert.equal(kind(), '', 'cleared while it is drawn again');
|
||||
assert.ok(await until(() => kind() === 'pano'), 'drawn again');
|
||||
assert.deepEqual(poses, ['p-0', 'p-0']);
|
||||
assert.deepEqual(actions.opened, [], 'not through the core');
|
||||
|
||||
@@ -59,7 +59,7 @@ function setup() {
|
||||
});
|
||||
const poses = [];
|
||||
viewer.onPose((pose) => poses.push(pose));
|
||||
return { viewer, poses, loads, renderers, stepped, host: fakeHost() };
|
||||
return { viewer, poses, loads, renderers, stepped, host: fakeHost(viewer) };
|
||||
}
|
||||
|
||||
const parts = (host) => {
|
||||
@@ -252,7 +252,7 @@ test('without WebGL a 360° picture shows its whole equirectangular image flat',
|
||||
throw new Error('no WebGL');
|
||||
},
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('pano');
|
||||
const { picture } = parts(host);
|
||||
@@ -272,7 +272,7 @@ test('a picture whose SD fails falls back to the thumbnail; the original only fo
|
||||
return { src: url };
|
||||
},
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('flat');
|
||||
assert.deepEqual(tried, [PHOTOS.flat.urls.sd, PHOTOS.flat.urls.thumb]);
|
||||
@@ -326,7 +326,7 @@ test('an SD panorama refused for its size is not fetched again as a flat photo',
|
||||
destroy() {},
|
||||
}),
|
||||
});
|
||||
const host = fakeHost();
|
||||
const host = fakeHost(viewer);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('pano');
|
||||
const sdLoads = loads.filter((l) => l.url === PHOTOS.pano.urls.sd);
|
||||
|
||||
@@ -63,6 +63,9 @@ export function filtersOf(provider) {
|
||||
* @property {(mode: 'letterbox'|'fill') => void} [setRenderMode]
|
||||
* @property {() => boolean} [supportsRenderModes] False while the image has no frame to fit (a panorama): FIT / FILL hide.
|
||||
* @property {(listener: (pose: StreetPose) => void) => () => void} onPose
|
||||
* @property {(listener: (view: ?object) => void) => () => void} [subscribeView]
|
||||
* An adapter that draws nothing itself presents its viewer here, for the
|
||||
* layer's `subscribePhotoViewer()` (see photoViewer.js).
|
||||
*
|
||||
* @typedef {object} StreetPose
|
||||
* @property {string} providerId
|
||||
|
||||
@@ -60,13 +60,119 @@ function fakeElement(tag) {
|
||||
return element;
|
||||
}
|
||||
|
||||
export function fakeHost() {
|
||||
const doc = { createElement: (tag) => fakeElement(tag) };
|
||||
/**
|
||||
* A host element; given a photo viewer, its view is drawn as
|
||||
* StreetLevelPhotoViewer draws it (see `drawPhotoViewer`).
|
||||
*/
|
||||
export function fakeHost(viewer = null) {
|
||||
const host = fakeElement('div');
|
||||
host.ownerDocument = doc;
|
||||
if (viewer) drawPhotoViewer(viewer);
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a photo viewer's presentations with fake elements, as
|
||||
* StreetLevelPhotoViewer does through Vue: changes made in one tick are
|
||||
* drawn once, on a microtask, with their final value; the root (keyed by
|
||||
* the mount) holds the picture, the panorama canvas (keyed by its canvas
|
||||
* key) and the ‹ › buttons, kept while their keys are, and every element
|
||||
* is handed back on each draw. A value is written only when the drawn
|
||||
* presentation changes it, and the elements' events reach the viewer's
|
||||
* actions.
|
||||
* @returns {() => void} Stop drawing.
|
||||
*/
|
||||
export function drawPhotoViewer(viewer) {
|
||||
let parts = null;
|
||||
let last = null;
|
||||
let pending = null;
|
||||
let queued = false;
|
||||
const on = (element, type, name) =>
|
||||
element.addEventListener(type, (event) => last?.actions[name](event));
|
||||
const canvasFor = () => {
|
||||
const canvas = fakeElement('canvas');
|
||||
canvas.classList.add('sl-photo-pano');
|
||||
for (const type of ['pointerdown', 'pointermove', 'pointerup', 'wheel'])
|
||||
on(canvas, type, type);
|
||||
on(canvas, 'pointercancel', 'pointerup');
|
||||
on(canvas, 'webglcontextlost', 'contextLost');
|
||||
return canvas;
|
||||
};
|
||||
const build = (view) => {
|
||||
const root = fakeElement('div');
|
||||
const picture = fakeElement('img');
|
||||
const canvas = canvasFor();
|
||||
const button = (delta) => {
|
||||
const element = fakeElement('button');
|
||||
element.addEventListener('click', (event) =>
|
||||
last?.actions.step(delta, event),
|
||||
);
|
||||
return element;
|
||||
};
|
||||
const prev = button(-1);
|
||||
const next = button(1);
|
||||
on(root, 'keydown', 'keydown');
|
||||
root.append(picture, canvas, prev, next);
|
||||
view.host.append(root);
|
||||
return { root, picture, canvas, prev, next };
|
||||
};
|
||||
const draw = (view) => {
|
||||
if (!view) {
|
||||
parts?.root.remove();
|
||||
parts = last = null;
|
||||
return;
|
||||
}
|
||||
if (!parts || last.mountKey !== view.mountKey) {
|
||||
parts?.root.remove();
|
||||
parts = build(view);
|
||||
last = null;
|
||||
} else if (last.host !== view.host) {
|
||||
parts.root.remove();
|
||||
view.host.append(parts.root);
|
||||
}
|
||||
const changed = (key) => !last || last[key] !== view[key];
|
||||
let canvasFresh = !last;
|
||||
if (last && changed('canvasKey')) {
|
||||
const canvas = canvasFor();
|
||||
parts.canvas.replaceWith(canvas);
|
||||
parts.canvas = canvas;
|
||||
canvasFresh = true;
|
||||
}
|
||||
const { root, picture, canvas, prev, next } = parts;
|
||||
if (changed('providerId')) root.dataset.provider = view.providerId;
|
||||
if (changed('label')) root.attributes['aria-label'] = view.label;
|
||||
if (changed('kind') && view.kind !== undefined)
|
||||
root.dataset.kind = view.kind;
|
||||
if (changed('alt')) picture.alt = view.alt;
|
||||
if (changed('src')) {
|
||||
if (view.src) picture.src = view.src;
|
||||
else picture.removeAttribute('src');
|
||||
}
|
||||
if (changed('fit')) picture.style.objectFit = view.fit;
|
||||
if (changed('kind')) picture.hidden = view.kind !== 'flat';
|
||||
if (changed('kind') || canvasFresh) canvas.hidden = view.kind !== 'pano';
|
||||
if (changed('dragging') || canvasFresh) {
|
||||
if (view.dragging) canvas.classList.add('sl-photo-dragging');
|
||||
else canvas.classList.remove('sl-photo-dragging');
|
||||
}
|
||||
if (changed('prevLabel')) prev.attributes['aria-label'] = view.prevLabel;
|
||||
if (changed('nextLabel')) next.attributes['aria-label'] = view.nextLabel;
|
||||
if (changed('prevDisabled')) prev.disabled = view.prevDisabled;
|
||||
if (changed('nextDisabled')) next.disabled = view.nextDisabled;
|
||||
last = view;
|
||||
for (const name of ['root', 'picture', 'canvas', 'prev', 'next'])
|
||||
view.actions.attach(name, parts[name]);
|
||||
};
|
||||
return viewer.subscribeView((view) => {
|
||||
pending = view;
|
||||
if (queued) return;
|
||||
queued = true;
|
||||
queueMicrotask(() => {
|
||||
queued = false;
|
||||
draw(pending);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait, a macrotask at a time, until `condition` holds; false if it never
|
||||
* does within `ticks`. A test asserting that something did not happen waits
|
||||
@@ -79,3 +185,6 @@ export async function until(condition, { ticks = 200 } = {}) {
|
||||
}
|
||||
return Boolean(await condition());
|
||||
}
|
||||
|
||||
/** Let a photo viewer's view draw what was presented in this tick. */
|
||||
export const drawn = () => new Promise((resolve) => queueMicrotask(resolve));
|
||||
@@ -65,6 +65,7 @@ import { useContextStore } from './stores/context.js';
|
||||
import { useSpaceMissionsStore } from './stores/spaceMissions.js';
|
||||
import { SpaceMissionsControls } from './spaceMissionsControls.js';
|
||||
import { useMissionReplayStore } from './stores/missionReplay.js';
|
||||
import { useStreetLevelPhotoStore } from './stores/streetLevelPhoto.js';
|
||||
import { useCelestialRingStore } from './stores/celestialRing.js';
|
||||
import { icrfToFixedAxes } from '../sky/icrf.js';
|
||||
import { useWorldOverlayActionsStore } from './stores/worldOverlayActions.js';
|
||||
@@ -1200,7 +1201,18 @@ export class StyleManager extends ShellFacade {
|
||||
const { streetLevelLayer } = this.services;
|
||||
this._streetLevelControls?.destroy();
|
||||
this._streetLevelControls = null;
|
||||
// The photo viewer (KartaView, Panoramax), which the layer panels draw
|
||||
// into the viewer host. A viewer left by an earlier shell must
|
||||
// not outlive its layer.
|
||||
const streetLevelPhotoStore = useStreetLevelPhotoStore(applicationPinia());
|
||||
this._streetLevelPhotoUnsubscribe?.();
|
||||
this._streetLevelPhotoUnsubscribe = null;
|
||||
streetLevelPhotoStore.publish(null);
|
||||
if (!this._streetLevelPanel || !streetLevelLayer) return;
|
||||
this._streetLevelPhotoUnsubscribe =
|
||||
streetLevelLayer.subscribePhotoViewer?.((view) =>
|
||||
streetLevelPhotoStore.publish(view),
|
||||
) ?? null;
|
||||
// Photo framing yields to newer navigation and releases tracking like
|
||||
// any deferred flight.
|
||||
streetLevelLayer.attachNavigation?.({
|
||||
@@ -1969,7 +1981,10 @@ export class StyleManager extends ShellFacade {
|
||||
this._spaceMissionsControls = null;
|
||||
this._missionReplayUnsubscribe?.();
|
||||
this._missionReplayUnsubscribe = null;
|
||||
this._streetLevelPhotoUnsubscribe?.();
|
||||
this._streetLevelPhotoUnsubscribe = null;
|
||||
useMissionReplayStore(applicationPinia()).publish(null);
|
||||
useStreetLevelPhotoStore(applicationPinia()).publish(null);
|
||||
const contextStore = useContextStore(applicationPinia());
|
||||
contextStore.connectAwareness(null);
|
||||
contextStore.publishAwareness(null);
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
// The Street Level photo viewer (KartaView, Panoramax) is a view with kit
|
||||
// controls: StreetLevelPhotoViewer draws what the photo viewer presents and
|
||||
// hand-writes no <button> and no Material Symbols span; its ‹ › are ghost
|
||||
// UiButtons, not pressed toggles, whose glyphs are text characters. The
|
||||
// photo viewer builds no element of its own, and the street-level layer it
|
||||
// belongs to needs no Vue: the application shell publishes its
|
||||
// presentation to the store the view renders.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const read = (file) =>
|
||||
readFileSync(new URL(`../../${file}`, import.meta.url), 'utf8');
|
||||
/** The template block: everything outside script, style and comments. */
|
||||
const template = (file) =>
|
||||
read(file)
|
||||
.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/g, '')
|
||||
.replace(/<!--[\s\S]*?-->/g, '');
|
||||
// An opening tag's attributes, quoted values may hold `>` (`() => x`).
|
||||
const ATTRS = String.raw`(?:[^>"']|"[^"]*"|'[^']*')*`;
|
||||
const tags = (markup, name) =>
|
||||
markup.match(new RegExp(String.raw`<${name}\b${ATTRS}>`, 'g')) ?? [];
|
||||
// An attribute's name, outside the quoted values.
|
||||
const names = (attrs) => attrs.replace(/"[^"]*"|'[^']*'/g, '""');
|
||||
|
||||
const VIEW = 'ui/views/panels/StreetLevelPhotoViewer.vue';
|
||||
const VIEWER = 'layers/streetLevel/photoViewer.js';
|
||||
|
||||
test('the photo viewer hand-writes no button; ‹ and › are kit buttons', () => {
|
||||
const markup = template(VIEW);
|
||||
assert.deepEqual(markup.match(/<button\b/gi) ?? [], [], VIEW);
|
||||
assert.deepEqual(markup.match(/<(select|input)\b/gi) ?? [], [], VIEW);
|
||||
const buttons = tags(markup, 'UiButton');
|
||||
assert.equal(buttons.length, 2);
|
||||
assert.equal(tags(markup, 'UiCloseButton').length, 0);
|
||||
// Ghost buttons with the classes street-level.css styles.
|
||||
for (const [button, side] of [
|
||||
[buttons[0], 'prev'],
|
||||
[buttons[1], 'next'],
|
||||
]) {
|
||||
assert.doesNotMatch(button, /\svariant=/);
|
||||
assert.match(
|
||||
button,
|
||||
new RegExp(`\\sclass="sl-photo-step sl-photo-step-${side}"`),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the photo viewer hand-writes no Material glyph', () => {
|
||||
const markup = template(VIEW);
|
||||
assert.deepEqual(markup.match(/material-symbols/g) ?? [], [], VIEW);
|
||||
assert.equal(tags(markup, 'UiIcon').length, 0);
|
||||
// ‹ and › are text characters.
|
||||
assert.match(markup, />‹<\/UiButton\s*>/);
|
||||
assert.match(markup, />›<\/UiButton\s*>/);
|
||||
});
|
||||
|
||||
test('the step buttons are not pressed toggles', () => {
|
||||
const markup = template(VIEW);
|
||||
for (const [tag, attrs] of markup.matchAll(
|
||||
new RegExp(String.raw`<[A-Za-z][\w-]*\b(${ATTRS})>`, 'g'),
|
||||
)) {
|
||||
assert.doesNotMatch(
|
||||
names(attrs),
|
||||
/(?:^|\s)(?::|v-bind:)?aria-pressed(?=[\s=/]|$)/,
|
||||
tag,
|
||||
);
|
||||
assert.doesNotMatch(
|
||||
attrs,
|
||||
/(?:^|\s)(?:v-bind|:)=(?:"[^"]*aria-?pressed[^"]*"|'[^']*aria-?pressed[^']*')/i,
|
||||
tag,
|
||||
);
|
||||
assert.doesNotMatch(names(attrs), /(?:^|\s)(?::|v-bind:)?pressed\b/, tag);
|
||||
}
|
||||
});
|
||||
|
||||
test('the photo viewer is drawn by its view, and its layer needs no Vue', () => {
|
||||
const source = read(VIEWER);
|
||||
// No element made, inserted, moved, removed or written: the view does
|
||||
// all of that. (An Image the loader downloads with is not on the page.)
|
||||
for (const write of [
|
||||
/createElement/,
|
||||
/\b(?:inner|outer)HTML\b/,
|
||||
/insertAdjacent(?:HTML|Element|Text)/,
|
||||
/\.(?:append|prepend|appendChild|insertBefore|replaceWith|replaceChildren|before|after)\(/,
|
||||
/\.remove\(\)/,
|
||||
/\.(?:set|remove|toggle)Attribute(?:NS)?\(/,
|
||||
/\.(?:classList|className|dataset|style)\b/,
|
||||
/\.(?:hidden|disabled|textContent|innerText|tabIndex|title|alt)\s*=[^=]/,
|
||||
/from 'vue'|\.vue'/,
|
||||
])
|
||||
assert.doesNotMatch(source, write);
|
||||
const markup = template(VIEW);
|
||||
const opening = (name, attribute) =>
|
||||
new RegExp(String.raw`<${name}\b${ATTRS}\s${attribute}${ATTRS}>`);
|
||||
for (const part of [
|
||||
opening('div', 'class="sl-photo-viewer"'),
|
||||
opening('img', 'class="sl-photo-flat"'),
|
||||
opening('canvas', ':key="photo.canvasKey"'),
|
||||
opening('canvas', 'class="sl-photo-pano"'),
|
||||
])
|
||||
assert.match(markup, part);
|
||||
const groups = JSON.parse(
|
||||
readFileSync(
|
||||
new URL('../../../scripts/package-boundaries.json', import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
const layer = groups['street-level-layer'];
|
||||
assert.ok(layer.modules.includes(`src/${VIEWER}`));
|
||||
assert.equal(layer.external.includes('vue'), false);
|
||||
assert.equal(
|
||||
layer.modules.some((module) => module.endsWith('.vue')),
|
||||
false,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { markRaw, shallowRef } from 'vue';
|
||||
|
||||
/**
|
||||
* The Street Level photo viewer (KartaView, Panoramax) as its view renders
|
||||
* it. The street-level layer presents it through `subscribePhotoViewer()`
|
||||
* and the application shell publishes each presentation here.
|
||||
*/
|
||||
export const useStreetLevelPhotoStore = defineStore(
|
||||
'gev-street-level-photo',
|
||||
() => {
|
||||
/**
|
||||
* `{host, providerId, label, alt, kind, src, fit, canvasKey, dragging,
|
||||
* prevLabel, prevDisabled, nextLabel, nextDisabled, actions}`, or null
|
||||
* while no photo viewer is mounted.
|
||||
*/
|
||||
const photo = shallowRef(null);
|
||||
|
||||
/** @param {?object} next A fresh presentation; every publish re-renders. */
|
||||
function publish(next) {
|
||||
photo.value = next && markRaw(next);
|
||||
}
|
||||
|
||||
return { photo, publish };
|
||||
},
|
||||
);
|
||||
@@ -12,6 +12,7 @@ import SceneOptions from './SceneOptions.vue';
|
||||
import SceneShotList from './SceneShotList.vue';
|
||||
import CctvPanel from './panels/CctvPanel.vue';
|
||||
import StreetLevelPanel from './panels/StreetLevelPanel.vue';
|
||||
import StreetLevelPhotoViewer from './panels/StreetLevelPhotoViewer.vue';
|
||||
import DrawControls from './panels/DrawControls.vue';
|
||||
import GeoImport from './panels/GeoImport.vue';
|
||||
import UiSelect from '../kit/UiSelect.js';
|
||||
@@ -71,6 +72,7 @@ async function importChosen(event) {
|
||||
|
||||
<!-- Street Level Panel (moved into #right-context-rail at runtime) -->
|
||||
<StreetLevelPanel />
|
||||
<StreetLevelPhotoViewer />
|
||||
|
||||
<!-- Scene Director Panel -->
|
||||
<RailPanel id="scene-panel" start-collapsed v-class-toggle="{ running: scene?.running, 'has-shots': scene?.hasShots }" :title="t('AUTHORING')" :hint="t('Create and edit scenes')" scroll>
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
// The Street Level photo viewer (KartaView, Panoramax) on a page: the photo
|
||||
// viewer presents it, the store carries it, and StreetLevelPhotoViewer draws
|
||||
// it into the viewer host, forwards its events and hands its elements back.
|
||||
import '../../../testSupport/registerDom.mjs';
|
||||
import test from 'node:test';
|
||||
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';
|
||||
|
||||
const PHOTOS = {
|
||||
f: { id: 'f', sequenceId: 's', lon: 1, lat: 2, heading: 0, isPano: false },
|
||||
g: { id: 'g', sequenceId: 's', lon: 1, lat: 2, heading: 0, isPano: false },
|
||||
p: { id: 'p', sequenceId: 's', lon: 1, lat: 2, heading: 0, isPano: true },
|
||||
};
|
||||
const SOURCES = {
|
||||
hasPano: (photo) => photo.isPano,
|
||||
panoFirst: (photo) => ({ url: `${photo.id}-pano`, width: 2048 }),
|
||||
panoSharp: () => null,
|
||||
flatFirst: (photo) => [`https://photos.test/${photo.id}.jpg`],
|
||||
flatSharp: () => null,
|
||||
describe: () => ({ externalUrl: 'https://example.org' }),
|
||||
};
|
||||
|
||||
/**
|
||||
* 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';
|
||||
const host = wrap.appendChild(document.createElement('div'));
|
||||
host.id = 'sl-viewer';
|
||||
const pinia = createPinia();
|
||||
const app = createApp(StreetLevelPhotoViewer).use(pinia);
|
||||
app.mount(document.body.appendChild(document.createElement('div')));
|
||||
const store = useStreetLevelPhotoStore(pinia);
|
||||
const renderers = [];
|
||||
const stepped = [];
|
||||
const create = (providerId) =>
|
||||
createPhotoViewer({
|
||||
providerId,
|
||||
name: providerId === 'panoramax' ? 'Panoramax' : 'KartaView',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
// f, then g: the whole sequence.
|
||||
step: async (photo, delta) =>
|
||||
({ f: { 1: 'g' }, g: { '-1': 'f' } })[photo.id]?.[delta] ?? null,
|
||||
onStep: (id) => stepped.push(id),
|
||||
load: async (url) => ({ src: url }),
|
||||
createRenderer: (canvas) => {
|
||||
const view = { yaw: 0, pitch: 0, fov: 80 };
|
||||
const renderer = {
|
||||
canvas,
|
||||
setImage() {},
|
||||
setView: (next) => Object.assign(view, next),
|
||||
getView: () => ({ ...view }),
|
||||
resize() {},
|
||||
clear() {},
|
||||
destroy() {},
|
||||
};
|
||||
renderers.push(renderer);
|
||||
return renderer;
|
||||
},
|
||||
...options,
|
||||
});
|
||||
const viewers = providerIds.map(create);
|
||||
const unsubscribe = subscribePhotoViewers(viewers, (view) =>
|
||||
store.publish(view),
|
||||
);
|
||||
t.after(() => {
|
||||
for (const viewer of viewers) viewer.unmount();
|
||||
unsubscribe();
|
||||
app.unmount();
|
||||
});
|
||||
return { viewer: viewers[0], viewers, host, wrap, renderers, stepped };
|
||||
}
|
||||
|
||||
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
test('the viewer draws in its host with the markup its stylesheet reads', async (t) => {
|
||||
const { viewer, host } = mount(t);
|
||||
await viewer.mount(host);
|
||||
const root = host.querySelector(':scope > .sl-photo-viewer');
|
||||
assert.ok(root, 'drawn in the host');
|
||||
assert.equal(root.dataset.provider, 'panoramax');
|
||||
assert.equal(root.getAttribute('tabindex'), '0');
|
||||
assert.equal(root.getAttribute('role'), 'group');
|
||||
assert.match(root.getAttribute('aria-label'), /^Panoramax photo\. ← and →/);
|
||||
assert.equal(root.hasAttribute('data-kind'), false, 'nothing shown yet');
|
||||
const [picture, canvas, prev, next] = root.children;
|
||||
assert.equal(picture.tagName, 'IMG');
|
||||
assert.equal(picture.className, 'sl-photo-flat');
|
||||
assert.equal(picture.getAttribute('alt'), 'Panoramax street-level photo');
|
||||
assert.equal(picture.getAttribute('draggable'), 'false');
|
||||
assert.equal(picture.hidden, true);
|
||||
assert.equal(picture.hasAttribute('src'), false);
|
||||
assert.equal(picture.style.objectFit, 'contain');
|
||||
assert.equal(canvas.tagName, 'CANVAS');
|
||||
assert.equal(canvas.className, 'sl-photo-pano');
|
||||
assert.equal(canvas.hidden, true);
|
||||
for (const [button, side, glyph, label] of [
|
||||
[prev, 'prev', '‹', 'Previous photo in this sequence (←)'],
|
||||
[next, 'next', '›', 'Next photo in this sequence (→)'],
|
||||
]) {
|
||||
assert.equal(button.tagName, 'BUTTON');
|
||||
assert.equal(button.type, 'button');
|
||||
assert.ok(button.classList.contains('sl-photo-step'));
|
||||
assert.ok(button.classList.contains(`sl-photo-step-${side}`));
|
||||
assert.equal(button.textContent.trim(), glyph);
|
||||
assert.equal(button.getAttribute('aria-label'), label);
|
||||
assert.equal(button.getAttribute('title'), label);
|
||||
assert.equal(button.disabled, true);
|
||||
assert.equal(button.hasAttribute('aria-pressed'), false);
|
||||
}
|
||||
viewer.setRenderMode('fill');
|
||||
await nextTick();
|
||||
assert.equal(picture.style.objectFit, 'cover');
|
||||
viewer.unmount();
|
||||
await nextTick();
|
||||
assert.equal(host.children.length, 0, 'unmounted: gone from the host');
|
||||
});
|
||||
|
||||
test('a flat photo shows, ‹ › and ← → step along its sequence, and close hides it', async (t) => {
|
||||
const { viewer, host, stepped } = mount(t);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('f');
|
||||
await nextTick();
|
||||
const root = host.querySelector('.sl-photo-viewer');
|
||||
const [picture, canvas, prev, next] = root.children;
|
||||
assert.equal(root.dataset.kind, 'flat');
|
||||
assert.equal(picture.hidden, false);
|
||||
assert.equal(canvas.hidden, true);
|
||||
assert.equal(picture.getAttribute('src'), 'https://photos.test/f.jpg');
|
||||
// ‹ is disabled once its end is known; › stays.
|
||||
await tick();
|
||||
await nextTick();
|
||||
assert.equal(prev.disabled, true);
|
||||
assert.equal(next.disabled, false);
|
||||
let bubbled = 0;
|
||||
host.addEventListener('click', () => bubbled++);
|
||||
next.click();
|
||||
await tick();
|
||||
assert.deepEqual(stepped, ['g']);
|
||||
assert.equal(bubbled, 0, 'the click stays in the viewer');
|
||||
await viewer.open('g');
|
||||
root.dispatchEvent(
|
||||
new KeyboardEvent('keydown', { key: 'ArrowLeft', bubbles: true }),
|
||||
);
|
||||
await tick();
|
||||
assert.deepEqual(stepped, ['g', 'f']);
|
||||
viewer.close();
|
||||
await nextTick();
|
||||
assert.equal(root.dataset.kind, '');
|
||||
assert.equal(picture.hidden, true);
|
||||
assert.equal(picture.hasAttribute('src'), false);
|
||||
});
|
||||
|
||||
test('a step button that disables under the keyboard hands focus to the viewer', async (t) => {
|
||||
// The sequence's end is known only once the user is on ›.
|
||||
let answer;
|
||||
const asked = new Promise((resolve) => (answer = resolve));
|
||||
const { viewer, host } = mount(t, {
|
||||
step: async (photo, delta) => (delta > 0 ? asked : null),
|
||||
});
|
||||
await viewer.mount(host);
|
||||
await viewer.open('g');
|
||||
await nextTick();
|
||||
const root = host.querySelector('.sl-photo-viewer');
|
||||
const next = root.children[3];
|
||||
assert.equal(next.disabled, false);
|
||||
next.focus();
|
||||
assert.ok(document.activeElement === next, 'on ›');
|
||||
answer(null);
|
||||
await tick();
|
||||
await nextTick();
|
||||
assert.equal(next.disabled, true, 'g is the last photo');
|
||||
assert.ok(document.activeElement === root, 'focus moved to the viewer');
|
||||
});
|
||||
|
||||
test('a panorama draws on the view’s canvas; drag, wheel and a lost context reach the viewer', async (t) => {
|
||||
const { viewer, host, renderers } = mount(t);
|
||||
await viewer.mount(host);
|
||||
await viewer.open('p');
|
||||
await nextTick();
|
||||
const root = host.querySelector('.sl-photo-viewer');
|
||||
const canvas = root.children[1];
|
||||
assert.ok(renderers[0].canvas === canvas, 'the canvas the view drew');
|
||||
assert.equal(root.dataset.kind, 'pano');
|
||||
assert.equal(canvas.hidden, false);
|
||||
assert.equal(root.children[0].hidden, true);
|
||||
canvas.dispatchEvent(
|
||||
new PointerEvent('pointerdown', {
|
||||
pointerId: 1,
|
||||
pointerType: 'mouse',
|
||||
button: 0,
|
||||
clientX: 0,
|
||||
clientY: 0,
|
||||
}),
|
||||
);
|
||||
await nextTick();
|
||||
assert.ok(canvas.classList.contains('sl-photo-dragging'));
|
||||
canvas.dispatchEvent(new PointerEvent('pointerup', { pointerId: 1 }));
|
||||
await nextTick();
|
||||
assert.equal(canvas.classList.contains('sl-photo-dragging'), false);
|
||||
const wheel = new WheelEvent('wheel', { deltaY: 100, cancelable: true });
|
||||
canvas.dispatchEvent(wheel);
|
||||
assert.equal(wheel.defaultPrevented, true, 'the page does not scroll');
|
||||
assert.ok(renderers[0].getView().fov > 80, 'zoomed out');
|
||||
// A lost context: a fresh canvas in its place, which the next renderer takes.
|
||||
canvas.dispatchEvent(new Event('webglcontextlost', { cancelable: true }));
|
||||
await nextTick();
|
||||
const fresh = root.children[1];
|
||||
assert.ok(fresh !== canvas, 'a new canvas');
|
||||
assert.equal(fresh.className, 'sl-photo-pano');
|
||||
assert.equal(canvas.isConnected, false, 'the lost one is gone');
|
||||
await viewer.open('p');
|
||||
assert.ok(renderers[1].canvas === fresh, 'the next renderer takes it');
|
||||
});
|
||||
|
||||
test('docked, Esc in the viewer’s box is offered to the provider first', async (t) => {
|
||||
let asked = 0;
|
||||
const { viewer, host } = mount(t, { onEscape: () => ++asked > 0 });
|
||||
await viewer.mount(host);
|
||||
const root = host.querySelector('.sl-photo-viewer');
|
||||
const escape = new KeyboardEvent('keydown', {
|
||||
key: 'Escape',
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
let reachedPage = false;
|
||||
document.body.addEventListener('keydown', () => (reachedPage = true));
|
||||
root.dispatchEvent(escape);
|
||||
assert.equal(asked, 1);
|
||||
assert.equal(escape.defaultPrevented, true);
|
||||
assert.equal(reachedPage, false, 'the panel does not collapse');
|
||||
});
|
||||
|
||||
test('without a view, the viewer cannot mount', async () => {
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
step: async () => null,
|
||||
onStep() {},
|
||||
});
|
||||
await assert.rejects(
|
||||
viewer.mount(document.createElement('div')),
|
||||
/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);
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
<script setup>
|
||||
import { storeToRefs } from 'pinia';
|
||||
import UiButton from '../../kit/UiButton.vue';
|
||||
import { useStreetLevelPhotoStore } from '../../stores/streetLevelPhoto.js';
|
||||
|
||||
// The Street Level photo viewer of the providers whose photos are plain
|
||||
// files (KartaView, Panoramax), in the viewer host the layer mounts it in.
|
||||
// The viewer (src/layers/streetLevel/photoViewer.js) keeps every decision:
|
||||
// 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. 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. */
|
||||
const keep = (name) => (element) => {
|
||||
if (element) photo.value?.actions.attach(name, element.$el ?? element);
|
||||
};
|
||||
// One function per element: a new one on each render would hand it back
|
||||
// again each time.
|
||||
const refs = {
|
||||
root: keep('root'),
|
||||
picture: keep('picture'),
|
||||
canvas: keep('canvas'),
|
||||
prev: keep('prev'),
|
||||
next: keep('next'),
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport v-if="photo" :to="photo.host"
|
||||
><div
|
||||
:key="photo.mountKey"
|
||||
:ref="refs.root"
|
||||
class="sl-photo-viewer"
|
||||
:data-provider="photo.providerId"
|
||||
tabindex="0"
|
||||
role="group"
|
||||
:aria-label="photo.label"
|
||||
:data-kind="photo.kind"
|
||||
@keydown="photo.actions.keydown"
|
||||
><img
|
||||
:ref="refs.picture"
|
||||
class="sl-photo-flat"
|
||||
:alt="photo.alt"
|
||||
draggable="false"
|
||||
:hidden="photo.kind !== 'flat'"
|
||||
:src="photo.src ?? undefined"
|
||||
:style="{ objectFit: photo.fit }" /><canvas
|
||||
:key="photo.canvasKey"
|
||||
:ref="refs.canvas"
|
||||
class="sl-photo-pano"
|
||||
:class="{ 'sl-photo-dragging': photo.dragging }"
|
||||
:hidden="photo.kind !== 'pano'"
|
||||
@pointerdown="photo.actions.pointerdown"
|
||||
@pointermove="photo.actions.pointermove"
|
||||
@pointerup="photo.actions.pointerup"
|
||||
@pointercancel="photo.actions.pointerup"
|
||||
@wheel="photo.actions.wheel"
|
||||
@webglcontextlost="photo.actions.contextLost"
|
||||
></canvas
|
||||
><UiButton
|
||||
:ref="refs.prev"
|
||||
class="sl-photo-step sl-photo-step-prev"
|
||||
:label="photo.prevLabel"
|
||||
:disabled="photo.prevDisabled"
|
||||
@click="photo.actions.step(-1, $event)"
|
||||
>‹</UiButton
|
||||
><UiButton
|
||||
:ref="refs.next"
|
||||
class="sl-photo-step sl-photo-step-next"
|
||||
:label="photo.nextLabel"
|
||||
:disabled="photo.nextDisabled"
|
||||
@click="photo.actions.step(1, $event)"
|
||||
>›</UiButton
|
||||
></div
|
||||
></Teleport
|
||||
>
|
||||
</template>
|
||||
Reference in new issue
Block a user