Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: the Layers list stays put — one home, no rebuild on selection or drag (#507)

nyblnet736f3d4fdbb1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c24409af..c2f92dd5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,13 @@ pre-1.0.
## [Unreleased]
+- **The Layers list stays put.** It sat at the top of the panel with nothing
+ selected and at the bottom once something was, so every click on the canvas
+ moved it; and it rebuilt itself on every frame of a drag, so it flickered
+ and lost its scroll while you moved things. It has one home now, first in
+ the panel whatever is selected, and it only redraws when the order, the
+ members or a label actually change — a move, a resize or a click never
+ touches it. Reported by the maintainer.
- **Pasting keeps the formatting.** Copy bold, italic or a list from one text
box and paste it into another, or into a table cell, and it arrives as it
was — the paste used to read only the plain-text copy and rebuild from
diff --git a/scripts/test-slides-aspect-lock.ts b/scripts/test-slides-aspect-lock.ts
index 84e14268..a2622321 100644
--- a/scripts/test-slides-aspect-lock.ts
+++ b/scripts/test-slides-aspect-lock.ts
@@ -59,7 +59,10 @@ ok(/'Keep aspect ratio': '/.test(panels), 'the row has a tooltip')
console.log('\nthe panel keeps its scroll\n')
const rebuild = panels.slice(panels.indexOf('private rebuild(force = false)'), panels.indexOf('CLOSED_BY_DEFAULT'))
ok(/const scrollTop = force \? 0 : this\.host\.scrollTop/.test(rebuild), 'a doc edit remembers the scroll; a selection/slide switch (force) starts at the top')
-ok(/this\.applyAccordion\(\)\s*\n\s*this\.host\.scrollTop = scrollTop/.test(rebuild), 'the scroll is restored after the accordion runs (which can change the height)')
+// The Layers list restores its OWN scroll between the two (#507) — a
+// different value; the panel's must still be the last thing set.
+ok(/this\.applyAccordion\(\)\s*\n(?:\s*this\.layers\.restoreScroll\(\)\s*\n)?\s*this\.host\.scrollTop = scrollTop/.test(rebuild), 'the panel scroll is restored after the accordion runs (which can change the height)')
+ok(!/this\.host\.scrollTop = scrollTop[\s\S]*this\.applyAccordion\(\)/.test(rebuild), 'and never before it')
console.log('\nwhat was set aside stays out\n')
for (const f of ['slides/src/model.ts', 'slides/src/render.ts', 'slides/src/editor/canvas.ts', 'slides/src/editor/editor.ts', 'slides/src/editor/panels.ts', 'slides/src/main.ts', 'kernel/src/save.ts', 'slides/src/styles.css']) {
diff --git a/scripts/test-slides-layers.ts b/scripts/test-slides-layers.ts
index 23512875..23b27059 100644
--- a/scripts/test-slides-layers.ts
+++ b/scripts/test-slides-layers.ts
@@ -13,12 +13,16 @@
// the panel's reorder('front'|'back')/step(±1) semantics (copied here as the
// reference — the same splice the Arrange kit does), a grouped element moves
// with its group, a no-op returns the same array; highlight follows the
-// selection and lights a whole group; and the list never reaches render.ts.
+// selection and lights a whole group; the list never reaches render.ts; the
+// row SIGNATURE ignores everything a drag changes (a move or resize is
+// 'same'; a content edit is 'labels'; reorder, add, remove or regroup is
+// 'rows'); the section is mounted FIRST whatever is selected (one home) and
+// the panel hands the same header/list nodes back on every rebuild.
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
-import { excerpt, labelFor, layerRows, moveInPaintOrder, highlighted } from '../slides/src/editor/layerrows.ts'
+import { excerpt, labelFor, layerRows, moveInPaintOrder, highlighted, rowSignature, signatureDiff } from '../slides/src/editor/layerrows.ts'
let failures = 0
let checks = 0
@@ -98,11 +102,43 @@ ok([...highlighted(grows, ['a'])].join() === 'a', 'a selected element lights its
ok([...highlighted(grows, ['g1'])].sort().join() === 'g1,g2', 'selecting one member lights the whole group (the canvas selects groups)')
ok(highlighted(grows, []).size === 0, 'nothing selected, nothing lit')
+console.log('\nsignature — what a drag must not touch\n')
+{
+ const mk = (els: E[]) => ({ elements: els as never })
+ const base = [el('a', 'text', { html: 'Hello' }), el('b'), el('c', 'text', { html: 'World', groupId: 'g' })]
+ const sig0 = rowSignature(mk(base))
+ const moved = base.map((e) => ({ ...e, x: 999, y: -5, w: 1, h: 1, rotation: 45, opacity: 0.2, fill: '#000' }))
+ ok(signatureDiff(sig0, rowSignature(mk(moved))) === 'same', 'moving, resizing, rotating, recolouring every element: same signature')
+ const relabel = base.map((e) => (e.id === 'a' ? { ...e, html: 'Hello there' } : e))
+ ok(signatureDiff(sig0, rowSignature(mk(relabel))) === 'labels', 'editing a text box\'s content: labels only')
+ ok(signatureDiff(sig0, rowSignature(mk([base[1], base[0], base[2]]))) === 'rows', 'reordering: rows')
+ ok(signatureDiff(sig0, rowSignature(mk([...base, el('d')]))) === 'rows', 'adding an element: rows')
+ ok(signatureDiff(sig0, rowSignature(mk(base.slice(0, 2)))) === 'rows', 'removing an element: rows')
+ ok(signatureDiff(sig0, rowSignature(mk(base.map((e) => (e.id === 'b' ? { ...e, groupId: 'g' } : e))))) === 'rows', 'regrouping: rows')
+ ok(signatureDiff(null, sig0) === 'rows', 'no rows yet: build them')
+ ok(signatureDiff(sig0, rowSignature(mk(base))) === 'same', 'the same document twice: same')
+}
+
console.log('\neditor-only\n')
ok(!/ed-layer|layers\.ts|LayersUI/.test(read('slides/src/render.ts')) && !/ed-layer|LayersUI/.test(read('slides/src/present.ts')), 'render.ts and present.ts know nothing of the list — thumbnails, the show, print and preview cannot carry it')
const panels = read('slides/src/editor/panels.ts')
-ok(/this\.layers\.mount\(this\.host\); this\.buildSlidePanel\(\)/.test(panels), 'Slide panel: the list comes FIRST')
-ok(/this\.buildElementPanel\(els\[0\]\); this\.layers\.mount\(this\.host\)/.test(panels), 'element panel: the list comes LAST')
+{
+ // one home: mounted once, before any of the three builders, whatever is selected
+ const rebuild = panels.slice(panels.indexOf('private rebuild(force = false)'), panels.indexOf('private static CLOSED_BY_DEFAULT'))
+ const mountAt = rebuild.indexOf('this.layers.mount(this.host)')
+ ok(mountAt >= 0 && rebuild.indexOf('this.buildSlidePanel()') > mountAt && rebuild.indexOf('this.buildElementPanel(') > mountAt && rebuild.indexOf('this.buildMultiPanel(') > mountAt,
+ 'the list is mounted FIRST, before the Slide, element and multi builders alike — one home')
+ ok((rebuild.match(/this\.layers\.mount\(/g) ?? []).length === 1, 'and only once per rebuild')
+ ok(/this\.layers\.detach\(\)\s*\n\s*this\.host\.innerHTML = ''/.test(rebuild), 'the list is told before the host is cleared, so its scroll survives')
+ ok(/this\.applyAccordion\(\)\s*\n\s*this\.layers\.restoreScroll\(\)/.test(rebuild), 'and put back AFTER the accordion has moved the list into its body (moving a node resets its scroll)')
+ const ui = read('slides/src/editor/layers.ts')
+ ok(/private header: HTMLElement/.test(ui) && /into\.appendChild\(this\.header\)\s*\n\s*into\.appendChild\(this\.list\)/.test(ui), 'mount re-appends the SAME header and list nodes (no new h3 per rebuild)')
+ ok(/const diff = signatureDiff\(this\.sig, next\)[\s\S]*if \(diff === 'same'\) \{ this\.highlight\(\); return \}/.test(ui), 'refresh compares the signature first and only re-highlights when nothing a row says changed')
+ ok(/if \(diff === 'labels'\)[\s\S]*label\.textContent = r\.label/.test(ui), 'a labels-only change patches text in place')
+ ok(!/store\.on\(/.test(ui) && !/'selection'/.test(ui), 'the list has no store subscription of its own — the panel drives it, and selection never rebuilds rows')
+ const acc = panels.slice(panels.indexOf('private applyAccordion()'), panels.indexOf('// --- builders'))
+ ok(/if \(h\.dataset\.acc\) continue/.test(acc) && /h\.nextElementSibling/.test(acc), 'the accordion attaches one click handler per header for its lifetime and finds the body live (the Layers h3 outlives rebuilds)')
+}
ok(/setOrder: \(elements\) => this\.store\.commit\(/.test(panels), 'a move is one store.commit — one undo step, no new field')
ok(/'Layers'\]\)/.test(panels.slice(panels.indexOf('CLOSED_BY_DEFAULT ='), panels.indexOf('CLOSED_BY_DEFAULT =') + 200)), 'closed by default (opened state persists per title like the other sections)')
const layers = read('slides/src/editor/layers.ts') + read('slides/src/editor/layerrows.ts')
diff --git a/slides/src/editor/layerrows.ts b/slides/src/editor/layerrows.ts
index 955e2196..d0def4e8 100644
--- a/slides/src/editor/layerrows.ts
+++ b/slides/src/editor/layerrows.ts
@@ -10,15 +10,24 @@
* somewhere ends up exactly where the Order buttons would have put it
* (moveInPaintOrder below is the same splice as repeated steps).
*
- * WHERE IT LIVES (measured, Chrome 1565 px wide, starter deck, panel
- * viewport 740 px): the Slide panel with nothing selected is 1192 px of
- * content; a selected text element's panel is 2219 px. So the list sits at
- * the TOP of the Slide panel — nothing selected is when a reader asks "what
- * is on this slide?" — and closes the element panel as its LAST section,
- * where it is one End-key from anywhere rather than lost between Typography
- * and Presenting; closed by default, open state shared under one title. The
- * list caps itself at ~12 rows and scrolls inside (the showcase title slide
- * has 39 elements: uncapped it pushed the Slide controls ~975 px down).
+ * WHERE IT LIVES: FIRST in the properties panel, whatever is selected. The
+ * #481 measurement (Chrome 1565 px wide, starter deck, panel viewport 740 px:
+ * the Slide panel is 1192 px of content, a selected text element's 2219 px)
+ * argued for putting it first on the Slide panel and LAST on an element's, so
+ * it was reachable in both — but a section that moves is worse than one that
+ * is far: every click on the canvas hopped it from the top of the panel to
+ * the bottom, and a user who opened it expected it where they left it. One
+ * home now: structure first, then the properties of the selection; closed by
+ * default, open state shared under one title. The list caps itself at ~12
+ * rows and scrolls inside (the showcase title slide has 39 elements: uncapped
+ * it pushed the Slide controls ~975 px down).
+ *
+ * WHAT REBUILDS IT: only a change in what the rows SAY. A drag emits a doc
+ * event per frame, and none of them changes a row (position and size are not
+ * in the list), so the list compares a signature of (ids in paint order,
+ * labels, groups) and does nothing when it is unchanged, patches label text
+ * when only labels changed, and rebuilds rows only on an order or membership
+ * change. Selection toggles highlight classes in place.
*
* This file is the DOM-free half (rows, labels, the move), imported plain by
* scripts/test-slides-layers.ts; layers.ts is the DOM.
@@ -106,3 +115,20 @@ export function highlighted(rows: readonly LayerRow[], selection: readonly strin
return new Set(rows.filter((r) => sel.has(r.id) || (r.groupId && groups.has(r.groupId))).map((r) => r.id))
}
+/** What the rows depend on, as one comparable string — ids in paint order,
+ * labels and groups. Position, size, colour and every other property are
+ * deliberately absent: a drag frame must not touch the list. */
+export function rowSignature(slide: Pick<Slide, 'elements'>, tr?: (s: string) => string): { order: string; labels: string } {
+ const rows = layerRows(slide, tr)
+ return {
+ order: rows.map((r) => `${r.id}\u001f${r.type}\u001f${r.groupId ?? ''}`).join('\u001e'),
+ labels: rows.map((r) => r.label).join('\u001e'),
+ }
+}
+
+/** How much of the list a change touches. */
+export function signatureDiff(a: ReturnType<typeof rowSignature> | null, b: ReturnType<typeof rowSignature>): 'same' | 'labels' | 'rows' {
+ if (!a) return 'rows'
+ if (a.order !== b.order) return 'rows'
+ return a.labels === b.labels ? 'same' : 'labels'
+}
diff --git a/slides/src/editor/layers.ts b/slides/src/editor/layers.ts
index f05cc1b6..a7a743e9 100644
--- a/slides/src/editor/layers.ts
+++ b/slides/src/editor/layers.ts
@@ -7,8 +7,8 @@
import type { Slide, SlideElement } from '../model'
import { ICONS } from '../icons'
import { t } from '../i18n'
-import { layerRows, moveInPaintOrder, highlighted, type LayerRow } from './layerrows'
-export { excerpt, labelFor, layerRows, moveInPaintOrder, highlighted, type LayerRow } from './layerrows'
+import { layerRows, moveInPaintOrder, highlighted, rowSignature, signatureDiff, type LayerRow } from './layerrows'
+export { excerpt, labelFor, layerRows, moveInPaintOrder, highlighted, rowSignature, signatureDiff, type LayerRow } from './layerrows'
/** The store surface the list needs — narrow so the rig never needs one. */
export interface LayersHost {
@@ -26,7 +26,13 @@ const GLYPH: Partial<Record<SlideElement['type'], string>> = {
export class LayersUI {
private list: HTMLElement
+ private header: HTMLElement
private dragId: string | null = null
+ /** what the rows on screen were built from — see layerrows.ts rowSignature */
+ private sig: ReturnType<typeof rowSignature> | null = null
+ /** the list's own scroll, carried across a panel rebuild (a detached node
+ * forgets its scroll position; the panel clears its host on every edit) */
+ private savedScroll = 0
constructor(private host: LayersHost) {
this.list = document.createElement('div')
@@ -34,19 +40,75 @@ export class LayersUI {
this.list.tabIndex = 0
this.list.setAttribute('role', 'listbox')
this.list.addEventListener('keydown', (ev) => this.onKey(ev))
+ // ONE header node for the life of the panel, like the list: the accordion
+ // retrofit keys its open state and its click handler off this element.
+ this.header = document.createElement('h3')
+ this.header.className = 'ed-section'
+ this.header.textContent = t('Layers')
}
- /** Append the section (header + list) to a panel host. */
+ /** Called by the panel before it clears its host, so the list's own scroll
+ * survives the re-append. */
+ detach() {
+ if (this.list.isConnected) this.savedScroll = this.list.scrollTop
+ }
+
+ /** Append the section (header + list) to a panel host. The same two nodes
+ * every time — rows are only rebuilt when they changed (refresh). */
mount(into: HTMLElement) {
- const h = document.createElement('h3')
- h.className = 'ed-section'
- h.textContent = t('Layers')
- into.appendChild(h)
+ this.header.textContent = t('Layers')
+ into.appendChild(this.header)
into.appendChild(this.list)
this.refresh()
}
+ /** After the panel's accordion has wrapped the list (moving a node into
+ * the section body resets its scroll), put the list's scroll back. */
+ restoreScroll() {
+ if (this.savedScroll && this.list.isConnected) this.list.scrollTop = this.savedScroll
+ }
+
+ /** Bring the rows up to date, touching as little as the change needs:
+ * nothing for a move/resize, label text for a content edit, the rows for
+ * an order or membership change; the highlight always, in place. */
refresh() {
+ const next = rowSignature(this.host.slide(), t)
+ const diff = signatureDiff(this.sig, next)
+ this.sig = next
+ if (diff === 'same') { this.highlight(); return }
+ if (diff === 'labels') {
+ for (const r of layerRows(this.host.slide(), t)) {
+ const node = this.rowNode(r.id)
+ const label = node?.querySelector<HTMLElement>('.ed-layer-label')
+ if (node && label && label.textContent !== r.label) { label.textContent = r.label; node.title = r.label }
+ }
+ this.highlight()
+ return
+ }
+ this.rebuildRows()
+ }
+
+ private rowNode(id: string): HTMLElement | null {
+ for (const n of this.list.children) if ((n as HTMLElement).dataset.id === id) return n as HTMLElement
+ return null
+ }
+
+ /** Selection changed: classes only, no DOM churn. */
+ private highlight() {
+ const rows = layerRows(this.host.slide(), t)
+ const lit = highlighted(rows, this.host.selection())
+ for (const n of this.list.children) {
+ const row = n as HTMLElement
+ if (!row.dataset.id) continue
+ const on = lit.has(row.dataset.id)
+ row.classList.toggle('sel', on)
+ row.setAttribute('aria-selected', on ? 'true' : 'false')
+ }
+ }
+
+ private rebuildRows() {
+ const scroll = this.list.scrollTop
+ const had = document.activeElement === this.list
this.list.innerHTML = ''
const rows = layerRows(this.host.slide(), t)
const lit = highlighted(rows, this.host.selection())
@@ -97,6 +159,8 @@ export class LayersUI {
})
this.list.appendChild(row)
}
+ this.list.scrollTop = scroll
+ if (had) this.list.focus({ preventScroll: true })
}
private dropAbove(ev: DragEvent, row: HTMLElement): boolean {
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index bd44bf5f..8ede920e 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -201,22 +201,27 @@ export class PropsPanel {
// every change. A selection or slide switch (force) starts the new
// inspector at the top, which is where a different subject belongs.
const scrollTop = force ? 0 : this.host.scrollTop
+ this.layers.detach()
this.host.innerHTML = ''
const els = this.store.selectedElements
- // Layers (editor/layers.ts): first thing on the Slide panel, last thing on
- // an element's — see that file's header for the measurement behind it.
- if (els.length === 0) { this.layers.mount(this.host); this.buildSlidePanel() }
- else if (els.length === 1) { this.buildElementPanel(els[0]); this.layers.mount(this.host) }
- else { this.buildMultiPanel(els); this.layers.mount(this.host) }
+ // Layers (editor/layers.ts): FIRST, whatever is selected — one home. It
+ // used to sit first on the Slide panel and last on an element's, so every
+ // canvas click hopped it from top to bottom (see layerrows.ts's header).
+ this.layers.mount(this.host)
+ if (els.length === 0) this.buildSlidePanel()
+ else if (els.length === 1) this.buildElementPanel(els[0])
+ else this.buildMultiPanel(els)
this.applyAccordion()
+ this.layers.restoreScroll()
this.host.scrollTop = scrollTop
}
/** Collapsed by default until the user opens them (persisted per title). */
private static CLOSED_BY_DEFAULT = new Set(['Slideshow', 'Presenting', 'Interactivity', 'Layout', 'Advanced (JSON)', 'Layers'])
- /** The layer list, one instance re-mounted on every rebuild (rows are
- * cheap; the node keeps focus and drag state across a doc edit). */
+ /** The layer list: one instance, the SAME header and list nodes re-appended
+ * on every rebuild (a drag emits a doc event per frame; the rows are only
+ * rebuilt when what they say changed — layers.ts refresh). */
private layers = new LayersUI({
slide: () => this.store.slide,
selection: () => this.store.selection,
@@ -247,15 +252,21 @@ export class PropsPanel {
h.after(body)
const isOpen = openState[key] ?? !PropsPanel.CLOSED_BY_DEFAULT.has(key)
h.classList.add('ed-sec-toggle')
- if (!isOpen) {
- h.classList.add('closed')
- body.style.display = 'none'
- }
+ h.classList.toggle('closed', !isOpen)
+ if (!isOpen) body.style.display = 'none'
+ // A header can outlive a rebuild (the Layers h3 is one node for the
+ // panel's life), so the click handler is attached once and reads its
+ // body from the DOM each time rather than closing over one that a later
+ // rebuild threw away.
+ if (h.dataset.acc) continue
+ h.dataset.acc = '1'
h.addEventListener('click', () => {
const nowClosed = h.classList.toggle('closed')
- body.style.display = nowClosed ? 'none' : ''
- openState[key] = !nowClosed
- lsSet('bento-panel-open', JSON.stringify(openState))
+ const live = h.nextElementSibling as HTMLElement | null
+ if (live?.classList.contains('ed-section-body')) live.style.display = nowClosed ? 'none' : ''
+ const state = lsJson<Record<string, boolean>>('bento-panel-open', {})
+ state[key] = !nowClosed
+ lsSet('bento-panel-open', JSON.stringify(state))
})
}
}