generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
188 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,115 @@ | ||
import { App, Editor, MarkdownView, Plugin, PluginSettingTab, Setting } from 'obsidian'; | ||
import { TranscriptView, TRANSCRIPT_TYPE_VIEW } from 'transcript-view'; | ||
import { EditorExtensions } from './editor-extensions'; | ||
import { EditorExtensions } from './editor-extensions'; | ||
|
||
interface YTranscriptSettings { | ||
timestampMod: number; | ||
lang: string; | ||
country: string; | ||
timestampMod: number; | ||
lang: string; | ||
country: string; | ||
} | ||
|
||
const DEFAULT_SETTINGS: YTranscriptSettings = { | ||
timestampMod: 5, | ||
lang: 'en', | ||
country: 'EN' | ||
timestampMod: 5, | ||
lang: 'en', | ||
country: 'EN' | ||
} | ||
|
||
export default class YTranscriptPlugin extends Plugin { | ||
settings: YTranscriptSettings; | ||
|
||
async onload() { | ||
await this.loadSettings(); | ||
|
||
this.registerView(TRANSCRIPT_TYPE_VIEW, | ||
(leaf) => new TranscriptView(leaf, this)); | ||
|
||
this.addCommand({ | ||
id: 'fetch-transcript', | ||
name: "Fetch transcription", | ||
editorCallback: (editor: Editor, _: MarkdownView) => { | ||
const url = EditorExtensions.getSelectedText(editor).trim(); | ||
console.log(url); | ||
this.openView(url); | ||
} | ||
}); | ||
|
||
this.addSettingTab(new YTranslateSettingTab(this.app, this)); | ||
} | ||
|
||
async openView(url: string) { | ||
const leaf = this.app.workspace.getRightLeaf(false); | ||
await leaf.setViewState({ | ||
type: TRANSCRIPT_TYPE_VIEW | ||
}); | ||
this.app.workspace.revealLeaf(leaf); | ||
leaf.setEphemeralState({ | ||
url, | ||
timestampMod: this.settings.timestampMod, | ||
lang: this.settings.lang, | ||
country: this.settings.country | ||
}); | ||
} | ||
|
||
onunload() { | ||
this.app.workspace.detachLeavesOfType(TRANSCRIPT_TYPE_VIEW); | ||
} | ||
|
||
async loadSettings() { | ||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | ||
} | ||
|
||
async saveSettings() { | ||
await this.saveData(this.settings); | ||
} | ||
settings: YTranscriptSettings; | ||
|
||
async onload() { | ||
await this.loadSettings(); | ||
|
||
this.registerView(TRANSCRIPT_TYPE_VIEW, | ||
(leaf) => new TranscriptView(leaf, this)); | ||
|
||
this.addCommand({ | ||
id: 'fetch-transcript', | ||
name: "Fetch transcription", | ||
editorCallback: (editor: Editor, _: MarkdownView) => { | ||
const url = EditorExtensions.getSelectedText(editor).trim(); | ||
this.openView(url); | ||
} | ||
}); | ||
|
||
this.addSettingTab(new YTranslateSettingTab(this.app, this)); | ||
} | ||
|
||
async openView(url: string) { | ||
const leaf = this.app.workspace.getRightLeaf(false); | ||
await leaf.setViewState({ | ||
type: TRANSCRIPT_TYPE_VIEW | ||
}); | ||
this.app.workspace.revealLeaf(leaf); | ||
leaf.setEphemeralState({ | ||
url, | ||
timestampMod: this.settings.timestampMod, | ||
lang: this.settings.lang, | ||
country: this.settings.country | ||
}); | ||
} | ||
|
||
onunload() { | ||
this.app.workspace.detachLeavesOfType(TRANSCRIPT_TYPE_VIEW); | ||
} | ||
|
||
async loadSettings() { | ||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | ||
} | ||
|
||
async saveSettings() { | ||
await this.saveData(this.settings); | ||
} | ||
} | ||
|
||
class YTranslateSettingTab extends PluginSettingTab { | ||
plugin: YTranscriptPlugin; | ||
values: Record<string, string>; | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
constructor(app: App, plugin: YTranscriptPlugin) { | ||
super(app, plugin); | ||
this.plugin = plugin; | ||
} | ||
|
||
display(): void { | ||
const { containerEl } = this; | ||
containerEl.empty(); | ||
containerEl.createEl('h2', { text: 'Settings for YTranslate' }); | ||
|
||
new Setting(containerEl) | ||
.setName('Timestamp mod') | ||
.setDesc('Indicates how often timestamp should occure in text (1 - every line, 10 - every 10 lines)') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.timestampMod.toFixed()) | ||
.onChange(async (value) => { | ||
console.log('Timestamp:', value); | ||
const v = Number.parseInt(value); | ||
this.plugin.settings.timestampMod = Number.isNaN(v) ? 5 : v; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
new Setting(containerEl) | ||
.setName('Language') | ||
.setDesc('Prefered transcript language') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.lang) | ||
.onChange(async (value) => { | ||
this.plugin.settings.lang = value; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
new Setting(containerEl) | ||
.setName('Country') | ||
.setDesc('Prefered transcript country code') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.country) | ||
.onChange(async (value) => { | ||
this.plugin.settings.country = value; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
} | ||
plugin: YTranscriptPlugin; | ||
values: Record<string, string>; | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
constructor(app: App, plugin: YTranscriptPlugin) { | ||
super(app, plugin); | ||
this.plugin = plugin; | ||
} | ||
|
||
display(): void { | ||
const { containerEl } = this; | ||
containerEl.empty(); | ||
containerEl.createEl('h2', { text: 'Settings for YTranslate' }); | ||
|
||
new Setting(containerEl) | ||
.setName('Timestamp mod') | ||
.setDesc('Indicates how often timestamp should occure in text (1 - every line, 10 - every 10 lines)') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.timestampMod.toFixed()) | ||
.onChange(async (value) => { | ||
const v = Number.parseInt(value); | ||
this.plugin.settings.timestampMod = Number.isNaN(v) ? 5 : v; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
new Setting(containerEl) | ||
.setName('Language') | ||
.setDesc('Prefered transcript language') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.lang) | ||
.onChange(async (value) => { | ||
this.plugin.settings.lang = value; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
new Setting(containerEl) | ||
.setName('Country') | ||
.setDesc('Prefered transcript country code') | ||
.addText(text => text | ||
.setValue(this.plugin.settings.country) | ||
.onChange(async (value) => { | ||
this.plugin.settings.country = value; | ||
await this.plugin.saveSettings(); | ||
})); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.