From 4c253044dd64fb15e6f0dce045d6818e4bbd2bca Mon Sep 17 00:00:00 2001 From: hdykokd Date: Sat, 15 Jul 2023 09:09:39 +0900 Subject: [PATCH] fix: issue where tab icon rule config breaks when already set --- src/setting.ts | 8 ++++---- src/types.ts | 2 +- src/util/view.ts | 6 +++--- src/view.test.ts | 6 +++--- src/view.ts | 7 ++++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/setting.ts b/src/setting.ts index 58b9fa8..eab8884 100644 --- a/src/setting.ts +++ b/src/setting.ts @@ -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 { @@ -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 = { @@ -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'; diff --git a/src/types.ts b/src/types.ts index de4598c..f76b5d4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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']; diff --git a/src/util/view.ts b/src/util/view.ts index 2034460..2b6d0c2 100644 --- a/src/util/view.ts +++ b/src/util/view.ts @@ -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, @@ -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; } diff --git a/src/view.test.ts b/src/view.test.ts index 97b817e..653e7ba 100644 --- a/src/view.test.ts +++ b/src/view.test.ts @@ -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, @@ -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'], diff --git a/src/view.ts b/src/view.ts index 65899e9..adec5e1 100644 --- a/src/view.ts +++ b/src/view.ts @@ -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'; @@ -25,7 +25,7 @@ type Leaf = WorkspaceLeaf & { id: string; pinned: boolean }; export class VerticalTabsViewView extends ItemView { settings: VerticalTabsViewSettings; - tabIconRules: TabIconConfig[]; + tabIconRules: TabIconRule[]; regexCompileCache: Record = {}; @@ -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() {