kamotive_ui 17.3.26 → 20.3.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -113,22 +113,6 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
113
113
|
}
|
|
114
114
|
return false;
|
|
115
115
|
}, [editor]);
|
|
116
|
-
const hasStyle = useCallback((element, property, values) => {
|
|
117
|
-
let current = element;
|
|
118
|
-
while (current && current !== (editor === null || editor === void 0 ? void 0 : editor.content)) {
|
|
119
|
-
const computedStyle = window.getComputedStyle(current);
|
|
120
|
-
const styleValue = computedStyle.getPropertyValue(property);
|
|
121
|
-
if (values.some((value) => styleValue.includes(value))) {
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
current = current.parentElement;
|
|
125
|
-
}
|
|
126
|
-
return false;
|
|
127
|
-
}, [editor]);
|
|
128
|
-
const isFormatActive = useCallback((element, tagNames, styleProperty, styleValues) => {
|
|
129
|
-
return (checkFormatting(element, tagNames) ||
|
|
130
|
-
(styleProperty && styleValues ? hasStyle(element, styleProperty, styleValues) : false));
|
|
131
|
-
}, [checkFormatting, hasStyle]);
|
|
132
116
|
const setCursorToEnd = () => {
|
|
133
117
|
var _a;
|
|
134
118
|
try {
|
|
@@ -200,19 +184,21 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
200
184
|
}
|
|
201
185
|
const range = selection.getRangeAt(0);
|
|
202
186
|
const element = getElementFromRange(range);
|
|
203
|
-
|
|
204
|
-
|
|
187
|
+
const isInH2 = element ? checkFormatting(element, ['H2']) : false;
|
|
188
|
+
const isBold = isInH2
|
|
189
|
+
? (element ? checkFormatting(element, ['B', 'STRONG']) : false)
|
|
190
|
+
: document.queryCommandState('bold');
|
|
205
191
|
const newStates = {
|
|
206
|
-
bold:
|
|
207
|
-
italic:
|
|
208
|
-
underline:
|
|
209
|
-
strikethrough:
|
|
210
|
-
heading2:
|
|
211
|
-
olist: checkFormatting(element, ['OL']) || !!element.closest('ol'),
|
|
192
|
+
bold: isBold,
|
|
193
|
+
italic: document.queryCommandState('italic'),
|
|
194
|
+
underline: document.queryCommandState('underline'),
|
|
195
|
+
strikethrough: document.queryCommandState('strikethrough'),
|
|
196
|
+
heading2: isInH2,
|
|
197
|
+
olist: element ? (checkFormatting(element, ['OL']) || !!element.closest('ol')) : false,
|
|
212
198
|
};
|
|
213
199
|
setActiveStates(newStates);
|
|
214
200
|
updateButtonStates(newStates);
|
|
215
|
-
}, [
|
|
201
|
+
}, [checkFormatting, updateButtonStates]);
|
|
216
202
|
const toggleHeading2 = () => {
|
|
217
203
|
var _a;
|
|
218
204
|
const selection = window.getSelection();
|
|
@@ -240,7 +226,9 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
240
226
|
}
|
|
241
227
|
if (h2Element) {
|
|
242
228
|
const div = document.createElement('div');
|
|
243
|
-
|
|
229
|
+
while (h2Element.firstChild) {
|
|
230
|
+
div.appendChild(h2Element.firstChild);
|
|
231
|
+
}
|
|
244
232
|
const rangeOffset = range.startOffset;
|
|
245
233
|
const textNode = range.startContainer;
|
|
246
234
|
(_a = h2Element.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(div, h2Element);
|
|
@@ -500,6 +488,52 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
500
488
|
setEditorHtml(defaultValue || '');
|
|
501
489
|
setTimeout(setCursorToEnd, 0);
|
|
502
490
|
}, [defaultValue]);
|
|
491
|
+
const handleBoldToggle = () => {
|
|
492
|
+
var _a, _b;
|
|
493
|
+
const selection = window.getSelection();
|
|
494
|
+
if (!selection || selection.rangeCount === 0)
|
|
495
|
+
return;
|
|
496
|
+
const range = getSafeRange();
|
|
497
|
+
if (!range)
|
|
498
|
+
return;
|
|
499
|
+
const element = getElementFromRange(range);
|
|
500
|
+
const isInH2 = element ? checkFormatting(element, ['H2']) : false;
|
|
501
|
+
if (!isInH2) {
|
|
502
|
+
document.execCommand('bold', false, undefined);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const hasBold = element ? checkFormatting(element, ['B', 'STRONG']) : false;
|
|
506
|
+
if (hasBold) {
|
|
507
|
+
document.execCommand('bold', false, undefined);
|
|
508
|
+
}
|
|
509
|
+
else if (!range.collapsed) {
|
|
510
|
+
try {
|
|
511
|
+
const b = document.createElement('b');
|
|
512
|
+
range.surroundContents(b);
|
|
513
|
+
const newRange = document.createRange();
|
|
514
|
+
newRange.selectNodeContents(b);
|
|
515
|
+
newRange.collapse(false);
|
|
516
|
+
selection.removeAllRanges();
|
|
517
|
+
selection.addRange(newRange);
|
|
518
|
+
(_a = pellRef.current) === null || _a === void 0 ? void 0 : _a.content.dispatchEvent(new Event('input', { bubbles: true }));
|
|
519
|
+
}
|
|
520
|
+
catch (_c) {
|
|
521
|
+
document.execCommand('insertHTML', false, `<b>${range.toString()}</b>`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
const b = document.createElement('b');
|
|
526
|
+
const zws = document.createTextNode('\u200B');
|
|
527
|
+
b.appendChild(zws);
|
|
528
|
+
range.insertNode(b);
|
|
529
|
+
const newRange = document.createRange();
|
|
530
|
+
newRange.setStart(zws, 1);
|
|
531
|
+
newRange.collapse(true);
|
|
532
|
+
selection.removeAllRanges();
|
|
533
|
+
selection.addRange(newRange);
|
|
534
|
+
(_b = pellRef.current) === null || _b === void 0 ? void 0 : _b.content.dispatchEvent(new Event('input', { bubbles: true }));
|
|
535
|
+
}
|
|
536
|
+
};
|
|
503
537
|
const setupToolbar = (pellEditor) => {
|
|
504
538
|
if (!editorRef.current)
|
|
505
539
|
return;
|
|
@@ -551,7 +585,10 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
551
585
|
htmlButton.addEventListener('mousedown', (e) => {
|
|
552
586
|
e.preventDefault();
|
|
553
587
|
e.stopPropagation();
|
|
554
|
-
if (command === '
|
|
588
|
+
if (command === 'bold') {
|
|
589
|
+
handleBoldToggle();
|
|
590
|
+
}
|
|
591
|
+
else if (command === 'heading2') {
|
|
555
592
|
toggleHeading2();
|
|
556
593
|
}
|
|
557
594
|
else if (command === 'olist') {
|
|
@@ -585,8 +622,9 @@ export const TextEditor = ({ defaultValue, attachedFiles, label, onSubmit, onCan
|
|
|
585
622
|
(_a = uploaderRef.current) === null || _a === void 0 ? void 0 : _a.click();
|
|
586
623
|
};
|
|
587
624
|
const handleEditorChange = useCallback((html) => {
|
|
588
|
-
|
|
589
|
-
|
|
625
|
+
const cleanHtml = html.replace(/\u200B/g, '');
|
|
626
|
+
setEditorHtml(cleanHtml);
|
|
627
|
+
redoContentRef.current = cleanHtml;
|
|
590
628
|
updateActiveStates();
|
|
591
629
|
}, [updateActiveStates]);
|
|
592
630
|
useEffect(() => {
|