When iframe code is pasted, inject it into the editor

This commit is contained in:
Austin Chen 2022-07-21 15:36:00 -07:00
parent 3a8968bdf1
commit 41a78ea09c
2 changed files with 12 additions and 7 deletions

View File

@ -99,9 +99,6 @@ function RichEditContract(props: { contract: Contract; isAdmin?: boolean }) {
.setContent(contract.description) .setContent(contract.description)
.focus('end') .focus('end')
.insertContent(`<p>${editTimestamp()}</p>`) .insertContent(`<p>${editTimestamp()}</p>`)
.insertContent(
`<iframe src="https://manifold.markets/embed/Austin/how-many-upvotes-will-the-new-versi" frameborder="0"></iframe>`
)
.run() .run()
}} }}
> >

View File

@ -71,12 +71,20 @@ export function useTextEditor(props: {
(file) => file.type.startsWith('image') (file) => file.type.startsWith('image')
) )
if (!imageFiles.length) { if (imageFiles.length) {
return // if no files pasted, use default paste handler event.preventDefault()
upload.mutate(imageFiles)
} }
event.preventDefault() // If the pasted content is iframe code, directly inject it
upload.mutate(imageFiles) const text = event.clipboardData?.getData('text/plain').trim() ?? ''
const isValidIframe = /^<iframe.*<\/iframe>$/.test(text)
if (isValidIframe) {
editor.chain().insertContent(text).run()
return true // Prevent the code from getting pasted as text
}
return // Otherwise, use default paste handler
}, },
}, },
}) })