From 23cc414b8e4707b0bff52ad7babb59a9fad79fdb Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 7 Dec 2021 08:36:35 +0100 Subject: [PATCH] apply changes from code review. --- manifest.json | 2 +- package.json | 2 +- src/main.ts | 143 ++++++++++++++++++++++++------------------------ src/settings.ts | 1 - versions.json | 3 +- 5 files changed, 76 insertions(+), 75 deletions(-) diff --git a/manifest.json b/manifest.json index 6295fb4..7a18e5d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-tts", "name": "Text to Speech", - "version": "0.2.0", + "version": "0.3.2", "minAppVersion": "0.12.0", "description": "Text to speech for Obsidian. Hear your notes.", "author": "Johannes Theiner", diff --git a/package.json b/package.json index 41acd4b..82e4770 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-tts", - "version": "0.2.0", + "version": "0.3.2", "description": "Text to speech for Obsidian. Hear your notes.", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index 72f7335..bd729bc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,6 @@ import { - MarkdownView, Menu, Notice, Platform, - Plugin + MarkdownView, Menu, Notice, Platform, + Plugin } from 'obsidian'; import {DEFAULT_SETTINGS, TTSSettings, TTSSettingsTab} from "./settings"; import {TTSService} from "./TTSService"; @@ -8,63 +8,64 @@ import {TTSService} from "./TTSService"; export default class TTSPlugin extends Plugin { ttsService: TTSService; - settings: TTSSettings; - statusbar: HTMLElement; + settings: TTSSettings; + statusbar: HTMLElement; - async onload(): Promise { + async onload(): Promise { this.ttsService = new TTSService(this); - console.log("loading tts plugin"); + console.log("loading tts plugin"); - //https://bugs.chromium.org/p/chromium/issues/detail?id=487255 - if (Platform.isAndroidApp) { - new Notice("TTS: due to a bug in android this plugin does not work on this platform"); - this.unload(); - } + //https://bugs.chromium.org/p/chromium/issues/detail?id=487255 + if (Platform.isAndroidApp) { + new Notice("TTS: due to a bug in android this plugin does not work on this platform"); + throw Error("TTS: due to a bug in android this plugin does not work on this platform"); + } - await this.loadSettings(); + await this.loadSettings(); - this.addCommand({ - id: 'start-tts-playback', - name: 'Start playback', - checkCallback: (checking: boolean) => { - const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); - if(checking) - return markdownView !== undefined; + this.addCommand({ + id: 'start-tts-playback', + name: 'Start playback', + checkCallback: (checking: boolean) => { + const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (!markdownView) return; + if (checking) + return !!(markdownView); this.ttsService.play(markdownView); - } - }); + } + }); - this.addCommand({ - id: 'cancel-tts-playback', - name: 'Stop playback', - checkCallback: (checking: boolean) => { - if(checking) + this.addCommand({ + id: 'cancel-tts-playback', + name: 'Stop playback', + checkCallback: (checking: boolean) => { + if (checking) return this.ttsService.isSpeaking(); this.ttsService.stop(); - } - }); - - this.addCommand({ - id: 'pause-tts-playback', - name: 'pause playback', - checkCallback: (checking: boolean) => { - if(checking) + } + }); + + this.addCommand({ + id: 'pause-tts-playback', + name: 'pause playback', + checkCallback: (checking: boolean) => { + if (checking) return this.ttsService.isSpeaking(); this.ttsService.pause(); - } - }); - - this.addCommand({ - id: 'resume-tts-playback', - name: 'Resume playback', - checkCallback: (checking: boolean) => { - if(checking) + } + }); + + this.addCommand({ + id: 'resume-tts-playback', + name: 'Resume playback', + checkCallback: (checking: boolean) => { + if (checking) return this.ttsService.isPaused(); this.ttsService.resume(); - } - }); + } + }); //clear statusbar text if not speaking this.registerInterval(window.setInterval(() => { @@ -73,9 +74,9 @@ export default class TTSPlugin extends Plugin { } }, 1000 * 10)); - this.addRibbonIcon("audio-file", "Text to Speech", async (event) => { - await this.createMenu(event); - }); + this.addRibbonIcon("audio-file", "Text to Speech", async (event) => { + await this.createMenu(event); + }); this.registerEvent(this.app.workspace.on('editor-menu', ((menu, editor, markdownView) => { menu.addItem((item) => { @@ -83,29 +84,29 @@ export default class TTSPlugin extends Plugin { .setTitle("Say selected text") .setIcon("audio-file") .onClick(() => { - this.ttsService.say("", editor.getSelection(), this.ttsService.getLanguageFromFrontmatter(markdownView)); - }); + this.ttsService.say("", editor.getSelection(), this.ttsService.getLanguageFromFrontmatter(markdownView)); + }); }); }))); this.registerEvent(this.app.workspace.on('layout-change', (() => { - if(this.settings.stopPlaybackWhenNoteChanges) { + if (this.settings.stopPlaybackWhenNoteChanges) { this.ttsService.stop(); } }))); - this.addSettingTab(new TTSSettingsTab(this)); - this.statusbar = this.addStatusBarItem(); - this.statusbar.setText("TTS"); - this.statusbar.classList.add("mod-clickable"); - this.statusbar.setAttribute("aria-label", "Text to Speech"); - this.statusbar.setAttribute("aria-label-position", "top"); - this.statusbar.onClickEvent(async (event) => { - await this.createMenu(event); - }); - } - - async createMenu(event: MouseEvent) : Promise { + this.addSettingTab(new TTSSettingsTab(this)); + this.statusbar = this.addStatusBarItem(); + this.statusbar.setText("TTS"); + this.statusbar.classList.add("mod-clickable"); + this.statusbar.setAttribute("aria-label", "Text to Speech"); + this.statusbar.setAttribute("aria-label-position", "top"); + this.statusbar.onClickEvent(async (event) => { + await this.createMenu(event); + }); + } + + async createMenu(event: MouseEvent): Promise { const menu = new Menu(this.app); const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); @@ -167,15 +168,15 @@ export default class TTSPlugin extends Plugin { menu.showAtPosition({x: event.x, y: event.y}); } - async onunload(): Promise { - console.log("unloading tts plugin"); - } + async onunload(): Promise { + console.log("unloading tts plugin"); + } - async loadSettings(): Promise { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - } + async loadSettings(): Promise { + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + } - async saveSettings(): Promise { - await this.saveData(this.settings); - } + async saveSettings(): Promise { + await this.saveData(this.settings); + } } diff --git a/src/settings.ts b/src/settings.ts index d97b95b..c4d9e77 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -111,7 +111,6 @@ export class TTSSettingsTab extends PluginSettingTab { const voicesDiv = additionalContainer.createDiv("voices"); for (const languageVoice of this.plugin.settings.languageVoices) { - console.log(languageVoice); //@ts-ignore const displayNames = new Intl.DisplayNames([languageVoice.language], {type: 'language', fallback: 'none'}); diff --git a/versions.json b/versions.json index 700e6c9..697d237 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ { "0.1.0": "0.9.12", - "0.2.0": "0.9.12" + "0.2.0": "0.9.12", + "0.3.2": "0.12.0" }