@bbl-digital/snorre 4.1.12 → 4.1.14
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/bundle.js +110 -124
- package/esm/core/QuillEditor/index.js +111 -125
- package/lib/core/QuillEditor/index.d.ts +1 -1
- package/lib/core/QuillEditor/index.d.ts.map +1 -1
- package/lib/core/QuillEditor/index.js +111 -125
- package/package.json +1 -1
package/dist/bundle.js
CHANGED
@@ -35458,7 +35458,7 @@ to {top: 100vh;}
|
|
35458
35458
|
'formats/ulist': MyListItem
|
35459
35459
|
});
|
35460
35460
|
Quill__default["default"].register(CustomLinkSanitizer, true);
|
35461
|
-
const QuillEditor = ({
|
35461
|
+
const QuillEditor = /*#__PURE__*/React.forwardRef(({
|
35462
35462
|
height,
|
35463
35463
|
width,
|
35464
35464
|
maxHeight,
|
@@ -35485,12 +35485,15 @@ to {top: 100vh;}
|
|
35485
35485
|
disabled,
|
35486
35486
|
pasteAsText,
|
35487
35487
|
name,
|
35488
|
-
id
|
35489
|
-
|
35490
|
-
}) => {
|
35488
|
+
id
|
35489
|
+
}, ref) => {
|
35491
35490
|
const [quill, setQuill] = React.useState(null);
|
35492
35491
|
const isMounted = React.useRef(false);
|
35493
35492
|
const containerRef = React.useRef(null);
|
35493
|
+
const defaultValueRef = React.useRef(initialValue);
|
35494
|
+
const onTextChangeRef = React.useRef(onChange);
|
35495
|
+
// const onFocusRef = useRef(onFocus)
|
35496
|
+
// const onBlurRef = useRef(onBlur)
|
35494
35497
|
const {
|
35495
35498
|
alert
|
35496
35499
|
} = react.useTheme();
|
@@ -35537,20 +35540,17 @@ to {top: 100vh;}
|
|
35537
35540
|
}
|
35538
35541
|
}
|
35539
35542
|
};
|
35540
|
-
const handleChange = React.useCallback((delta, _oldDelta, source) => {
|
35541
|
-
|
35542
|
-
|
35543
|
-
|
35544
|
-
onChange(html, delta, source, quill);
|
35545
|
-
}
|
35543
|
+
const handleChange = React.useCallback((delta, _oldDelta, source, instance) => {
|
35544
|
+
const html = instance.root.innerHTML;
|
35545
|
+
if (onChange) {
|
35546
|
+
onChange(html, delta, source, quill);
|
35546
35547
|
}
|
35547
35548
|
}, [quill, onChange]);
|
35548
|
-
const handelFocus = React.useCallback(
|
35549
|
-
|
35550
|
-
const html = quill.root.innerHTML;
|
35549
|
+
const handelFocus = React.useCallback(instance => {
|
35550
|
+
const html = instance.root.innerHTML;
|
35551
35551
|
onFocus && onFocus(html);
|
35552
35552
|
}, [quill, onFocus]);
|
35553
|
-
const applySizes =
|
35553
|
+
const applySizes = quill => {
|
35554
35554
|
if (!quill) return;
|
35555
35555
|
minHeight && (quill.root.style.minHeight = `${minHeight}px`);
|
35556
35556
|
maxHeight && (quill.root.style.maxHeight = `${maxHeight}px`);
|
@@ -35559,122 +35559,108 @@ to {top: 100vh;}
|
|
35559
35559
|
height && (quill.root.style.height = `${height}px`);
|
35560
35560
|
minWidth && (quill.root.style.minWidth = `${minWidth}px`);
|
35561
35561
|
};
|
35562
|
+
React.useLayoutEffect(() => {
|
35563
|
+
onTextChangeRef.current = onChange;
|
35564
|
+
});
|
35562
35565
|
React.useEffect(() => {
|
35563
|
-
if (
|
35564
|
-
|
35565
|
-
|
35566
|
-
|
35567
|
-
|
35568
|
-
|
35569
|
-
|
35570
|
-
|
35571
|
-
|
35572
|
-
|
35573
|
-
|
35574
|
-
|
35575
|
-
|
35576
|
-
|
35577
|
-
|
35578
|
-
|
35579
|
-
|
35566
|
+
if (isMounted.current) return;
|
35567
|
+
const container = containerRef.current;
|
35568
|
+
const editor = container?.appendChild(container.ownerDocument.createElement('div'));
|
35569
|
+
if (!editor) return;
|
35570
|
+
const quillInstance = new Quill__default["default"](editor, {
|
35571
|
+
theme: 'snow',
|
35572
|
+
modules: {
|
35573
|
+
...modules
|
35574
|
+
},
|
35575
|
+
formats,
|
35576
|
+
placeholder,
|
35577
|
+
bounds,
|
35578
|
+
readOnly
|
35579
|
+
});
|
35580
|
+
ref && ref.current && (ref.current = editor);
|
35581
|
+
const editorActual = editor.querySelector('.ql-editor');
|
35582
|
+
if (editorActual) {
|
35583
|
+
editorActual.setAttribute('id', id || 'ql-editor');
|
35584
|
+
editorActual.setAttribute('name', name || 'ql-editor');
|
35585
|
+
}
|
35586
|
+
applySizes(quillInstance);
|
35587
|
+
quillInstance.keyboard.addBinding({
|
35588
|
+
key: 'Backspace'
|
35589
|
+
}, {
|
35590
|
+
format: {
|
35591
|
+
list: true,
|
35592
|
+
indent: true
|
35580
35593
|
}
|
35581
|
-
|
35582
|
-
|
35583
|
-
|
35584
|
-
|
35585
|
-
|
35586
|
-
|
35587
|
-
|
35588
|
-
|
35589
|
-
|
35590
|
-
|
35591
|
-
|
35592
|
-
|
35593
|
-
quillInstance.
|
35594
|
-
|
35595
|
-
}, () => {
|
35596
|
-
// Remove focus from editor
|
35597
|
-
quillInstance.blur();
|
35594
|
+
}, (range, context) => {
|
35595
|
+
return handleBackspaceWithIndentetList(range, context, quillInstance);
|
35596
|
+
});
|
35597
|
+
// Move the last binding in the list, to the top. Ensure that the custom binding is called first.
|
35598
|
+
quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
|
35599
|
+
quillInstance.keyboard.addBinding({
|
35600
|
+
key: 'Escape'
|
35601
|
+
}, () => {
|
35602
|
+
// Remove focus from editor
|
35603
|
+
quillInstance.blur();
|
35604
|
+
});
|
35605
|
+
if (defaultValueRef.current) {
|
35606
|
+
const delta = quillInstance.clipboard.convert({
|
35607
|
+
html: defaultValueRef.current
|
35598
35608
|
});
|
35599
|
-
|
35600
|
-
if (quillInstance.root.innerHTML !== initialValue) {
|
35601
|
-
const delta = quillInstance.clipboard.convert({
|
35602
|
-
html: initialValue
|
35603
|
-
});
|
35604
|
-
quillInstance.setContents(delta);
|
35605
|
-
}
|
35606
|
-
}
|
35607
|
-
isMounted.current = true;
|
35608
|
-
setQuill(quillInstance);
|
35609
|
+
quillInstance.setContents(delta);
|
35609
35610
|
}
|
35610
|
-
|
35611
|
-
|
35612
|
-
|
35613
|
-
|
35614
|
-
|
35615
|
-
|
35616
|
-
onBlur
|
35617
|
-
}
|
35618
|
-
};
|
35619
|
-
quill.root.addEventListener('blur', handleBlur);
|
35620
|
-
return () => {
|
35621
|
-
quill.root.removeEventListener('blur', handleBlur);
|
35622
|
-
};
|
35623
|
-
}, [quill]);
|
35624
|
-
React.useEffect(() => {
|
35625
|
-
if (!quill) return;
|
35626
|
-
quill.on('selection-change', (range, oldRange, source) => {
|
35627
|
-
if (!range) return;
|
35628
|
-
handelFocus();
|
35611
|
+
quillInstance.on(Quill__default["default"].events.TEXT_CHANGE, (...args) => handleChange(...args, quillInstance));
|
35612
|
+
quillInstance.on(Quill__default["default"].events.SELECTION_CHANGE, range => {
|
35613
|
+
const html = quillInstance.root.innerHTML;
|
35614
|
+
if (!range) {
|
35615
|
+
handelFocus?.(quillInstance);
|
35616
|
+
} else {
|
35617
|
+
onBlur?.(html);
|
35618
|
+
}
|
35629
35619
|
});
|
35620
|
+
isMounted.current = true;
|
35621
|
+
setQuill(quillInstance);
|
35630
35622
|
return () => {
|
35631
|
-
|
35632
|
-
|
35633
|
-
}, [quill]);
|
35634
|
-
React.useEffect(() => {
|
35635
|
-
if (!quill) return;
|
35636
|
-
quill.on('text-change', handleChange);
|
35637
|
-
return () => {
|
35638
|
-
quill.off('text-change', handleChange);
|
35623
|
+
ref && ref.current && (ref.current = null);
|
35624
|
+
container.innerHTML = '';
|
35639
35625
|
};
|
35640
|
-
}, [
|
35641
|
-
|
35642
|
-
|
35643
|
-
|
35644
|
-
|
35645
|
-
|
35646
|
-
|
35647
|
-
|
35648
|
-
|
35649
|
-
|
35650
|
-
|
35651
|
-
|
35652
|
-
|
35653
|
-
|
35654
|
-
|
35655
|
-
|
35656
|
-
|
35657
|
-
|
35658
|
-
|
35659
|
-
|
35660
|
-
|
35661
|
-
|
35662
|
-
|
35663
|
-
|
35664
|
-
|
35665
|
-
|
35666
|
-
|
35667
|
-
|
35668
|
-
|
35669
|
-
|
35670
|
-
|
35671
|
-
|
35672
|
-
|
35673
|
-
|
35674
|
-
|
35675
|
-
|
35676
|
-
|
35677
|
-
|
35626
|
+
}, [ref]);
|
35627
|
+
|
35628
|
+
// useEffect(() => {
|
35629
|
+
// if (!quill) return
|
35630
|
+
|
35631
|
+
// quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
|
35632
|
+
// if (!pasteAsText) return delta
|
35633
|
+
// let ops: Op[] = []
|
35634
|
+
// delta.ops.forEach((op) => {
|
35635
|
+
// if (op.insert && typeof op.insert === 'string') {
|
35636
|
+
// ops.push({
|
35637
|
+
// insert: op.insert,
|
35638
|
+
// })
|
35639
|
+
// }
|
35640
|
+
// })
|
35641
|
+
// delta.ops = ops
|
35642
|
+
// return delta
|
35643
|
+
// })
|
35644
|
+
// }, [quill, pasteAsText])
|
35645
|
+
|
35646
|
+
// useEffect(() => {
|
35647
|
+
// if (quill && overrideValue !== undefined) {
|
35648
|
+
// const currentContent = quill.root.innerHTML
|
35649
|
+
// if (currentContent !== overrideValue) {
|
35650
|
+
// const delta = quill.clipboard.convert({ html: overrideValue })
|
35651
|
+
// quill.setContents(delta)
|
35652
|
+
// }
|
35653
|
+
// }
|
35654
|
+
// }, [quill, overrideValue])
|
35655
|
+
|
35656
|
+
// useEffect(() => {
|
35657
|
+
// if (quill && (disabled || readOnly)) {
|
35658
|
+
// quill.enable(false)
|
35659
|
+
// } else if (quill) {
|
35660
|
+
// quill.enable(true)
|
35661
|
+
// }
|
35662
|
+
// }, [readOnly, quill])
|
35663
|
+
|
35678
35664
|
return jsxRuntime.jsxs("div", {
|
35679
35665
|
css: theme => [styles.default(theme, maxHeight)],
|
35680
35666
|
children: [label && jsxRuntime.jsx(Label, {
|
@@ -35701,7 +35687,7 @@ to {top: 100vh;}
|
|
35701
35687
|
})
|
35702
35688
|
})]
|
35703
35689
|
});
|
35704
|
-
};
|
35690
|
+
});
|
35705
35691
|
|
35706
35692
|
const getReducedArray = (array, indexesAmount) => {
|
35707
35693
|
// copies x amount of indexes from array (in order) to a new array
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/** @jsxImportSource @emotion/react */
|
2
|
-
import { useRef, useState, useEffect, useCallback } from 'react';
|
2
|
+
import { useRef, useState, useEffect, useCallback, forwardRef, useLayoutEffect } from 'react';
|
3
3
|
import Quill from 'quill';
|
4
4
|
import 'quill/dist/quill.snow.css';
|
5
5
|
import { basicToolbar, richToolbar } from './config';
|
@@ -55,7 +55,7 @@ Quill.register({
|
|
55
55
|
'formats/ulist': MyListItem
|
56
56
|
});
|
57
57
|
Quill.register(CustomLinkSanitizer, true);
|
58
|
-
const QuillEditor = ({
|
58
|
+
const QuillEditor = /*#__PURE__*/forwardRef(({
|
59
59
|
height,
|
60
60
|
width,
|
61
61
|
maxHeight,
|
@@ -82,12 +82,15 @@ const QuillEditor = ({
|
|
82
82
|
disabled,
|
83
83
|
pasteAsText,
|
84
84
|
name,
|
85
|
-
id
|
86
|
-
|
87
|
-
}) => {
|
85
|
+
id
|
86
|
+
}, ref) => {
|
88
87
|
const [quill, setQuill] = useState(null);
|
89
88
|
const isMounted = useRef(false);
|
90
89
|
const containerRef = useRef(null);
|
90
|
+
const defaultValueRef = useRef(initialValue);
|
91
|
+
const onTextChangeRef = useRef(onChange);
|
92
|
+
// const onFocusRef = useRef(onFocus)
|
93
|
+
// const onBlurRef = useRef(onBlur)
|
91
94
|
const {
|
92
95
|
alert
|
93
96
|
} = useTheme();
|
@@ -134,20 +137,17 @@ const QuillEditor = ({
|
|
134
137
|
}
|
135
138
|
}
|
136
139
|
};
|
137
|
-
const handleChange = useCallback((delta, _oldDelta, source) => {
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
onChange(html, delta, source, quill);
|
142
|
-
}
|
140
|
+
const handleChange = useCallback((delta, _oldDelta, source, instance) => {
|
141
|
+
const html = instance.root.innerHTML;
|
142
|
+
if (onChange) {
|
143
|
+
onChange(html, delta, source, quill);
|
143
144
|
}
|
144
145
|
}, [quill, onChange]);
|
145
|
-
const handelFocus = useCallback(
|
146
|
-
|
147
|
-
const html = quill.root.innerHTML;
|
146
|
+
const handelFocus = useCallback(instance => {
|
147
|
+
const html = instance.root.innerHTML;
|
148
148
|
onFocus && onFocus(html);
|
149
149
|
}, [quill, onFocus]);
|
150
|
-
const applySizes =
|
150
|
+
const applySizes = quill => {
|
151
151
|
if (!quill) return;
|
152
152
|
minHeight && (quill.root.style.minHeight = `${minHeight}px`);
|
153
153
|
maxHeight && (quill.root.style.maxHeight = `${maxHeight}px`);
|
@@ -156,122 +156,108 @@ const QuillEditor = ({
|
|
156
156
|
height && (quill.root.style.height = `${height}px`);
|
157
157
|
minWidth && (quill.root.style.minWidth = `${minWidth}px`);
|
158
158
|
};
|
159
|
+
useLayoutEffect(() => {
|
160
|
+
onTextChangeRef.current = onChange;
|
161
|
+
});
|
159
162
|
useEffect(() => {
|
160
|
-
if (
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
163
|
+
if (isMounted.current) return;
|
164
|
+
const container = containerRef.current;
|
165
|
+
const editor = container?.appendChild(container.ownerDocument.createElement('div'));
|
166
|
+
if (!editor) return;
|
167
|
+
const quillInstance = new Quill(editor, {
|
168
|
+
theme: 'snow',
|
169
|
+
modules: {
|
170
|
+
...modules
|
171
|
+
},
|
172
|
+
formats,
|
173
|
+
placeholder,
|
174
|
+
bounds,
|
175
|
+
readOnly
|
176
|
+
});
|
177
|
+
ref && ref.current && (ref.current = editor);
|
178
|
+
const editorActual = editor.querySelector('.ql-editor');
|
179
|
+
if (editorActual) {
|
180
|
+
editorActual.setAttribute('id', id || 'ql-editor');
|
181
|
+
editorActual.setAttribute('name', name || 'ql-editor');
|
182
|
+
}
|
183
|
+
applySizes(quillInstance);
|
184
|
+
quillInstance.keyboard.addBinding({
|
185
|
+
key: 'Backspace'
|
186
|
+
}, {
|
187
|
+
format: {
|
188
|
+
list: true,
|
189
|
+
indent: true
|
177
190
|
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
quillInstance.
|
191
|
-
|
192
|
-
}, () => {
|
193
|
-
// Remove focus from editor
|
194
|
-
quillInstance.blur();
|
191
|
+
}, (range, context) => {
|
192
|
+
return handleBackspaceWithIndentetList(range, context, quillInstance);
|
193
|
+
});
|
194
|
+
// Move the last binding in the list, to the top. Ensure that the custom binding is called first.
|
195
|
+
quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
|
196
|
+
quillInstance.keyboard.addBinding({
|
197
|
+
key: 'Escape'
|
198
|
+
}, () => {
|
199
|
+
// Remove focus from editor
|
200
|
+
quillInstance.blur();
|
201
|
+
});
|
202
|
+
if (defaultValueRef.current) {
|
203
|
+
const delta = quillInstance.clipboard.convert({
|
204
|
+
html: defaultValueRef.current
|
195
205
|
});
|
196
|
-
|
197
|
-
if (quillInstance.root.innerHTML !== initialValue) {
|
198
|
-
const delta = quillInstance.clipboard.convert({
|
199
|
-
html: initialValue
|
200
|
-
});
|
201
|
-
quillInstance.setContents(delta);
|
202
|
-
}
|
203
|
-
}
|
204
|
-
isMounted.current = true;
|
205
|
-
setQuill(quillInstance);
|
206
|
+
quillInstance.setContents(delta);
|
206
207
|
}
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
onBlur
|
214
|
-
}
|
215
|
-
};
|
216
|
-
quill.root.addEventListener('blur', handleBlur);
|
217
|
-
return () => {
|
218
|
-
quill.root.removeEventListener('blur', handleBlur);
|
219
|
-
};
|
220
|
-
}, [quill]);
|
221
|
-
useEffect(() => {
|
222
|
-
if (!quill) return;
|
223
|
-
quill.on('selection-change', (range, oldRange, source) => {
|
224
|
-
if (!range) return;
|
225
|
-
handelFocus();
|
208
|
+
quillInstance.on(Quill.events.TEXT_CHANGE, (...args) => handleChange(...args, quillInstance));
|
209
|
+
quillInstance.on(Quill.events.SELECTION_CHANGE, range => {
|
210
|
+
const html = quillInstance.root.innerHTML;
|
211
|
+
if (!range) {
|
212
|
+
handelFocus?.(quillInstance);
|
213
|
+
} else {
|
214
|
+
onBlur?.(html);
|
215
|
+
}
|
226
216
|
});
|
217
|
+
isMounted.current = true;
|
218
|
+
setQuill(quillInstance);
|
227
219
|
return () => {
|
228
|
-
|
220
|
+
ref && ref.current && (ref.current = null);
|
221
|
+
container.innerHTML = '';
|
229
222
|
};
|
230
|
-
}, [
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
}
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
useEffect(() => {
|
269
|
-
if (quill && (disabled || readOnly)) {
|
270
|
-
quill.enable(false);
|
271
|
-
} else if (quill) {
|
272
|
-
quill.enable(true);
|
273
|
-
}
|
274
|
-
}, [readOnly, quill]);
|
223
|
+
}, [ref]);
|
224
|
+
|
225
|
+
// useEffect(() => {
|
226
|
+
// if (!quill) return
|
227
|
+
|
228
|
+
// quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
|
229
|
+
// if (!pasteAsText) return delta
|
230
|
+
// let ops: Op[] = []
|
231
|
+
// delta.ops.forEach((op) => {
|
232
|
+
// if (op.insert && typeof op.insert === 'string') {
|
233
|
+
// ops.push({
|
234
|
+
// insert: op.insert,
|
235
|
+
// })
|
236
|
+
// }
|
237
|
+
// })
|
238
|
+
// delta.ops = ops
|
239
|
+
// return delta
|
240
|
+
// })
|
241
|
+
// }, [quill, pasteAsText])
|
242
|
+
|
243
|
+
// useEffect(() => {
|
244
|
+
// if (quill && overrideValue !== undefined) {
|
245
|
+
// const currentContent = quill.root.innerHTML
|
246
|
+
// if (currentContent !== overrideValue) {
|
247
|
+
// const delta = quill.clipboard.convert({ html: overrideValue })
|
248
|
+
// quill.setContents(delta)
|
249
|
+
// }
|
250
|
+
// }
|
251
|
+
// }, [quill, overrideValue])
|
252
|
+
|
253
|
+
// useEffect(() => {
|
254
|
+
// if (quill && (disabled || readOnly)) {
|
255
|
+
// quill.enable(false)
|
256
|
+
// } else if (quill) {
|
257
|
+
// quill.enable(true)
|
258
|
+
// }
|
259
|
+
// }, [readOnly, quill])
|
260
|
+
|
275
261
|
return _jsxs("div", {
|
276
262
|
css: theme => [styles.default(theme, maxHeight)],
|
277
263
|
children: [label && _jsx(Label, {
|
@@ -298,5 +284,5 @@ const QuillEditor = ({
|
|
298
284
|
})
|
299
285
|
})]
|
300
286
|
});
|
301
|
-
};
|
287
|
+
});
|
302
288
|
export default QuillEditor;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import 'quill/dist/quill.snow.css';
|
2
2
|
import { QuillEditorProps } from './models';
|
3
|
-
declare const QuillEditor:
|
3
|
+
declare const QuillEditor: import("react").ForwardRefExoticComponent<QuillEditorProps & import("react").RefAttributes<HTMLDivElement>>;
|
4
4
|
export default QuillEditor;
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packages/core/QuillEditor/index.tsx"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packages/core/QuillEditor/index.tsx"],"names":[],"mappings":"AAUA,OAAO,2BAA2B,CAAA;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAgE3C,QAAA,MAAM,WAAW,6GAiRhB,CAAA;AAED,eAAe,WAAW,CAAA"}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/** @jsxImportSource @emotion/react */
|
2
|
-
import { useRef, useState, useEffect, useCallback } from 'react';
|
2
|
+
import { useRef, useState, useEffect, useCallback, forwardRef, useLayoutEffect } from 'react';
|
3
3
|
import Quill from 'quill';
|
4
4
|
import 'quill/dist/quill.snow.css';
|
5
5
|
import { basicToolbar, richToolbar } from './config';
|
@@ -55,7 +55,7 @@ Quill.register({
|
|
55
55
|
'formats/ulist': MyListItem
|
56
56
|
});
|
57
57
|
Quill.register(CustomLinkSanitizer, true);
|
58
|
-
const QuillEditor = ({
|
58
|
+
const QuillEditor = /*#__PURE__*/forwardRef(({
|
59
59
|
height,
|
60
60
|
width,
|
61
61
|
maxHeight,
|
@@ -82,12 +82,15 @@ const QuillEditor = ({
|
|
82
82
|
disabled,
|
83
83
|
pasteAsText,
|
84
84
|
name,
|
85
|
-
id
|
86
|
-
|
87
|
-
}) => {
|
85
|
+
id
|
86
|
+
}, ref) => {
|
88
87
|
const [quill, setQuill] = useState(null);
|
89
88
|
const isMounted = useRef(false);
|
90
89
|
const containerRef = useRef(null);
|
90
|
+
const defaultValueRef = useRef(initialValue);
|
91
|
+
const onTextChangeRef = useRef(onChange);
|
92
|
+
// const onFocusRef = useRef(onFocus)
|
93
|
+
// const onBlurRef = useRef(onBlur)
|
91
94
|
const {
|
92
95
|
alert
|
93
96
|
} = useTheme();
|
@@ -134,20 +137,17 @@ const QuillEditor = ({
|
|
134
137
|
}
|
135
138
|
}
|
136
139
|
};
|
137
|
-
const handleChange = useCallback((delta, _oldDelta, source) => {
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
onChange(html, delta, source, quill);
|
142
|
-
}
|
140
|
+
const handleChange = useCallback((delta, _oldDelta, source, instance) => {
|
141
|
+
const html = instance.root.innerHTML;
|
142
|
+
if (onChange) {
|
143
|
+
onChange(html, delta, source, quill);
|
143
144
|
}
|
144
145
|
}, [quill, onChange]);
|
145
|
-
const handelFocus = useCallback(
|
146
|
-
|
147
|
-
const html = quill.root.innerHTML;
|
146
|
+
const handelFocus = useCallback(instance => {
|
147
|
+
const html = instance.root.innerHTML;
|
148
148
|
onFocus && onFocus(html);
|
149
149
|
}, [quill, onFocus]);
|
150
|
-
const applySizes =
|
150
|
+
const applySizes = quill => {
|
151
151
|
if (!quill) return;
|
152
152
|
minHeight && (quill.root.style.minHeight = `${minHeight}px`);
|
153
153
|
maxHeight && (quill.root.style.maxHeight = `${maxHeight}px`);
|
@@ -156,122 +156,108 @@ const QuillEditor = ({
|
|
156
156
|
height && (quill.root.style.height = `${height}px`);
|
157
157
|
minWidth && (quill.root.style.minWidth = `${minWidth}px`);
|
158
158
|
};
|
159
|
+
useLayoutEffect(() => {
|
160
|
+
onTextChangeRef.current = onChange;
|
161
|
+
});
|
159
162
|
useEffect(() => {
|
160
|
-
if (
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
163
|
+
if (isMounted.current) return;
|
164
|
+
const container = containerRef.current;
|
165
|
+
const editor = container?.appendChild(container.ownerDocument.createElement('div'));
|
166
|
+
if (!editor) return;
|
167
|
+
const quillInstance = new Quill(editor, {
|
168
|
+
theme: 'snow',
|
169
|
+
modules: {
|
170
|
+
...modules
|
171
|
+
},
|
172
|
+
formats,
|
173
|
+
placeholder,
|
174
|
+
bounds,
|
175
|
+
readOnly
|
176
|
+
});
|
177
|
+
ref && ref.current && (ref.current = editor);
|
178
|
+
const editorActual = editor.querySelector('.ql-editor');
|
179
|
+
if (editorActual) {
|
180
|
+
editorActual.setAttribute('id', id || 'ql-editor');
|
181
|
+
editorActual.setAttribute('name', name || 'ql-editor');
|
182
|
+
}
|
183
|
+
applySizes(quillInstance);
|
184
|
+
quillInstance.keyboard.addBinding({
|
185
|
+
key: 'Backspace'
|
186
|
+
}, {
|
187
|
+
format: {
|
188
|
+
list: true,
|
189
|
+
indent: true
|
177
190
|
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
quillInstance.
|
191
|
-
|
192
|
-
}, () => {
|
193
|
-
// Remove focus from editor
|
194
|
-
quillInstance.blur();
|
191
|
+
}, (range, context) => {
|
192
|
+
return handleBackspaceWithIndentetList(range, context, quillInstance);
|
193
|
+
});
|
194
|
+
// Move the last binding in the list, to the top. Ensure that the custom binding is called first.
|
195
|
+
quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
|
196
|
+
quillInstance.keyboard.addBinding({
|
197
|
+
key: 'Escape'
|
198
|
+
}, () => {
|
199
|
+
// Remove focus from editor
|
200
|
+
quillInstance.blur();
|
201
|
+
});
|
202
|
+
if (defaultValueRef.current) {
|
203
|
+
const delta = quillInstance.clipboard.convert({
|
204
|
+
html: defaultValueRef.current
|
195
205
|
});
|
196
|
-
|
197
|
-
if (quillInstance.root.innerHTML !== initialValue) {
|
198
|
-
const delta = quillInstance.clipboard.convert({
|
199
|
-
html: initialValue
|
200
|
-
});
|
201
|
-
quillInstance.setContents(delta);
|
202
|
-
}
|
203
|
-
}
|
204
|
-
isMounted.current = true;
|
205
|
-
setQuill(quillInstance);
|
206
|
+
quillInstance.setContents(delta);
|
206
207
|
}
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
onBlur
|
214
|
-
}
|
215
|
-
};
|
216
|
-
quill.root.addEventListener('blur', handleBlur);
|
217
|
-
return () => {
|
218
|
-
quill.root.removeEventListener('blur', handleBlur);
|
219
|
-
};
|
220
|
-
}, [quill]);
|
221
|
-
useEffect(() => {
|
222
|
-
if (!quill) return;
|
223
|
-
quill.on('selection-change', (range, oldRange, source) => {
|
224
|
-
if (!range) return;
|
225
|
-
handelFocus();
|
208
|
+
quillInstance.on(Quill.events.TEXT_CHANGE, (...args) => handleChange(...args, quillInstance));
|
209
|
+
quillInstance.on(Quill.events.SELECTION_CHANGE, range => {
|
210
|
+
const html = quillInstance.root.innerHTML;
|
211
|
+
if (!range) {
|
212
|
+
handelFocus?.(quillInstance);
|
213
|
+
} else {
|
214
|
+
onBlur?.(html);
|
215
|
+
}
|
226
216
|
});
|
217
|
+
isMounted.current = true;
|
218
|
+
setQuill(quillInstance);
|
227
219
|
return () => {
|
228
|
-
|
220
|
+
ref && ref.current && (ref.current = null);
|
221
|
+
container.innerHTML = '';
|
229
222
|
};
|
230
|
-
}, [
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
}
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
useEffect(() => {
|
269
|
-
if (quill && (disabled || readOnly)) {
|
270
|
-
quill.enable(false);
|
271
|
-
} else if (quill) {
|
272
|
-
quill.enable(true);
|
273
|
-
}
|
274
|
-
}, [readOnly, quill]);
|
223
|
+
}, [ref]);
|
224
|
+
|
225
|
+
// useEffect(() => {
|
226
|
+
// if (!quill) return
|
227
|
+
|
228
|
+
// quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
|
229
|
+
// if (!pasteAsText) return delta
|
230
|
+
// let ops: Op[] = []
|
231
|
+
// delta.ops.forEach((op) => {
|
232
|
+
// if (op.insert && typeof op.insert === 'string') {
|
233
|
+
// ops.push({
|
234
|
+
// insert: op.insert,
|
235
|
+
// })
|
236
|
+
// }
|
237
|
+
// })
|
238
|
+
// delta.ops = ops
|
239
|
+
// return delta
|
240
|
+
// })
|
241
|
+
// }, [quill, pasteAsText])
|
242
|
+
|
243
|
+
// useEffect(() => {
|
244
|
+
// if (quill && overrideValue !== undefined) {
|
245
|
+
// const currentContent = quill.root.innerHTML
|
246
|
+
// if (currentContent !== overrideValue) {
|
247
|
+
// const delta = quill.clipboard.convert({ html: overrideValue })
|
248
|
+
// quill.setContents(delta)
|
249
|
+
// }
|
250
|
+
// }
|
251
|
+
// }, [quill, overrideValue])
|
252
|
+
|
253
|
+
// useEffect(() => {
|
254
|
+
// if (quill && (disabled || readOnly)) {
|
255
|
+
// quill.enable(false)
|
256
|
+
// } else if (quill) {
|
257
|
+
// quill.enable(true)
|
258
|
+
// }
|
259
|
+
// }, [readOnly, quill])
|
260
|
+
|
275
261
|
return _jsxs("div", {
|
276
262
|
css: theme => [styles.default(theme, maxHeight)],
|
277
263
|
children: [label && _jsx(Label, {
|
@@ -298,5 +284,5 @@ const QuillEditor = ({
|
|
298
284
|
})
|
299
285
|
})]
|
300
286
|
});
|
301
|
-
};
|
287
|
+
});
|
302
288
|
export default QuillEditor;
|