From d541c3bcbfb799eb84a648e2b2bc8b88e58aafb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Strz=C4=99pek?= Date: Fri, 3 Feb 2023 20:30:12 +0100 Subject: [PATCH] Removed console logs --- main.ts | 204 ++++++++++++++++++++++----------------------- manifest.json | 2 +- transcript-view.ts | 173 +++++++++++++++++++------------------- 3 files changed, 188 insertions(+), 191 deletions(-) diff --git a/main.ts b/main.ts index 98f25e6..e9f813c 100644 --- a/main.ts +++ b/main.ts @@ -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; - - - /** - * - */ - 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; + + + /** + * + */ + 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(); + })); + + } } diff --git a/manifest.json b/manifest.json index 9620a13..d7fa917 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-ytranscript", "name": "YTranscript", - "version": "0.6.1", + "version": "0.6.2", "minAppVersion": "0.15.0", "description": "This is simple plugin to fetch transcription for Youtube.", "author": "Łukasz Strzępek", diff --git a/transcript-view.ts b/transcript-view.ts index cb912ac..c24bb3e 100644 --- a/transcript-view.ts +++ b/transcript-view.ts @@ -4,103 +4,102 @@ import YoutubeTranscript, { TranscriptResponse } from "youtube-transcript"; export const TRANSCRIPT_TYPE_VIEW = "transcript-view"; const formatTimestamp = (t: number): string => { - const fnum = (n: number) => (n && n < 10) ? "0" + n.toFixed() : n.toFixed(); - const s = 1000; - const m = 60 * s; - const h = 60 * m; - const hours = Math.floor(t / h); - const minutes = Math.floor((t - hours * h) / m); - const seconds = Math.floor((t - minutes * m) / s); - const time = hours ? [hours, minutes, seconds] : [minutes, seconds]; - return time.map(fnum).join(':') + const fnum = (n: number) => (n && n < 10) ? "0" + n.toFixed() : n.toFixed(); + const s = 1000; + const m = 60 * s; + const h = 60 * m; + const hours = Math.floor(t / h); + const minutes = Math.floor((t - hours * h) / m); + const seconds = Math.floor((t - minutes * m) / s); + const time = hours ? [hours, minutes, seconds] : [minutes, seconds]; + return time.map(fnum).join(':') } export class TranscriptView extends ItemView { - plugin: YTranscriptPlugin; - dataLoaded: boolean; - constructor(leaf: WorkspaceLeaf, plugin: YTranscriptPlugin) { - super(leaf); - this.plugin = plugin; - this.dataLoaded = false; - } + plugin: YTranscriptPlugin; + dataLoaded: boolean; + constructor(leaf: WorkspaceLeaf, plugin: YTranscriptPlugin) { + super(leaf); + this.plugin = plugin; + this.dataLoaded = false; + } - getViewType(): string { - return TRANSCRIPT_TYPE_VIEW; - } - getDisplayText(): string { - return "Transcript"; - } - getIcon(): string { - return "scroll"; - } + getViewType(): string { + return TRANSCRIPT_TYPE_VIEW; + } + getDisplayText(): string { + return "Transcript"; + } + getIcon(): string { + return "scroll"; + } - protected async onOpen(): Promise { - console.log('open!'); - const { contentEl } = this; - contentEl.empty(); - contentEl.createEl("h4", { text: "Transcript" }); - } + protected async onOpen(): Promise { + const { contentEl } = this; + contentEl.empty(); + contentEl.createEl("h4", { text: "Transcript" }); + } - async setEphemeralState({ url, timestampMod, lang, country }: any): Promise { - if(this.dataLoaded) - return; + async setEphemeralState({ url, timestampMod, lang, country }: any): Promise { + if (this.dataLoaded) + return; - try { - const data = await YoutubeTranscript.fetchTranscript(url, { lang, country }); - if (!data) { - this.contentEl.empty(); - this.contentEl.createEl('h4', { text: "No transcript found" }); - this.contentEl.createEl('div', { text: "Please check if video contains any transcript or try adjust language and country in plugin settings." }); - return; - } + try { + const data = await YoutubeTranscript.fetchTranscript(url, { lang, country }); + if (!data) { + this.contentEl.empty(); + this.contentEl.createEl('h4', { text: "No transcript found" }); + this.contentEl.createEl('div', { text: "Please check if video contains any transcript or try adjust language and country in plugin settings." }); + return; + } - const handleDrag = (quote: string) => { - return (event: DragEvent) => { - event.dataTransfer?.setData('text/plain', quote); - } - }; + const handleDrag = (quote: string) => { + return (event: DragEvent) => { + event.dataTransfer?.setData('text/plain', quote); + } + }; - var quote = ''; - var quoteTimeOffset = 0; - data.forEach((line, i) => { - if (i === 0) { - quoteTimeOffset = line.offset; - quote += (line.text + ' '); - return; - } - if (i % timestampMod == 0) { - const div = createEl('div', { cls: "transcript-block" }); + var quote = ''; + var quoteTimeOffset = 0; + data.forEach((line, i) => { + if (i === 0) { + quoteTimeOffset = line.offset; + quote += (line.text + ' '); + return; + } + if (i % timestampMod == 0) { + const div = createEl('div', { cls: "transcript-block" }); - const button = createEl('button', { cls: "timestamp", attr: { "data-timestamp": quoteTimeOffset.toFixed() } }); - const link = createEl('a', { text: formatTimestamp(quoteTimeOffset), attr: { "href": url + '&t=' + Math.floor(quoteTimeOffset / 1000) } }); - button.appendChild(link); + const button = createEl('button', { cls: "timestamp", attr: { "data-timestamp": quoteTimeOffset.toFixed() } }); + const link = createEl('a', { text: formatTimestamp(quoteTimeOffset), attr: { "href": url + '&t=' + Math.floor(quoteTimeOffset / 1000) } }); + button.appendChild(link); - const span = this.contentEl.createEl('span', { cls: "transcript-line", text: quote }); - span.setAttr('draggable', 'true'); - span.addEventListener('dragstart', handleDrag(quote)); + const span = this.contentEl.createEl('span', { cls: "transcript-line", text: quote }); + span.setAttr('draggable', 'true'); + span.addEventListener('dragstart', handleDrag(quote)); - div.appendChild(button); - div.appendChild(span); - div.addEventListener('contextmenu', (event: MouseEvent) => { - const menu = new Menu(); - menu.addItem((item) => item - .setTitle('Copy all') - .onClick(() => { - navigator.clipboard.writeText(data.map(t => t.text).join(' ')); - })); - menu.showAtPosition({ x: event.clientX, y: event.clientY }); - }); - this.contentEl.appendChild(div); + div.appendChild(button); + div.appendChild(span); + div.addEventListener('contextmenu', (event: MouseEvent) => { + const menu = new Menu(); + menu.addItem((item) => item + .setTitle('Copy all') + .onClick(() => { + navigator.clipboard.writeText(data.map(t => t.text).join(' ')); + })); + menu.showAtPosition({ x: event.clientX, y: event.clientY }); + }); + this.contentEl.appendChild(div); - quote = ''; - quoteTimeOffset = line.offset; - } - quote += (line.text + ' '); - }); - this.dataLoaded = true; - } catch (err) { - this.contentEl.empty(); - this.contentEl.createEl('h4', { text: "Error" }); - this.contentEl.createEl('div', { text: err }); - } - } + quote = ''; + quoteTimeOffset = line.offset; + } + quote += (line.text + ' '); + }); + this.dataLoaded = true; + } catch (err) { + this.contentEl.empty(); + this.contentEl.createEl('h4', { text: "Error" }); + this.contentEl.createEl('div', { text: err }); + } + } }