-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(comments): weaken references in content snapshot
- Loading branch information
1 parent
78caa54
commit a50bd75
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/sanity/src/structure/comments/src/utils/weakenReferencesInContentSnapshot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {type CommentDocument} from '../types' | ||
|
||
function weakenReferences( | ||
node: Partial<Record<string, any>>, | ||
): Partial<Record<string, any>> | undefined { | ||
// Check if the node is directly a reference | ||
if (node && typeof node === 'object' && node.hasOwnProperty('_ref')) { | ||
// Return a new object with the _weak property added | ||
return {...node, _weak: true} | ||
} else if (Array.isArray(node)) { | ||
// Check if the node is an array | ||
// Process each item in the array | ||
return node.map((item) => weakenReferences(item)) | ||
} else if (node && typeof node === 'object') { | ||
// For all other objects, create a new object to accumulate the results | ||
const result: Partial<Record<string, any>> = {} | ||
|
||
Object.keys(node).forEach((key) => { | ||
const value = node[key] | ||
if (typeof value === 'object' && value !== null) { | ||
// Recursively apply the function to object properties or array items | ||
result[key] = weakenReferences(value) | ||
} else { | ||
// Directly copy other values | ||
result[key] = value | ||
} | ||
}) | ||
return result | ||
} | ||
|
||
// Return undefined for non-objects | ||
return node | ||
} | ||
|
||
export function weakenReferencesInContentSnapshot( | ||
snapshot: CommentDocument['contentSnapshot'], | ||
): CommentDocument['contentSnapshot'] { | ||
if (!snapshot) return snapshot | ||
|
||
return Array.isArray(snapshot) ? snapshot.map(weakenReferences) : weakenReferences(snapshot) | ||
} |