Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jump to ToC for nested headers #4281

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 6 additions & 51 deletions src/public/app/widgets/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,16 @@ export default class TocWidget extends RightPanelWidget {
// See https://github.com/zadam/trilium/issues/2828
const isReadOnly = await this.noteContext.isReadOnly();

let $container;
if (isReadOnly) {
const $container = await this.noteContext.getContentElement();
const headingElement = $container.find(":header:not(section.include-note :header)")[headingIndex];

if (headingElement != null) {
headingElement.scrollIntoView({ behavior: "smooth" });
}
$container = await this.noteContext.getContentElement();
} else {
const textEditor = await this.noteContext.getTextEditor();

const model = textEditor.model;
const doc = model.document;
const root = doc.getRoot();

const headingNode = findHeadingNodeByIndex(root, headingIndex);

// headingNode could be null if the html was malformed or
// with headings inside elements, just ignore and don't
// navigate (note that the TOC rendering and other TOC
// entries' navigation could be wrong too)
if (headingNode != null) {
$(textEditor.editing.view.domRoots.values().next().value).find(':header:not(section.include-note :header)')[headingIndex].scrollIntoView({
behavior: 'smooth'
});
}
$container = $(textEditor.sourceElement);
}

const headingElement = $container?.find(":header:not(section.include-note :header)")?.[headingIndex];
headingElement?.scrollIntoView({ behavior: "smooth" });
}

async closeTocCommand() {
Expand All @@ -231,35 +215,6 @@ export default class TocWidget extends RightPanelWidget {
}
}

/**
* Find a heading node in the parent's children given its index.
*
* @param {Element} parent Parent node to find a headingIndex'th in.
* @param {uint} headingIndex Index for the heading
* @returns {Element|null} Heading node with the given index, null couldn't be
* found (ie malformed like nested headings, etc.)
*/
function findHeadingNodeByIndex(parent, headingIndex) {
let headingNode = null;
for (let i = 0; i < parent.childCount; ++i) {
let child = parent.getChild(i);

// Headings appear as flattened top level children in the CKEditor
// document named as "heading" plus the level, eg "heading2",
// "heading3", "heading2", etc. and not nested wrt the heading level. If
// a heading node is found, decrement the headingIndex until zero is
// reached
if (child.name.startsWith("heading")) {
if (headingIndex === 0) {
headingNode = child;
break;
}
headingIndex--;
}
}

return headingNode;
}

class CloseTocButton extends OnClickButtonWidget {
constructor() {
Expand Down
Loading