3.0 KiB
3.0 KiB
Sanitization Explanation
Why Sanitization Exists
Sanitization is necessary because TLDraw has strict schema requirements that must be met for shapes to render correctly. Without sanitization, we get validation errors and broken shapes.
Critical Fixes (MUST KEEP)
These fixes are required for TLDraw to work:
-
Move w/h/geo from top-level to props for geo shapes
- TLDraw schema requires
w,h, andgeoto be inprops, not at the top level - Without this, TLDraw throws validation errors
- TLDraw schema requires
-
Remove w/h from group shapes
- Group shapes don't have
w/hproperties - Having them causes validation errors
- Group shapes don't have
-
Remove w/h from line shapes
- Line shapes use
points, notw/h - Having them causes validation errors
- Line shapes use
-
Fix richText structure
- TLDraw requires
richTextto be{ content: [...], type: 'doc' } - Old data might have it as an array or missing structure
- We preserve all content, just fix the structure
- TLDraw requires
-
Fix crop structure for image/video
- TLDraw requires
cropto be{ topLeft: {x,y}, bottomRight: {x,y} }ornull - Old data might have
{ x, y, w, h }format - We convert the format, preserving the crop area
- TLDraw requires
-
Remove h/geo from text shapes
- Text shapes don't have
horgeoproperties - Having them causes validation errors
- Text shapes don't have
-
Ensure required properties exist
- Some shapes require certain properties (e.g.,
pointsfor line shapes) - We only add defaults if truly missing
- Some shapes require certain properties (e.g.,
What We Preserve
We preserve all user data:
- ✅
richTextcontent (we only fix structure, never delete content) - ✅
textproperty on arrows - ✅ All metadata (
metaobject) - ✅ All valid shape properties
- ✅ Custom shape properties
What We Remove (Only When Necessary)
We only remove properties that:
- Cause validation errors (e.g.,
w/hon groups/lines) - Are invalid for the shape type (e.g.,
geoon text shapes)
We never remove:
- User-created content (text, richText)
- Valid metadata
- Properties that don't cause errors
Current Sanitization Locations
-
TLStoreToAutomerge.ts - When saving from TLDraw to Automerge
- Minimal fixes only
- Preserves all data
-
AutomergeToTLStore.ts - When loading from Automerge to TLDraw
- Minimal fixes only
- Preserves all data
-
useAutomergeStoreV2.ts - Initial load processing
- More extensive (handles migration from old formats)
- Still preserves all user data
Can We Simplify?
Yes, but carefully:
- ✅ We can remove property deletions that don't cause validation errors
- ✅ We can consolidate duplicate logic
- ❌ We cannot remove schema fixes (w/h/geo movement, richText structure)
- ❌ We cannot remove property deletions that cause validation errors
Recommendation
Keep sanitization but:
- Only delete properties that actually cause validation errors
- Preserve all user data (text, richText, metadata)
- Consolidate duplicate logic between files
- Add comments explaining why each fix is necessary