@skillre/dsh-plugin-sticky-wall 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/layout.d.ts CHANGED
@@ -4,15 +4,18 @@
4
4
  * The wall is a canvas, not a grid: every card owns a world position, a
5
5
  * rotation and a width preset. Two operations produce those numbers:
6
6
  *
7
- * - {@link arrangeLayouts} is the Host-side "tidy" pass. It is deterministic:
8
- * the same notes always produce the same wall, so a user who presses tidy
9
- * twice does not get a reshuffled board, and a test can assert the exact
10
- * packing without a browser.
7
+ * - {@link arrangeLayouts} is the Host-side "tidy" pass. It packs the wall into
8
+ * a phyllotaxis (sunflower) disc **centred on the world origin**, so the wall
9
+ * has a middle instead of a top-left corner: the Client opens looking at that
10
+ * middle, and a tidied wall reads as one composition rather than a list that
11
+ * happens to wrap. It is deterministic — the same notes always produce the
12
+ * same wall — and it is collision-checked rather than estimated, so two cards
13
+ * can never overlap even at the height cap.
11
14
  * - {@link previewLayout} is the Client-side fallback for a note that was never
12
15
  * placed (a wall written before the canvas existed, or a note created by the
13
- * agent tool). It derives a position from the note's own id and its index in
14
- * the stable order, so the first render is already a scattered wall instead
15
- * of one tall stack, and it never writes to the Host.
16
+ * agent tool). It is the same spiral without the collision check: O(1) per
17
+ * note, index-stable, and centred, so the first render is already a scattered
18
+ * wall around the middle instead of one tall stack.
16
19
  *
17
20
  * Both halves import this module, so the geometry never drifts between the
18
21
  * value the Host persists and the value the Client previews.
@@ -41,8 +44,10 @@ export declare function layoutOf(note: Note, index: number): NoteLayout;
41
44
  /**
42
45
  * Derived position for a note the user never placed.
43
46
  *
44
- * Index-based so two never-placed notes cannot collide, hash-jittered so the
45
- * result still looks scattered rather than gridded.
47
+ * Index-based so two never-placed notes cannot collide, golden-angle spiralled
48
+ * so the result reads as a wall scattered around the middle rather than a grid.
49
+ * The height used for centring is a nominal constant, not the real estimate:
50
+ * a derived position must not move when the user edits the note's text.
46
51
  */
47
52
  export declare function previewLayout(note: Note, index: number): NoteLayout;
48
53
  /**
@@ -60,17 +65,23 @@ export declare function rotatedExtent(width: number, height: number, rotate: num
60
65
  /**
61
66
  * Deterministic tidy arrangement for the whole wall.
62
67
  *
63
- * Shelf packing: cards flow left to right with a hash jitter and a small tilt,
64
- * wrapping to a new shelf when the row is full. The shelf width adapts to the
65
- * wall: a large wall gets wider shelves so the arrangement stays roughly square
66
- * instead of growing into a single column kilometres tall. The output is shifted
67
- * so the top-left card starts near the origin, which keeps the canvas' scroll
68
- * extent predictable instead of starting inside negative space.
68
+ * Phyllotaxis packing around the world origin: candidate positions are visited
69
+ * on a sunflower spiral and the first one that clears every placed card is
70
+ * accepted. The spiral's step is derived from the wall's own average card area,
71
+ * so a wall of small notes packs tightly while a wall of large ones does not
72
+ * overlap, and a big wall grows outward in a disc instead of a column that runs
73
+ * off the coordinate limit.
74
+ *
75
+ * A collision check rather than a shelf estimate is what makes the result
76
+ * trustworthy: the estimate is only a height guess, so any packing that merely
77
+ * trusts it can overlap at the cap. This one cannot.
69
78
  *
70
79
  * Returns a map from note id to its new layout, for every note that needs to
71
- * move. Notes already at exactly that layout are omitted, so pressing tidy on
72
- * an already-tidy wall writes nothing.
80
+ * move. Notes already at exactly that layout are omitted by the caller, so
81
+ * pressing tidy on an already-tidy wall writes nothing.
73
82
  */
74
- export declare function arrangeLayouts(notes: readonly Note[]): Map<string, NoteLayout>;
83
+ export declare function arrangeLayouts(notes: readonly Note[], options?: {
84
+ centreRadius?: number;
85
+ }): Map<string, NoteLayout>;
75
86
  /** Normalize a submitted width preset, defaulting to the middle size. */
76
87
  export declare function normalizeWidth(raw: unknown): NoteWidth;
package/lib/layout.js CHANGED
@@ -4,35 +4,41 @@
4
4
  * The wall is a canvas, not a grid: every card owns a world position, a
5
5
  * rotation and a width preset. Two operations produce those numbers:
6
6
  *
7
- * - {@link arrangeLayouts} is the Host-side "tidy" pass. It is deterministic:
8
- * the same notes always produce the same wall, so a user who presses tidy
9
- * twice does not get a reshuffled board, and a test can assert the exact
10
- * packing without a browser.
7
+ * - {@link arrangeLayouts} is the Host-side "tidy" pass. It packs the wall into
8
+ * a phyllotaxis (sunflower) disc **centred on the world origin**, so the wall
9
+ * has a middle instead of a top-left corner: the Client opens looking at that
10
+ * middle, and a tidied wall reads as one composition rather than a list that
11
+ * happens to wrap. It is deterministic — the same notes always produce the
12
+ * same wall — and it is collision-checked rather than estimated, so two cards
13
+ * can never overlap even at the height cap.
11
14
  * - {@link previewLayout} is the Client-side fallback for a note that was never
12
15
  * placed (a wall written before the canvas existed, or a note created by the
13
- * agent tool). It derives a position from the note's own id and its index in
14
- * the stable order, so the first render is already a scattered wall instead
15
- * of one tall stack, and it never writes to the Host.
16
+ * agent tool). It is the same spiral without the collision check: O(1) per
17
+ * note, index-stable, and centred, so the first render is already a scattered
18
+ * wall around the middle instead of one tall stack.
16
19
  *
17
20
  * Both halves import this module, so the geometry never drifts between the
18
21
  * value the Host persists and the value the Client previews.
19
22
  */
20
- import { LAYOUT_LIMITS, WIDTHS, WIDTH_PX } from './types.js';
21
- /** Minimum horizontal breathing room between two cards in the tidy packing. */
22
- const SHELF_GAP_X = 26;
23
- /** Minimum vertical breathing room between two shelves in the tidy packing. */
24
- const SHELF_GAP_Y = 30;
25
- /** Narrowest tidy shelf; the packer widens it for very large walls. */
26
- const SHELF_WIDTH = 1180;
27
- /** Preview grid cell, generous enough that scattered cards rarely overlap. */
28
- const PREVIEW_CELL_W = 300;
29
- const PREVIEW_CELL_H = 232;
30
- /** Preview columns; four cells read as a wall on a typical centre column. */
31
- const PREVIEW_COLUMNS = 4;
23
+ import { CENTRE_RADIUS, LAYOUT_LIMITS, WIDTHS, WIDTH_PX } from './types.js';
24
+ /** Golden angle: the phyllotaxis constant that keeps a spiral from lining up. */
25
+ const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
26
+ /** Breathing room kept between two tidied cards, in world pixels. */
27
+ const TIDY_GAP = 26;
28
+ /** Hash-driven radial offset applied to a tidied card, in world pixels. */
29
+ const TIDY_JITTER = 5;
32
30
  /** Maximum rotation the tidy pass adds, in degrees. */
33
31
  const TIDY_TILT = 3.4;
32
+ /** Radial growth per derived (never-placed) note, in world pixels. */
33
+ const PREVIEW_STEP = 196;
34
+ /** Height assumed for a derived position, so the derived box looks centred. */
35
+ const PREVIEW_NOMINAL_HEIGHT = 200;
34
36
  /** Maximum rotation the preview fallback adds, in degrees. */
35
37
  const PREVIEW_TILT = 4;
38
+ /** Spatial-grid cell; wider than the biggest rotated card plus the gap. */
39
+ const GRID_CELL = 430;
40
+ /** Candidate spiral positions tried per card before the safety fallback. */
41
+ const MAX_CANDIDATES = 4096;
36
42
  /** Deterministic 32-bit FNV-1a hash; no crypto, identical on both halves. */
37
43
  function hash(text) {
38
44
  let value = 0x811c9dc5;
@@ -154,16 +160,20 @@ export function layoutOf(note, index) {
154
160
  /**
155
161
  * Derived position for a note the user never placed.
156
162
  *
157
- * Index-based so two never-placed notes cannot collide, hash-jittered so the
158
- * result still looks scattered rather than gridded.
163
+ * Index-based so two never-placed notes cannot collide, golden-angle spiralled
164
+ * so the result reads as a wall scattered around the middle rather than a grid.
165
+ * The height used for centring is a nominal constant, not the real estimate:
166
+ * a derived position must not move when the user edits the note's text.
159
167
  */
160
168
  export function previewLayout(note, index) {
161
- const column = index % PREVIEW_COLUMNS;
162
- const row = Math.floor(index / PREVIEW_COLUMNS);
163
169
  const width = previewWidth(note.id);
170
+ // The derived wall leaves the same clear middle as the tidy pass: the centre
171
+ // is the wall's centrepiece, not another place for a card.
172
+ const radius = PREVIEW_STEP * Math.sqrt(Math.max(0, index) + (CENTRE_RADIUS / PREVIEW_STEP) ** 2);
173
+ const theta = index * GOLDEN_ANGLE + (unit(`${note.id}:pa`) - 0.5) * 0.55;
164
174
  return clampLayout({
165
- x: column * PREVIEW_CELL_W + unit(`${note.id}:px`) * 42,
166
- y: row * PREVIEW_CELL_H + unit(`${note.id}:py`) * 34,
175
+ x: Math.cos(theta) * radius - WIDTH_PX[width] / 2,
176
+ y: Math.sin(theta) * radius - PREVIEW_NOMINAL_HEIGHT / 2,
167
177
  rotate: (unit(`${note.id}:pr`) - 0.5) * 2 * PREVIEW_TILT,
168
178
  width,
169
179
  });
@@ -182,27 +192,85 @@ export function rotatedExtent(width, height, rotate) {
182
192
  const sin = Math.sin(radians);
183
193
  return { width: width * cos + height * sin, height: height * cos + width * sin };
184
194
  }
195
+ /**
196
+ * Distance from the world origin to the nearest point of one footprint.
197
+ *
198
+ * The tidy pass keeps the wall's middle clear for the centrepiece, and "clear"
199
+ * has to mean the card's EDGE: measuring from the centre let a wide card reach
200
+ * over the capture field by half its own width.
201
+ */
202
+ function nearestEdgeRadius(box) {
203
+ const centreX = box.x + box.width / 2;
204
+ const centreY = box.y + box.height / 2;
205
+ const dx = Math.max(0, Math.abs(centreX) - box.width / 2);
206
+ const dy = Math.max(0, Math.abs(centreY) - box.height / 2);
207
+ return Math.hypot(dx, dy);
208
+ }
209
+ /** Cell coordinate of one world value. */
210
+ function cellOf(value) {
211
+ return Math.floor(value / GRID_CELL);
212
+ }
213
+ /**
214
+ * Whether `box` (plus the inter-card gap) hits anything already placed.
215
+ *
216
+ * The gap is applied on both sides, so a candidate exactly `TIDY_GAP` away from
217
+ * a placed card still fits; that is the contract the packing test asserts.
218
+ */
219
+ function collides(box, grid) {
220
+ for (let cellX = cellOf(box.x - TIDY_GAP); cellX <= cellOf(box.x + box.width + TIDY_GAP); cellX += 1) {
221
+ for (let cellY = cellOf(box.y - TIDY_GAP); cellY <= cellOf(box.y + box.height + TIDY_GAP); cellY += 1) {
222
+ const bucket = grid.get(`${cellX}:${cellY}`);
223
+ if (bucket === undefined)
224
+ continue;
225
+ for (const other of bucket) {
226
+ if (box.x < other.x + other.width + TIDY_GAP &&
227
+ other.x < box.x + box.width + TIDY_GAP &&
228
+ box.y < other.y + other.height + TIDY_GAP &&
229
+ other.y < box.y + box.height + TIDY_GAP) {
230
+ return true;
231
+ }
232
+ }
233
+ }
234
+ }
235
+ return false;
236
+ }
237
+ /** Register one accepted footprint in every cell it touches. */
238
+ function remember(box, grid) {
239
+ for (let cellX = cellOf(box.x); cellX <= cellOf(box.x + box.width); cellX += 1) {
240
+ for (let cellY = cellOf(box.y); cellY <= cellOf(box.y + box.height); cellY += 1) {
241
+ const key = `${cellX}:${cellY}`;
242
+ const bucket = grid.get(key);
243
+ if (bucket === undefined)
244
+ grid.set(key, [box]);
245
+ else
246
+ bucket.push(box);
247
+ }
248
+ }
249
+ }
185
250
  /**
186
251
  * Deterministic tidy arrangement for the whole wall.
187
252
  *
188
- * Shelf packing: cards flow left to right with a hash jitter and a small tilt,
189
- * wrapping to a new shelf when the row is full. The shelf width adapts to the
190
- * wall: a large wall gets wider shelves so the arrangement stays roughly square
191
- * instead of growing into a single column kilometres tall. The output is shifted
192
- * so the top-left card starts near the origin, which keeps the canvas' scroll
193
- * extent predictable instead of starting inside negative space.
253
+ * Phyllotaxis packing around the world origin: candidate positions are visited
254
+ * on a sunflower spiral and the first one that clears every placed card is
255
+ * accepted. The spiral's step is derived from the wall's own average card area,
256
+ * so a wall of small notes packs tightly while a wall of large ones does not
257
+ * overlap, and a big wall grows outward in a disc instead of a column that runs
258
+ * off the coordinate limit.
259
+ *
260
+ * A collision check rather than a shelf estimate is what makes the result
261
+ * trustworthy: the estimate is only a height guess, so any packing that merely
262
+ * trusts it can overlap at the cap. This one cannot.
194
263
  *
195
264
  * Returns a map from note id to its new layout, for every note that needs to
196
- * move. Notes already at exactly that layout are omitted, so pressing tidy on
197
- * an already-tidy wall writes nothing.
265
+ * move. Notes already at exactly that layout are omitted by the caller, so
266
+ * pressing tidy on an already-tidy wall writes nothing.
198
267
  */
199
- export function arrangeLayouts(notes) {
268
+ export function arrangeLayouts(notes, options = {}) {
200
269
  const placed = new Map();
201
270
  if (notes.length === 0)
202
271
  return placed;
203
- // Decide each card's rotation and box up front: the shelf width and the
204
- // advancement both depend on the rotated extent, and clamping has to happen
205
- // before anything is measured.
272
+ // Decide each card's rotation and footprint up front: the spiral step and the
273
+ // collision test both work on the rotated box.
206
274
  const items = notes.map((note) => {
207
275
  const width = note.layout?.width ?? previewWidth(note.id);
208
276
  const cardWidth = WIDTH_PX[width];
@@ -211,43 +279,102 @@ export function arrangeLayouts(notes) {
211
279
  const extent = rotatedExtent(cardWidth, cardHeight, rotate);
212
280
  return { note, width, cardWidth, cardHeight, rotate, extent };
213
281
  });
214
- // Grow the shelf so a large wall stays square-ish. The bound is the world
215
- // coordinate limit: an arrangement that would exceed it is a bug, not
216
- // something to silently clamp (clamping pins every card to the same edge and
217
- // produces massive overlap).
218
282
  const totalArea = items.reduce((sum, item) => sum + item.extent.width * item.extent.height, 0);
219
- const targetWidth = Math.max(SHELF_WIDTH, Math.ceil(Math.sqrt(totalArea) * 1.25));
220
- const shelfWidth = Math.min(targetWidth, LAYOUT_LIMITS.coord);
221
- let cursorX = 0;
222
- let shelfY = 0;
223
- let shelfHeight = 0;
224
- let minX = 0;
225
- let minY = 0;
283
+ // A phyllotaxis point at index k owns `PI * step^2` of area, so a step derived
284
+ // from the average card area fills the disc without a hole in the middle or a
285
+ // sparse rim. The 1.06 slack absorbs the gap the packing has to keep.
286
+ const step = Math.max(72, Math.sqrt(totalArea / (Math.PI * items.length)) * 1.06);
287
+ // Leave a clear disc in the middle.
288
+ //
289
+ // The wall is a composition around a centre, and that centre is where the
290
+ // greeting and the quick-capture field live: packing a card on top of them
291
+ // would make the wall fight its own centrepiece. Offsetting the spiral's start
292
+ // index puts every card beyond `centreRadius` while keeping the same
293
+ // phyllotaxis density, so the ring around the middle stays evenly packed.
294
+ const centreRadius = Math.max(0, options.centreRadius ?? CENTRE_RADIUS);
295
+ const startIndex = centreRadius > 0 ? (centreRadius / step) ** 2 : 0;
296
+ const grid = new Map();
297
+ let cursor = 0;
298
+ let fallbackY = 0;
226
299
  for (const item of items) {
227
- if (cursorX > 0 && cursorX + item.extent.width > shelfWidth) {
228
- cursorX = 0;
229
- shelfY += shelfHeight + SHELF_GAP_Y;
230
- shelfHeight = 0;
300
+ const { extent } = item;
301
+ let box = null;
302
+ for (let attempt = 0; attempt < MAX_CANDIDATES; attempt += 1) {
303
+ const index = startIndex + cursor + attempt;
304
+ const jitter = (unit(`${item.note.id}:aj`) - 0.5) * 2 * TIDY_JITTER;
305
+ const radius = step * Math.sqrt(index) + jitter;
306
+ const theta = index * GOLDEN_ANGLE;
307
+ const candidate = {
308
+ x: Math.cos(theta) * radius - extent.width / 2,
309
+ y: Math.sin(theta) * radius - extent.height / 2,
310
+ width: extent.width,
311
+ height: extent.height,
312
+ };
313
+ // The reserve is measured to the card's NEAREST EDGE, not its centre: a
314
+ // card whose centre clears the radius can still reach into the middle by
315
+ // half its own width, which is what let a wide card sit on the centrepiece.
316
+ if (nearestEdgeRadius(candidate) < centreRadius)
317
+ continue;
318
+ if (!collides(candidate, grid)) {
319
+ box = candidate;
320
+ cursor = index + 1;
321
+ break;
322
+ }
323
+ }
324
+ if (box === null) {
325
+ // Unreachable for a wall inside the note cap; the fallback exists so the
326
+ // function still returns a placement for every note instead of throwing.
327
+ fallbackY += extent.height + TIDY_GAP;
328
+ box = { x: -extent.width / 2, y: fallbackY, width: extent.width, height: extent.height };
231
329
  }
232
- // The jitter is bounded so it can never close the inter-card gap: the gap
233
- // budget is SHELF_GAP_X plus both cards' rotation expansion, and the jitter
234
- // spends at most 5px of it.
235
- const layout = clampLayout({
236
- x: cursorX + (unit(`${item.note.id}:ax`) - 0.5) * 6,
237
- y: shelfY + (unit(`${item.note.id}:ay`) - 0.5) * 8,
330
+ remember(box, grid);
331
+ // The rotated footprint is centred on the card, so the card's own top-left
332
+ // is the footprint centre minus the UNROTATED half size.
333
+ placed.set(item.note.id, clampLayout({
334
+ x: box.x + extent.width / 2 - item.cardWidth / 2,
335
+ y: box.y + extent.height / 2 - item.cardHeight / 2,
238
336
  rotate: item.rotate,
239
337
  width: item.width,
240
- });
241
- minX = Math.min(minX, layout.x);
242
- minY = Math.min(minY, layout.y);
243
- placed.set(item.note.id, layout);
244
- cursorX += item.extent.width + SHELF_GAP_X;
245
- shelfHeight = Math.max(shelfHeight, item.extent.height);
338
+ }));
246
339
  }
247
- if (minX === 0 && minY === 0)
248
- return placed;
340
+ // Re-centre the finished disc on its own centre of mass.
341
+ //
342
+ // The spiral is centred on the origin, but the first cards sit near it and the
343
+ // rest surround them unevenly, so the centroid of the PLACED cards is a few
344
+ // dozen pixels off. The camera frames the centroid, so leaving the offset in
345
+ // means the wall never looks quite centred after a tidy. A uniform translation
346
+ // cannot change any of the collision guarantees above.
347
+ let sumX = 0;
348
+ let sumY = 0;
249
349
  for (const [id, layout] of placed) {
250
- placed.set(id, clampLayout({ ...layout, x: layout.x - minX + SHELF_GAP_X, y: layout.y - minY + SHELF_GAP_Y }));
350
+ const note = notes.find((candidate) => candidate.id === id);
351
+ const width = layout.width;
352
+ const height = note === undefined ? 0 : estimateHeight(note, width);
353
+ sumX += layout.x + WIDTH_PX[width] / 2;
354
+ sumY += layout.y + height / 2;
355
+ }
356
+ const shiftX = sumX / placed.size;
357
+ const shiftY = sumY / placed.size;
358
+ if (Math.abs(shiftX) >= 0.05 || Math.abs(shiftY) >= 0.05) {
359
+ // A uniform translation cannot create an overlap, but it CAN push a card back
360
+ // into the reserved middle — which is exactly what happened when the shift
361
+ // was applied unconditionally. Shift only when the reserve survives it.
362
+ const safe = [...placed].every(([id, layout]) => {
363
+ const note = notes.find((candidate) => candidate.id === id);
364
+ const height = note === undefined ? 0 : estimateHeight(note, layout.width);
365
+ const box = {
366
+ x: layout.x - shiftX,
367
+ y: layout.y - shiftY,
368
+ width: WIDTH_PX[layout.width],
369
+ height,
370
+ };
371
+ return nearestEdgeRadius(box) >= centreRadius - 0.5;
372
+ });
373
+ if (safe) {
374
+ for (const [id, layout] of placed) {
375
+ placed.set(id, clampLayout({ ...layout, x: layout.x - shiftX, y: layout.y - shiftY }));
376
+ }
377
+ }
251
378
  }
252
379
  return placed;
253
380
  }
package/lib/types.d.ts CHANGED
@@ -47,6 +47,15 @@ export declare const WIDTHS: readonly ["sm", "md", "lg"];
47
47
  export type NoteWidth = (typeof WIDTHS)[number];
48
48
  /** Rendered card width for each preset, in world pixels at zoom 1. */
49
49
  export declare const WIDTH_PX: Readonly<Record<NoteWidth, number>>;
50
+ /**
51
+ * Radius of the clear disc the tidy pass leaves at the wall's centre, in world
52
+ * pixels at zoom 1.
53
+ *
54
+ * The centre of the wall is its centrepiece: the greeting, the running tally and
55
+ * the quick-capture field live there. Packing a card on top of them would make
56
+ * the wall fight its own middle, so tidy starts every card outside this radius.
57
+ */
58
+ export declare const CENTRE_RADIUS = 300;
50
59
  /** Bounds of the free canvas: a note may sit anywhere inside this square. */
51
60
  export declare const LAYOUT_LIMITS: Readonly<{
52
61
  /** Absolute value of x and y, in world pixels. */
package/lib/types.js CHANGED
@@ -55,6 +55,15 @@ export const WIDTH_PX = Object.freeze({
55
55
  md: 222,
56
56
  lg: 276,
57
57
  });
58
+ /**
59
+ * Radius of the clear disc the tidy pass leaves at the wall's centre, in world
60
+ * pixels at zoom 1.
61
+ *
62
+ * The centre of the wall is its centrepiece: the greeting, the running tally and
63
+ * the quick-capture field live there. Packing a card on top of them would make
64
+ * the wall fight its own middle, so tidy starts every card outside this radius.
65
+ */
66
+ export const CENTRE_RADIUS = 300;
58
67
  /** Bounds of the free canvas: a note may sit anywhere inside this square. */
59
68
  export const LAYOUT_LIMITS = Object.freeze({
60
69
  /** Absolute value of x and y, in world pixels. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skillre/dsh-plugin-sticky-wall",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "便签墙:DSH 侧栏的自由画布便签板,可拖动摆放、一键整理,正文按 Markdown 渲染",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",