Skip to content

Commit 36fc086

Browse files
authored
Merge pull request #328 from SSAF-SOUND/feature/auto-insert-link-#327
URL 붙여넣기시 링크 자동삽입
2 parents 8980d3e + 048e8ba commit 36fc086

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/components/Editor/Editor.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DeltaStatic } from 'quill';
12
import type { ReactQuillProps } from 'react-quill';
23

34
import dynamic from 'next/dynamic';
@@ -9,6 +10,7 @@ import { editorClassnames as cn } from '~/components/Editor/editorClassnames';
910
import EditorSkeleton from '~/components/Editor/EditorSkeleton';
1011
import { articleCss } from '~/services/article';
1112
import { fontCss, palettes } from '~/styles/utils';
13+
import { regex } from '~/utils';
1214

1315
const ReactQuill = dynamic(import('react-quill'), {
1416
ssr: false,
@@ -32,6 +34,31 @@ const Editor = (props: EditorProps) => {
3234
);
3335
};
3436

37+
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node.text_node
38+
const TEXT_NODE = 3;
39+
40+
// https://github.com/quilljs/quill/issues/109#issuecomment-278181150
41+
const convertUrlTextToLink = (node: Text, delta: DeltaStatic) => {
42+
if (typeof node.data !== 'string') return;
43+
const matches = node.data.match(regex.looseUrl);
44+
45+
if (matches && matches.length > 0) {
46+
const ops = [];
47+
let str = node.data;
48+
matches.forEach((match) => {
49+
const split = str.split(match);
50+
const beforeLink = split.shift();
51+
ops.push({ insert: beforeLink });
52+
ops.push({ insert: match, attributes: { link: match } });
53+
str = split.join(match);
54+
});
55+
ops.push({ insert: str });
56+
delta.ops = ops;
57+
}
58+
59+
return delta;
60+
};
61+
3562
const modules = {
3663
toolbar: {
3764
container: [
@@ -43,6 +70,9 @@ const modules = {
4370
['clean'],
4471
],
4572
},
73+
clipboard: {
74+
matchers: [[TEXT_NODE, convertUrlTextToLink]],
75+
},
4676
};
4777

4878
const formats = [

0 commit comments

Comments
 (0)