@skillre/dsh-plugin-sticky-wall 0.2.2 → 0.3.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,16 +65,20 @@ 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
83
  export declare function arrangeLayouts(notes: readonly Note[]): Map<string, NoteLayout>;
75
84
  /** Normalize a submitted width preset, defaulting to the middle size. */
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
23
  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;
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,18 @@ 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
+ const radius = PREVIEW_STEP * Math.sqrt(Math.max(0, index));
171
+ const theta = index * GOLDEN_ANGLE + (unit(`${note.id}:pa`) - 0.5) * 0.55;
164
172
  return clampLayout({
165
- x: column * PREVIEW_CELL_W + unit(`${note.id}:px`) * 42,
166
- y: row * PREVIEW_CELL_H + unit(`${note.id}:py`) * 34,
173
+ x: Math.cos(theta) * radius - WIDTH_PX[width] / 2,
174
+ y: Math.sin(theta) * radius - PREVIEW_NOMINAL_HEIGHT / 2,
167
175
  rotate: (unit(`${note.id}:pr`) - 0.5) * 2 * PREVIEW_TILT,
168
176
  width,
169
177
  });
@@ -182,27 +190,71 @@ export function rotatedExtent(width, height, rotate) {
182
190
  const sin = Math.sin(radians);
183
191
  return { width: width * cos + height * sin, height: height * cos + width * sin };
184
192
  }
193
+ /** Cell coordinate of one world value. */
194
+ function cellOf(value) {
195
+ return Math.floor(value / GRID_CELL);
196
+ }
197
+ /**
198
+ * Whether `box` (plus the inter-card gap) hits anything already placed.
199
+ *
200
+ * The gap is applied on both sides, so a candidate exactly `TIDY_GAP` away from
201
+ * a placed card still fits; that is the contract the packing test asserts.
202
+ */
203
+ function collides(box, grid) {
204
+ for (let cellX = cellOf(box.x - TIDY_GAP); cellX <= cellOf(box.x + box.width + TIDY_GAP); cellX += 1) {
205
+ for (let cellY = cellOf(box.y - TIDY_GAP); cellY <= cellOf(box.y + box.height + TIDY_GAP); cellY += 1) {
206
+ const bucket = grid.get(`${cellX}:${cellY}`);
207
+ if (bucket === undefined)
208
+ continue;
209
+ for (const other of bucket) {
210
+ if (box.x < other.x + other.width + TIDY_GAP &&
211
+ other.x < box.x + box.width + TIDY_GAP &&
212
+ box.y < other.y + other.height + TIDY_GAP &&
213
+ other.y < box.y + box.height + TIDY_GAP) {
214
+ return true;
215
+ }
216
+ }
217
+ }
218
+ }
219
+ return false;
220
+ }
221
+ /** Register one accepted footprint in every cell it touches. */
222
+ function remember(box, grid) {
223
+ for (let cellX = cellOf(box.x); cellX <= cellOf(box.x + box.width); cellX += 1) {
224
+ for (let cellY = cellOf(box.y); cellY <= cellOf(box.y + box.height); cellY += 1) {
225
+ const key = `${cellX}:${cellY}`;
226
+ const bucket = grid.get(key);
227
+ if (bucket === undefined)
228
+ grid.set(key, [box]);
229
+ else
230
+ bucket.push(box);
231
+ }
232
+ }
233
+ }
185
234
  /**
186
235
  * Deterministic tidy arrangement for the whole wall.
187
236
  *
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.
237
+ * Phyllotaxis packing around the world origin: candidate positions are visited
238
+ * on a sunflower spiral and the first one that clears every placed card is
239
+ * accepted. The spiral's step is derived from the wall's own average card area,
240
+ * so a wall of small notes packs tightly while a wall of large ones does not
241
+ * overlap, and a big wall grows outward in a disc instead of a column that runs
242
+ * off the coordinate limit.
243
+ *
244
+ * A collision check rather than a shelf estimate is what makes the result
245
+ * trustworthy: the estimate is only a height guess, so any packing that merely
246
+ * trusts it can overlap at the cap. This one cannot.
194
247
  *
195
248
  * 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.
249
+ * move. Notes already at exactly that layout are omitted by the caller, so
250
+ * pressing tidy on an already-tidy wall writes nothing.
198
251
  */
199
252
  export function arrangeLayouts(notes) {
200
253
  const placed = new Map();
201
254
  if (notes.length === 0)
202
255
  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.
256
+ // Decide each card's rotation and footprint up front: the spiral step and the
257
+ // collision test both work on the rotated box.
206
258
  const items = notes.map((note) => {
207
259
  const width = note.layout?.width ?? previewWidth(note.id);
208
260
  const cardWidth = WIDTH_PX[width];
@@ -211,43 +263,72 @@ export function arrangeLayouts(notes) {
211
263
  const extent = rotatedExtent(cardWidth, cardHeight, rotate);
212
264
  return { note, width, cardWidth, cardHeight, rotate, extent };
213
265
  });
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
266
  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;
267
+ // A phyllotaxis point at index k owns `PI * step^2` of area, so a step derived
268
+ // from the average card area fills the disc without a hole in the middle or a
269
+ // sparse rim. The 1.06 slack absorbs the gap the packing has to keep.
270
+ const step = Math.max(72, Math.sqrt(totalArea / (Math.PI * items.length)) * 1.06);
271
+ const grid = new Map();
272
+ let cursor = 0;
273
+ let fallbackY = 0;
226
274
  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;
275
+ const { extent } = item;
276
+ let box = null;
277
+ for (let attempt = 0; attempt < MAX_CANDIDATES; attempt += 1) {
278
+ const index = cursor + attempt;
279
+ const jitter = (unit(`${item.note.id}:aj`) - 0.5) * 2 * TIDY_JITTER;
280
+ const radius = step * Math.sqrt(index) + jitter;
281
+ const theta = index * GOLDEN_ANGLE;
282
+ const candidate = {
283
+ x: Math.cos(theta) * radius - extent.width / 2,
284
+ y: Math.sin(theta) * radius - extent.height / 2,
285
+ width: extent.width,
286
+ height: extent.height,
287
+ };
288
+ if (!collides(candidate, grid)) {
289
+ box = candidate;
290
+ cursor = index + 1;
291
+ break;
292
+ }
293
+ }
294
+ if (box === null) {
295
+ // Unreachable for a wall inside the note cap; the fallback exists so the
296
+ // function still returns a placement for every note instead of throwing.
297
+ fallbackY += extent.height + TIDY_GAP;
298
+ box = { x: -extent.width / 2, y: fallbackY, width: extent.width, height: extent.height };
231
299
  }
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,
300
+ remember(box, grid);
301
+ // The rotated footprint is centred on the card, so the card's own top-left
302
+ // is the footprint centre minus the UNROTATED half size.
303
+ placed.set(item.note.id, clampLayout({
304
+ x: box.x + extent.width / 2 - item.cardWidth / 2,
305
+ y: box.y + extent.height / 2 - item.cardHeight / 2,
238
306
  rotate: item.rotate,
239
307
  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);
308
+ }));
246
309
  }
247
- if (minX === 0 && minY === 0)
248
- return placed;
310
+ // Re-centre the finished disc on its own centre of mass.
311
+ //
312
+ // The spiral is centred on the origin, but the first cards sit near it and the
313
+ // rest surround them unevenly, so the centroid of the PLACED cards is a few
314
+ // dozen pixels off. The camera frames the centroid, so leaving the offset in
315
+ // means the wall never looks quite centred after a tidy. A uniform translation
316
+ // cannot change any of the collision guarantees above.
317
+ let sumX = 0;
318
+ let sumY = 0;
249
319
  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 }));
320
+ const note = notes.find((candidate) => candidate.id === id);
321
+ const width = layout.width;
322
+ const height = note === undefined ? 0 : estimateHeight(note, width);
323
+ sumX += layout.x + WIDTH_PX[width] / 2;
324
+ sumY += layout.y + height / 2;
325
+ }
326
+ const shiftX = sumX / placed.size;
327
+ const shiftY = sumY / placed.size;
328
+ if (Math.abs(shiftX) >= 0.05 || Math.abs(shiftY) >= 0.05) {
329
+ for (const [id, layout] of placed) {
330
+ placed.set(id, clampLayout({ ...layout, x: layout.x - shiftX, y: layout.y - shiftY }));
331
+ }
251
332
  }
252
333
  return placed;
253
334
  }
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.3.0",
4
4
  "description": "便签墙:DSH 侧栏的自由画布便签板,可拖动摆放、一键整理,正文按 Markdown 渲染",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",