a tool for shared writing and social publishing

if pasting just a url with selection make inline link

+39
+14
components/Blocks/TextBlock/useHandlePaste.ts
··· 11 11 import { v7 } from "uuid"; 12 12 import { Replicache } from "replicache"; 13 13 import { markdownToHtml } from "src/htmlMarkdownParsers"; 14 + import { betterIsUrl, isUrl } from "src/utils/isURL"; 15 + import { TextSelection } from "prosemirror-state"; 14 16 15 17 const parser = ProsemirrorDOMParser.fromSchema(schema); 16 18 export const useHandlePaste = ( ··· 28 30 let text = e.clipboardData.getData("text"); 29 31 let editorState = useEditorStates.getState().editorStates[entityID]; 30 32 if (!editorState) return; 33 + if (text && betterIsUrl(text)) { 34 + let selection = view.state.selection as TextSelection; 35 + if (selection.empty) return; 36 + let tr = view.state.tr; 37 + let { from, to } = selection; 38 + tr.addMark(from, to, schema.marks.link.create({ href: text })); 39 + 40 + setEditorState(entityID, { 41 + editor: view.state.apply(tr), 42 + }); 43 + return true; 44 + } 31 45 if (!textHTML && text) { 32 46 console.log(text); 33 47 textHTML = markdownToHtml(text);
+25
src/utils/isURL.ts
··· 8 8 export function isUrl(str: string) { 9 9 return str.includes("."); 10 10 } 11 + 12 + export function betterIsUrl(string: string) { 13 + if (typeof string !== "string") { 14 + return false; 15 + } 16 + 17 + var match = string.match(protocolAndDomainRE); 18 + if (!match) { 19 + return false; 20 + } 21 + 22 + var everythingAfterProtocol = match[1]; 23 + if (!everythingAfterProtocol) { 24 + return false; 25 + } 26 + 27 + if ( 28 + localhostDomainRE.test(everythingAfterProtocol) || 29 + nonLocalhostDomainRE.test(everythingAfterProtocol) 30 + ) { 31 + return true; 32 + } 33 + 34 + return false; 35 + }