-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.ts
101 lines (85 loc) · 2.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useRef, useState, useEffect, RefObject } from 'react';
import Quill, { QuillOptions } from 'quill';
const theme = 'snow';
const modules = {
toolbar: [
['bold', 'italic', 'underline', 'strike'],
[{ align: [] }],
[{ list: 'ordered' }, { list: 'bullet' }],
[{ indent: '-1' }, { indent: '+1' }],
[{ size: ['small', false, 'large', 'huge'] }],
[{ header: [1, 2, 3, 4, 5, 6, false] }],
['link', 'image', 'video'],
[{ color: [] }, { background: [] }],
['clean'],
],
clipboard: {
matchVisual: false,
},
};
const formats = [
'bold',
'italic',
'underline',
'strike',
'align',
'list',
'indent',
'size',
'header',
'link',
'image',
'video',
'color',
'background',
];
function assign(target: any, _varArgs: any) {
'use strict';
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
const to = Object(target);
for (let index = 1; index < arguments.length; index++) {
const nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (const nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
}
/**
*
* @param options Quill static options. https://github.com/gtgalone/react-quilljs#options
* @returns Returns quill, quillRef, and Quill. https://github.com/gtgalone/react-quilljs#return
*/
export const useQuill = (options: QuillOptions | undefined = { theme, modules, formats }) => {
const quillRef: RefObject<any> = useRef(null);
const [isLoaded, setIsLoaded] = useState(false);
const [obj, setObj] = useState({
Quill: undefined as any | undefined,
quillRef,
quill: undefined as Quill | undefined,
editorRef: quillRef,
editor: undefined as Quill | undefined,
});
useEffect(() => {
if (!obj.Quill) {
setObj(prev => assign(prev, { Quill: require('quill').default }));
}
if (obj.Quill && !obj.quill && quillRef && quillRef.current && isLoaded) {
const opts = assign(options, {
modules: assign(modules, options.modules),
formats: options.formats || formats,
theme: options.theme || theme,
});
const quill = new obj.Quill(quillRef.current, opts);
setObj(assign(assign({}, obj), { quill, editor: quill }));
}
setIsLoaded(true);
}, [isLoaded, obj, options]);
return obj;
};