| ... | ... | @@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = { |
| 42 | 42 | export type ComposerCitationCommentTarget = { |
| 43 | 43 | nodeKey: NodeKey; |
| 44 | 44 | sourceAnchor?: AssistantCitationSourceAnchor; |
| 45 | removeOnCancel?: boolean; |
| 45 | 46 | }; |
| 46 | 47 | |
| 47 | 48 | export const ComposerCitationCommentContext = createContext<{ |
| ... | ... | @@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: { |
| 68 | 69 | let offset = 0; |
| 69 | 70 | for (const node of paragraph.getChildren()) { |
| 70 | 71 | if (offset === request.citationStart && node instanceof ComposerCitationNode) { |
| 71 | | return { nodeKey: node.getKey(), sourceAnchor: request.sourceAnchor }; |
| 72 | return { |
| 73 | nodeKey: node.getKey(), |
| 74 | sourceAnchor: request.sourceAnchor, |
| 75 | removeOnCancel: true, |
| 76 | }; |
| 72 | 77 | } |
| 73 | 78 | offset += node.getTextContentSize(); |
| 74 | 79 | } |
| ... | ... | @@ -78,6 +83,8 @@ export function $consumeComposerCitationCommentRequest(requestRef: { |
| 78 | 83 | function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey: NodeKey }) { |
| 79 | 84 | const [editor] = useLexicalComposerContext(); |
| 80 | 85 | const commentContext = use(ComposerCitationCommentContext); |
| 86 | const commentTarget = |
| 87 | commentContext.openComment?.nodeKey === props.nodeKey ? commentContext.openComment : null; |
| 81 | 88 | const onSaveComment = (comment: string): boolean => { |
| 82 | 89 | if (!editor.isEditable()) return false; |
| 83 | 90 | let accepted = false; |
| ... | ... | @@ -118,15 +125,13 @@ function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey |
| 118 | 125 | <AssistantCitationChip |
| 119 | 126 | citation={props.citation} |
| 120 | 127 | commentEditor={{ |
| 121 | | open: commentContext.openComment?.nodeKey === props.nodeKey, |
| 122 | | sourceAnchor: |
| 123 | | commentContext.openComment?.nodeKey === props.nodeKey |
| 124 | | ? commentContext.openComment.sourceAnchor |
| 125 | | : undefined, |
| 128 | open: commentTarget !== null, |
| 129 | sourceAnchor: commentTarget?.sourceAnchor, |
| 126 | 130 | onOpenChange: (open) => { |
| 127 | 131 | if (open && !editor.isEditable()) return; |
| 128 | 132 | commentContext.onOpenChange(props.nodeKey, open); |
| 129 | 133 | }, |
| 134 | ...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}), |
| 130 | 135 | onSave: onSaveComment, |
| 131 | 136 | onSaveAndSend: (comment) => { |
| 132 | 137 | if (!onSaveComment(comment)) return false; |