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: #43 Use new tree api for collapse and expand #44

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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
}
]
}
};
10 changes: 5 additions & 5 deletions src/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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;
}
Expand Down
25 changes: 2 additions & 23 deletions src/provider/bookmarks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Command, WorkspaceLeaf } from 'obsidian';
import { ProviderType } from '../constants';
import { ProviderBase } from './base';

Expand All @@ -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';
}
19 changes: 19 additions & 0 deletions src/provider/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
26 changes: 20 additions & 6 deletions src/provider/tag-pane.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { WorkspaceLeaf } from 'obsidian';
import { Command, WorkspaceLeaf } from 'obsidian';
import { ProviderType } from '../constants';
import { ProviderBase } from './base';

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
}
}
18 changes: 11 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 4 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}