Skip to content

AI-native design editor. Open-source Figma alternative.

feat: draw sticky notes the way frames are drawn (#1252)

Danila Poyarkov1c1986b20d15
diff --git a/changes/1252/added.md b/changes/1252/added.md
new file mode 100644
index 000000000..c84e7280b
--- /dev/null
+++ b/changes/1252/added.md
@@ -0,0 +1 @@
+- The sticky tool draws notes the way frames are drawn: a note appears at the press and grows with the drag, its text scaling with it, and a click places one with its corner there.
diff --git a/changes/1252/fixed.md b/changes/1252/fixed.md
new file mode 100644
index 000000000..f4b656e8a
--- /dev/null
+++ b/changes/1252/fixed.md
@@ -0,0 +1 @@
+- In Safari, the canvas cursor keeps changing after a drag, so resize and rotate cursors show over handles again.
diff --git a/packages/vue/src/canvas/tools/input.ts b/packages/vue/src/canvas/tools/input.ts
index d8678bb06..28efd1906 100644
--- a/packages/vue/src/canvas/tools/input.ts
+++ b/packages/vue/src/canvas/tools/input.ts
@@ -9,7 +9,7 @@ import { startShapeDraw, startTextDraw } from '#vue/shared/input/draw'
import { startPanDrag } from '#vue/shared/input/pan/drag'
import { handleSelectDown } from '#vue/shared/input/select'
import type { HitTestFns } from '#vue/shared/input/select'
-import { startStickyPlace } from '#vue/shared/input/sticky/place'
+import { startStickyDraw } from '#vue/shared/input/sticky/draw'
import type { DragState } from '#vue/shared/input/types'
type ToolMouseDownOptions = {
@@ -84,7 +84,7 @@ export function handleToolMouseDown({
}
if (tool === 'STICKY') {
- startStickyPlace(cx, cy, editor, setDrag)
+ startStickyDraw(cx, cy, editor, setDrag)
return
}
diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.ts
index 9c1efa238..c890ac143 100644
--- a/packages/vue/src/canvas/useCanvasInput.ts
+++ b/packages/vue/src/canvas/useCanvasInput.ts
@@ -185,6 +185,9 @@ export function useCanvasInput(
e.preventDefault()
return
}
+ // Safari treats an unprevented press as the start of a text selection drag and freezes the
+ // cursor until the pointer leaves the canvas; the canvas handles its own drags.
+ if (e.button === 0) e.preventDefault()
editor.setMeasurementMode('off')
const openValueEdit = valueEdit.edit.value
if (openValueEdit) valueEdit.commit(openValueEdit.value)
@@ -330,9 +333,8 @@ export function useCanvasInput(
return
}
- if (d.type === 'place') {
- d.x = cx
- d.y = cy
+ if (d.type === 'sticky-draw') {
+ d.move(cx, cy)
return
}
@@ -395,7 +397,7 @@ export function useCanvasInput(
/** Drops what a resize or pencil stroke showed without changing the document. */
function discardDrawnPreview(d: DragState) {
if (d.type === 'resize') cancelResizePreview(d, editor)
- else if (d.type === 'sticky-scale') d.cancel()
+ else if (d.type === 'sticky-scale' || d.type === 'sticky-draw') d.cancel()
else if (d.type === 'pencil-drag') editor.pencilCancel()
}
@@ -527,6 +529,7 @@ export function useCanvasInput(
const type = drag.value?.type
if (
type === 'draw' ||
+ type === 'sticky-draw' ||
type === 'rotate' ||
type === 'shape-handle' ||
type === 'sticky-scale'
diff --git a/packages/vue/src/shared/input/sticky/draw.ts b/packages/vue/src/shared/input/sticky/draw.ts
new file mode 100644
index 000000000..026b5efbb
--- /dev/null
+++ b/packages/vue/src/shared/input/sticky/draw.ts
@@ -0,0 +1,101 @@
+import { toRaw } from 'vue'
+
+import type { Editor } from '@open-pencil/core/editor'
+import { scaleNodeChanges, STICKY_SIZE, stickySublayers } from '@open-pencil/scene-graph'
+import type { SceneNode } from '@open-pencil/scene-graph'
+import type { Vector } from '@open-pencil/scene-graph/primitives'
+
+import { parentSpace } from '#vue/shared/input/draw'
+import { findDrawParent } from '#vue/shared/input/drop-target'
+import { subtree } from '#vue/shared/input/sticky/scale'
+import type { DragState, DragStickyDraw } from '#vue/shared/input/types'
+
+/** A drag shorter than this either way is a click, as for a drawn frame. */
+const CLICK_SIZE = 2
+
+/**
+ * Draws a note as a frame is drawn: it appears at the press and grows with the drag from that
+ * corner, its text and padding scaling with it since a note keeps its square shape. A click
+ * places a note of the default size with its corner there. Release types into it.
+ */
+export function startStickyDraw(
+ cx: number,
+ cy: number,
+ editor: Editor,
+ setDrag: (d: DragState) => void
+) {
+ editor.undo.beginBatch('Create sticky')
+ const parentId = findDrawParent(cx, cy, 'STICKY', editor)
+ const toLocal = parentSpace(editor, parentId)
+ const snap = editor.state.snappingPreferences.pixelGrid ? Math.round : (value: number) => value
+ const local = toLocal(cx, cy)
+ const start = { x: snap(local.x), y: snap(local.y) }
+ const sticky = editor.createStickyNote(parentId, {
+ x: start.x + STICKY_SIZE / 2,
+ y: start.y + STICKY_SIZE / 2
+ })
+ const originals = subtree(editor, sticky)
+ const graph = editor.graph
+ let size = 0
+ let finished = false
+
+ /** Shows the note at `side` from the press corner towards `toward`. */
+ function show(side: number, toward: Vector) {
+ const scale = Math.max(side, 1) / STICKY_SIZE
+ const extent = STICKY_SIZE * scale
+ const position = {
+ x: toward.x < start.x ? start.x - extent : start.x,
+ y: toward.y < start.y ? start.y - extent : start.y
+ }
+ for (const [id, original] of originals) {
+ const changes: Partial<SceneNode> = scaleNodeChanges(original, scale, id !== sticky.id)
+ if (id === sticky.id) Object.assign(changes, position)
+ graph.updateNodePreview(id, changes)
+ }
+ editor.requestRepaint()
+ return { scale, position }
+ }
+
+ let shown = show(0, start)
+
+ function restore() {
+ for (const [id, original] of originals) {
+ const changes: Partial<SceneNode> = scaleNodeChanges(original, 1, id !== sticky.id)
+ if (id === sticky.id) Object.assign(changes, { x: original.x, y: original.y })
+ graph.updateNodePreview(id, changes)
+ }
+ }
+
+ const drag: DragStickyDraw = {
+ type: 'sticky-draw',
+ move(x, y) {
+ const point = toLocal(x, y)
+ const toward = { x: snap(point.x), y: snap(point.y) }
+ size = Math.max(Math.abs(toward.x - start.x), Math.abs(toward.y - start.y))
+ shown = show(size, toward)
+ },
+ commit() {
+ if (finished) return
+ finished = true
+ restore()
+ if (editor.graph !== graph) {
+ editor.undo.rollbackBatch()
+ return
+ }
+ // A click keeps the note as it was made: the default size, its corner at the click.
+ if (size >= CLICK_SIZE) editor.scaleStickyNote(sticky.id, shown.scale, shown.position)
+ editor.undo.commitBatch()
+ editor.setTool('SELECT')
+ const body = stickySublayers(graph, toRaw(sticky))?.body
+ if (body) editor.startTextEditing(body.id)
+ },
+ cancel() {
+ if (finished) return
+ finished = true
+ restore()
+ if (editor.graph === graph) editor.undo.rollbackBatch()
+ editor.requestRender()
+ }
+ }
+ setDrag(drag)
+}
diff --git a/packages/vue/src/shared/input/sticky/place.ts b/packages/vue/src/shared/input/sticky/place.ts
deleted file mode 100644
index dd1120230..000000000
--- a/packages/vue/src/shared/input/sticky/place.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import type { Editor } from '@open-pencil/core/editor'
-import { stickySublayers } from '@open-pencil/scene-graph'
-
-import { parentSpace } from '#vue/shared/input/draw'
-import { findDrawParent } from '#vue/shared/input/drop-target'
-import type { DragPlace, DragState } from '#vue/shared/input/types'
-
-/** Places a note centred on the canvas point, then types into its body, as FigJam does. */
-function placeSticky(editor: Editor, cx: number, cy: number) {
- const parentId = findDrawParent(cx, cy, 'STICKY', editor)
- const sticky = editor.createStickyNote(parentId, parentSpace(editor, parentId)(cx, cy))
- editor.setTool('SELECT')
- const body = stickySublayers(editor.graph, sticky)?.body
- if (body) editor.startTextEditing(body.id)
-}
-
-/** The sticky tool places a note where the pointer is released, a click or the end of a drag. */
-export function startStickyPlace(
- cx: number,
- cy: number,
- editor: Editor,
- setDrag: (d: DragState) => void
-) {
- const drag: DragPlace = {
- type: 'place',
- x: cx,
- y: cy,
- commit: () => placeSticky(editor, drag.x, drag.y)
- }
- setDrag(drag)
-}
diff --git a/packages/vue/src/shared/input/sticky/scale.ts b/packages/vue/src/shared/input/sticky/scale.ts
index dd99c5384..54e3f36fb 100644
--- a/packages/vue/src/shared/input/sticky/scale.ts
+++ b/packages/vue/src/shared/input/sticky/scale.ts
@@ -17,7 +17,8 @@ import type { DragStickyScale } from '#vue/shared/input/types'
/** The smallest scale a corner drag takes a note to, so it never folds away. */
const MIN_STICKY_SCALE = 0.1
-function subtree(editor: Editor, root: SceneNode): Map<string, SceneNode> {
+/** The note and every layer in it, as they are now, to scale from. */
+export function subtree(editor: Editor, root: SceneNode): Map<string, SceneNode> {
const nodes = new Map<string, SceneNode>()
const visit = (node: SceneNode) => {
nodes.set(node.id, { ...node })
diff --git a/packages/vue/src/shared/input/types.ts b/packages/vue/src/shared/input/types.ts
index 91dd1d2ca..b9c5adde7 100644
--- a/packages/vue/src/shared/input/types.ts
+++ b/packages/vue/src/shared/input/types.ts
@@ -32,13 +32,13 @@ export interface DragDraw {
cancel: () => void
}
-/** A layer placed where the pointer is released, such as a sticky note. */
-export interface DragPlace {
- type: 'place'
- /** Where the pointer is, in canvas coordinates. */
- x: number
- y: number
+/** A sticky note being drawn: it grows from the press corner with the drag, as a frame does. */
+export interface DragStickyDraw {
+ type: 'sticky-draw'
+ /** Grows the note to the pointer, in canvas coordinates. */
+ move: (cx: number, cy: number) => void
commit: () => void
+ cancel: () => void
}
/**
@@ -237,7 +237,7 @@ export interface DragShapeHandle {
export type DragState =
| DragShapeHandle
| DragDraw
- | DragPlace
+ | DragStickyDraw
| DragGradient
| DragMove
| DragPan
diff --git a/tests/e2e/tools/sticky.spec.ts b/tests/e2e/tools/sticky.spec.ts
index b40ac78a2..2da7fb552 100644
--- a/tests/e2e/tools/sticky.spec.ts
+++ b/tests/e2e/tools/sticky.spec.ts
@@ -1,3 +1,5 @@
+import type { Vector } from '@open-pencil/scene-graph/primitives'
+
import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
import { stickyDriver } from '#tests/helpers/canvas/sticky-driver'
import { toolbarToolTestId } from '#tests/helpers/test-ids'
@@ -31,12 +33,12 @@ test('the toolbar keeps the sticky tool hidden, and S does not pick it', async (
expect((await sticky.focus()).tool).toBe('SELECT')
})
-test('the sticky tool places a note centred on the click to type into', async () => {
+test('a click with the sticky tool places a note with its corner there, to type into', async () => {
await sticky.showTool()
await sticky.pickTool()
await clickNote(300, 200)
const placed = await lastNote()
- expect(placed).toMatchObject({ x: 180, y: 80, width: 240, height: 240, body: { text: '' } })
+ expect(placed).toMatchObject({ x: 300, y: 200, width: 240, height: 240, body: { text: '' } })
const focus = await sticky.focus()
expect(focus.selected).toEqual([placed.id])
expect(focus.editing).not.toBeNull()
@@ -52,6 +54,65 @@ test('the sticky tool places a note centred on the click to type into', async ()
})
})
+/** Presses at `from` with the sticky tool and moves to `to`, both page points, holding the button. */
+async function pressAndDrag(from: Vector, to: Vector) {
+ const box = await editor.canvas.canvas.boundingBox()
+ if (!box) throw new Error('Missing canvas')
+ const start = sticky.point(from.x, from.y)
+ const end = sticky.point(to.x, to.y)
+ await editor.page.mouse.move(box.x + start.x, box.y + start.y)
+ await editor.page.mouse.down()
+ await editor.page.mouse.move(box.x + end.x, box.y + end.y, { steps: 6 })
+ await editor.canvas.waitForRender()
+}
+
+test('the sticky tool draws a note as a frame is drawn, growing with the drag', async () => {
+ await sticky.showTool()
+ await sticky.pickTool()
+ const before = (await sticky.notes()).length
+ await pressAndDrag({ x: 300, y: 200 }, { x: 420, y: 260 })
+ // While the button is held, the note is already there, as large as the drag.
+ expect(await sticky.notes()).toHaveLength(before + 1)
+ expect(await lastNote()).toMatchObject({ x: 300, y: 200, width: 120, height: 120 })
+ await editor.page.mouse.up()
+ await editor.canvas.waitForRender()
+ // Its text scales with it, as a note keeps its shape.
+ expect(await lastNote()).toMatchObject({
+ x: 300,
+ y: 200,
+ width: 120,
+ height: 120,
+ body: { fontSize: 8 }
+ })
+ expect((await sticky.focus()).editing).not.toBeNull()
+ await editor.page.keyboard.press('Escape')
+ // Drawing it was one step.
+ await editor.page.keyboard.press('ControlOrMeta+z')
+ await editor.canvas.waitForRender()
+ expect(await sticky.notes()).toHaveLength(before)
+})
+
+test('a note drawn up and to the left grows from the press that way', async () => {
+ await sticky.showTool()
+ await sticky.pickTool()
+ await pressAndDrag({ x: 600, y: 500 }, { x: 420, y: 440 })
+ await editor.page.mouse.up()
+ await editor.canvas.waitForRender()
+ expect(await lastNote()).toMatchObject({ x: 420, y: 320, width: 180, height: 180 })
+ await editor.page.keyboard.press('Escape')
+})
+
+test('Escape while drawing a note leaves no note', async () => {
+ await sticky.showTool()
+ await sticky.pickTool()
+ const before = (await sticky.notes()).length
+ await pressAndDrag({ x: 300, y: 200 }, { x: 400, y: 300 })
+ await editor.page.keyboard.press('Escape')
+ await editor.page.mouse.up()
+ await editor.canvas.waitForRender()
+ expect(await sticky.notes()).toHaveLength(before)
+})
+
test('the Sticky note section recolours a note, sizes its text, and hides its author', async () => {
await sticky.add([{ x: 300, y: 200, text: 'Panel', author: 'Ada' }])
await clickNote(300, 200)