tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
27
pulls
pipelines
if pasting just a url with selection make inline link
awarm.space
2 years ago
e9c3aac7
3eff254a
+39
2 changed files
expand all
collapse all
unified
split
components
Blocks
TextBlock
useHandlePaste.ts
src
utils
isURL.ts
+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
14
+
import { betterIsUrl, isUrl } from "src/utils/isURL";
15
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
33
+
if (text && betterIsUrl(text)) {
34
34
+
let selection = view.state.selection as TextSelection;
35
35
+
if (selection.empty) return;
36
36
+
let tr = view.state.tr;
37
37
+
let { from, to } = selection;
38
38
+
tr.addMark(from, to, schema.marks.link.create({ href: text }));
39
39
+
40
40
+
setEditorState(entityID, {
41
41
+
editor: view.state.apply(tr),
42
42
+
});
43
43
+
return true;
44
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
11
+
12
12
+
export function betterIsUrl(string: string) {
13
13
+
if (typeof string !== "string") {
14
14
+
return false;
15
15
+
}
16
16
+
17
17
+
var match = string.match(protocolAndDomainRE);
18
18
+
if (!match) {
19
19
+
return false;
20
20
+
}
21
21
+
22
22
+
var everythingAfterProtocol = match[1];
23
23
+
if (!everythingAfterProtocol) {
24
24
+
return false;
25
25
+
}
26
26
+
27
27
+
if (
28
28
+
localhostDomainRE.test(everythingAfterProtocol) ||
29
29
+
nonLocalhostDomainRE.test(everythingAfterProtocol)
30
30
+
) {
31
31
+
return true;
32
32
+
}
33
33
+
34
34
+
return false;
35
35
+
}