Address Stories review feedback

This commit is contained in:
Manjunath committed 2026-11-16 22:35:07 +05:30
1 parent 2edcd5bc69
commit fec84dbf78
48 files changed
+783 -232

No files matched your search

+31 -1
View File
@@ -26,6 +26,7 @@ import {
cameraOf,
canvasOf,
findCamera,
findCredits,
findFrame,
} from '../render/rendererOf.js';
import {
@@ -455,6 +456,7 @@ export class SceneDirector {
hasRun: !!this._lastRunJson,
}),
actions: {
selectScene: (id) => this.selectShot(id),
selectShot: (id) => {
this._selectedShotId = id;
this._publish({ type: 'selection-changed' });
@@ -637,11 +639,29 @@ export class SceneDirector {
async captureSceneThumb() {
const scene = this._getSelectedScene();
if (!scene || this._destroyed) return false;
const canCapture = () => {
const map = this.styleManager.mapStackController?.getState();
return map?.activeId === 'esri-imagery' && map.status === 'ready';
};
if (!canCapture()) {
this._updateStatus(() =>
t('Switch to Esri World Imagery to capture a thumbnail'),
);
return false;
}
let thumb;
try {
thumb = await captureStoryThumb(
{ frame: findFrame(this.viewer), canvas: canvasOf(this.viewer) },
{ createCanvas: () => document.createElement('canvas') },
{
createCanvas: () => document.createElement('canvas'),
creditForFrame: () =>
canCapture()
? findCredits(this.viewer)
?.list()
.find(({ text }) => text.startsWith('Powered by Esri'))?.text
: null,
},
);
} catch (error) {
console.warn('[Scenes] Could not capture the thumbnail:', error);
@@ -649,6 +669,11 @@ export class SceneDirector {
this._updateStatus(() => t('Thumbnail could not be captured'));
return false;
}
if (!thumb) {
if (!this._destroyed)
this._updateStatus(() => t('Thumbnail could not be captured'));
return false;
}
// The scene may have gone while the frame was drawn.
if (this._destroyed || !this._project.scenes.includes(scene)) return false;
return this.setSceneStoryThumb(scene.id, thumb);
@@ -2121,6 +2146,11 @@ export class SceneDirector {
return this._running;
}
/** Whether an existing run, LOAD, seek or interaction still owns work. */
get busy() {
return this.running || Boolean(this._pendingWork?.size);
}
/**
* Start a full scene run beginning at the given scene.
*
+37 -7
View File
@@ -1905,7 +1905,7 @@ test('unsupported stored documents cannot be overwritten by fallback edits', asy
assert.equal(localStorage.getItem('godsEyeView.sceneProject.v2'), saved);
await director.importProjectFile({ name: 'valid.json', text: async () => JSON.stringify(PROJECT_FIXTURE) });
assert.equal(director._storageReadError, null);
assert.equal(JSON.parse(localStorage.getItem('godsEyeView.sceneProject.v2')).version, 6);
assert.equal(JSON.parse(localStorage.getItem('godsEyeView.sceneProject.v2')).version, 7);
} finally { restore(); }
});
@@ -2499,7 +2499,8 @@ test('story thumbnail and tags survive a reload and an export/import round trip'
try {
first.director.setSceneStoryThumb('scene-1', THUMB);
first.director.setSceneStoryTags('scene-1', ['Storms']);
saved = localStorage.getItem('godsEyeView.sceneProject.v2');
// Draft builds wrote the same optional Stories fields in version 6.
saved = JSON.stringify({ ...savedProject(), version: 6 });
} finally {
first.restore();
}
@@ -2511,6 +2512,7 @@ test('story thumbnail and tags survive a reload and an export/import round trip'
assert.equal(scene.storyThumb, THUMB);
assert.deepEqual(scene.storyTags, ['Storms']);
const exported = stringifySceneDocument(director._project);
assert.equal(JSON.parse(exported).version, 7);
director.setSceneStoryTags('scene-1', []);
director.setSceneStoryThumb('scene-1', 'data:image/png;base64,AAAA');
assert.equal(
@@ -2565,8 +2567,8 @@ test('imports with a malformed thumbnail or tags are refused', async () => {
}
});
test('THUMBNAIL stores the frame read in postRender on the selected scene', async () => {
const { director, viewer, restore } = makeDirector();
test('THUMBNAIL stores only a credited Esri frame on the selected scene', async () => {
const { director, viewer, styleManager, restore } = makeDirector();
const createElement = document.createElement;
try {
assert.equal(
@@ -2575,26 +2577,54 @@ test('THUMBNAIL stores the frame read in postRender on the selected scene', asyn
'no scene, no capture',
);
director.selectShot('scene-1');
let activeId = 'photoreal';
styleManager.mapStackController = {
getState: () => ({ activeId, status: 'ready' }),
};
// The renderer's frame port: postRender runs on the frame it is asked for.
const listeners = new Set();
const renderer = rendererOf(viewer);
viewer.canvas = { width: 640, height: 360 };
renderer.credits = {
list: () => [{ text: 'Powered by Esri — Source: Esri and Maxar' }],
};
renderer.frame = {
on(phase, listener) {
assert.equal(phase, 'postRender');
listeners.add(listener);
return () => listeners.delete(listener);
},
requestRender: () => [...listeners].forEach((listener) => listener({})),
requestRender() {},
};
const frame = () => [...listeners].forEach((listener) => listener());
const credit = [];
document.createElement = () => ({
getContext: () => ({ drawImage() {} }),
getContext: () => ({
drawImage() {},
fillRect() {},
fillText: (line) => credit.push(line),
}),
toDataURL: () => THUMB,
});
assert.equal(await director.captureSceneThumb(), true);
assert.equal(await director.captureSceneThumb(), false);
assert.equal(listeners.size, 0);
activeId = 'esri-imagery';
const capturing = director.captureSceneThumb();
frame();
assert.equal(await capturing, true);
assert.ok(credit.some((line) => /Esri/.test(line)));
assert.equal(director._project.scenes[0].storyThumb, THUMB);
assert.equal(director._presentation.status, 'Thumbnail captured');
const switching = director.captureSceneThumb();
activeId = 'photoreal';
credit.length = 0;
frame();
assert.equal(await switching, false);
assert.deepEqual(credit, []);
assert.equal(director._project.scenes[0].storyThumb, THUMB);
activeId = 'esri-imagery';
delete renderer.frame;
assert.equal(await director.captureSceneThumb(), false);
assert.equal(
+24 -3
View File
@@ -54,9 +54,10 @@ export function centreCrop(
* URL, halving in steps so a large canvas downscales smoothly.
* @param {HTMLCanvasElement} source
* @param {() => HTMLCanvasElement} createCanvas Makes an empty canvas.
* @param {string} [credit] Attribution to include in an exported thumbnail.
* @returns {string}
*/
export function encodeStoryThumb(source, createCanvas) {
export function encodeStoryThumb(source, createCanvas, credit = '') {
const crop = centreCrop(source.width, source.height);
if (!crop.width || !crop.height) throw new Error('Nothing to capture');
let image = source;
@@ -98,6 +99,19 @@ export function encodeStoryThumb(source, createCanvas) {
STORY_THUMB_WIDTH,
STORY_THUMB_HEIGHT,
);
if (credit) {
const lines = credit.split(' — ');
const height = lines.length * 11 + 6;
const top = STORY_THUMB_HEIGHT - height;
context.fillStyle = 'rgba(0, 0, 0, 0.8)';
context.fillRect(0, top, STORY_THUMB_WIDTH, height);
context.fillStyle = '#fff';
context.font = '9px sans-serif';
context.textBaseline = 'top';
lines.forEach((line, i) =>
context.fillText(line, 5, top + 3 + i * 11, STORY_THUMB_WIDTH - 10),
);
}
// A very busy view can exceed the limit; lower the quality until it fits.
for (const quality of [STORY_THUMB_QUALITY, 0.7, 0.55, 0.4]) {
const url = out.toDataURL('image/jpeg', quality);
@@ -116,12 +130,14 @@ export function encodeStoryThumb(source, createCanvas) {
* the canvas it draws in, as the caller found them for its scene.
* @param {object} options
* @param {() => HTMLCanvasElement} options.createCanvas Makes an empty canvas.
* @param {() => string | null | undefined} [options.creditForFrame] If supplied,
* the frame must have this attribution before the thumbnail can be copied.
* @param {number} [options.timeoutMs] Give up when no frame is drawn.
* @returns {Promise<string>} JPEG data URL.
*/
export function captureStoryThumb(
{ frame, canvas } = {},
{ createCanvas, timeoutMs = 3000 },
{ createCanvas, creditForFrame, timeoutMs = 3000 },
) {
if (!frame || !canvas) return Promise.reject(new Error('Globe not ready'));
return new Promise((resolve, reject) => {
@@ -130,7 +146,12 @@ export function captureStoryThumb(
remove();
clearTimeout(timer);
try {
resolve(encodeStoryThumb(canvas, createCanvas));
const credit = creditForFrame?.();
resolve(
creditForFrame && !credit
? ''
: encodeStoryThumb(canvas, createCanvas, credit),
);
} catch (error) {
reject(error);
}