@skillre/dsh-plugin-sticky-wall 0.2.1 → 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/README.en.md +7 -5
- package/README.md +7 -5
- package/compatibility.json +12 -5
- package/docs/preview.svg +61 -54
- package/docs/preview.zh.svg +61 -54
- package/lib/client.js +398 -159
- package/lib/layout.d.ts +26 -17
- package/lib/layout.js +146 -65
- package/package.json +1 -1
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
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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,
|
|
45
|
-
* result
|
|
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
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
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
|
|
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
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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
|
-
/**
|
|
22
|
-
const
|
|
23
|
-
/**
|
|
24
|
-
const
|
|
25
|
-
/**
|
|
26
|
-
const
|
|
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,
|
|
158
|
-
* result
|
|
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:
|
|
166
|
-
y:
|
|
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
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
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
|
|
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
|
|
204
|
-
//
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
let
|
|
225
|
-
let
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
x:
|
|
237
|
-
y:
|
|
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
|
-
|
|
248
|
-
|
|
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
|
-
|
|
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
|
}
|