Added proper handling for pasted links in Plunk editor

This commit is contained in:
Dries Augustyns
2024-09-07 12:34:07 +02:00
parent 02085e7a3d
commit de51eb94d2
2 changed files with 52 additions and 38 deletions
@@ -108,6 +108,22 @@ export default function Editor({ value, onChange, mode, modeSwitcher }: Markdown
keydown: (_view, event) => {
return event.key === "Enter" && !event.shiftKey;
},
paste: (view, event) => {
const text = event.clipboardData?.getData("text");
const urlPattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*({{[\w-]+}})?)*$/;
if (editor && text && urlPattern.test(text)) {
event.preventDefault();
editor
.chain()
.focus()
.extendMarkRange("link")
.setLink({ href: text.startsWith("http") ? text : `https://${text}`, target: "_blank" })
.run();
editor.commands.insertContent(text);
}
return true;
},
},
},
onUpdate: ({ editor }) => {