fix(sticky): restore zoom-aware preset creation and live editing sync

This commit is contained in:
Hiren Kangad
2026-03-13 00:42:32 +05:30
parent 2d05226ddd
commit ce1cc60d67
4 changed files with 60 additions and 16 deletions
+37 -3
View File
@@ -1,9 +1,9 @@
/**
* stickyPresets — sticky note size presets (shared domain module).
*
* Defines the S/M/L/XL/XXL presets for sticky text size and card width.
* Used by both canvas logic (tools, SceneManager) and UI (TextFormatToolbar).
* No React dependencies.
* Presets are defined in screen-space so they preserve the same visual intent
* regardless of viewport zoom. Scene data still stores world-space width and
* fontSize; callers convert through the helpers below.
*/
export type StickyTextSize = 'S' | 'M' | 'L' | 'XL' | 'XXL';
@@ -36,3 +36,37 @@ export function nearestStickySize(fontSize: number): StickyTextSize {
export function getStickyWidthForSize(fontSize: number): number {
return STICKY_WIDTH_MAP[nearestStickySize(fontSize)];
}
/**
* Convert a preset's screen-space target to world-space sticky metrics.
* objectScale is included so old non-1 scaled stickies still respond
* predictably when a preset is applied.
*/
export function getStickyWorldMetricsForPreset(
preset: StickyTextSize,
zoom: number,
objectScale: number = 1,
): { fontSize: number; width: number } {
const safeZoom = Math.max(zoom, 0.001);
const safeScale = Math.max(Math.abs(objectScale), 0.001);
return {
fontSize: STICKY_FONT_MAP[preset] / (safeZoom * safeScale),
width: STICKY_WIDTH_MAP[preset] / (safeZoom * safeScale),
};
}
/**
* Convert a desired on-screen font size into world-space sticky metrics by
* snapping to the nearest preset first.
*/
export function getStickyWorldMetricsForScreenFont(
screenFontSize: number,
zoom: number,
objectScale: number = 1,
): { fontSize: number; width: number; preset: StickyTextSize } {
const preset = nearestStickySize(screenFontSize);
return {
...getStickyWorldMetricsForPreset(preset, zoom, objectScale),
preset,
};
}
+2 -2
View File
@@ -5,8 +5,8 @@
* Direct manipulation (resize-bake) intentionally bypasses these
* clamps so users can scale text to any size.
*
* Sticky notes use discrete presets (S/M/L/XL/XXL) defined in
* TextFormatToolbar.tsx — not continuous clamps.
* Sticky notes use discrete presets defined in stickyPresets.ts — not
* continuous clamps.
*/
const TEXT_FONT_MIN = 6;
+8 -4
View File
@@ -14,7 +14,7 @@ import { DrawingSprite } from './sprites/DrawingSprite';
import type { DrawingObject } from './scene-format';
import { TextEditor } from './TextEditor';
import { clampTextFontSize } from './textLimits';
import { DEFAULT_STICKY_PRESET, STICKY_FONT_MAP, STICKY_WIDTH_MAP } from './stickyPresets';
import { DEFAULT_STICKY_PRESET, getStickyWorldMetricsForPreset } from './stickyPresets';
export enum ToolType {
SELECT = 'SELECT',
@@ -324,8 +324,10 @@ export function activateTool(
const rect = container.getBoundingClientRect();
const world = viewport.toWorld(e.clientX - rect.left, e.clientY - rect.top);
const cardW = STICKY_WIDTH_MAP[DEFAULT_STICKY_PRESET];
const cardH = 60; // auto-computed by StickySprite
const zoom = viewport.scale.x;
const presetMetrics = getStickyWorldMetricsForPreset(DEFAULT_STICKY_PRESET, zoom);
const cardW = presetMetrics.width;
const cardH = screenToWorld(60, zoom, 40, 200);
const stickyData = {
id: crypto.randomUUID(),
type: 'sticky' as const,
@@ -342,7 +344,7 @@ export function activateTool(
name: '',
visible: true,
text: '',
fontSize: STICKY_FONT_MAP[DEFAULT_STICKY_PRESET],
fontSize: presetMetrics.fontSize,
fontFamily: 'Inter, system-ui, sans-serif',
fill: '#ffd43b', // default yellow
textColor: '#1a1a1a',
@@ -374,6 +376,8 @@ export function activateTool(
}
ctx.broadcastElements?.([item.id]);
ctx.onChange();
}, (resizedItem) => {
ctx.scene.updateSpatialEntry(resizedItem);
});
ctx.textEditor!.clearText();
});