Skip to content

Commit

Permalink
fix: issue where tab icon rule config breaks when already set
Browse files Browse the repository at this point in the history
  • Loading branch information
hdykokd committed Jul 15, 2023
1 parent a3937f8 commit 4c25304
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/setting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PluginSettingTab, App, Setting, setIcon } from 'obsidian';
import { DEFAULT_POSITION_OPTIONS, DEFAULT_TAB_ICON_CONFIG, TAB_ICON_OPTIONS } from './constants';
import VerticalTabsView from './main';
import { TabIconConfig } from './types';
import { TabIconRule } from './types';
import { createSelect, createText, createToggle } from './util/setting';

export interface VerticalTabsViewSettings {
Expand All @@ -10,9 +10,9 @@ export interface VerticalTabsViewSettings {
showPinIconIfNotPinned: boolean;
showTabIcon: boolean;
defaultTabIcon: string;
tabIconRules: TabIconConfig[];
tabIconRules: TabIconRule[];
// deprecated
tabIconConfigs?: TabIconConfig[];
tabIconConfigs?: TabIconRule[];
}

export const DEFAULT_SETTINGS: VerticalTabsViewSettings = {
Expand Down Expand Up @@ -137,7 +137,7 @@ export class VerticalTabsViewSettingTab extends PluginSettingTab {
return { previewIconWrapper, previewIcon, previewIconText };
}

addTabIconRule(parentEl: HTMLElement, config: TabIconConfig, index: number) {
addTabIconRule(parentEl: HTMLElement, config: TabIconRule, index: number) {
const wrapperEl = parentEl.createEl('div');
wrapperEl.className = 'vertical-tabs-view-settings-tab-icon-rule-wrapper';

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TAB_ICON_OPTIONS } from './constants';

export type TabIconConfig = {
export type TabIconRule = {
matchConfig: {
target: keyof (typeof TAB_ICON_OPTIONS)['TARGET'];
condition: keyof (typeof TAB_ICON_OPTIONS)['CONDITION'];
Expand Down
6 changes: 3 additions & 3 deletions src/util/view.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TabIconConfig } from 'src/types';
import { TabIconRule } from 'src/types';

export const getMatchedTabIconConfig = (
configs: TabIconConfig[],
configs: TabIconRule[],
dirname: string,
title: string,
regexpCompileCache: Record<string, RegExp>,
Expand All @@ -14,7 +14,7 @@ export const getMatchedTabIconConfig = (
return regexp;
};

const matchValue = (c: TabIconConfig, value: string) => {
const matchValue = (c: TabIconRule, value: string) => {
if (c.matchConfig.condition === 'startsWith') {
if (value.startsWith(c.matchConfig.value)) return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/view.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it } from 'vitest';
import { DEFAULT_TAB_ICON_CONFIG } from './constants';
import { TabIconConfig } from './types';
import { TabIconRule } from './types';
import { getMatchedTabIconConfig } from './util/view';

const generateConfig = (matchConfig: TabIconConfig['matchConfig']) => {
const generateConfig = (matchConfig: TabIconRule['matchConfig']) => {
return {
...DEFAULT_TAB_ICON_CONFIG,
matchConfig,
Expand All @@ -12,7 +12,7 @@ const generateConfig = (matchConfig: TabIconConfig['matchConfig']) => {

describe('VerticalTabsViewView', () => {
describe('getMatchedTabIconConfig', () => {
const cases: [TabIconConfig, string, string][] = [
const cases: [TabIconRule, string, string][] = [
[generateConfig({ target: 'directory', condition: 'startsWith', value: 'dir' }), 'dirname', 'title'],
[generateConfig({ target: 'directory', condition: 'endsWith', value: 'name' }), 'dirname', 'title'],
[generateConfig({ target: 'directory', condition: 'includes', value: 'ir' }), 'dirname', 'title'],
Expand Down
7 changes: 4 additions & 3 deletions src/view.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ItemView, setIcon, WorkspaceLeaf } from 'obsidian';
import { VerticalTabsViewSettings } from './setting';
import { TabIconConfig } from './types';
import { TabIconRule } from './types';
import { getMatchedTabIconConfig } from './util/view';
import Sortable, { type SortableEvent } from 'sortablejs';
import { setActiveLeafById } from './util/leaf';
Expand All @@ -25,7 +25,7 @@ type Leaf = WorkspaceLeaf & { id: string; pinned: boolean };

export class VerticalTabsViewView extends ItemView {
settings: VerticalTabsViewSettings;
tabIconRules: TabIconConfig[];
tabIconRules: TabIconRule[];

regexCompileCache: Record<string, RegExp> = {};

Expand Down Expand Up @@ -59,7 +59,8 @@ export class VerticalTabsViewView extends ItemView {
}
setSettings(settings: VerticalTabsViewSettings) {
this.settings = settings;
this.tabIconRules = settings.tabIconRules.sort((a, b) => b.priority - a.priority);
const tabIconRules = structuredClone(settings.tabIconRules) as TabIconRule[];
this.tabIconRules = tabIconRules.sort((a, b) => b.priority - a.priority);
}

getViewType() {
Expand Down

0 comments on commit 4c25304

Please sign in to comment.