From 16ffa1e59a79487c1d0c50a7c1076c26f5324eff Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Sat, 23 Mar 2024 13:50:10 -0400 Subject: [PATCH] fix: #43 Use new tree api for collapse and expand There were internal breaking changes to the Obsidian api. Updated each provider with its current state. Closes: #43 --- .eslintrc.js | 38 ++++++++++++++++++++++++++++++++++++++ src/provider/base.ts | 10 +++++----- src/provider/bookmarks.ts | 25 ++----------------------- src/provider/search.ts | 19 +++++++++++++++++++ src/provider/tag-pane.ts | 26 ++++++++++++++++++++------ src/types.ts | 18 +++++++++++------- tsconfig.json | 14 ++++---------- 7 files changed, 99 insertions(+), 51 deletions(-) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..9e73715 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,38 @@ +module.exports = { + env: { + es2021: true, + node: true + }, + extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'], + overrides: [ + { + env: { + node: true + }, + files: ['.eslintrc.{js,cjs}'], + parserOptions: { + sourceType: 'script' + } + } + ], + parser: '@typescript-eslint/parser', + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module' + }, + plugins: ['@typescript-eslint'], + rules: { + '@typescript-eslint/no-unused-vars': [ + 'error', + { + args: 'all', + argsIgnorePattern: '^_', + caughtErrors: 'all', + caughtErrorsIgnorePattern: '^_', + destructuredArrayIgnorePattern: '^_', + varsIgnorePattern: '^_', + ignoreRestSiblings: true + } + ] + } +}; diff --git a/src/provider/base.ts b/src/provider/base.ts index aec2320..2f2937c 100644 --- a/src/provider/base.ts +++ b/src/provider/base.ts @@ -102,19 +102,19 @@ export abstract class ProviderBase { collapsed?: boolean ): void { if (collapsed === undefined) { - if (!leaf.view.toggleCollapseAll) { + if (!leaf.view.tree?.toggleCollapseAll) { console.error( `No toggle collapse function found on ${this.leafType} view.` ); return; } - leaf.view.toggleCollapseAll(); + leaf.view.tree?.toggleCollapseAll(); } else { - if (!leaf.view.setCollapseAll) { + if (!leaf.view.tree?.setCollapseAll) { console.error(`No collapse function found on ${this.leafType} view.`); return; } - leaf.view.setCollapseAll(collapsed); + leaf.view.tree.setCollapseAll(collapsed); } } @@ -125,7 +125,7 @@ export abstract class ProviderBase { const leaves = singleLeaf ? [singleLeaf] : this.leaves; let collapsed = true; for (const leaf of leaves) { - if (!leaf.view.isAllCollapsed) { + if (leaf.view.tree?.isAllCollapsed === undefined) { console.error('No collapsed state found on view.'); collapsed = false; } diff --git a/src/provider/bookmarks.ts b/src/provider/bookmarks.ts index 5133a71..64063ae 100644 --- a/src/provider/bookmarks.ts +++ b/src/provider/bookmarks.ts @@ -1,4 +1,3 @@ -import { Command, WorkspaceLeaf } from 'obsidian'; import { ProviderType } from '../constants'; import { ProviderBase } from './base'; @@ -7,26 +6,6 @@ export class BookmarksProvider extends ProviderBase { public readonly displayName = 'Bookmarks'; protected readonly leafType = 'bookmarks'; protected readonly toggleCommandName = 'Toggle collapse in bookmarks view'; - protected readonly collapseCommandName = 'Not available'; - protected readonly expandCommandName = 'Not available'; - - protected override get commands(): Command[] { - return [this.toggleCommand]; - } - - public override allCollapsed(_?: WorkspaceLeaf): boolean { - return false; - } - - public override collapseAll(): void { - // Not available - } - - public override expandAll(): void { - // Not available - } - - protected override collapseOrExpandAll(leaf: WorkspaceLeaf): void { - leaf.view.collapseOrExpandAllEl?.click(); - } + protected readonly collapseCommandName = 'Collapse all in bookmarks view'; + protected readonly expandCommandName = 'Expand all in bookmarks view'; } diff --git a/src/provider/search.ts b/src/provider/search.ts index 00a2cf3..ea812c0 100644 --- a/src/provider/search.ts +++ b/src/provider/search.ts @@ -17,4 +17,23 @@ export class SearchProvider extends ProviderBase { public override toggleCollapse(): void { // Not available } + + /** + * Collapse or expand all items for the given leaf + * @argument collapsed if not provided, will toggle the state + */ + protected collapseOrExpandAll( + leaf: WorkspaceLeaf, + collapsed?: boolean + ): void { + if (collapsed === undefined) { + // Not availabble + } else { + if (!leaf.view.setCollapseAll) { + console.error(`No collapse function found on ${this.leafType} view.`); + return; + } + leaf.view.setCollapseAll(collapsed); + } + } } diff --git a/src/provider/tag-pane.ts b/src/provider/tag-pane.ts index be51479..2752a8a 100644 --- a/src/provider/tag-pane.ts +++ b/src/provider/tag-pane.ts @@ -1,4 +1,4 @@ -import { WorkspaceLeaf } from 'obsidian'; +import { Command, WorkspaceLeaf } from 'obsidian'; import { ProviderType } from '../constants'; import { ProviderBase } from './base'; @@ -6,16 +6,30 @@ export class TagPaneProvider extends ProviderBase { providerType: ProviderType = ProviderType.TagPane; displayName = 'Tag pane'; protected leafType = 'tag'; - protected readonly collapseCommandName = - 'Collapse open tags in all tag explorers'; - protected readonly expandCommandName = - 'Expand closed tags in all tag explorers'; + protected readonly collapseCommandName = 'Not available'; + protected readonly expandCommandName = 'Not available'; protected readonly toggleCommandName = 'Toggle collapse in all tag explorers'; + protected override get commands(): Command[] { + return [this.toggleCommand]; + } + public override toggleCollapse(singleLeaf?: WorkspaceLeaf): void { const leaves = singleLeaf ? [singleLeaf] : this.leaves; for (const leaf of leaves) { - leaf.view.collapseOrExpandAllEl?.click(); + if (!leaf.view.collapseOrExpandAllEl) { + console.error(`No collapse element found on ${this.leafType} view.`); + return; + } + leaf.view.collapseOrExpandAllEl.click(); } } + + public override collapseAll(_?: WorkspaceLeaf | null): void { + // Not available + } + + public override expandAll(_?: WorkspaceLeaf | null): void { + // Not available + } } diff --git a/src/types.ts b/src/types.ts index b80d36d..2b55c2d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,15 @@ -// eslint-disable-next-line - Need at least one import so this compiles. -import { View } from 'obsidian'; - +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { View } from 'obsidian'; declare module 'obsidian' { interface View { - toggleCollapseAll: () => void; - setCollapseAll: (collapse: boolean) => void; - isAllCollapsed: boolean; - collapseOrExpandAllEl: HTMLDivElement; + tree?: { + toggleCollapseAll?: () => void; + setCollapseAll?: (collapse: boolean) => void; + isAllCollapsed?: boolean; + }; + toggleCollapseAll?: () => void; + setCollapseAll?: (collapse: boolean) => void; + isAllCollapsed?: boolean; + collapseOrExpandAllEl?: HTMLDivElement; } } diff --git a/tsconfig.json b/tsconfig.json index 09cf7ec..32fcdaa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,18 +5,12 @@ "inlineSources": true, "module": "ESNext", "target": "es6", - "allowJs": true, + "allowJs": false, "noImplicitAny": true, + "strict": true, "moduleResolution": "node", "importHelpers": true, - "lib": [ - "dom", - "es5", - "scripthost", - "es2015" - ] + "lib": ["dom", "es5", "scripthost", "es2015"] }, - "include": [ - "**/*.ts" - ] + "include": ["**/*.ts"] }