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