fix coordinates
This commit is contained in:
parent
0bea258d39
commit
eb4dafaf9b
|
|
@ -411,62 +411,13 @@ export function useAutomergeStoreV2({
|
||||||
} else if (attempts < maxAttempts) {
|
} else if (attempts < maxAttempts) {
|
||||||
setTimeout(checkForPatches, 200)
|
setTimeout(checkForPatches, 200)
|
||||||
} else {
|
} else {
|
||||||
// Patches didn't come through - handler may have missed them if data was written before handler was set up
|
// Patches didn't come through - this may indicate a sync issue
|
||||||
// In this case, we need to manually apply the data via patches
|
// REMOVED: Fallback that applies records directly - it was causing coordinate loss
|
||||||
// We'll trigger patches by making a safe change that doesn't modify existing objects
|
// If patches don't work, it's a deeper sync issue that needs to be fixed
|
||||||
console.log(`⚠️ Patches didn't populate store. Handler may have missed initial patches. Applying data directly via patches...`)
|
// The fallback was resetting coordinates to 0,0 when re-applying records
|
||||||
|
console.warn(`⚠️ No patches received after ${maxAttempts} attempts. This may indicate a sync issue.`)
|
||||||
try {
|
console.warn(`⚠️ NOT using fallback direct record application to prevent coordinate loss.`)
|
||||||
// Read all records from Automerge doc and apply them directly to store
|
console.warn(`⚠️ If shapes aren't loading, check that patches are being generated correctly.`)
|
||||||
// This is a fallback when patches are missed (works for both dev and production)
|
|
||||||
// Use the same sanitization as patches would use to ensure consistency
|
|
||||||
const allRecords: TLRecord[] = []
|
|
||||||
Object.entries(doc.store).forEach(([id, record]: [string, any]) => {
|
|
||||||
// Skip invalid records and custom record types (same as patch processing)
|
|
||||||
if (!record || !record.typeName || !record.id) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip obsidian_vault records - they're not TLDraw records
|
|
||||||
if (record.typeName === 'obsidian_vault' ||
|
|
||||||
(typeof record.id === 'string' && record.id.startsWith('obsidian_vault:'))) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Create a clean copy of the record
|
|
||||||
const cleanRecord = JSON.parse(JSON.stringify(record))
|
|
||||||
// CRITICAL: Use the same sanitizeRecord function that patches use
|
|
||||||
// This ensures consistency between dev and production
|
|
||||||
const sanitized = sanitizeRecord(cleanRecord)
|
|
||||||
allRecords.push(sanitized)
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(`⚠️ Could not serialize/sanitize record ${id}:`, e)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (allRecords.length > 0) {
|
|
||||||
// Apply records directly to store using mergeRemoteChanges
|
|
||||||
// This bypasses patches but ensures data is loaded (works for both dev and production)
|
|
||||||
// Use mergeRemoteChanges to mark as remote changes (prevents feedback loop)
|
|
||||||
store.mergeRemoteChanges(() => {
|
|
||||||
// Separate pages, shapes, and other records to ensure proper loading order
|
|
||||||
const pageRecords = allRecords.filter(r => r.typeName === 'page')
|
|
||||||
const shapeRecords = allRecords.filter(r => r.typeName === 'shape')
|
|
||||||
const otherRecords = allRecords.filter(r => r.typeName !== 'page' && r.typeName !== 'shape')
|
|
||||||
|
|
||||||
// Put pages first, then other records, then shapes (ensures pages exist before shapes reference them)
|
|
||||||
const recordsToAdd = [...pageRecords, ...otherRecords, ...shapeRecords]
|
|
||||||
store.put(recordsToAdd)
|
|
||||||
})
|
|
||||||
console.log(`✅ Applied ${allRecords.length} records directly to store (fallback for missed patches - works in dev and production)`)
|
|
||||||
|
|
||||||
// REMOVED: Aggressive shape refresh that was causing coordinate loss
|
|
||||||
// Shapes loaded directly should be visible without forced refresh
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`❌ Error applying records directly:`, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
setStoreWithStatus({
|
setStoreWithStatus({
|
||||||
store,
|
store,
|
||||||
|
|
|
||||||
|
|
@ -377,10 +377,28 @@ export function Board() {
|
||||||
if (shapesWithInvalidParent.length > 0) {
|
if (shapesWithInvalidParent.length > 0) {
|
||||||
console.warn(`📊 Board: ${shapesWithInvalidParent.length} shapes have invalid or missing parentId. Fixing...`)
|
console.warn(`📊 Board: ${shapesWithInvalidParent.length} shapes have invalid or missing parentId. Fixing...`)
|
||||||
// Fix shapes with invalid parentId by assigning them to current page
|
// Fix shapes with invalid parentId by assigning them to current page
|
||||||
const fixedShapes = shapesWithInvalidParent.map((s: any) => ({
|
// CRITICAL: Preserve x and y coordinates when fixing parentId
|
||||||
...s,
|
const fixedShapes = shapesWithInvalidParent.map((s: any) => {
|
||||||
parentId: currentPageId
|
// Get the shape from store to ensure we have all properties
|
||||||
}))
|
const shapeFromStore = store.store!.get(s.id)
|
||||||
|
if (shapeFromStore && shapeFromStore.typeName === 'shape') {
|
||||||
|
// Preserve original x and y coordinates
|
||||||
|
const originalX = s.x !== undefined && typeof s.x === 'number' && !isNaN(s.x) ? s.x : (shapeFromStore as any).x
|
||||||
|
const originalY = s.y !== undefined && typeof s.y === 'number' && !isNaN(s.y) ? s.y : (shapeFromStore as any).y
|
||||||
|
|
||||||
|
// Create fixed shape with preserved coordinates
|
||||||
|
const fixed = { ...shapeFromStore, parentId: currentPageId }
|
||||||
|
if (typeof originalX === 'number' && !isNaN(originalX)) {
|
||||||
|
(fixed as any).x = originalX
|
||||||
|
}
|
||||||
|
if (typeof originalY === 'number' && !isNaN(originalY)) {
|
||||||
|
(fixed as any).y = originalY
|
||||||
|
}
|
||||||
|
return fixed as TLRecord
|
||||||
|
}
|
||||||
|
// Fallback if shape not in store
|
||||||
|
return { ...s, parentId: currentPageId } as TLRecord
|
||||||
|
})
|
||||||
try {
|
try {
|
||||||
store.store.put(fixedShapes)
|
store.store.put(fixedShapes)
|
||||||
console.log(`📊 Board: Fixed ${fixedShapes.length} shapes by assigning them to current page ${currentPageId}`)
|
console.log(`📊 Board: Fixed ${fixedShapes.length} shapes by assigning them to current page ${currentPageId}`)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue