Group sticky notes into rows and return reading order
A whiteboard app can export a board of sticky notes to a document. People place notes by hand, so notes that look like one row are rarely aligned to the pixel. The export has to read the board the way a person would: row by row from the top, and across each row in the reading direction.
Implement orderNotes(notes, options). It returns the note ids in reading order.
orderNotes(notes, options?) // => string[]
// notes: Array<{ id: string, x: number, y: number, width: number, height: number }>
// options: { direction?: 'ltr' | 'rtl' } (default 'ltr')
x and y are the note’s top-left corner. y grows downward. width and height are positive.
y, then by x, then by their position in the input.y + height / 2) is less than the anchor’s bottom edge (anchor.y + anchor.height). Otherwise it starts a new row and becomes that row’s anchor.The anchor never changes once a row starts, even if a taller note joins the row later.
'ltr': inside a row, sort by left edge x, smallest first.'rtl': inside a row, sort by right edge x + width, largest first.y (smaller first), then by position in the input.const board = [
{ id: 'done', x: 360, y: 170, width: 100, height: 60 },
{ id: 'title', x: 40, y: 10, width: 300, height: 60 },
{ id: 'idea', x: 20, y: 120, width: 140, height: 100 },
{ id: 'todo', x: 380, y: 30, width: 120, height: 50 },
{ id: 'bug', x: 200, y: 100, width: 120, height: 80 },
];
orderNotes(board);
// => ['title', 'todo', 'idea', 'bug', 'done']
orderNotes(board, { direction: 'rtl' });
// => ['todo', 'title', 'bug', 'idea', 'done']
Sorted by y, the notes are title, todo, bug, idea, done. “title” anchors row 1 (bottom 70), and todo’s center (55) is above that, so it joins. “bug” starts row 2 (bottom 180). idea’s center (170) joins row 2. done’s center (200) is not above 180, so it starts row 3.
[] for an empty board.notes or the note objects.