mirror of
https://github.com/zadam/trilium.git
synced 2025-12-21 14:54:24 +01:00
refactor(toc): decouple CKEditor TOC
This commit is contained in:
parent
094f77b1af
commit
97a3e439d2
@ -2,7 +2,7 @@ import { CKTextEditor, ModelElement } from "@triliumnext/ckeditor5";
|
|||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
import { t } from "../../services/i18n";
|
import { t } from "../../services/i18n";
|
||||||
import { useActiveNoteContext, useTriliumEvent } from "../react/hooks";
|
import { useActiveNoteContext, useIsNoteReadOnly, useNoteProperty, useTriliumEvent } from "../react/hooks";
|
||||||
import RightPanelWidget from "./RightPanelWidget";
|
import RightPanelWidget from "./RightPanelWidget";
|
||||||
|
|
||||||
interface CKHeading {
|
interface CKHeading {
|
||||||
@ -12,11 +12,22 @@ interface CKHeading {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function TableOfContents() {
|
export default function TableOfContents() {
|
||||||
|
const { note, noteContext } = useActiveNoteContext();
|
||||||
|
const noteType = useNoteProperty(note, "type");
|
||||||
|
const { isReadOnly } = useIsNoteReadOnly(note, noteContext);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<RightPanelWidget title={t("toc.table_of_contents")}>
|
||||||
|
{noteType === "text" && !isReadOnly && <EditableTextTableOfContents />}
|
||||||
|
</RightPanelWidget>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function EditableTextTableOfContents() {
|
||||||
const { ntxId } = useActiveNoteContext();
|
const { ntxId } = useActiveNoteContext();
|
||||||
const [ textEditor, setTextEditor ] = useState<CKTextEditor | null>(null);
|
const [ textEditor, setTextEditor ] = useState<CKTextEditor | null>(null);
|
||||||
const [ headings, setHeadings ] = useState<CKHeading[]>([]);
|
const [ headings, setHeadings ] = useState<CKHeading[]>([]);
|
||||||
|
|
||||||
// Populate the cache with the toolbar of every note context.
|
|
||||||
useTriliumEvent("textEditorRefreshed", ({ ntxId: eventNtxId, editor }) => {
|
useTriliumEvent("textEditorRefreshed", ({ ntxId: eventNtxId, editor }) => {
|
||||||
if (eventNtxId !== ntxId) return;
|
if (eventNtxId !== ntxId) return;
|
||||||
setTextEditor(editor);
|
setTextEditor(editor);
|
||||||
@ -46,14 +57,18 @@ export default function TableOfContents() {
|
|||||||
return () => textEditor.model.document.off("change:data", changeCallback);
|
return () => textEditor.model.document.off("change:data", changeCallback);
|
||||||
}, [ textEditor ]);
|
}, [ textEditor ]);
|
||||||
|
|
||||||
return (
|
return <AbstractTableOfContents headings={headings} />;
|
||||||
<RightPanelWidget title={t("toc.table_of_contents")}>
|
}
|
||||||
{headings.map(heading => (
|
|
||||||
<li>{heading.text}</li>
|
|
||||||
))}
|
|
||||||
</RightPanelWidget>
|
|
||||||
);
|
|
||||||
|
|
||||||
|
function AbstractTableOfContents({ headings }: {
|
||||||
|
headings: {
|
||||||
|
level: number;
|
||||||
|
text: string;
|
||||||
|
}[];
|
||||||
|
}) {
|
||||||
|
return headings.map(heading => (
|
||||||
|
<li>{heading.text}</li>
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractTocFromTextEditor(editor: CKTextEditor) {
|
function extractTocFromTextEditor(editor: CKTextEditor) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user