Skip to content

Commit 854c2d0

Browse files
author
anpigon
committed
fix(main.ts): refactor openNewBookNote function to improve readability and remove unnecessary code
feat(main.ts): add support for opening new book note in source mode and setting ephemeral state fix(settings.ts): fix formatting and indentation in DEFAULT_SETTINGS and BookSearchSettingTab
1 parent 520920f commit 854c2d0

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/main.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -123,34 +123,28 @@ export default class BookSearchPlugin extends Plugin {
123123
// if use Templater plugin
124124
await useTemplaterPluginInFile(this.app, targetFile);
125125
this.openNewBookNote(targetFile);
126-
127126
} catch (err) {
128127
console.warn(err);
129128
this.showNotice(err);
130129
}
131130
}
132131

133-
134132
async openNewBookNote(targetFile: TFile) {
135-
if (!this.settings.openPageOnCompletion)
136-
return;
133+
if (!this.settings.openPageOnCompletion) return;
137134

135+
// open file
136+
const activeLeaf = this.app.workspace.getLeaf();
137+
if (!activeLeaf) {
138+
console.warn('No active leaf');
139+
return;
140+
}
138141

139-
// open file
140-
const activeLeaf = this.app.workspace.getLeaf();
141-
if (!activeLeaf) {
142-
console.warn('No active leaf');
143-
return;
144-
}
145-
146-
await activeLeaf.openFile(targetFile, { state: { mode: 'source' } });
147-
activeLeaf.setEphemeralState({ rename: 'all' });
148-
// cursor focus
149-
await new CursorJumper(this.app).jumpToNextCursorLocation();
142+
await activeLeaf.openFile(targetFile, { state: { mode: 'source' } });
143+
activeLeaf.setEphemeralState({ rename: 'all' });
144+
// cursor focus
145+
await new CursorJumper(this.app).jumpToNextCursorLocation();
150146
}
151147

152-
153-
154148
async openBookSearchModal(query = ''): Promise<Book[]> {
155149
return new Promise((resolve, reject) => {
156150
return new BookSearchModal(this, query, (error, results) => {

src/settings/settings.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const DEFAULT_SETTINGS: BookSearchPluginSettings = {
4242
naverClientId: '',
4343
naverClientSecret: '',
4444
localePreference: 'default',
45-
openPageOnCompletion: true
45+
openPageOnCompletion: true,
4646
};
4747

4848
export class BookSearchSettingTab extends PluginSettingTab {
@@ -209,18 +209,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
209209
});
210210

211211
new Setting(containerEl)
212-
.setName("Open New Book Note")
213-
.setDesc("Enable or disable the automatic opening of the note on creation.")
212+
.setName('Open New Book Note')
213+
.setDesc('Enable or disable the automatic opening of the note on creation.')
214214
.addToggle(toggle =>
215-
toggle
216-
.setValue(this.plugin.settings.openPageOnCompletion)
217-
.onChange(async value => {
218-
this.plugin.settings.openPageOnCompletion = value;
219-
await this.plugin.saveSettings();
220-
})
215+
toggle.setValue(this.plugin.settings.openPageOnCompletion).onChange(async value => {
216+
this.plugin.settings.openPageOnCompletion = value;
217+
await this.plugin.saveSettings();
218+
}),
221219
);
222220

223-
224221
// Frontmatter Settings
225222
const formatterSettingsChildren: Setting[] = [];
226223
createFoldingHeader(containerEl, 'Frontmatter Settings', formatterSettingsChildren);

0 commit comments

Comments
 (0)