Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: press and hold opens the same menu on a phone (#276)

iamgeo92948cdd66eb08
diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.ts
index 021954a8..c0d912eb 100644
--- a/slides/src/editor/editor.ts
+++ b/slides/src/editor/editor.ts
@@ -52,6 +52,13 @@ const JUST_UPDATED_KEY = 'bento-just-updated'
/** Show the language search once the available list outgrows a glance. */
const SEARCH_FROM = 8
+/** How long a finger must rest before a press becomes a menu. 500ms is what
+ * iOS itself uses for the callout, so it matches the muscle memory already on
+ * the device. */
+const LONG_PRESS_MS = 500
+/** …and how far it may wander first. Past this it was a drag or a pan. */
+const LONG_PRESS_SLOP = 10
+
const SHAPE_MENU: Array<{ kind: ShapeKind; label: string; icon: string; draw?: 'line' | 'path' | 'connector' | 'free' | 'poly'; tip: string }> = [
{ kind: 'rect', label: 'Rectangle', icon: ICONS.rect, tip: 'A rectangle — rounded corners, fills, gradients and shadows in the panel' },
{ kind: 'ellipse', label: 'Ellipse', icon: ICONS.ellipse, tip: 'An ellipse or circle' },
@@ -2929,38 +2936,104 @@ export class Editor {
}, true)
document.addEventListener('contextmenu', (ev) => {
- // Leave the browser's own menu wherever it is the better one: form
- // fields, links, and above all a text element mid-edit, where the native
- // menu carries spelling, dictation, look-up and the system paste.
const target = ev.target as HTMLElement | null
- if (!target?.closest) return
- if (target.closest('input, textarea, a, [contenteditable="true"]')) return
- // Text mid-edit belongs to the SYSTEM menu — spelling, dictation,
- // look-up and a real paste are all things this menu cannot offer. Only
- // when the press landed IN the text being edited, though: a right-click
- // elsewhere commits the edit and is a click on whatever it hit.
if (pressInsideEdit) return
- if (this.store.readOnly || this.presenting) return
+ if (this.openContextMenuAt(target, ev.clientX, ev.clientY)) ev.preventDefault()
+ })
- const thumb = target.closest<HTMLElement>('.ed-sidebar .ed-thumb')
- if (thumb) {
- ev.preventDefault()
- openCtxMenu(ev.clientX, ev.clientY, this.slideMenuItems(Number(thumb.dataset.index), thumb))
- return
- }
- if (!target.closest('.ed-scroll')) return // not the canvas — leave it alone
- ev.preventDefault()
- const id = this.elementIdAtPoint(ev.clientX, ev.clientY)
- if (!id) {
- openCtxMenu(ev.clientX, ev.clientY, this.canvasMenuItems())
- return
+ this.wireLongPress()
+ }
+
+ /**
+ * Open the right menu for whatever is at (x, y), or return false to say "not
+ * mine" — which is how the browser's own menu survives wherever it is the
+ * better one: form fields, links, and text mid-edit, where the system
+ * carries spelling, dictation, look-up and a real paste.
+ *
+ * Shared by the right-click and the long press, so the two can never drift.
+ */
+ private openContextMenuAt(target: HTMLElement | null, x: number, y: number): boolean {
+ if (!target?.closest) return false
+ if (target.closest('input, textarea, a, [contenteditable="true"]')) return false
+ if (this.store.readOnly || this.presenting) return false
+
+ const thumb = target.closest<HTMLElement>('.ed-sidebar .ed-thumb')
+ if (thumb) {
+ openCtxMenu(x, y, this.slideMenuItems(Number(thumb.dataset.index), thumb))
+ return true
+ }
+ if (!target.closest('.ed-scroll')) return false // not the canvas — leave it alone
+ const id = this.elementIdAtPoint(x, y)
+ if (!id) {
+ openCtxMenu(x, y, this.canvasMenuItems())
+ return true
+ }
+ // Aiming outside the selection moves it there first — the rule every editor
+ // follows, and the only way the menu's verbs can be honest about what they
+ // will act on.
+ if (!this.store.selection.includes(id)) this.store.select([id])
+ openCtxMenu(x, y, this.elementMenuItems())
+ return true
+ }
+
+ /**
+ * Touch: a press held in place IS the right-click.
+ *
+ * It has to be recognised by hand. iOS fires no `contextmenu` event for an
+ * ordinary element — a long press there raises the system callout, not a
+ * menu — so without this the whole feature above is mouse-only, and a phone
+ * keeps having no way to reach Duplicate, Delete, Group or the z-order.
+ *
+ * Cancelled by movement (that press was a drag or a pan) and by an early
+ * lift (that was a tap). Both matter: this listener sits on the same surface
+ * Moveable drags elements on, and stealing a drag would be worse than having
+ * no menu at all.
+ */
+ private wireLongPress() {
+ // TOUCH events, not pointer events. A pointer handler runs for a mouse too
+ // and has to filter itself back out by pointerType; cancelling the touchend
+ // then stops the browser SYNTHESIZING the tap that ends the press, instead
+ // of racing it with a listener that swallows mouse events after the fact.
+ // Same reasoning as the tap-to-edit recogniser in canvas.ts.
+ let press: { x: number; y: number; target: HTMLElement; timer: number; opened: boolean } | null = null
+ const cancel = () => {
+ if (!press) return
+ clearTimeout(press.timer)
+ press = null
+ }
+ this.root.addEventListener('touchstart', (ev) => {
+ cancel()
+ // a second finger is a pinch or a two-finger pan, never a press
+ if (ev.touches.length !== 1) return
+ const t = ev.touches[0]
+ const target = ev.target as HTMLElement | null
+ if (!target) return
+ const x = t.clientX
+ const y = t.clientY
+ const p: NonNullable<typeof press> = {
+ x, y, target, opened: false,
+ timer: window.setTimeout(() => {
+ // The element under the finger can have changed while the finger was
+ // down (a remote edit, a re-render), so the target is re-read here.
+ const at = (document.elementFromPoint(x, y) as HTMLElement | null) ?? target
+ p.opened = this.openContextMenuAt(at, x, y)
+ }, LONG_PRESS_MS),
}
- // Right-clicking outside the selection moves it there first — the rule
- // every editor follows, and the only way the menu's verbs can be honest
- // about what they will act on.
- if (!this.store.selection.includes(id)) this.store.select([id])
- openCtxMenu(ev.clientX, ev.clientY, this.elementMenuItems())
- })
+ press = p
+ }, true)
+ this.root.addEventListener('touchmove', (ev) => {
+ const t = ev.touches[0]
+ if (press && t && Math.hypot(t.clientX - press.x, t.clientY - press.y) > LONG_PRESS_SLOP) cancel()
+ }, true)
+ // non-passive: this is the listener that has to be able to cancel
+ this.root.addEventListener('touchend', (ev) => {
+ const p = press
+ cancel()
+ // The lift would otherwise be replayed as a click ON the menu that just
+ // appeared under the finger, and the row beneath it would fire itself.
+ if (p?.opened && ev.cancelable) ev.preventDefault()
+ }, { passive: false })
+ this.root.addEventListener('touchcancel', cancel, true)
}
private elementMenuItems(): CtxItem[] {
diff --git a/slides/src/styles.css b/slides/src/styles.css
index ffc6b3d6..90dc834c 100644
--- a/slides/src/styles.css
+++ b/slides/src/styles.css
@@ -2538,6 +2538,19 @@ input.ed-toggle { width: 16px; height: 16px; accent-color: #5E7699; cursor: poin
.ed-ctxmenu-danger:hover:not(:disabled) { background: #fdeced; }
.ed-ctxmenu-sep { height: 1px; margin: 4px 2px; background: var(--line); }
+/* A long press on the canvas or the slide list is the context-menu gesture
+ (editor.ts). iOS answers the same press with its own callout bubble — "Copy /
+ Look Up / Share" over a chunk of slide text — which would race ours and win
+ the user's attention. Text being EDITED is exempt: there the system bubble is
+ the better menu, and it carries paste, dictation and spelling with it. */
+.ed-scroll,
+.ed-sidebar .ed-thumb {
+ -webkit-touch-callout: none;
+}
+.ed-scroll .bento-editing {
+ -webkit-touch-callout: default;
+}
+
/* ————— saved-from-the-web notice, and the return gate ————————————————————
The web demo always starts a new deck. Someone who saved from it and came
back got a blank starter and read it as lost work (docs/DECISIONS.md