generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
206 lines (181 loc) · 5.4 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
getIconIds,
addIcon,
removeIcon,
App,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import rpgAwesomeSvgStr from "./node_modules/rpg-awesome/fonts/rpgawesome-webfont.svg";
import govIconsSvgStr from "./node_modules/govicons/fonts/govicons-webfont.svg";
import weatherIconsSvgStr from "./node_modules/weather-icons/font/weathericons-regular-webfont.svg";
// Need to load in and use a regex like
// ^@([a-z-0-9]+)\s*:\s*"\\([0-9a-f]*)";/gm
// to get the less variables.
interface iconSets {
name: string;
description: string;
prefix: string;
contents: string;
parseName: (element: Element) => string;
}
// https://www.npmjs.com/package/foundation-icon-fonts
// https://github.com/zurb/foundation-icon-fonts
// https://zurb.com/playground/foundation-icon-fonts-3
// https://www.npmjs.com/package/map-icons
// http://map-icons.com/o
import weatherIconsVariables from "./node_modules/weather-icons/weather-icons/variables.less";
const weatherIconsVariableRexexp = /^@([a-z-0-9]+)\s*:\s*"\\([0-9a-f]*)";/gm;
const unicodeCharToIconName = Object.fromEntries(
Array.from(
weatherIconsVariables.matchAll(weatherIconsVariableRexexp),
(m) => [String.fromCharCode(parseInt(m[2], 16)), m[1]]
)
);
function dereferenceWeatherIcons(element: Element): string {
const iconUnicode = element.getAttribute("unicode") || "unknown";
return unicodeCharToIconName[iconUnicode];
}
function glyphElement(element: Element): string {
return element.getAttribute("glyph-name") || "";
}
const availableIcons: iconSets[] = [
{
name: "RPG Awesome",
description:
"The RPG Awesome Icon Set from https://nagoshiashumari.github.io/Rpg-Awesome/",
prefix: "ra",
contents: rpgAwesomeSvgStr,
parseName: glyphElement,
},
{
name: "GovIcons",
description: "Government Icons from https://govicons.io/",
prefix: "gi",
contents: govIconsSvgStr,
parseName: glyphElement,
},
{
name: "Weather Icons",
description:
"Weather-related Icons from https://erikflowers.github.io/weather-icons/",
prefix: "wi",
contents: weatherIconsSvgStr,
parseName: dereferenceWeatherIcons,
},
];
interface AdditionalIconsPluginSettings {
useIconsets: {
[key: string]: boolean;
};
}
const DEFAULT_SETTINGS: AdditionalIconsPluginSettings = {
useIconsets: Object.fromEntries(availableIcons.map((k) => [k.name, true])),
};
export default class AdditionalIconsPlugin extends Plugin {
settings: AdditionalIconsPluginSettings;
async onload() {
await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new AdditionalIconsSettingTab(this.app, this));
availableIcons.forEach(async (iconSet) => {
if (this.settings.useIconsets[iconSet.name]) {
await this.addIconsToObsidian(
iconSet.contents,
iconSet.prefix,
iconSet.parseName
);
}
});
}
async addIconsToObsidian(
svgFontString: string,
prefix: string,
parseName: (element: Element) => string
): Promise<void> {
const parser = new DOMParser();
const svgDom = parser.parseFromString(svgFontString, "image/svg+xml");
const fontFace = svgDom.querySelector("font-face");
const fontHorizAdvance = +(
svgDom.querySelector("font")?.getAttribute("horiz-adv-x") || 1024
);
const fontAscent = +(fontFace?.getAttribute("ascent") || 960);
const fontDescent = +(fontFace?.getAttribute("descent") || -64);
// Ultimately, the transform should be determined from the glyph
// attributes like ascent and descent attributes. For icons generated
// with icomoon this should be sufficient for now.
svgDom.querySelectorAll("glyph").forEach((el) => {
const horizAdvance = +(
el.getAttribute("horiz-adv-x") || fontHorizAdvance
);
const xAspect = 100.0 / horizAdvance;
const yAspect = 100 / (fontAscent - fontDescent);
const aspectRatio = xAspect > yAspect ? yAspect : xAspect;
const matrix = [
aspectRatio,
-aspectRatio,
(fontAscent * 100) / (fontAscent - fontDescent),
];
addIcon(
`${prefix}-${parseName(el)}`,
`<path transform="matrix(${matrix[0]} 0 0 ${matrix[1]} 0 ${
matrix[2]
})" d="${el.getAttribute("d")}"/>`
);
// console.log(
// "Adding",
// `${prefix}-${parseName(el)} with transform ${matrix}`
// );
});
}
async onunload() {
availableIcons.forEach(async (iconSet) => {
if (this.settings.useIconsets[iconSet.name]) {
await this.removePrefixedIcons(iconSet.prefix);
}
});
}
async removePrefixedIcons(prefix: string): Promise<void> {
for (const iconName of getIconIds()) {
if (iconName.startsWith(`${prefix}-`)) removeIcon(iconName);
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class AdditionalIconsSettingTab extends PluginSettingTab {
plugin: AdditionalIconsPlugin;
constructor(app: App, plugin: AdditionalIconsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
availableIcons.forEach((iconSet) => {
new Setting(containerEl)
.setName(iconSet.name)
.setDesc(iconSet.description)
.addToggle((toggle) =>
toggle
.setValue(
this.plugin.settings.useIconsets[iconSet.name]
)
.onChange(async (value) => {
this.plugin.settings.useIconsets[iconSet.name] =
value;
await this.plugin.saveSettings();
})
);
});
}
}