diff --git a/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts b/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts index baff49833430..893f1b637b74 100644 --- a/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts +++ b/packages/sanity/src/structure/comments/src/hooks/use-comment-operations/createOperation.ts @@ -9,6 +9,7 @@ import { type CommentIntentGetter, type CommentPostPayload, } from '../../types' +import {weakenReferencesInContentSnapshot} from '../../utils' interface CreateOperationProps { activeTool: Tool | undefined @@ -109,6 +110,12 @@ export async function createOperation(props: CreateOperationProps): Promise>, +): Partial> | 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> = {} + + 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) +}