generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
180 lines (152 loc) · 5.06 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { App, MarkdownPostProcessor, MarkdownPostProcessorContext, MarkdownPreviewRenderer, MarkdownRenderer, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface ScalesChordsPluginSettings {
instrument: string;
}
const DEFAULT_SETTINGS: ScalesChordsPluginSettings = {
instrument: 'piano'
}
export default class ScalesChordsPlugin extends Plugin {
settings: ScalesChordsPluginSettings;
async onload() {
const chordRegex = new RegExp("\[:(.*?):\]");
console.log('loading ScalesChords');
//@ts-ignore
window.scales_chords_api_debug = true;
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("tab", (source, el, ctx)=>{
let chords = new Set();
let pre = document.createElement("pre")
let lines = source.split("\n")
for (var line of lines) {
// parse tab lines out into separate tokens, preserving white space
if (line[line.length-1] == "%") {
let tokens = [];
var cur_token = '';
var last_char = '';
for (var char of line.split("")) {
if (char == "%") char = " ";
if ((last_char == ' ' && char != ' ') || (last_char != ' ' && char == ' ')) {
tokens.push(cur_token);
cur_token = '';
}
cur_token += char;
last_char = char;
}
tokens.push(cur_token);
let div = document.createElement('div');
for (var token of tokens) {
if (token[0] != ' ') {
chords.add(token);
let e = document.createElement('b');
e.innerHTML = token;
let _token = token;
this.registerDomEvent(e, 'click', (evt)=>{
new TabModal(this.app, _token, this.settings.instrument).open();
});
div.appendChild(e);
} else {
div.appendChild(document.createTextNode(token));
}
}
pre.appendChild(div);
} else {
let line_elem = document.createTextNode(line+"\n");
pre.appendChild(line_elem);
}
}
el.appendChild(pre)
for (var chord of chords) {
if (chord == '') continue;
append_chord_image(el, chord, this.settings.instrument);
}
});
}
onunload() {
console.log('unloading plugin');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class TabModal extends Modal {
chord: string;
constructor(app: App, _chord: string, _instrument: string) {
super(app);
this.instrument = _instrument;
this.chord = _chord;
}
onOpen() {
let {contentEl} = this;
append_chord_image(contentEl, this.chord, this.instrument);
}
onClose() {
let {contentEl} = this;
contentEl.empty();
}
}
class SettingTab extends PluginSettingTab {
plugin: ScalesChordsPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Scales and Chords'});
new Setting(containerEl)
.setName('instrument')
.setDesc('musical instrument to render')
.addText(text => text
.setPlaceholder('Enter instrument')
.setValue(this.plugin.settings.instrument)
.onChange(async (value) => {
this.plugin.settings.instrument = value;
await this.plugin.saveSettings();
}));
}
}
function append_chord_image(el: Any, chord: string, instrument: string) {
postData(
"https://www.scales-chords.com/api/scapi.1.3.php",
{
'id': 'scapiobjid1',
'class': 'scales_chords_api',
'chord': chord,
'instrument': instrument
}
)
.then(res=>res.text())
.then(text=>{
let arr = text.split("###RAWR###");
let inner = document.createElement("div");
inner.innerHTML = arr[arr.length-1];
el.appendChild(inner);
});
}
function postData(url = '', data = {}) {
// Default options are marked with *
const response = fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer',
body: serialize(data) // body data type must match "Content-Type" header
});
return response;
}
function serialize(obj: Any) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}