Skip to content

Commit 1c31f3f

Browse files
author
Jeremy Valentine
committed
0.1.9
1 parent 07192a2 commit 1c31f3f

File tree

6 files changed

+811
-31
lines changed

6 files changed

+811
-31
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "obsidian-leaflet-plugin",
33
"name": "Obsidian Leaflet",
44
"description": "Leaflet integration for Obsidian.md",
5-
"version": "0.1.8",
5+
"version": "0.1.9",
66
"minAppVersion": "0.11.0",
77
"author": "Jeremy Valentine",
88
"repo": "valentine195/obsidian-leaflet-plugin",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-leaflet-plugin",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "Leaflet integration for Obsidian.md",
55
"main": "main.js",
66
"scripts": {

src/main.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ input.is-invalid {
9595

9696
.leaflet-div-icon {
9797

98-
background: transparent;
99-
border: none;
98+
background: transparent !important;
99+
border: none !important;
100100
width: 25px !important;
101101
height: 25px !important;
102102
margin-left: -12.5px !important;

src/main.ts

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { point, latLng } from "leaflet";
1313

1414
//Local Imports
15-
import './main.css';
15+
import "./main.css";
1616

1717
import { ObsidianLeafletSettingTab, DEFAULT_SETTINGS } from "./settings";
1818
import {
@@ -86,16 +86,16 @@ export default class ObsidianLeaflet extends Plugin {
8686
);
8787

8888
this.registerEvent(
89-
this.app.vault.on("delete", async file => {
89+
this.app.vault.on("delete", async (file) => {
9090
if (
91-
this.AppData.mapMarkers.find(marker =>
91+
this.AppData.mapMarkers.find((marker) =>
9292
marker.path.includes(file.path)
9393
)
9494
) {
9595
this.AppData.mapMarkers = this.AppData.mapMarkers.filter(
96-
marker =>
96+
(marker) =>
9797
marker !=
98-
this.AppData.mapMarkers.find(marker =>
98+
this.AppData.mapMarkers.find((marker) =>
9999
marker.path.includes(file.path)
100100
)
101101
);
@@ -118,7 +118,7 @@ export default class ObsidianLeaflet extends Plugin {
118118
ctx: MarkdownPostProcessorContextActual
119119
): Promise<void> {
120120
let { image, height = "500px" } = Object.fromEntries(
121-
source.split("\n").map(l => l.split(": "))
121+
source.split("\n").map((l) => l.split(": "))
122122
);
123123

124124
if (!image) {
@@ -129,37 +129,38 @@ export default class ObsidianLeaflet extends Plugin {
129129
return;
130130
}
131131

132+
const imageData = await this.toDataURL(image);
132133
let map = new LeafletMap(
133134
el,
134-
image,
135+
imageData,
135136
height,
136-
ctx.sourcePath,
137+
`${ctx.sourcePath}/${image}`,
137138
this.markerIcons
138139
);
139140

140141
if (
141142
this.AppData.mapMarkers.find(
142-
map => map.path == `${ctx.sourcePath}/${image}`
143+
(map) => map.path == `${ctx.sourcePath}/${image}`
143144
)
144145
) {
145146
await map.loadData(
146147
this.AppData.mapMarkers.find(
147-
map => map.path == `${ctx.sourcePath}/${image}`
148+
(map) => map.path == `${ctx.sourcePath}/${image}`
148149
).markers
149150
);
150151
}
151152

152-
if (this.maps.find(map => map.path == `${ctx.sourcePath}/${image}`)) {
153+
if (this.maps.find((map) => map.path == `${ctx.sourcePath}/${image}`)) {
153154
this.maps = this.maps.filter(
154-
map => map.path != `${ctx.sourcePath}/${image}`
155+
(map) => map.path != `${ctx.sourcePath}/${image}`
155156
);
156157
}
157158
this.maps.push(map);
158159

159-
this.registerDomEvent(el, "dragover", evt => {
160+
this.registerDomEvent(el, "dragover", (evt) => {
160161
evt.preventDefault();
161162
});
162-
this.registerDomEvent(el, "drop", evt => {
163+
this.registerDomEvent(el, "drop", (evt) => {
163164
evt.stopPropagation();
164165

165166
let file = decodeURIComponent(
@@ -201,29 +202,29 @@ export default class ObsidianLeaflet extends Plugin {
201202
.setDesc(
202203
"Path of note to open, e.g. Folder1/Folder2/Note.md"
203204
)
204-
.addText(text => {
205+
.addText((text) => {
205206
text.setPlaceholder("Path")
206207
.setValue(marker.link)
207-
.onChange(async value => {
208+
.onChange(async (value) => {
208209
marker.link = value;
209210
await this.saveSettings();
210211
});
211212
});
212213

213214
new Setting(markerSettingsModal.contentEl)
214215
.setName("Marker Type")
215-
.addDropdown(drop => {
216+
.addDropdown((drop) => {
216217
drop.addOption("default", "Base Marker");
217-
this.AppData.markerIcons.forEach(marker => {
218+
this.AppData.markerIcons.forEach((marker) => {
218219
drop.addOption(marker.type, marker.type);
219220
});
220221
drop.setValue(marker.marker.type).onChange(
221-
async value => {
222+
async (value) => {
222223
let newMarker =
223224
value == "default"
224225
? this.AppData.defaultMarker
225226
: this.AppData.markerIcons.find(
226-
m => m.type == value
227+
(m) => m.type == value
227228
);
228229
let html: string,
229230
iconNode: AbstractElement = icon(
@@ -259,14 +260,14 @@ export default class ObsidianLeaflet extends Plugin {
259260
);
260261
});
261262

262-
new Setting(markerSettingsModal.contentEl).addButton(b => {
263+
new Setting(markerSettingsModal.contentEl).addButton((b) => {
263264
b.setIcon("trash")
264265
.setWarning()
265266
.setTooltip("Delete Marker")
266267
.onClick(async () => {
267268
marker.leafletInstance.remove();
268269
map.markers = map.markers.filter(
269-
m => m.id != marker.id
270+
(m) => m.id != marker.id
270271
);
271272
markerSettingsModal.close();
272273
await this.saveSettings();
@@ -311,13 +312,13 @@ export default class ObsidianLeaflet extends Plugin {
311312
this.AppData.mapMarkers = markers;
312313
await this.saveData(this.AppData);
313314

314-
this.AppData.markerIcons.forEach(marker => {
315+
this.AppData.markerIcons.forEach((marker) => {
315316
addIcon(marker.type, icon(getIcon(marker.iconName)).html[0]);
316317
});
317318

318319
this.markerIcons = this.generateMarkerMarkup(this.AppData.markerIcons);
319320

320-
this.maps.forEach(map => map.setMarkerIcons(this.markerIcons));
321+
this.maps.forEach((map) => map.setMarkerIcons(this.markerIcons));
321322
}
322323
getEditor() {
323324
let view = this.app.workspace.getActiveViewOfType(MarkdownView);
@@ -330,7 +331,7 @@ export default class ObsidianLeaflet extends Plugin {
330331
generateMarkerMarkup(
331332
markers: Marker[] = this.AppData.markerIcons
332333
): MarkerIcon[] {
333-
let ret = markers.map(marker => {
334+
let ret = markers.map((marker) => {
334335
if (!marker.transform) {
335336
marker.transform = this.AppData.defaultMarker.transform;
336337
}
@@ -358,7 +359,6 @@ export default class ObsidianLeaflet extends Plugin {
358359
return { type: marker.type, html: html };
359360
});
360361
if (this.AppData.defaultMarker.iconName) {
361-
362362
ret.unshift({
363363
type: "default",
364364
html: icon(getIcon(this.AppData.defaultMarker.iconName), {
@@ -372,4 +372,41 @@ export default class ObsidianLeaflet extends Plugin {
372372

373373
return ret;
374374
}
375+
376+
async toDataURL(url: string): Promise<string> {
377+
//determine link type
378+
let response, blob: Blob;
379+
if (/http[s]*:/.test(url)) {
380+
//url
381+
response = await fetch(url);
382+
blob = await response.blob();
383+
} else if (/obsidian:\/\/open/.test(url)) {
384+
//obsidian link
385+
let [, vault, file] = url.match(
386+
/\?vault=([\w\s\d]+)&file=([\s\S]+)/
387+
);
388+
file = decodeURIComponent(file);
389+
if (await this.app.vault.adapter.exists(file)) {
390+
let buffer = await this.app.vault.readBinary(
391+
this.app.vault.getAbstractFileByPath(file) as TFile
392+
);
393+
blob = new Blob([new Uint8Array(buffer)]);
394+
}
395+
} else if (await this.app.vault.adapter.exists(url)) {
396+
//file exists on disk
397+
let buffer = await this.app.vault.readBinary(
398+
this.app.vault.getAbstractFileByPath(url) as TFile
399+
);
400+
blob = new Blob([new Uint8Array(buffer)]);
401+
}
402+
403+
return new Promise((resolve, reject) => {
404+
const reader = new FileReader();
405+
reader.onloadend = () => {
406+
resolve(reader.result as string);
407+
};
408+
reader.onerror = reject;
409+
reader.readAsDataURL(blob);
410+
});
411+
}
375412
}

0 commit comments

Comments
 (0)