Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
slides: paste onto another slide keeps the copied position; +20 only over the same element (#569) (#689)
nyblnetb80c409ba9fa
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 76f4eac8..1898639e 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -2369,6 +2369,10 @@ jobs:# range across a parent copies parents only, never its states; plural# and singular toasts.run: node scripts/test-slides-copy-slides-browser.mjs+ - name: Paste position in Chromium+ # ⌘C then ⌘V onto another slide keeps the element's position; onto the+ # source slide it steps +20 and cascades (#569).+ run: node scripts/test-slides-paste-position-browser.mjs- name: Save queue in Chromium# Real ⌘S and auto-save with the file write held open: an edit made# during a write stays dirty, writes never overlap, the dot goes outdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex c50fa8ed..e9c3622a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,12 @@ pre-1.0.## [Unreleased]+- **Pasting onto another slide keeps the position.** Copy a logo, a header or+ any element and paste it onto a different slide, and it lands exactly where+ it was. It steps down and right only when it would cover the same element+ (pasting onto the slide you copied from, or pasting twice), so repeated+ pastes still fan out. Reported in #569.+## [1.2.7] — 2026-10-11- **One page for PowerPoint, in and out: bento.page/convert.** Drop adiff --git a/scripts/test-clipboard.ts b/scripts/test-clipboard.tsindex 0fdec33b..671feac9 100644--- a/scripts/test-clipboard.ts+++ b/scripts/test-clipboard.ts@@ -72,6 +72,28 @@ console.log('\nelement clipboard')ok(font != null && target.assets![font.asset] === source.assets!['font-acme'], 'pasted element font points at its source bytes')}+console.log('\npaste position (#569)')+{+ const doc = newDoc()+ doc.slides.push({ id: 's2', background: '#fff', elements: [] } as never)+ const a = defaultShape('rect', { id: 'a', x: 100, y: 80 }), b = defaultText({ id: 'b', html: 'B', x: 300, y: 80 })+ doc.slides[0].elements = [a, b]+ const clip = parseClip(serializeElements([a, b], doc))!+ const at = (els: SlideElement[]) => els.map((e) => `${e.x},${e.y}`).join(' ')+ const other = insertElements(clip, doc, doc.slides[1])+ ok(at(other) === '100,80 300,80', `onto another slide it keeps the copied position (${at(other)})`)+ const same1 = insertElements(clip, doc, doc.slides[0])+ ok(at(same1) === '120,100 320,100', `onto the source slide it steps +20, as one group (${at(same1)})`)+ const same2 = insertElements(clip, doc, doc.slides[0])+ ok(at(same2) === '140,120 340,120', `pasting again cascades another +20 (${at(same2)})`)+ const again = insertElements(clip, doc, doc.slides[1])+ ok(at(again) === '120,100 320,100', `a second paste on the other slide steps off the first (${at(again)})`)+ // an element at the spot that is NOT the same thing (other type/size) does not count+ doc.slides.push({ id: 's3', background: '#fff', elements: [defaultShape('ellipse', { id: 'c', x: 100, y: 80, w: 37, h: 37 })] } as never)+ const unlike = insertElements(clip, doc, doc.slides[2])+ ok(at(unlike) === '100,80 300,80', `something different at the spot does not push the paste (${at(unlike)})`)+}+console.log('\nslide clipboard'){const source = sourceDoc()diff --git a/scripts/test-slides-paste-position-browser.mjs b/scripts/test-slides-paste-position-browser.mjsnew file mode 100644index 00000000..6663e69c--- /dev/null+++ b/scripts/test-slides-paste-position-browser.mjs@@ -0,0 +1,82 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors+// ⌘C an element, ⌘V it onto another slide: it lands where it was (#569).+//+// npm run build:single --prefix slides && node scripts/test-slides-paste-position-browser.mjs+//+// In the packaged editor: real ⌘C (its payload captured from the editor's own+// navigator.clipboard.writeText), then that payload handed to the editor's+// paste handler as a ClipboardEvent — the system clipboard of a headless+// browser is not dependable, the editor's code paths are.+// 1. onto ANOTHER slide the copy keeps its position;+// 2. onto the source slide it steps +20/+20 off the original;+// 3. pasted there again it cascades another +20.+const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright')++let checks = 0+let failures = 0+function ok(cond, msg) {+ checks+++ if (!cond) { failures++; console.log(` FAIL ${msg}`) } else console.log(` ok ${msg}`)+}+const MOD = process.platform === 'darwin' ? 'Meta' : 'Control'++const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH, headless: true })+try {+ const ctx = await browser.newContext({ viewport: { width: 1440, height: 1000 } })+ await ctx.route(/^https?:/, (r) => r.abort())+ await ctx.addInitScript(() => {+ window.__clips = []+ const clip = navigator.clipboard ?? {}+ Object.defineProperty(navigator, 'clipboard', {+ configurable: true,+ value: Object.assign(Object.create(Object.getPrototypeOf(clip) ?? Object.prototype), clip, {+ writeText: (t) => { window.__clips.push(String(t)); return Promise.resolve() },+ }),+ })+ })+ const p = await ctx.newPage()+ const errors = []+ p.on('pageerror', (e) => errors.push(e.message))+ await p.goto(new URL('../slides/dist-single/Bento_Slides.bento.html', import.meta.url).href)+ await p.waitForFunction(() => window.bento?.doc && document.querySelector('.ed-stage-scale'))+ await p.evaluate(() => {+ const d = JSON.parse(JSON.stringify(window.bento.doc))+ d.slides = [+ { id: 's1', background: '#ffffff', elements: [{ id: 'box', type: 'shape', shape: 'rect', x: 200, y: 160, w: 180, h: 120, rotation: 0, opacity: 1, fill: '#5E7699' }] },+ { id: 's2', background: '#ffffff', elements: [] },+ ]+ window.bento.loadDoc(JSON.stringify(d))+ })+ await p.waitForTimeout(400)++ const thumb = async (i) => { const b = await p.locator(`.ed-sidebar .ed-thumb[data-index="${i}"]`).boundingBox(); await p.mouse.click(b.x + b.width / 2, b.y + b.height / 2); await p.waitForTimeout(250) }+ const paste = () => p.evaluate(() => {+ const dt = new DataTransfer(); dt.setData('text/plain', window.__clips.at(-1))+ document.dispatchEvent(new ClipboardEvent('paste', { bubbles: true, cancelable: true, clipboardData: dt }))+ }).then(() => p.waitForTimeout(250))+ const pos = (i) => p.evaluate((s) => window.bento.doc.slides[s].elements.map((e) => `${e.x},${e.y}`), i)++ const b = await p.locator('.ed-stage-scale [data-el-id="box"]').boundingBox()+ await p.mouse.click(b.x + b.width / 2, b.y + b.height / 2)+ await p.waitForTimeout(150)+ await p.keyboard.press(`${MOD}+c`)+ await p.waitForTimeout(150)+ ok(/"__bento":"clip"/.test(await p.evaluate(() => window.__clips.at(-1) ?? '')), '⌘C wrote a Bento clip payload')++ await thumb(1)+ await paste()+ const s2 = await pos(1)+ ok(s2.length === 1 && s2[0] === '200,160', `onto another slide it keeps its position (${s2.join(' ')})`)++ await thumb(0)+ await paste()+ await paste()+ const s1 = await pos(0)+ ok(s1.join(' ') === '200,160 220,180 240,200', `onto the source slide it steps +20, then cascades (${s1.join(' ')})`)++ ok(errors.length === 0, `no page errors${errors.length ? ': ' + errors.join('; ') : ''}`)+} finally { await browser.close() }++console.log(`\n${checks - failures}/${checks} checks passed`)+process.exit(failures ? 1 : 0)diff --git a/slides/src/editor/clipboard.ts b/slides/src/editor/clipboard.tsindex 8a68239b..a3585af2 100644--- a/slides/src/editor/clipboard.ts+++ b/slides/src/editor/clipboard.ts@@ -181,14 +181,34 @@ function rewriteRefs(els: SlideElement[], remap: Map<string, string>) {}}-/** Insert pasted elements onto a slide with fresh ids, nudged so they're visible. */+/**+ * How far to nudge a pasted group so it never lands exactly on top of an+ * element it duplicates (#569). Pasting onto ANOTHER slide keeps the copied+ * position — the point of copying a header or a logo across is that it lands+ * where it was. Only where an element of the same type and size already sits+ * at a pasted element's spot (the source slide, or a slide that got this+ * paste before) does the whole group step +20/+20, as many times as it takes,+ * so repeated pastes still cascade. Payload-independent, so a clip written by+ * an older build behaves the same.+ */+function pasteOffset(els: SlideElement[], slide: Slide): number {+ const sits = (e: SlideElement, d: number) => slide.elements.some((o) =>+ o.type === e.type && o.w === e.w && o.h === e.h && o.x === (e.x ?? 0) + d && o.y === (e.y ?? 0) + d)+ let d = 0+ while (d < 20 * 50 && els.some((e) => sits(e, d))) d += 20+ return d+}++/** Insert pasted elements onto a slide with fresh ids — at their copied+ * position, nudged only where they would cover what they duplicate. */export function insertElements(payload: ClipPayload, doc: BentoDoc, slide: Slide): SlideElement[] {const remap = mergeAssets(payload, doc)mergeFonts(payload, doc, remap)+ const d = pasteOffset(payload.elements ?? [], slide)const els: SlideElement[] = (payload.elements ?? []).map((e) => ({...(JSON.parse(JSON.stringify(e)) as SlideElement),id: uid(e.type[0]),- x: (e.x ?? 0) + 20, y: (e.y ?? 0) + 20,+ x: (e.x ?? 0) + d, y: (e.y ?? 0) + d,}))rewriteRefs(els, remap)slide.elements.push(...els)