mirror of
https://github.com/zadam/trilium.git
synced 2026-01-06 14:44:25 +01:00
chore(highlights_list): react to changes
This commit is contained in:
parent
7085e62cfc
commit
b42a4dcb36
@ -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({
|
||||||
|
|||||||
@ -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 ]);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user