@@ -11,12 +11,9 @@ import {
1111 XIcon ,
1212} from "@phosphor-icons/react" ;
1313import { Chip } from "@posthog/quill" ;
14- import { useSettingsStore as useFeatureSettingsStore } from "@posthog/ui/features/settings/settingsStore" ;
1514import { Tooltip } from "@posthog/ui/primitives/Tooltip" ;
16- import type { Node as PmNode } from "@tiptap/pm/model" ;
17- import type { Editor } from "@tiptap/react" ;
1815import { type NodeViewProps , NodeViewWrapper } from "@tiptap/react" ;
19- import { readAbsoluteFile } from "../hostApi " ;
16+ import { usePasteUndoStore } from "../pasteUndoStore " ;
2017import type { ChipType , MentionChipAttrs } from "./MentionChipNode" ;
2118
2219const chipBase = "group/chip relative top-px active:translate-y-0 pl-1" ;
@@ -68,15 +65,22 @@ function DefaultChip({
6865 type,
6966 id,
7067 label,
68+ chipId,
69+ pastedText,
7170 selected,
7271 onRemove,
7372} : {
7473 type : string ;
7574 id : string ;
7675 label : string ;
76+ chipId : string | null ;
77+ pastedText : boolean ;
7778 selected : boolean ;
7879 onRemove : ( ) => void ;
7980} ) {
81+ const undoableChipId = usePasteUndoStore ( ( state ) => state . undoableChipId ) ;
82+ const canUndoPaste =
83+ pastedText && chipId !== null && chipId === undoableChipId ;
8084 const isCommand = type === "command" ;
8185 const prefix = isCommand ? "/" : "@" ;
8286 const isFile = type === "file" ;
@@ -101,69 +105,24 @@ function DefaultChip({
101105 ) ;
102106
103107 if ( isFile || isFolder ) {
104- return < Tooltip content = { id } > { chipContent } </ Tooltip > ;
108+ return (
109+ < Tooltip content = { canUndoPaste ? "Paste again to expand as text" : id } >
110+ { chipContent }
111+ </ Tooltip >
112+ ) ;
105113 }
106114
107115 return chipContent ;
108116}
109117
110- function PastedTextChip ( {
111- label,
112- filePath,
113- editor,
114- node,
115- getPos,
116- selected,
117- onRemove,
118- } : {
119- label : string ;
120- filePath : string ;
121- editor : Editor ;
122- node : PmNode ;
123- getPos : ( ) => number | undefined ;
124- selected : boolean ;
125- onRemove : ( ) => void ;
126- } ) {
127- const handleClick = async ( ) => {
128- useFeatureSettingsStore . getState ( ) . markHintLearned ( "paste-as-file" ) ;
129-
130- const content = await readAbsoluteFile ( {
131- filePath,
132- } ) ;
133- if ( ! content ) return ;
134-
135- const pos = getPos ( ) ;
136- if ( pos == null ) return ;
137-
138- editor
139- . chain ( )
140- . focus ( )
141- . deleteRange ( { from : pos , to : pos + node . nodeSize } )
142- . insertContentAt ( pos , content )
143- . run ( ) ;
144- } ;
145-
146- return (
147- < Tooltip content = "Click to paste as text instead" >
148- < Chip
149- size = "xs"
150- contentEditable = { false }
151- onClick = { handleClick }
152- className = { `${ chipBase } cli-file-mention cursor-pointer! ${ selected ? selectedRing : "" } ` }
153- >
154- < IconCloseButton type = "file" onRemove = { onRemove } /> @{ label }
155- </ Chip >
156- </ Tooltip >
157- ) ;
158- }
159-
160118export function MentionChipView ( {
161119 node,
162120 getPos,
163121 editor,
164122 selected,
165123} : NodeViewProps ) {
166- const { type, id, label, pastedText } = node . attrs as MentionChipAttrs ;
124+ const { type, id, label, pastedText, chipId } =
125+ node . attrs as MentionChipAttrs ;
167126
168127 const handleRemove = ( ) => {
169128 const pos = getPos ( ) ;
@@ -177,25 +136,15 @@ export function MentionChipView({
177136
178137 return (
179138 < NodeViewWrapper as = "span" className = "inline" >
180- { pastedText ? (
181- < PastedTextChip
182- label = { label }
183- filePath = { id }
184- editor = { editor }
185- node = { node }
186- getPos = { getPos }
187- selected = { selected }
188- onRemove = { handleRemove }
189- />
190- ) : (
191- < DefaultChip
192- type = { type }
193- id = { id }
194- label = { label }
195- selected = { selected }
196- onRemove = { handleRemove }
197- />
198- ) }
139+ < DefaultChip
140+ type = { type }
141+ id = { id }
142+ label = { label }
143+ chipId = { chipId ?? null }
144+ pastedText = { pastedText }
145+ selected = { selected }
146+ onRemove = { handleRemove }
147+ />
199148 </ NodeViewWrapper >
200149 ) ;
201150}
0 commit comments