Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: an inline edit's listeners die with it — a re-edited cell or text box no longer pastes N times (#504)

nyblnet9fed268cde94
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bc9ff7dc..0c8d6ce9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1388,6 +1388,13 @@ jobs:
# routes the argument. Bare {{date}} stays viewer-locale (#381).
run: node scripts/test-slides-fields.ts
+ - name: Inline-edit listeners rig
+ # Every keydown/input/paste/blur listener a text-box or cell edit
+ # attaches carries that edit's AbortSignal, and both commits abort it —
+ # a node re-edited without a re-render used to stack listeners and
+ # paste N times.
+ run: node scripts/test-slides-edit-listeners.ts
+
- name: Shell loader and carrier rig
# base86 payloads (scripts/lib/b86.mjs) round-trip and can never
# contain </script, <!--, -->, ]]> or ${; the loader inflates
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 502e5203..a6f1e9b4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,12 @@ pre-1.0.
## [Unreleased]
+- **Pasting into a table cell lands once.** Edit a cell, leave it unchanged,
+ edit it again — each visit quietly added another set of keystroke and paste
+ handlers to the same cell, so a later paste landed once per visit (four
+ visits, four copies; the same could happen to a text box). Each edit now
+ takes its handlers with it when it ends. Found by the maintainer, copying
+ an asset code between cells.
## [1.2.0] — 2026-09-16
- **A deck opens inside Teams and SharePoint again.** Their viewer refuses
diff --git a/scripts/test-slides-edit-listeners.ts b/scripts/test-slides-edit-listeners.ts
new file mode 100644
index 00000000..1cf97c83
--- /dev/null
+++ b/scripts/test-slides-edit-listeners.ts
@@ -0,0 +1,54 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// Inline-edit listeners die with the edit that made them.
+//
+// node scripts/test-slides-edit-listeners.ts
+//
+// WHAT THIS PROVES. canvas.ts attaches keydown/input/paste/blur listeners to
+// a text box's or a table cell's inner node every time an edit starts. When
+// an edit ends with the text unchanged nothing re-renders, so the SAME node
+// is edited next time — and, until this fix, gained another set of listeners
+// each time. Tab through a table's cells, then paste into one: the paste
+// landed once per visit (measured on the 1.2.0 shell: four visits, four
+// copies of the pasted text; on the fix, one). Every edit-time listener now
+// carries the AbortSignal of one per-edit controller, and both commit paths
+// abort it — asserted here on the source; the browser measurement is the one
+// in the PR.
+
+import { readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { dirname, join } from 'node:path'
+
+let failures = 0
+let checks = 0
+function ok(cond: boolean, msg: string) {
+ checks++
+ if (!cond) { failures++; console.log(` FAIL ${msg}`) }
+ else console.log(` ok ${msg}`)
+}
+const root = join(dirname(fileURLToPath(import.meta.url)), '..')
+const src = readFileSync(join(root, 'slides/src/editor/canvas.ts'), 'utf8')
+
+console.log('one controller per edit\n')
+const text = src.slice(src.indexOf(' startTextEdit(node: HTMLElement) {'), src.indexOf(' commitTextEdit() {'))
+const cell = src.slice(src.indexOf(' private editCellAt('), src.indexOf(' private commitCellEdit('))
+for (const [name, block] of [['a text box', text], ['a table cell', cell]] as const) {
+ ok(/this\.editListeners\?\.abort\(\)\s*\n\s*this\.editListeners = new AbortController\(\)/.test(block), `${name}: starting an edit aborts the previous controller and makes a fresh one`)
+ // each listener: the text from its addEventListener to the next one (or the
+ // end of the block); the call's closing options object must name the signal
+ const pieces = block.split("inner.addEventListener('").slice(1)
+ const adds = pieces.map((piece) => ({ 1: piece.slice(0, piece.indexOf("'")), 0: piece }))
+ const kinds = adds.map((m) => m[1])
+ ok(kinds.length >= 4 && ['keydown', 'input', 'paste', 'blur'].every((k) => kinds.includes(k)), `${name}: keydown, input, paste and blur are attached (${kinds.join(', ')})`)
+ const unsignalled = adds.filter((m) => !/\bsignal\b[^\n]*\}\)/.test(m[0]))
+ ok(unsignalled.length === 0, `${name}: every listener carries the edit's signal${unsignalled.length ? ` — missing on ${unsignalled.map((m) => m[1]).join(', ')}` : ''}`)
+}
+console.log('\nboth commits end the listeners\n')
+const commitText = src.slice(src.indexOf(' commitTextEdit() {'), src.indexOf(' private editCellAt('))
+const commitCell = src.slice(src.indexOf(' private commitCellEdit('), src.indexOf(' private moveCell('))
+ok(/this\.editListeners\?\.abort\(\)\s*\n\s*this\.editListeners = null\s*\n\s*this\.editing = null/.test(commitText), 'commitTextEdit aborts before clearing the edit')
+ok(/this\.editListeners\?\.abort\(\)\s*\n\s*this\.editListeners = null\s*\n\s*this\.editing = null/.test(commitCell), 'commitCellEdit aborts before clearing the edit')
+
+console.log(`\n${checks - failures}/${checks} checks passed`)
+process.exit(failures ? 1 : 0)
diff --git a/slides/src/editor/canvas.ts b/slides/src/editor/canvas.ts
index e9b40e56..8235ed8c 100644
--- a/slides/src/editor/canvas.ts
+++ b/slides/src/editor/canvas.ts
@@ -46,6 +46,12 @@ export class SlideCanvas {
private pinching = false
private zoomLabel: HTMLElement | null = null
private editing: HTMLElement | null = null
+ /** The listeners of the CURRENT inline edit (text box or table cell), so
+ * they die with it. Without this a node that was edited, committed with
+ * no change (no re-render, same node) and edited again carried one more
+ * keydown/input/paste listener each time — Tab through a table's cells,
+ * then paste into one, and the paste landed N times over. */
+ private editListeners: AbortController | null = null
/** Slide identity captured when an inline edit begins. Element ids may be
* shared across duplicated slides, so resolving through store.slide at
* commit time can write into the wrong slide after navigation or a remote
@@ -1413,6 +1419,9 @@ export class SlideCanvas {
this.editingShowedRaw = true
}
this.editing = node
+ this.editListeners?.abort()
+ this.editListeners = new AbortController()
+ const signal = this.editListeners.signal
this.editingSlideId = this.store.slide.id
node.classList.add('bento-editing')
inner.contentEditable = 'true'
@@ -1444,20 +1453,20 @@ export class SlideCanvas {
execFormat(cmd)
}
}
- })
+ }, { signal })
// markdown affordances: **bold** / *italic* / `code` / ~~strike~~ / "- "
// collapse as you type (⌘Z reverts, backslash escapes); pasted plain
// text converts the same patterns
inner.addEventListener('input', () => {
if (!autoformatAtCaret()) clearAutoformat()
- })
+ }, { signal })
inner.addEventListener('paste', (ev) => {
const text = ev.clipboardData?.getData('text/plain')
if (!text) return
ev.preventDefault()
document.execCommand('insertHTML', false, sanitizeHtml(markdownToHtml(text)))
- })
- inner.addEventListener('blur', () => this.commitTextEdit(), { once: true })
+ }, { signal })
+ inner.addEventListener('blur', () => this.commitTextEdit(), { once: true, signal })
}
/** collaborator presence: notified when text editing starts/stops */
@@ -1472,6 +1481,8 @@ export class SlideCanvas {
if (!node) return
if (this.editingCell) { this.commitCellEdit(node); return }
const slideId = this.editingSlideId
+ this.editListeners?.abort()
+ this.editListeners = null
this.editing = null
this.editingSlideId = null
this.onTextEditChange?.(undefined)
@@ -1541,6 +1552,9 @@ export class SlideCanvas {
const inner = td.querySelector<HTMLElement>('.bento-cell-inner')
if (!node || !inner) return
this.editing = node
+ this.editListeners?.abort()
+ this.editListeners = new AbortController()
+ const signal = this.editListeners.signal
this.editingSlideId = this.store.slide.id
this.editingCell = { r, c }
node.classList.add('bento-editing')
@@ -1561,21 +1575,23 @@ export class SlideCanvas {
const cmd = { b: 'bold', i: 'italic', u: 'underline' }[ev.key.toLowerCase()]
if (cmd) { ev.preventDefault(); execFormat(cmd) }
}
- })
- inner.addEventListener('input', () => { if (!autoformatAtCaret()) clearAutoformat() })
+ }, { signal })
+ inner.addEventListener('input', () => { if (!autoformatAtCaret()) clearAutoformat() }, { signal })
inner.addEventListener('paste', (ev) => {
const text = ev.clipboardData?.getData('text/plain')
if (!text) return
ev.preventDefault()
document.execCommand('insertHTML', false, sanitizeHtml(markdownToHtml(text)))
- })
- inner.addEventListener('blur', () => this.commitTextEdit(), { once: true })
+ }, { signal })
+ inner.addEventListener('blur', () => this.commitTextEdit(), { once: true, signal })
}
private commitCellEdit(node: HTMLElement) {
const cell = this.editingCell!
const id = node.dataset.elId
const slideId = this.editingSlideId
+ this.editListeners?.abort()
+ this.editListeners = null
this.editing = null
this.editingSlideId = null
this.editingCell = null