fix(editor): five locals in stepHistory and MoreTabs, sync tag in tests
This commit is contained in:
1 parent
08fab77d0e
commit
bb35e86dbb
5 files changed
+48
-35
No files matched your search
@@ -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
|
* The choice of a tab in « Plus »: the menu closes, and the panel of the tab picked takes
|
||||||
* entry that held it is gone with the list, so the focus would fall to the page.
|
* 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)
|
const [wanted, setWanted] = useState<EditorTabId | null>(null)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (wanted === null || wanted !== active) return
|
if (wanted === null || wanted !== active) return
|
||||||
document.getElementById(panelDomId(active))?.focus()
|
document.getElementById(panelDomId(active))?.focus()
|
||||||
setWanted(null)
|
setWanted(null)
|
||||||
}, [wanted, active])
|
}, [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. */
|
/** Below 640 px: « Plus » and its dropdown of the tabs that do not fit. */
|
||||||
function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
|
function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const { open, close, toggle } = useMoreMenu(active)
|
const { open, close, toggle } = useMoreMenu(active)
|
||||||
const wantPanel = usePanelFocus(active)
|
const pick = usePanelPick(active, onSelect, close)
|
||||||
const pick = (tab: EditorTabId) => {
|
|
||||||
if (onSelect(tab) !== false) wantPanel(tab)
|
|
||||||
close()
|
|
||||||
}
|
|
||||||
useMoreDismiss(open, close)
|
useMoreDismiss(open, close)
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -69,6 +69,16 @@ export function commit<R>(
|
|||||||
return commitBatch(api, [command], accept)
|
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(
|
export function stepHistory(
|
||||||
api: StoreApi,
|
api: StoreApi,
|
||||||
direction: 'undo' | 'redo',
|
direction: 'undo' | 'redo',
|
||||||
@@ -82,8 +92,7 @@ export function stepHistory(
|
|||||||
const replaced = preview.replaceDocument(step.text)
|
const replaced = preview.replaceDocument(step.text)
|
||||||
if (!replaced.ok) return replaced
|
if (!replaced.ok) return replaced
|
||||||
const patch = replacedPatch(state, step.text, replaced.state, step.history)
|
const patch = replacedPatch(state, step.text, replaced.state, step.history)
|
||||||
const key = appearedFinding(state.documentText, step.text)
|
api.set(withReveal(patch, state.documentText, step.text))
|
||||||
api.set({ ...patch, revealed: key ? { key } : null })
|
|
||||||
return { ok: true }
|
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). */
|
/** The session tag of USER in this tab: entries and flags carry it (criterion 22). */
|
||||||
let TAG: string
|
let TAG: string
|
||||||
beforeEach(async () => {
|
beforeEach(() => {
|
||||||
useLocaleStore.setState({ locale: 'fr' })
|
useLocaleStore.setState({ locale: 'fr' })
|
||||||
TAG = await resolveSessionTag(USER)
|
TAG = resolveSessionTag(USER)
|
||||||
})
|
})
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
cleanup()
|
cleanup()
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ const foreign = (patch: Partial<HistoryEntry>) => ({
|
|||||||
|
|
||||||
/** The session tag of USER in this tab (criterion 22). */
|
/** The session tag of USER in this tab (criterion 22). */
|
||||||
let TAG: string
|
let TAG: string
|
||||||
beforeEach(async () => {
|
beforeEach(() => {
|
||||||
TAG = await resolveSessionTag(USER)
|
TAG = resolveSessionTag(USER)
|
||||||
useLocaleStore.setState({ locale: 'fr' })
|
useLocaleStore.setState({ locale: 'fr' })
|
||||||
serverHas(emptyText(), 3, false)
|
serverHas(emptyText(), 3, false)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ afterEach(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('resolveSessionTag', () => {
|
describe('resolveSessionTag', () => {
|
||||||
it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', async () => {
|
it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', () => {
|
||||||
const tag = await resolveSessionTag('account-a')
|
const tag = resolveSessionTag('account-a')
|
||||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||||
const record = stored()
|
const record = stored()
|
||||||
expect(record?.tag).toBe(tag)
|
expect(record?.tag).toBe(tag)
|
||||||
@@ -37,37 +37,37 @@ describe('resolveSessionTag', () => {
|
|||||||
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
|
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('reuses the tag for the same account, after a reload too', async () => {
|
it('reuses the tag for the same account, after a reload too', () => {
|
||||||
const tag = await resolveSessionTag('account-a')
|
const tag = resolveSessionTag('account-a')
|
||||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
expect(resolveSessionTag('account-a')).toBe(tag)
|
||||||
reload()
|
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 () => {
|
it('rotates on another account and clears the flags of the old tag', () => {
|
||||||
const old = await resolveSessionTag('account-a')
|
const old = resolveSessionTag('account-a')
|
||||||
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
||||||
sessionStorage.setItem(flag, '{}')
|
sessionStorage.setItem(flag, '{}')
|
||||||
reload()
|
reload()
|
||||||
const tag = await resolveSessionTag('account-b')
|
const tag = resolveSessionTag('account-b')
|
||||||
expect(tag).not.toBe(old)
|
expect(tag).not.toBe(old)
|
||||||
expect(sessionStorage.getItem(flag)).toBeNull()
|
expect(sessionStorage.getItem(flag)).toBeNull()
|
||||||
reload()
|
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(() => {
|
vi.spyOn(sessionStorage, 'getItem').mockImplementation(() => {
|
||||||
throw new Error('blocked')
|
throw new Error('blocked')
|
||||||
})
|
})
|
||||||
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
||||||
throw new Error('blocked')
|
throw new Error('blocked')
|
||||||
})
|
})
|
||||||
const tag = await resolveSessionTag('account-a')
|
const tag = resolveSessionTag('account-a')
|
||||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
expect(resolveSessionTag('account-a')).toBe(tag)
|
||||||
reload()
|
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', () => {
|
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')
|
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"}')
|
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(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||||
expect(stored()?.tag).toBe(tag)
|
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', () => {
|
describe('forgetSessionTag and the sign-out', () => {
|
||||||
it('rotates the tag of the same account and clears its flags', async () => {
|
it('rotates the tag of the same account and clears its flags', () => {
|
||||||
const old = await resolveSessionTag('account-a')
|
const old = resolveSessionTag('account-a')
|
||||||
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
||||||
sessionStorage.setItem(flag, '{}')
|
sessionStorage.setItem(flag, '{}')
|
||||||
forgetSessionTag()
|
forgetSessionTag()
|
||||||
expect(stored()).toBeNull()
|
expect(stored()).toBeNull()
|
||||||
expect(sessionStorage.getItem(flag)).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 () => {
|
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(useSessionTagStore.getState().held).toBeNull()
|
||||||
expect(stored()).toBeNull()
|
expect(stored()).toBeNull()
|
||||||
stop()
|
stop()
|
||||||
expect(await resolveSessionTag('account-a')).not.toBe(old)
|
expect(resolveSessionTag('account-a')).not.toBe(old)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user