diff --git a/src/client/app.js b/src/client/app.js index 01f0ab1..a741b2a 100644 --- a/src/client/app.js +++ b/src/client/app.js @@ -1,11 +1,12 @@ /* * 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. @@ -13,7 +14,7 @@ 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'; @@ -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 @@ -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(); diff --git a/src/client/index.html b/src/client/index.html index aa6812f..65c90e3 100644 --- a/src/client/index.html +++ b/src/client/index.html @@ -49,6 +49,9 @@ + @@ -63,7 +66,7 @@ - + @@ -141,7 +144,10 @@
Comment - + + + +
@@ -224,8 +230,12 @@

What needs GitHub

  • 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. +
  • +
  • + Signed out, comments are still written and saved locally. Copy puts one on the clipboard, and the + screenshot's name above the image opens it in the Files tab to paste it on.
  • Signed out, a new push is noticed more slowly, because GitHub allows far fewer anonymous requests and the @@ -247,7 +257,7 @@

    Keyboard

    z
    Cycle zoom
    c
    -
    Focus the comment box, when there is a pull request to post to
    +
    Focus the comment box, when reviewing a pull request
    /
    Focus the filter
    u
    diff --git a/src/client/scripts/comments.js b/src/client/scripts/comments.js index 6ed889e..647dedd 100644 --- a/src/client/scripts/comments.js +++ b/src/client/scripts/comments.js @@ -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 @@ -170,14 +172,15 @@ 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 @@ -185,6 +188,26 @@ export const renderDelete = () => { : '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} + */ +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. @@ -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(); } }; @@ -284,7 +307,7 @@ export const publishComments = async () => { } } renderPublish(); - renderDelete(); + renderActions(); if (result.url) { window.open(result.url, '_blank', 'noreferrer'); @@ -320,7 +343,7 @@ export const wireComments = () => { state.unsent.delete(path); } renderPublish(); - renderDelete(); + renderActions(); dom.commentState.textContent = 'Saving…'; clearTimeout(commentTimer); diff --git a/src/client/scripts/controls.js b/src/client/scripts/controls.js index a17c818..5f28692 100644 --- a/src/client/scripts/controls.js +++ b/src/client/scripts/controls.js @@ -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'; @@ -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', () => { @@ -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); diff --git a/src/client/scripts/dom.js b/src/client/scripts/dom.js index 6f2745a..07c7323 100644 --- a/src/client/scripts/dom.js +++ b/src/client/scripts/dom.js @@ -21,6 +21,8 @@ const IDS = /** @type {const} */ ([ 'orphansOnlyLabel', 'reencodedOnly', 'reencodedOnlyLabel', + 'commentedOnly', + 'commentedOnlyLabel', 'facets', 'carried', 'pushed', @@ -66,6 +68,7 @@ const IDS = /** @type {const} */ ([ 'comment', 'commentSection', 'commentState', + 'copyComment', 'deleteComment', 'threads', ]); @@ -73,9 +76,9 @@ const IDS = /** @type {const} */ ([ /** * 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 */ diff --git a/src/client/scripts/entries.js b/src/client/scripts/entries.js index 6557b1c..0bf7826 100644 --- a/src/client/scripts/entries.js +++ b/src/client/scripts/entries.js @@ -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(); diff --git a/src/client/scripts/filters.js b/src/client/scripts/filters.js index 337ce54..4b6f95f 100644 --- a/src/client/scripts/filters.js +++ b/src/client/scripts/filters.js @@ -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'; @@ -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])) { diff --git a/src/client/scripts/keyboard.js b/src/client/scripts/keyboard.js index 570acef..eb8e589 100644 --- a/src/client/scripts/keyboard.js +++ b/src/client/scripts/keyboard.js @@ -59,7 +59,7 @@ export const wireKeyboard = () => { applyZoom(); }, c: () => { - if (state.pr && state.viewer) { + if (state.pr) { dom.comment.focus(); } }, diff --git a/src/client/scripts/links.js b/src/client/scripts/links.js index cc71dd1..e784e23 100644 --- a/src/client/scripts/links.js +++ b/src/client/scripts/links.js @@ -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. */ @@ -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} + */ +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} + */ +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] }; /** diff --git a/src/client/scripts/stage.js b/src/client/scripts/stage.js index 0a1dde9..78b2653 100644 --- a/src/client/scripts/stage.js +++ b/src/client/scripts/stage.js @@ -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 */ @@ -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)); diff --git a/src/client/scripts/state.js b/src/client/scripts/state.js index 58ebac3..a2d8be9 100644 --- a/src/client/scripts/state.js +++ b/src/client/scripts/state.js @@ -54,6 +54,8 @@ export const state = { /* Comments that are on the pull request right now. */ posted: new Set(), pr: null, + /* The pull request on github.com, for linking a screenshot into its Files tab. */ + prUrl: null, /* OPEN, CLOSED or MERGED. Only an open pull request takes new comments. */ prState: null, /* The GitHub account gh is logged in as, or null when it has none. */ diff --git a/src/client/styles/comment.css b/src/client/styles/comment.css index 5d55f15..b5dd9f4 100644 --- a/src/client/styles/comment.css +++ b/src/client/styles/comment.css @@ -1,8 +1,14 @@ -/* Comment box, teammate threads, and the publish and delete buttons. */ +/* Comment box, teammate threads, and the publish, copy and delete buttons. */ + +.comment-actions { + display: flex; -.delete-comment { margin-left: auto; + gap: 6px; +} + +.comment-action { padding: 1px 8px; border: 1px solid var(--line); @@ -17,13 +23,19 @@ cursor: pointer; } +.comment-action:hover:not(:disabled) { + border-color: var(--accent); + + color: var(--accent); +} + .delete-comment:hover:not(:disabled) { border-color: var(--removed); color: var(--removed); } -.delete-comment[hidden] { +.comment-action[hidden] { display: none; } @@ -76,9 +88,9 @@ font-size: 11px; font-weight: 500; - /* An explicit gap, because the delete button's auto margin swallows the free space - that space-between would otherwise distribute, leaving the label and the status - touching the moment that button appears. */ + /* An explicit gap, because the buttons' auto margin swallows the free space that + space-between would otherwise distribute, leaving the label and the status + touching the moment a button appears. */ gap: 10px; } diff --git a/src/client/styles/crumbs.css b/src/client/styles/crumbs.css index a849127..ed61cf9 100644 --- a/src/client/styles/crumbs.css +++ b/src/client/styles/crumbs.css @@ -97,6 +97,20 @@ text-decoration: underline; } +.stem-link { + color: inherit; + + text-decoration: none; +} + +/* Only a link while reviewing a pull request. Without one it has no href and reads as + plain text. */ +.stem-link[href]:hover { + color: var(--accent); + + text-decoration: underline; +} + .test-link[hidden] { display: none; } diff --git a/test/e2e/review.e2e.js b/test/e2e/review.e2e.js index 72d4c86..5102393 100644 --- a/test/e2e/review.e2e.js +++ b/test/e2e/review.e2e.js @@ -1,11 +1,14 @@ import { expect, test } from '@playwright/test'; import { spawn } from 'node:child_process'; +import { createHash } from 'node:crypto'; import { fileURLToPath } from 'node:url'; import { FIXTURE, createFixtureRepo } from '../fixtures/repo.js'; const CLI = fileURLToPath(new URL('../../src/cli.mjs', import.meta.url)); +const PR_URL = 'https://github.com/ionic-team/ionic-framework/pull/1'; + /* One server for the file, so the tests share it and run in order. */ test.describe.configure({ mode: 'serial' }); @@ -110,3 +113,56 @@ test('should mark a row viewed from its checkbox without opening it', async ({ p await page.reload(); await expect(page.locator('.row').nth(1).locator('.row-box')).toBeChecked(); }); + +test('should narrow to commented snapshots', async ({ page }) => { + /* A range review has no comment box, so the checkbox waits for a saved comment. */ + await expect(page.getByLabel('Commented only')).toBeHidden(); + + await page.request.post(`${address}/api/state`, { data: { path: FIXTURE.added, comment: 'Looks off' } }); + /* The store writes after a short debounce, so give it time before reloading. */ + await page.waitForTimeout(400); + await page.reload(); + + await page.getByLabel('Commented only').check(); + await expect(page.locator('.row')).toHaveCount(1); + await expect(page.locator(`.row[data-path="${FIXTURE.added}"]`)).toBeVisible(); +}); + +/** + * Makes the page believe it is reviewing pull request #1 signed out: a manifest with a + * pr and no viewer. The fixture can't open a real one offline, so the manifest is + * rewritten instead. + * @param {import('@playwright/test').Page} page + */ +const signOutOfPullRequest = async (page) => { + await page.route('**/api/manifest', async (route) => { + const manifest = await (await route.fetch()).json(); + manifest.pr = 1; + manifest.viewer = null; + manifest.range.url = PR_URL; + manifest.state.comments = { [FIXTURE.modified]: 'Band is too tall' }; + await route.fulfill({ json: manifest }); + }); + await page.reload(); +}; + +test('should copy a comment when signed out of a pull request', async ({ page }) => { + await page.context().grantPermissions(['clipboard-read', 'clipboard-write']); + await signOutOfPullRequest(page); + + await page.locator(`.row[data-path="${FIXTURE.modified}"]`).click(); + await expect(page.locator('#publish')).toBeHidden(); + + await page.getByRole('button', { name: 'Copy' }).click(); + await expect(page.locator('#copyComment')).toHaveText('Copied'); + expect(await page.evaluate(() => navigator.clipboard.readText())).toBe('Band is too tall'); +}); + +test('should link the open screenshot into the pull request Files tab', async ({ page }) => { + await signOutOfPullRequest(page); + + await page.locator(`.row[data-path="${FIXTURE.added}"]`).click(); + + const anchor = createHash('sha256').update(FIXTURE.added).digest('hex'); + await expect(page.locator('#crumbStem')).toHaveAttribute('href', `${PR_URL}/files#diff-${anchor}`); +});