chore(highlights_list): react to changes

This commit is contained in:
Elian Doran 2025-12-18 13:12:58 +02:00
parent 7085e62cfc
commit b42a4dcb36
No known key found for this signature in database
2 changed files with 33 additions and 5 deletions

View File

@ -54,9 +54,40 @@ function EditableTextHighlightsList() {
useEffect(() => { useEffect(() => {
if (!textEditor) return; if (!textEditor) return;
setHighlights(extractHighlightsFromTextEditor(textEditor));
const highlights = extractHighlightsFromTextEditor(textEditor); // React to changes.
setHighlights(highlights); const changeCallback = () => {
const changes = textEditor.model.document.differ.getChanges();
const affectsHighlights = changes.some(change => {
// Text inserted or removed
if (change.type === 'insert' || change.type === 'remove') {
return true;
}
// Formatting attribute changed
if (change.type === 'attribute' &&
(
change.attributeKey === 'bold' ||
change.attributeKey === 'italic' ||
change.attributeKey === 'underline' ||
change.attributeKey === 'fontColor' ||
change.attributeKey === 'fontBackgroundColor'
)
) {
return true;
}
return false;
});
if (affectsHighlights) {
setHighlights(extractHighlightsFromTextEditor(textEditor));
}
};
textEditor.model.document.on("change:data", changeCallback);
return () => textEditor.model.document.off("change:data", changeCallback);
}, [ textEditor, note ]); }, [ textEditor, note ]);
const scrollToHeading = useCallback((highlight: CKHighlight) => { const scrollToHeading = useCallback((highlight: CKHighlight) => {
@ -84,7 +115,6 @@ function extractHighlightsFromTextEditor(editor: CKTextEditor) {
for (const { item } of editor.model.createRangeIn(root).getWalker({ ignoreElementEnd: true })) { for (const { item } of editor.model.createRangeIn(root).getWalker({ ignoreElementEnd: true })) {
if (!item.is('$textProxy')) continue; if (!item.is('$textProxy')) continue;
console.log("Got ", item);
const attrs = { const attrs = {
bold: item.hasAttribute('bold'), bold: item.hasAttribute('bold'),
@ -93,7 +123,6 @@ function extractHighlightsFromTextEditor(editor: CKTextEditor) {
color: item.getAttribute('fontColor'), color: item.getAttribute('fontColor'),
background: item.getAttribute('fontBackgroundColor') background: item.getAttribute('fontBackgroundColor')
}; };
console.log("Got ", attrs);
if (Object.values(attrs).some(Boolean)) { if (Object.values(attrs).some(Boolean)) {
result.push({ result.push({

View File

@ -131,7 +131,6 @@ function EditableTextTableOfContents() {
}; };
textEditor.model.document.on("change:data", changeCallback); textEditor.model.document.on("change:data", changeCallback);
return () => textEditor.model.document.off("change:data", changeCallback); return () => textEditor.model.document.off("change:data", changeCallback);
}, [ textEditor, note ]); }, [ textEditor, note ]);