generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
142 lines (121 loc) · 3.88 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
import { Plugin, MarkdownPostProcessorContext, parseYaml } from "obsidian";
import {
SiteswapSettingTab,
SiteswapSettings,
DEFAULT_SETTINGS,
} from "settings";
const defaultSettingsObject = Object.assign({}, DEFAULT_SETTINGS);
// See https://github.com/jkboyce/jugglinglab/blob/c0226400230714571de078893c46aef734856ebc/source/jugglinglab/notation/MHNNotationControl.java#L24
function expandBuiltInHandString(
hands: string | undefined
): string | undefined {
if (hands == undefined) {
return undefined;
}
const BUILT_IN_HAND_STRINGS: Map<string, string> = new Map(
Object.entries({
inside: "(10)(32.5).",
outside: "(32.5)(10).",
half: "(32.5)(10).(10)(32.5).",
mills: "(-25)(2.5).(25)(-2.5).(-25)(0).",
})
);
return BUILT_IN_HAND_STRINGS.get(hands.toLowerCase()) || hands;
}
export class SiteswapPlugin extends Plugin {
settings: SiteswapSettings;
static postprocessor = (settings: SiteswapSettings) => {
return (
source: string,
el: HTMLElement,
ctx: MarkdownPostProcessorContext
) => {
let failure: string | null = null;
let yaml = null;
try {
// Replacing ':' with ': ' is a bit of a hack, but we know a priori that keys and values will
// never contain colons, so this is safe.
yaml = parseYaml(source.replaceAll(":", ": "));
} catch (e) {
failure = e.message;
}
if (failure != null) {
// Pass.
} else if (typeof yaml == "object") {
if (!("pattern" in yaml)) {
failure =
'Invalid siteswap: the "pattern" attribute is required.';
}
} else if (typeof yaml == "number" || typeof yaml == "string") {
yaml = {
pattern: "" + yaml,
};
} else {
failure = "Invalid siteswap.";
}
if (failure !== null) {
const message = document.createElement("p");
message.textContent = failure;
message.style.color = "var(--text-error)";
el.appendChild(message);
return;
}
const paramsObject = { redirect: true, ...settings, ...yaml };
const displayWidth = paramsObject.width;
paramsObject.width = paramsObject.width / paramsObject.scale;
paramsObject.height = paramsObject.height / paramsObject.scale;
paramsObject.hands = expandBuiltInHandString(paramsObject.hands);
// Don't pass default params. We want to fetched cached animations as much as possible,
// and passing default params means we'll miss commonly cached animations.
for (const key in paramsObject) {
// Width and height are a bit tricky as the default used here isn't the default
// for the gif generator. Special case those defaults here.
if (key == "width" && paramsObject[key] == 400) {
delete paramsObject["width"];
} else if (key == "height" && paramsObject[key] == 450) {
delete paramsObject["height"];
} else {
if (defaultSettingsObject[key] == paramsObject[key]) {
delete paramsObject[key];
}
}
}
delete paramsObject.scale;
const params = Object.keys(paramsObject)
.map(
(key) =>
encodeURIComponent(key) +
"=" +
encodeURIComponent(paramsObject[key])
)
.join(";");
const img = document.createElement("img");
img.src = "https://jugglinglab.org/anim?" + params;
img.style.width = displayWidth + "px";
el.appendChild(img);
};
};
async onload() {
await this.loadSettings();
console.log("loading siteswap plugin");
this.registerMarkdownCodeBlockProcessor(
"siteswap",
SiteswapPlugin.postprocessor(this.settings)
);
this.addSettingTab(new SiteswapSettingTab(this.app, this));
}
onunload() {
console.log("unloading siteswap plugin");
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
export default SiteswapPlugin;