refactor(note_tree): improve type safety

This commit is contained in:
Elian Doran 2025-08-30 20:02:32 +03:00
parent bb55544f25
commit f55e33f303
No known key found for this signature in database
2 changed files with 10 additions and 10 deletions

View file

@ -113,7 +113,7 @@ declare namespace Fancytree {
generateFormElements(selected?: boolean, active?: boolean): void; generateFormElements(selected?: boolean, active?: boolean): void;
/** Return the currently active node or null. */ /** Return the currently active node or null. */
getActiveNode(): FancytreeNode; getActiveNode(): FancytreeNode | null;
/** Return the first top level node if any (not the invisible root node). */ /** Return the first top level node if any (not the invisible root node). */
getFirstChild(): FancytreeNode; getFirstChild(): FancytreeNode;

View file

@ -382,7 +382,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
if (event.shiftKey && !ctrlKey) { if (event.shiftKey && !ctrlKey) {
const activeNode = this.getActiveNode(); const activeNode = this.getActiveNode();
if (activeNode.getParent() !== node.getParent()) { if (activeNode?.getParent() !== node.getParent()) {
return true; return true;
} }
@ -729,7 +729,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
shortcutService.bindElShortcut($(this.tree.$container), key, () => { shortcutService.bindElShortcut($(this.tree.$container), key, () => {
const node = this.tree.getActiveNode(); const node = this.tree.getActiveNode();
return handler(node, {} as JQuery.KeyDownEvent); return node && handler(node, {} as JQuery.KeyDownEvent);
// return false from the handler will stop default handling. // return false from the handler will stop default handling.
}); });
} }
@ -921,8 +921,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
nodes.push(node); nodes.push(node);
} }
if (nodes.length === 0) { const activeNode = this.getActiveNode();
nodes.push(this.getActiveNode()); if (nodes.length === 0 && activeNode) {
nodes.push(activeNode);
} }
// hidden subtree is hackily hidden via CSS when hoisted to root // hidden subtree is hackily hidden via CSS when hoisted to root
@ -967,9 +968,6 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
this.collapseTree(); this.collapseTree();
} }
/**
* @returns {FancytreeNode|null}
*/
getActiveNode() { getActiveNode() {
return this.tree.getActiveNode(); return this.tree.getActiveNode();
} }
@ -1219,7 +1217,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
} }
const activeNode = this.getActiveNode(); const activeNode = this.getActiveNode();
const activeNodeFocused = activeNode?.hasFocus(); const activeNodeFocused = !!activeNode?.hasFocus();
const activeNotePath = activeNode ? treeService.getNotePath(activeNode) : null; const activeNotePath = activeNode ? treeService.getNotePath(activeNode) : null;
const refreshCtx: RefreshContext = { const refreshCtx: RefreshContext = {
@ -1531,7 +1529,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
// Automatically expand the hoisted note by default // Automatically expand the hoisted note by default
const node = this.getActiveNode(); const node = this.getActiveNode();
if (node.data.noteId === this.noteContext.hoistedNoteId){ if (node?.data.noteId === this.noteContext.hoistedNoteId){
this.setExpanded(node.data.branchId, true); this.setExpanded(node.data.branchId, true);
} }
} }
@ -1809,6 +1807,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
buildTouchBarCommand({ TouchBar, buildIcon }: CommandListenerData<"buildTouchBar">) { buildTouchBarCommand({ TouchBar, buildIcon }: CommandListenerData<"buildTouchBar">) {
const triggerCommand = (command: TreeCommandNames) => { const triggerCommand = (command: TreeCommandNames) => {
const node = this.getActiveNode(); const node = this.getActiveNode();
if (!node) return;
const notePath = treeService.getNotePath(node); const notePath = treeService.getNotePath(node);
this.triggerCommand<TreeCommandNames>(command, { this.triggerCommand<TreeCommandNames>(command, {
@ -1825,6 +1824,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
icon: buildIcon("NSImageNameTouchBarAddTemplate"), icon: buildIcon("NSImageNameTouchBarAddTemplate"),
click: () => { click: () => {
const node = this.getActiveNode(); const node = this.getActiveNode();
if (!node) return;
const notePath = treeService.getNotePath(node); const notePath = treeService.getNotePath(node);
noteCreateService.createNote(notePath, { noteCreateService.createNote(notePath, {
isProtected: node.data.isProtected isProtected: node.data.isProtected