@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 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
- resize // Trenger vi denne?,
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
- if (quill) {
35542
- const html = quill.root.innerHTML;
35543
- if (onChange) {
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
- if (!quill) return;
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 (!isMounted.current) {
35564
- const editor = document.createElement('div');
35565
- containerRef.current?.appendChild(editor);
35566
- const quillInstance = new Quill__default["default"](editor, {
35567
- theme: 'snow',
35568
- modules: {
35569
- ...modules
35570
- },
35571
- formats,
35572
- placeholder,
35573
- bounds,
35574
- readOnly
35575
- });
35576
- const editorActual = editor.querySelector('.ql-editor');
35577
- if (editorActual) {
35578
- editorActual.setAttribute('id', id || 'ql-editor');
35579
- editorActual.setAttribute('name', name || 'ql-editor');
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
- quillInstance.keyboard.addBinding({
35582
- key: 'Backspace'
35583
- }, {
35584
- format: {
35585
- list: true,
35586
- indent: true
35587
- }
35588
- }, (range, context) => {
35589
- return handleBackspaceWithIndentetList(range, context, quillInstance);
35590
- });
35591
- // Move the last binding in the list, to the top. Ensure that the custom binding is called first.
35592
- quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
35593
- quillInstance.keyboard.addBinding({
35594
- key: 'Escape'
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
- if (initialValue !== undefined) {
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
- React.useEffect(() => {
35612
- if (!quill) return;
35613
- const handleBlur = () => {
35614
- setTimeout(() => {
35615
- const html = quill.root.innerHTML;
35616
- onBlur && onBlur(html);
35617
- }, 50);
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
- quill.off('selection-change');
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
- }, [quill, isMounted]);
35641
- React.useEffect(() => {
35642
- applySizes();
35643
- }, [quill, height, width, maxHeight, maxWidth, minHeight, minWidth]);
35644
- React.useEffect(() => {
35645
- if (!quill) return;
35646
- quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
35647
- if (!pasteAsText) return delta;
35648
- let ops = [];
35649
- delta.ops.forEach(op => {
35650
- if (op.insert && typeof op.insert === 'string') {
35651
- ops.push({
35652
- insert: op.insert
35653
- });
35654
- }
35655
- });
35656
- delta.ops = ops;
35657
- return delta;
35658
- });
35659
- }, [quill, pasteAsText]);
35660
- React.useEffect(() => {
35661
- if (quill && overrideValue !== undefined) {
35662
- const currentContent = quill.root.innerHTML;
35663
- if (currentContent !== overrideValue) {
35664
- const delta = quill.clipboard.convert({
35665
- html: overrideValue
35666
- });
35667
- quill.setContents(delta);
35668
- }
35669
- }
35670
- }, [quill, overrideValue]);
35671
- React.useEffect(() => {
35672
- if (quill && (disabled || readOnly)) {
35673
- quill.enable(false);
35674
- } else if (quill) {
35675
- quill.enable(true);
35676
- }
35677
- }, [readOnly, quill]);
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
- resize // Trenger vi denne?,
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
- if (quill) {
139
- const html = quill.root.innerHTML;
140
- if (onChange) {
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
- if (!quill) return;
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 (!isMounted.current) {
161
- const editor = document.createElement('div');
162
- containerRef.current?.appendChild(editor);
163
- const quillInstance = new Quill(editor, {
164
- theme: 'snow',
165
- modules: {
166
- ...modules
167
- },
168
- formats,
169
- placeholder,
170
- bounds,
171
- readOnly
172
- });
173
- const editorActual = editor.querySelector('.ql-editor');
174
- if (editorActual) {
175
- editorActual.setAttribute('id', id || 'ql-editor');
176
- editorActual.setAttribute('name', name || 'ql-editor');
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
- quillInstance.keyboard.addBinding({
179
- key: 'Backspace'
180
- }, {
181
- format: {
182
- list: true,
183
- indent: true
184
- }
185
- }, (range, context) => {
186
- return handleBackspaceWithIndentetList(range, context, quillInstance);
187
- });
188
- // Move the last binding in the list, to the top. Ensure that the custom binding is called first.
189
- quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
190
- quillInstance.keyboard.addBinding({
191
- key: 'Escape'
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
- if (initialValue !== undefined) {
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
- useEffect(() => {
209
- if (!quill) return;
210
- const handleBlur = () => {
211
- setTimeout(() => {
212
- const html = quill.root.innerHTML;
213
- onBlur && onBlur(html);
214
- }, 50);
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
- quill.off('selection-change');
220
+ ref && ref.current && (ref.current = null);
221
+ container.innerHTML = '';
229
222
  };
230
- }, [quill]);
231
- useEffect(() => {
232
- if (!quill) return;
233
- quill.on('text-change', handleChange);
234
- return () => {
235
- quill.off('text-change', handleChange);
236
- };
237
- }, [quill, isMounted]);
238
- useEffect(() => {
239
- applySizes();
240
- }, [quill, height, width, maxHeight, maxWidth, minHeight, minWidth]);
241
- useEffect(() => {
242
- if (!quill) return;
243
- quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
244
- if (!pasteAsText) return delta;
245
- let ops = [];
246
- delta.ops.forEach(op => {
247
- if (op.insert && typeof op.insert === 'string') {
248
- ops.push({
249
- insert: op.insert
250
- });
251
- }
252
- });
253
- delta.ops = ops;
254
- return delta;
255
- });
256
- }, [quill, pasteAsText]);
257
- useEffect(() => {
258
- if (quill && overrideValue !== undefined) {
259
- const currentContent = quill.root.innerHTML;
260
- if (currentContent !== overrideValue) {
261
- const delta = quill.clipboard.convert({
262
- html: overrideValue
263
- });
264
- quill.setContents(delta);
265
- }
266
- }
267
- }, [quill, overrideValue]);
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: React.FC<QuillEditorProps>;
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":"AAGA,OAAO,2BAA2B,CAAA;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AA+D3C,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,gBAAgB,CAsR3C,CAAA;AAED,eAAe,WAAW,CAAA"}
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
- resize // Trenger vi denne?,
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
- if (quill) {
139
- const html = quill.root.innerHTML;
140
- if (onChange) {
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
- if (!quill) return;
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 (!isMounted.current) {
161
- const editor = document.createElement('div');
162
- containerRef.current?.appendChild(editor);
163
- const quillInstance = new Quill(editor, {
164
- theme: 'snow',
165
- modules: {
166
- ...modules
167
- },
168
- formats,
169
- placeholder,
170
- bounds,
171
- readOnly
172
- });
173
- const editorActual = editor.querySelector('.ql-editor');
174
- if (editorActual) {
175
- editorActual.setAttribute('id', id || 'ql-editor');
176
- editorActual.setAttribute('name', name || 'ql-editor');
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
- quillInstance.keyboard.addBinding({
179
- key: 'Backspace'
180
- }, {
181
- format: {
182
- list: true,
183
- indent: true
184
- }
185
- }, (range, context) => {
186
- return handleBackspaceWithIndentetList(range, context, quillInstance);
187
- });
188
- // Move the last binding in the list, to the top. Ensure that the custom binding is called first.
189
- quillInstance.keyboard.bindings['Backspace'].unshift(quillInstance.keyboard.bindings['Backspace'].pop());
190
- quillInstance.keyboard.addBinding({
191
- key: 'Escape'
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
- if (initialValue !== undefined) {
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
- useEffect(() => {
209
- if (!quill) return;
210
- const handleBlur = () => {
211
- setTimeout(() => {
212
- const html = quill.root.innerHTML;
213
- onBlur && onBlur(html);
214
- }, 50);
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
- quill.off('selection-change');
220
+ ref && ref.current && (ref.current = null);
221
+ container.innerHTML = '';
229
222
  };
230
- }, [quill]);
231
- useEffect(() => {
232
- if (!quill) return;
233
- quill.on('text-change', handleChange);
234
- return () => {
235
- quill.off('text-change', handleChange);
236
- };
237
- }, [quill, isMounted]);
238
- useEffect(() => {
239
- applySizes();
240
- }, [quill, height, width, maxHeight, maxWidth, minHeight, minWidth]);
241
- useEffect(() => {
242
- if (!quill) return;
243
- quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
244
- if (!pasteAsText) return delta;
245
- let ops = [];
246
- delta.ops.forEach(op => {
247
- if (op.insert && typeof op.insert === 'string') {
248
- ops.push({
249
- insert: op.insert
250
- });
251
- }
252
- });
253
- delta.ops = ops;
254
- return delta;
255
- });
256
- }, [quill, pasteAsText]);
257
- useEffect(() => {
258
- if (quill && overrideValue !== undefined) {
259
- const currentContent = quill.root.innerHTML;
260
- if (currentContent !== overrideValue) {
261
- const delta = quill.clipboard.convert({
262
- html: overrideValue
263
- });
264
- quill.setContents(delta);
265
- }
266
- }
267
- }, [quill, overrideValue]);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbl-digital/snorre",
3
- "version": "4.1.12",
3
+ "version": "4.1.14",
4
4
  "description": "Design library for BBL Digital",
5
5
  "license": "MIT",
6
6
  "main": "./lib/index.js",