Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/client/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* Entry point for the page: wires the controls, loads the review and renders it.
*
* - The comment box, teammates' comments and the c shortcut appear only for a pull
* request with gh signed in. Viewed marks work in every case.
* - The comment box and the c shortcut appear only for a pull request. Teammates'
* comments and Post also need gh signed in. Viewed marks work in every case.
* - If a push changed screenshots since they were marked viewed, the sidebar says how
* many lost their mark.
* - The orphans only and encoding only checkboxes appear only when there is one.
* Commented only also appears while a comment can be written.
* - A newer push is checked for on load and whenever the tab regains focus.
* - Every image is prefetched after the first render, so stepping through stays
* instant.
*/
import { renderPublish, wireComments } from './scripts/comments.js';
import { wireControls } from './scripts/controls.js';
import { dom } from './scripts/dom.js';
import { indexGroups, reencodedOnly } from './scripts/entries.js';
import { commented, indexGroups, reencodedOnly } from './scripts/entries.js';
import { applyFilter, renderFacets, restoreGrouping } from './scripts/filters.js';
import { wireKeyboard } from './scripts/keyboard.js';
import { blobUrl } from './scripts/links.js';
Expand Down Expand Up @@ -47,14 +48,15 @@ const init = async () => {
state.posted = new Set(manifest.state.posted ?? []);
state.threads = manifest.threads ?? {};
state.pr = manifest.pr ?? null;
state.prUrl = state.pr ? (manifest.range.url ?? null) : null;
state.prState = manifest.prState ?? null;
state.viewer = manifest.viewer ?? null;

/* A comment exists to be posted on a pull request. Without one, or without an account
to post as, there is nowhere for it to go, so the box would only collect notes that
never leave this machine. Viewed stays either way: it is how a range is worked
through, pull request or not. */
dom.commentSection.hidden = !state.pr || !state.viewer;
/* A comment exists to end up on a pull request. Without one there is nowhere for it
to go, so the box would only collect notes that never leave this machine. Signed
out, it still has a way there: Copy, then paste it into the pull request. Viewed
stays either way: it is how a range is worked through, pull request or not. */
dom.commentSection.hidden = !state.pr;
renderPublish();

/* Only the screenshots that lost their tick are worth saying out loud. Carrying
Expand All @@ -80,6 +82,9 @@ const init = async () => {
is left out of the facet bar. */
dom.orphansOnlyLabel.hidden = !state.entries.some((entry) => entry.orphaned);
dom.reencodedOnlyLabel.hidden = !state.entries.some(reencodedOnly);
/* Also offered while a comment can be written, so the first one has somewhere to be
found without a reload. */
dom.commentedOnlyLabel.hidden = dom.commentSection.hidden && !state.entries.some(commented);

renderFacets();

Expand Down
20 changes: 15 additions & 5 deletions src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<label id="reencodedOnlyLabel" class="check" hidden
><input id="reencodedOnly" type="checkbox" /> Encoding differs only</label
>
<label id="commentedOnlyLabel" class="check" hidden
><input id="commentedOnly" type="checkbox" /> Commented only</label
>
</div>
</div>
<p id="carried" class="carried" hidden></p>
Expand All @@ -63,7 +66,7 @@
<span id="crumbGroup" class="mono dim"></span>
<a id="crumbTest" class="test-link" target="_blank" rel="noreferrer" hidden></a>
<span id="crumbOrphan" class="orphan" hidden><span class="warn"></span>No test writes this screenshot</span>
<span id="crumbStem" class="mono strong"></span>
<a id="crumbStem" class="stem-link mono strong" target="_blank" rel="noreferrer"></a>
<span id="crumbRenamed" class="renamed-from" hidden></span>
<span id="crumbBrowser" class="chip"></span>
<span id="crumbMeta" class="mono dim"></span>
Expand Down Expand Up @@ -141,7 +144,10 @@
<div class="comment-head">
<span>Comment</span>
<span id="commentState" class="dim"></span>
<button id="deleteComment" class="delete-comment" hidden>Delete</button>
<span class="comment-actions">
<button id="copyComment" class="comment-action" hidden>Copy</button>
<button id="deleteComment" class="comment-action delete-comment" hidden>Delete</button>
</span>
</div>
<div id="threads" class="threads" hidden></div>
<textarea id="comment" rows="3" placeholder="Notes on this screenshot. Saved locally as you type."></textarea>
Expand Down Expand Up @@ -224,8 +230,12 @@ <h2>What needs GitHub</h2>
</li>
<li>
Signed out, a pull request still opens and every screenshot is there, read anonymously from GitHub. What
needs an account is switched off: comments, and the copy of the viewed state that reaches the Files tab.
Comments left by teammates do not appear either.
needs an account is switched off: posting comments, and the copy of the viewed state that reaches the Files
tab. Comments left by teammates do not appear either.
</li>
<li>
Signed out, comments are still written and saved locally. <b>Copy</b> puts one on the clipboard, and the
screenshot's name above the image opens it in the Files tab to paste it on.
</li>
<li>
Signed out, a new push is noticed more slowly, because GitHub allows far fewer anonymous requests and the
Expand All @@ -247,7 +257,7 @@ <h2>Keyboard</h2>
<dt><kbd>z</kbd></dt>
<dd>Cycle zoom</dd>
<dt><kbd>c</kbd></dt>
<dd>Focus the comment box, when there is a pull request to post to</dd>
<dd>Focus the comment box, when reviewing a pull request</dd>
<dt><kbd>/</kbd></dt>
<dd>Focus the filter</dd>
<dt><kbd>u</kbd></dt>
Expand Down
43 changes: 33 additions & 10 deletions src/client/scripts/comments.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Comment box, teammates' comments, posting and deleting.
* Comment box, teammates' comments, posting, copying and deleting.
*
* - Shown only for a pull request with gh signed in. Without either, there is
* nowhere for a comment to go, so the whole section is hidden.
* - Shown only for a pull request. Without one there is nowhere for a comment to go,
* so the whole section is hidden.
* - Signed out, comments still save locally but Post is hidden. Copy puts the box's
* text on the clipboard to paste into the pull request by hand.
* - A comment saves locally 400 ms after typing stops. Nothing reaches GitHub until
* Post.
* - Post sends every unsent comment as one review, after confirming. On a merged or
Expand Down Expand Up @@ -170,21 +172,42 @@ export const renderThreads = () => {
};

/**
* Shows Delete when there is a comment, labelled for the pull request when it is posted
* there.
* Shows Copy when the box has text, and Delete when there is a comment, labelled for the
* pull request when it is posted there.
*/
export const renderDelete = () => {
export const renderActions = () => {
const path = state.current;
const published = state.posted.has(path);
const has = Boolean(state.comments.get(path)) || published;

dom.copyComment.hidden = !dom.comment.value.trim();
dom.deleteComment.hidden = !has;
dom.deleteComment.textContent = published ? 'Delete from PR' : 'Delete';
dom.deleteComment.title = published
? `Removes this comment from #${state.pr} as well as here`
: 'Clears this comment';
};

let copiedTimer = null;

/**
* Puts the comment on the clipboard, for pasting into the pull request by hand when
* there is no account to post it with.
* @returns {Promise<void>}
*/
export const copyCurrentComment = async () => {
try {
await navigator.clipboard.writeText(dom.comment.value);
dom.copyComment.textContent = 'Copied';
clearTimeout(copiedTimer);
copiedTimer = setTimeout(() => {
dom.copyComment.textContent = 'Copy';
}, 1500);
} catch (error) {
dom.commentState.textContent = `Could not copy: ${error.message ?? error}`;
}
};

/**
* Clears the selected screenshot's comment. One already posted is deleted from the pull
* request too, after confirming.
Expand Down Expand Up @@ -225,12 +248,12 @@ export const deleteCurrentComment = async () => {
refreshRow(path);
renderPublish();
} catch (error) {
/* Reported on the status line, not the button's title: renderDelete rewrites that
/* Reported on the status line, not the button's title: renderActions rewrites that
title on the way out, so a failure put there would vanish silently. */
dom.commentState.textContent = `Could not delete: ${error.message ?? error}`;
} finally {
dom.deleteComment.disabled = false;
renderDelete();
renderActions();
}
};

Expand Down Expand Up @@ -284,7 +307,7 @@ export const publishComments = async () => {
}
}
renderPublish();
renderDelete();
renderActions();

if (result.url) {
window.open(result.url, '_blank', 'noreferrer');
Expand Down Expand Up @@ -320,7 +343,7 @@ export const wireComments = () => {
state.unsent.delete(path);
}
renderPublish();
renderDelete();
renderActions();

dom.commentState.textContent = 'Saving…';
clearTimeout(commentTimer);
Expand Down
6 changes: 5 additions & 1 deletion src/client/scripts/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* shortcuts keep working.
* - Clicking outside the help dialog closes it. Fit zoom recalculates on resize.
*/
import { deleteCurrentComment, publishComments } from './comments.js';
import { copyCurrentComment, deleteCurrentComment, publishComments } from './comments.js';
import { dom } from './dom.js';
import { entryFor, setKeyOf } from './entries.js';
import { GROUP_KEY, applyFilter, refilter, renderFacets } from './filters.js';
Expand Down Expand Up @@ -125,6 +125,8 @@ export const wireControls = () => {

dom.reencodedOnly.addEventListener('change', refilter);

dom.commentedOnly.addEventListener('change', refilter);

/* Focus would otherwise stay on the checkbox, where the review shortcuts are treated
as typing and swallowed. */
dom.hideViewed.addEventListener('change', () => {
Expand All @@ -137,6 +139,8 @@ export const wireControls = () => {
dom.theme.blur();
});

dom.copyComment.addEventListener('click', copyCurrentComment);

dom.deleteComment.addEventListener('click', deleteCurrentComment);

dom.publish.addEventListener('click', publishComments);
Expand Down
9 changes: 6 additions & 3 deletions src/client/scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const IDS = /** @type {const} */ ([
'orphansOnlyLabel',
'reencodedOnly',
'reencodedOnlyLabel',
'commentedOnly',
'commentedOnlyLabel',
'facets',
'carried',
'pushed',
Expand Down Expand Up @@ -66,16 +68,17 @@ const IDS = /** @type {const} */ ([
'comment',
'commentSection',
'commentState',
'copyComment',
'deleteComment',
'threads',
]);

/**
* Every id as an HTMLElement, narrowed where scripts use more than that offers.
* @typedef {Record<(typeof IDS)[number], HTMLElement>
* & Record<'rangeLabel' | 'crumbTest', HTMLAnchorElement>
* & Record<'helpButton' | 'publish' | 'deleteComment', HTMLButtonElement>
* & Record<'groupBrowsers' | 'filter' | 'hideViewed' | 'orphansOnly' | 'reencodedOnly' | 'viewed' | 'onion', HTMLInputElement>
* & Record<'rangeLabel' | 'crumbTest' | 'crumbStem', HTMLAnchorElement>
* & Record<'helpButton' | 'publish' | 'copyComment' | 'deleteComment', HTMLButtonElement>
* & Record<'groupBrowsers' | 'filter' | 'hideViewed' | 'orphansOnly' | 'reencodedOnly' | 'commentedOnly' | 'viewed' | 'onion', HTMLInputElement>
* & Record<'sxsExpected' | 'sxsActual' | 'sliderExpected' | 'sliderActual' | 'onionSizer' | 'onionExpected' | 'onionActual' | 'diffImage', HTMLImageElement>
* & { help: HTMLDialogElement, theme: HTMLSelectElement, comment: HTMLTextAreaElement }} Dom
*/
Expand Down
9 changes: 9 additions & 0 deletions src/client/scripts/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export const worthComparing = (entry) => Boolean(entry?.comparable) && entry.rat
export const reencodedOnly = (entry) =>
Boolean(entry?.comparable) && entry.ratio === 0 && entry.expectedSha !== entry.actualSha;

/**
* Whether anyone has said something about a screenshot: a comment of mine, posted or
* not, or a thread on the pull request.
* @param {Entry | undefined} entry
* @returns {boolean}
*/
export const commented = (entry) =>
Boolean(entry) && (Boolean(state.comments.get(entry.path)) || (state.threads[entry.path] ?? []).length > 0);

/** Indexes every entry by directory and by browser set. */
export const indexGroups = () => {
state.groups = new Map();
Expand Down
9 changes: 7 additions & 2 deletions src/client/scripts/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* Values combine within a row, and a screenshot must match every row in use.
* - A row with only one value is left out, except status. Theme is left out when it
* repeats mode, which is always the case on main.
* - Text, facets, orphans only and encoding only decide what a directory covers.
* - Text, facets, orphans only, encoding only and commented only decide what a
* directory covers. Commented only counts my comments and teammates' threads.
* Hide viewed just hides rows, so directory counts stay whole.
* - One row per test collapses each browser set to one row, led by Chrome, and is
* remembered in this browser.
* - When the open screenshot is filtered out, the first visible one opens instead.
*/
import { BROWSER_ORDER, STATUS_COUNT_LABEL, dom } from './dom.js';
import { reencodedOnly, setKeyOf, setLead, shortBrowser } from './entries.js';
import { commented, reencodedOnly, setKeyOf, setLead, shortBrowser } from './entries.js';
import { renderList } from './list.js';
import { select } from './stage.js';
import { state } from './state.js';
Expand Down Expand Up @@ -130,6 +131,10 @@ export const applyFilter = () => {
return false;
}

if (dom.commentedOnly.checked && !commented(entry)) {
return false;
}

for (const facet of FACETS) {
const selected = state.facets[facet.key];
if (selected.size && !selected.has(entry[facet.key])) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/scripts/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const wireKeyboard = () => {
applyZoom();
},
c: () => {
if (state.pr && state.viewer) {
if (state.pr) {
dom.comment.focus();
}
},
Expand Down
40 changes: 40 additions & 0 deletions src/client/scripts/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* gets plain captions and no test link.
* - The test link opens the spec at the line that took the screenshot, or the whole
* spec when the line can't be found. It is hidden when the spec is gone.
* - The screenshot's name links it in the pull request's Files tab, where a copied
* comment can be pasted. Without a pull request it is plain text.
* - Image URLs carry the head commit, so after Refresh the browser never shows an
* image cached from the previous push.
*/
Expand Down Expand Up @@ -47,6 +49,44 @@ export const setTestLink = (entry) => {
: 'Open the spec that took this screenshot. The exact line could not be placed.';
};

/**
* The hex SHA-256 of a path, which is how GitHub anchors a file in a pull request's
* Files tab.
* @param {string} path
* @returns {Promise<string>}
*/
const diffAnchor = async (path) => {
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(path));
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
};

/**
* Links the screenshot's name to it in the pull request's Files tab, so a copied
* comment can be pasted on it. Plain text when there is no pull request.
* @param {Entry} entry
* @returns {Promise<void>}
*/
export const setPrLink = async (entry) => {
/* Cleared first, so the name never points at the previous screenshot while the
hash for this one is worked out. */
dom.crumbStem.removeAttribute('href');
dom.crumbStem.title = '';

if (!state.prUrl) {
return;
}

const anchor = await diffAnchor(entry.path);

/* Hashing is async, so a quick step through the list can finish out of order. */
if (state.current !== entry.path) {
return;
}

dom.crumbStem.href = `${state.prUrl}/files#diff-${anchor}`;
dom.crumbStem.title = `Open this screenshot in #${state.pr}'s Files tab`;
};

const CAPTION = { M: ['Expected', 'Actual'], R: ['Expected', 'Actual'], A: [null, 'Added'], D: ['Removed', null] };

/**
Expand Down
7 changes: 4 additions & 3 deletions src/client/scripts/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* - Fit zoom scales up to 3x. 1:1 and 2x use the screenshot's own size.
* - Opening a screenshot also loads its viewed tick, comment and teammates' comments.
*/
import { renderDelete, renderThreads } from './comments.js';
import { renderActions, renderThreads } from './comments.js';
import { COMPARING, STATUS_LABEL, VIEWS, dom } from './dom.js';
import { entryFor, worthComparing } from './entries.js';
import { kb, percent } from './format.js';
import { blobUrl, setCaptionLink, setTestLink } from './links.js';
import { blobUrl, setCaptionLink, setPrLink, setTestLink } from './links.js';
import { state } from './state.js';

/** @typedef {import('./state.js').Entry} Entry */
Expand Down Expand Up @@ -203,7 +203,8 @@ export const select = (path, { scroll = true } = {}) => {
dom.viewed.checked = state.viewed.has(entry.path);
dom.comment.value = state.comments.get(entry.path) ?? '';
renderThreads();
renderDelete();
renderActions();
setPrLink(entry);
dom.commentState.textContent = state.stale.has(entry.path) ? 'Written before this screenshot changed' : '';
dom.comment.classList.toggle('stale', state.stale.has(entry.path));

Expand Down
Loading
Loading