fix(editor): five locals in stepHistory and MoreTabs, sync tag in tests

This commit is contained in:
bchanot committed 2026-11-14 18:52:41 +01:00
1 parent 08fab77d0e
commit bb35e86dbb
5 files changed
+48 -35

No files matched your search

+13 -9
View File
@@ -148,28 +148,32 @@ function MoreList({ more, active, onPick }: MoreListProps) {
}
/**
* The panel of a tab picked in « Plus » takes the focus once that tab is the shown one: the
* entry that held it is gone with the list, so the focus would fall to the page.
* The choice of a tab in « Plus »: the menu closes, and the panel of the tab picked takes
* the focus once that tab is the shown one (the entry that held it is gone with the list,
* so the focus would fall to the page).
*/
function usePanelFocus(active: EditorTabId) {
function usePanelPick(
active: EditorTabId,
onSelect: MoreTabsProps['onSelect'],
close: () => void,
) {
const [wanted, setWanted] = useState<EditorTabId | null>(null)
useEffect(() => {
if (wanted === null || wanted !== active) return
document.getElementById(panelDomId(active))?.focus()
setWanted(null)
}, [wanted, active])
return setWanted
return (tab: EditorTabId) => {
if (onSelect(tab) !== false) setWanted(tab)
close()
}
}
/** Below 640 px: « Plus » and its dropdown of the tabs that do not fit. */
function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
const t = useT()
const { open, close, toggle } = useMoreMenu(active)
const wantPanel = usePanelFocus(active)
const pick = (tab: EditorTabId) => {
if (onSelect(tab) !== false) wantPanel(tab)
close()
}
const pick = usePanelPick(active, onSelect, close)
useMoreDismiss(open, close)
return (
<>
+11 -2
View File
@@ -69,6 +69,16 @@ export function commit<R>(
return commitBatch(api, [command], accept)
}
/** A step's patch with the finding it brought back, revealed (null when none). */
function withReveal(
patch: Partial<EditorState>,
before: string,
after: string,
): Partial<EditorState> {
const key = appearedFinding(before, after)
return { ...patch, revealed: key ? { key } : null }
}
export function stepHistory(
api: StoreApi,
direction: 'undo' | 'redo',
@@ -82,8 +92,7 @@ export function stepHistory(
const replaced = preview.replaceDocument(step.text)
if (!replaced.ok) return replaced
const patch = replacedPatch(state, step.text, replaced.state, step.history)
const key = appearedFinding(state.documentText, step.text)
api.set({ ...patch, revealed: key ? { key } : null })
api.set(withReveal(patch, state.documentText, step.text))
return { ok: true }
}
@@ -58,9 +58,9 @@ const back = () => act(async () => navigate(-1))
/** The session tag of USER in this tab: entries and flags carry it (criterion 22). */
let TAG: string
beforeEach(async () => {
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
TAG = await resolveSessionTag(USER)
TAG = resolveSessionTag(USER)
})
afterEach(() => {
cleanup()
+2 -2
View File
@@ -56,8 +56,8 @@ const foreign = (patch: Partial<HistoryEntry>) => ({
/** The session tag of USER in this tab (criterion 22). */
let TAG: string
beforeEach(async () => {
TAG = await resolveSessionTag(USER)
beforeEach(() => {
TAG = resolveSessionTag(USER)
useLocaleStore.setState({ locale: 'fr' })
serverHas(emptyText(), 3, false)
})
+20 -20
View File
@@ -28,8 +28,8 @@ afterEach(() => {
})
describe('resolveSessionTag', () => {
it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', async () => {
const tag = await resolveSessionTag('account-a')
it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', () => {
const tag = resolveSessionTag('account-a')
expect(tag).toMatch(/^[0-9a-f]{32}$/)
const record = stored()
expect(record?.tag).toBe(tag)
@@ -37,37 +37,37 @@ describe('resolveSessionTag', () => {
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
})
it('reuses the tag for the same account, after a reload too', async () => {
const tag = await resolveSessionTag('account-a')
expect(await resolveSessionTag('account-a')).toBe(tag)
it('reuses the tag for the same account, after a reload too', () => {
const tag = resolveSessionTag('account-a')
expect(resolveSessionTag('account-a')).toBe(tag)
reload()
expect(await resolveSessionTag('account-a')).toBe(tag)
expect(resolveSessionTag('account-a')).toBe(tag)
})
it('rotates on another account and clears the flags of the old tag', async () => {
const old = await resolveSessionTag('account-a')
it('rotates on another account and clears the flags of the old tag', () => {
const old = resolveSessionTag('account-a')
const flag = leftDraftKey({ sheetId: 'S', tag: old })
sessionStorage.setItem(flag, '{}')
reload()
const tag = await resolveSessionTag('account-b')
const tag = resolveSessionTag('account-b')
expect(tag).not.toBe(old)
expect(sessionStorage.getItem(flag)).toBeNull()
reload()
expect(await resolveSessionTag('account-a')).not.toBe(old)
expect(resolveSessionTag('account-a')).not.toBe(old)
})
it('keeps the tag in memory when the storage refuses: a reload restores nothing', async () => {
it('keeps the tag in memory when the storage refuses: a reload restores nothing', () => {
vi.spyOn(sessionStorage, 'getItem').mockImplementation(() => {
throw new Error('blocked')
})
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
throw new Error('blocked')
})
const tag = await resolveSessionTag('account-a')
const tag = resolveSessionTag('account-a')
expect(tag).toMatch(/^[0-9a-f]{32}$/)
expect(await resolveSessionTag('account-a')).toBe(tag)
expect(resolveSessionTag('account-a')).toBe(tag)
reload()
expect(await resolveSessionTag('account-a')).not.toBe(tag)
expect(resolveSessionTag('account-a')).not.toBe(tag)
})
it('works without crypto.subtle (http): kept on reload, rotated per account', () => {
@@ -93,9 +93,9 @@ describe('resolveSessionTag', () => {
expect(typeof resolveSessionTag('account-a')).toBe('string')
})
it('reads a tampered record as none', async () => {
it('reads a tampered record as none', () => {
sessionStorage.setItem(STORAGE_KEY, '{"tag":"x","account":"y"}')
const tag = await resolveSessionTag('account-a')
const tag = resolveSessionTag('account-a')
expect(tag).toMatch(/^[0-9a-f]{32}$/)
expect(stored()?.tag).toBe(tag)
})
@@ -137,14 +137,14 @@ describe('the sweep of flags keyed by an account id (older shape)', () => {
})
describe('forgetSessionTag and the sign-out', () => {
it('rotates the tag of the same account and clears its flags', async () => {
const old = await resolveSessionTag('account-a')
it('rotates the tag of the same account and clears its flags', () => {
const old = resolveSessionTag('account-a')
const flag = leftDraftKey({ sheetId: 'S', tag: old })
sessionStorage.setItem(flag, '{}')
forgetSessionTag()
expect(stored()).toBeNull()
expect(sessionStorage.getItem(flag)).toBeNull()
expect(await resolveSessionTag('account-a')).not.toBe(old)
expect(resolveSessionTag('account-a')).not.toBe(old)
})
it('follows the account: a sign-out forgets, a sign-in resolves', async () => {
@@ -158,6 +158,6 @@ describe('forgetSessionTag and the sign-out', () => {
expect(useSessionTagStore.getState().held).toBeNull()
expect(stored()).toBeNull()
stop()
expect(await resolveSessionTag('account-a')).not.toBe(old)
expect(resolveSessionTag('account-a')).not.toBe(old)
})
})