Skip to content

Commit ab4c2cd

Browse files
committed
Prevent pasting HTML or images in the prompt field by enforcing plain text only
1 parent f99c0a2 commit ab4c2cd

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
130130
[]
131131
)
132132

133+
// Prevents pasting html or images
134+
const handle_paste = useCallback(
135+
(e: React.ClipboardEvent<HTMLDivElement>) => {
136+
e.preventDefault()
137+
const text = e.clipboardData.getData('text/plain')
138+
139+
const selection = window.getSelection()
140+
if (!selection || !selection.rangeCount) return
141+
const range = selection.getRangeAt(0)
142+
range.deleteContents()
143+
const text_node = document.createTextNode(text)
144+
range.insertNode(text_node)
145+
range.setStartAfter(text_node)
146+
range.setEndAfter(text_node)
147+
selection.removeAllRanges()
148+
selection.addRange(range)
149+
input_ref.current?.dispatchEvent(new Event('input', { bubbles: true }))
150+
},
151+
[]
152+
)
153+
133154
const is_mac = use_is_mac()
134155

135156
const highlighted_html = useMemo(() => {
@@ -302,6 +323,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
302323
onInput={handle_input_change}
303324
onKeyDown={handle_key_down}
304325
onCopy={handle_copy}
326+
onPaste={handle_paste}
305327
onClick={handle_input_click}
306328
onMouseDown={handle_mouse_down}
307329
onDragStart={handle_drag_start}

0 commit comments

Comments
 (0)