-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
120 lines (104 loc) · 3.11 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
Editor,
MarkdownView,
Plugin,
Menu,
MarkdownFileInfo,
} from "obsidian";
import {
ElevenLabsPluginSettings,
DEFAULT_SETTINGS,
ElevenLabsSettingTab,
} from "./src/settings";
import ElevenLabsApi from "./src/eleven_labs_api";
import { ElevenLabsModal } from "./src/modals";
export default class ElevenLabsPlugin extends Plugin {
settings: ElevenLabsPluginSettings;
voices: any[];
models: any[];
addContextMenuItems = (
menu: Menu,
editor: Editor,
info: MarkdownView | MarkdownFileInfo
) => {
menu.addItem((item) => {
const markdownView =
this.app.workspace.getActiveViewOfType(MarkdownView);
const selectedText = markdownView?.editor.getSelection();
item.setTitle("Eleven Labs")
.setIcon("file-audio")
.onClick(async () => {
if (selectedText != null) {
new ElevenLabsModal(this, selectedText).open();
}
});
item.setDisabled(selectedText == null); // Disable the item if no text is selected
});
};
openModalCommand = {
id: "eleven-labs-open-modal",
name: "Open Modal",
editorCheckCallback: (
checking: boolean,
editor: Editor,
view: MarkdownView
) => {
const selectedText = view?.editor.getSelection();
if (selectedText) {
if (!checking) {
new ElevenLabsModal(this, selectedText).open();
}
return true;
}
return false;
},
};
async onload() {
await this.loadSettings();
// Load voices
this.loadVoices();
// Load models
this.loadModels();
// Add context menu item
this.app.workspace.on("editor-menu", this.addContextMenuItems);
// Add command to open modal
this.addCommand(this.openModalCommand);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new ElevenLabsSettingTab(this.app, this));
}
onunload() {
this.app.workspace.off("editor-menu", this.addContextMenuItems);
}
async loadVoices() {
try {
const response = await ElevenLabsApi.getVoices(
this.settings.apiKey
);
this.voices = response.json.voices;
} catch (error) {
console.log(error);
}
}
async loadModels() {
try {
const response = await ElevenLabsApi.getModels(
this.settings.apiKey
);
this.models = response.json.filter(
(m: any) => m.can_do_text_to_speech
);
} catch (error) {
console.log(error);
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}