From b6218a7c3870b3cc7445b824f647e159d81350b2 Mon Sep 17 00:00:00 2001 From: tim-hub Date: Tue, 3 Oct 2023 09:16:17 +1300 Subject: [PATCH] reduce code duplication --- src/ui/BibleReferenceSettingTab.ts | 33 ++++++++++++------------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/ui/BibleReferenceSettingTab.ts b/src/ui/BibleReferenceSettingTab.ts index eb55e6f..7b38ef9 100644 --- a/src/ui/BibleReferenceSettingTab.ts +++ b/src/ui/BibleReferenceSettingTab.ts @@ -137,6 +137,17 @@ export class BibleReferenceSettingTab extends PluginSettingTab { }) ) + const getOutgoingLinkPosition = (linkingPostion: string | OutgoingLinkPositionEnum | undefined) => { + let value = linkingPostion + if (!value) { + value = OutgoingLinkPositionEnum.None + } else if (value as any === true) { + value = OutgoingLinkPositionEnum.Header + } + // otherwise no change + return value as string; + } + new Setting(this.expertSettingContainer) .setName('Add a Book Outgoing Link') .setDesc('Makes an outgoing link for the book, for example [[John]]') @@ -144,16 +155,7 @@ export class BibleReferenceSettingTab extends PluginSettingTab { Object.keys(OutgoingLinkPositionEnum).forEach((name) => { dropdown.addOption(name, name) }) - const defaultPosition = - (this.plugin.settings?.bookBacklinking as any) === true - ? OutgoingLinkPositionEnum.Header - : OutgoingLinkPositionEnum.None - const value: string = ( - this.plugin.settings?.bookBacklinking && - (this.plugin.settings?.bookBacklinking as any) !== true - ? this.plugin.settings.bookBacklinking - : defaultPosition - ) as string + const value = getOutgoingLinkPosition(this.plugin.settings?.bookBacklinking) dropdown.setValue(value) dropdown.onChange(async (value) => { this.plugin.settings.bookBacklinking = @@ -171,16 +173,7 @@ export class BibleReferenceSettingTab extends PluginSettingTab { Object.keys(OutgoingLinkPositionEnum).forEach((name) => { dropdown.addOption(name, name) }) - const defaultPosition = - (this.plugin.settings?.chapterBacklinking as any) === true - ? OutgoingLinkPositionEnum.Header - : OutgoingLinkPositionEnum.None - const value: string = ( - this.plugin.settings?.chapterBacklinking && - (this.plugin.settings?.chapterBacklinking as any) !== true - ? this.plugin.settings.chapterBacklinking - : defaultPosition - ) as string + const value = getOutgoingLinkPosition(this.plugin.settings?.chapterBacklinking) dropdown.setValue(value) dropdown.onChange(async (value) => { this.plugin.settings.chapterBacklinking =