feat(editor): browser history for tabs, sections and Aller a
EditorView held by the navigation reducer (restore_view), a history adapter hook mounted in EditorWorkspace (push, replace, back, mount rule, ownership, write budget, scroll map, refused POP), a guard stack by layer, an announcer queue joined after open dialogs.
This commit is contained in:
1 parent
69e225ca7a
commit
792394d3b2
11 files changed
+1268
-40
No files matched your search
@@ -0,0 +1,154 @@
|
||||
/** R2F-1: the history entry, its ownership, the write plan and the write budget. */
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
createWriteBudget,
|
||||
ENTRY_KEY,
|
||||
layerCount,
|
||||
ownedEntry,
|
||||
planWrite,
|
||||
readEntry,
|
||||
sanitizeView,
|
||||
type HistoryEntry,
|
||||
} from '@/editor/history/entry'
|
||||
import {
|
||||
defaultView,
|
||||
initialNavigation,
|
||||
navigationReducer,
|
||||
sameView,
|
||||
viewOf,
|
||||
type EditorView,
|
||||
} from '@/editor/navigation'
|
||||
|
||||
const view = (tab: EditorView['tab'], patch: Partial<EditorView> = {}) => ({
|
||||
...defaultView(tab),
|
||||
...patch,
|
||||
})
|
||||
const entry = (patch: Partial<HistoryEntry> = {}): HistoryEntry => ({
|
||||
id: 'e1',
|
||||
view: view('general'),
|
||||
below: null,
|
||||
sheetId: 'S',
|
||||
userId: 'U',
|
||||
...patch,
|
||||
})
|
||||
const stateOf = (value: unknown) => ({ [ENTRY_KEY]: value })
|
||||
|
||||
describe('sanitizeView', () => {
|
||||
it('keeps a tab and a section, and holds no layer yet', () => {
|
||||
const raw = { tab: 'annotations', section: 'views', finding: { key: {} } }
|
||||
expect(sanitizeView(raw)).toEqual(view('annotations', { section: 'views' }))
|
||||
})
|
||||
|
||||
it('drops what is no view: an unknown tab, a non-object', () => {
|
||||
expect(sanitizeView({ tab: 'nowhere', section: 'views' })).toBeNull()
|
||||
expect(sanitizeView('general')).toBeNull()
|
||||
expect(sanitizeView(null)).toBeNull()
|
||||
})
|
||||
|
||||
it('falls back to the drawing on an unknown section', () => {
|
||||
expect(sanitizeView({ tab: 'annotations', section: 'x' })?.section).toBe(
|
||||
'drawing',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('readEntry and ownership', () => {
|
||||
it('reads a well-formed entry and nothing else', () => {
|
||||
expect(readEntry(stateOf(entry()))).toEqual(entry())
|
||||
expect(readEntry(null)).toBeNull()
|
||||
expect(readEntry({})).toBeNull()
|
||||
expect(readEntry(stateOf({ ...entry(), id: 3 }))).toBeNull()
|
||||
expect(readEntry(stateOf({ ...entry(), view: 'x' }))).toBeNull()
|
||||
})
|
||||
|
||||
it('restores only for the sheet and the account that wrote it', () => {
|
||||
const mine = { sheetId: 'S', userId: 'U' }
|
||||
expect(ownedEntry(stateOf(entry()), mine)).not.toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, sheetId: 'T' })).toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, userId: 'V' })).toBeNull()
|
||||
})
|
||||
|
||||
it('keeps only navigation: nothing typed survives the read', () => {
|
||||
const raw = { ...entry(), draft: 'typed text', term: 'search' }
|
||||
expect(JSON.stringify(readEntry(stateOf(raw)))).not.toContain('typed')
|
||||
})
|
||||
})
|
||||
|
||||
describe('planWrite', () => {
|
||||
const top = entry({ view: view('general'), below: null })
|
||||
|
||||
it('does nothing for the same view', () => {
|
||||
expect(planWrite(top, view('general'), false)).toBe('none')
|
||||
// a section only counts on Annotations
|
||||
const other = view('general', { section: 'views' })
|
||||
expect(planWrite(top, other, false)).toBe('none')
|
||||
})
|
||||
|
||||
it('pushes an added tab or section, replaces on a replace hint', () => {
|
||||
expect(planWrite(top, view('findings'), false)).toBe('push')
|
||||
expect(planWrite(top, view('findings'), true)).toBe('replace')
|
||||
const onAnnotations = entry({ view: view('annotations') })
|
||||
const views = view('annotations', { section: 'views' })
|
||||
expect(planWrite(onAnnotations, views, false)).toBe('push')
|
||||
})
|
||||
|
||||
it('goes back for a layer removed when the entry holds the view below', () => {
|
||||
const layer = view('findings', { reference: { create: true } })
|
||||
const opened = entry({ view: layer, below: view('findings') })
|
||||
expect(layerCount(layer)).toBe(1)
|
||||
expect(planWrite(opened, view('findings'), false)).toBe('back')
|
||||
})
|
||||
|
||||
it('replaces a layer removed when the entry below is another view', () => {
|
||||
const layer = view('findings', { reference: { create: true } })
|
||||
const opened = entry({ view: layer, below: view('general') })
|
||||
expect(planWrite(opened, view('findings'), false)).toBe('replace')
|
||||
expect(planWrite(entry({ view: layer }), view('findings'), false)).toBe(
|
||||
'replace',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createWriteBudget', () => {
|
||||
it('lets the limit through per window, then refuses until the window slides', () => {
|
||||
const take = createWriteBudget(3, 1000)
|
||||
expect([0, 100, 200].map(take)).toEqual([true, true, true])
|
||||
expect(take(300)).toBe(false)
|
||||
expect(take(999)).toBe(false)
|
||||
expect(take(1000)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('the view the reducer holds', () => {
|
||||
it('restore_view lands on a view without a move or a focus request', () => {
|
||||
const state = initialNavigation('general')
|
||||
const next = navigationReducer(state, {
|
||||
type: 'restore_view',
|
||||
view: view('annotations', { section: 'regions' }),
|
||||
})
|
||||
expect(viewOf(next)).toEqual(view('annotations', { section: 'regions' }))
|
||||
expect(next.focus).toBeUndefined()
|
||||
expect(next.visitedSections).toContain('regions')
|
||||
})
|
||||
|
||||
it('keeps the remembered section when the view is on another tab', () => {
|
||||
const base = navigationReducer(initialNavigation('annotations'), {
|
||||
type: 'open_section',
|
||||
section: 'views',
|
||||
})
|
||||
const next = navigationReducer(base, {
|
||||
type: 'restore_view',
|
||||
view: view('general'),
|
||||
})
|
||||
expect(next.section).toBe('views')
|
||||
expect(
|
||||
sameView(viewOf(next), view('general', { section: 'drawing' })),
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('opens the state on a view read at mount', () => {
|
||||
const state = initialNavigation('annotations', view('findings'))
|
||||
expect(state.tab).toBe('findings')
|
||||
expect(state.visitedTabs).toEqual(['findings'])
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user