Skip to content

Commit

Permalink
Merge pull request #243 from valentine195:4.3.0
Browse files Browse the repository at this point in the history
4.3.0
  • Loading branch information
valentine195 authored Oct 11, 2021
2 parents 0e2b0d7 + 0d68816 commit d33ecd5
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 71 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ darkMode: true
## Options

| Option | Description | Default |
| -------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------ |
| -------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------ | --- |
| id | Unique identifier (can be anything). **Required.** | |
| image | Direct URL/file path to an image file to be used as the map layer. | OpenStreetMap map |
| tileServer | Add additional tile servers as different layers | |
Expand Down Expand Up @@ -502,6 +502,26 @@ All markers created from the note will automatically have their link set to the
| mapmarkers | Array of markers to create. See below for syntax. |
| mapoverlay | Array of overlays to create. See below for syntax. |

##### mapmarker

The `mapmarker` parameter can be used to define the _type_ of marker created. This can be one of two things:

1. The name given to the marker type in settings.
2. A definition defining the icon name, color, and whether or not to layer the icon on the default marker type.

Examples:

```
mapmarker: event # A marker type named event has been created in settings.
# OR
mapmarker:
icon: user # Font Awesome icon name.
color: 00ff00 # Hex color string. Optional.
layer: false # Whether or not to layer. Optional.
```

##### mapmarkers

The `mapmarkers` parameter can be used to define an arbitrary number of markers to display on the map. This does not require the `location` tag to be set.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "obsidian-leaflet-plugin",
"name": "Obsidian Leaflet",
"description": "Interactive maps inside your notes",
"version": "4.2.2",
"version": "4.3.0",
"minAppVersion": "0.12.12",
"author": "Jeremy Valentine",
"repo": "valentine195/obsidian-leaflet-plugin",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-leaflet-plugin",
"version": "4.2.2",
"version": "4.3.0",
"description": "Leaflet integration for Obsidian.md",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/@types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ declare class ObsidianLeaflet extends Plugin {

loadSettings(): Promise<void>;
saveSettings: Debouncer<Promise<void>[]>;
saveMarkerTypes(): Promise<void>;

parseIcon(icon: Icon): MarkerIcon;
generateMarkerMarkup(markers: Icon[]): MarkerIcon[];

registerMapEvents(map: BaseMapType): void;
Expand Down
8 changes: 7 additions & 1 deletion src/@types/marker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export interface MarkerProperties {
}

export interface SavedMarkerProperties {
type: string;
type:
| string
| {
icon: string;
color: string;
layer: boolean;
};
loc: [number, number];
percent: [number, number];
id: string;
Expand Down
38 changes: 38 additions & 0 deletions src/controls/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,44 @@ export class FilterMarkers extends FontAwesomeControl {
this.inputs.set(type, input);
}
}

if (
this.map.currentGroup.markers.custom &&
this.map.currentGroup.markers.custom.getLayers()?.length
) {
const li = ul.createEl("div", "input-item");

const id = getId();
const input = li.createEl("input", {
attr: {
id: "leaflet-control-expandable-item-label-" + id,
...(this.map.displaying.get("custom")
? { checked: true }
: {})
},
type: "checkbox"
});

const label = li.createEl("label", {
attr: { for: "leaflet-control-expandable-item-label-" + id }
});

label.createDiv({
text: "Custom"
});

input.addEventListener("click", (evt) => {
if (input.checked) {
this.show("custom");
} else if (this.map.displaying.get("custom")) {
this.hide("custom");
}

this.map.displaying.set("custom", input.checked);
});

this.inputs.set("custom", input);
}
}
private update() {
for (let [type, input] of this.inputs) {
Expand Down
3 changes: 2 additions & 1 deletion src/layer/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class Marker extends Layer<DivIconMarker> implements MarkerDefinition {
}: MarkerProperties
) {
super();

this.leafletInstance = divIconMarker(
loc,
{
Expand Down Expand Up @@ -508,7 +509,7 @@ export class Marker extends Layer<DivIconMarker> implements MarkerDefinition {
const min = this.minZoom ?? this.map.zoom.min;
const max = this.maxZoom ?? this.map.zoom.max;
if (min <= zoom && zoom <= max) {
return this.map.displaying.get(this.type);
return this.map.displaying.get(this.type) ?? true;
}
}
return false;
Expand Down
82 changes: 45 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,20 @@ export default class ObsidianLeaflet
);

await this.saveData(this.data);

this.markerIcons = this.generateMarkerMarkup(this.data.markerIcons);

this.maps.forEach((map) => {
map.map.updateMarkerIcons();
});
},
100,
false
);

async saveMarkerTypes() {
await this.saveSettings();
this.markerIcons = this.generateMarkerMarkup(this.data.markerIcons);

this.maps.forEach((map) => {
map.map.updateMarkerIcons();
});
}

async saveData(data: Record<any, any>) {
if (this.configDirectory) {
try {
Expand Down Expand Up @@ -484,40 +488,44 @@ export default class ObsidianLeaflet
});
}

public parseIcon(icon: Icon): MarkerIcon {
if (!icon.transform) {
icon.transform = this.data.defaultMarker.transform;
}
if (!icon.iconName) {
icon.iconName = this.data.defaultMarker.iconName;
}
const params =
icon.layer && !this.data.defaultMarker.isImage
? {
transform: icon.transform,
mask: getIcon(this.data.defaultMarker.iconName)
}
: {};
let node = getMarkerIcon(icon, {
...params,
classes: ["full-width-height"]
}).node as HTMLElement;
node.style.color = icon.color
? icon.color
: this.data.defaultMarker.color;

return {
type: icon.type,
html: node.outerHTML,
icon: markerDivIcon({
html: node.outerHTML,
className: `leaflet-div-icon`
})
};
}

public generateMarkerMarkup(
markers: Icon[] = this.data.markerIcons
): MarkerIcon[] {
let ret: MarkerIcon[] = markers.map((marker): MarkerIcon => {
if (!marker.transform) {
marker.transform = this.data.defaultMarker.transform;
}
if (!marker.iconName) {
marker.iconName = this.data.defaultMarker.iconName;
}
const params =
marker.layer && !this.data.defaultMarker.isImage
? {
transform: marker.transform,
mask: getIcon(this.data.defaultMarker.iconName)
}
: {};
let node = getMarkerIcon(marker, {
...params,
classes: ["full-width-height"]
}).node as HTMLElement;
node.style.color = marker.color
? marker.color
: this.data.defaultMarker.color;

return {
type: marker.type,
html: node.outerHTML,
icon: markerDivIcon({
html: node.outerHTML,
className: `leaflet-div-icon`
})
};
});
let ret: MarkerIcon[] = markers.map(
(marker): MarkerIcon => this.parseIcon(marker)
);
const defaultHtml = getMarkerIcon(this.data.defaultMarker, {
classes: ["full-width-height"],
styles: {
Expand Down
46 changes: 31 additions & 15 deletions src/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,38 @@ export abstract class BaseMap extends Events implements BaseMapDefinition {
addMarker(...markers: SavedMarkerProperties[]) {
let toReturn: Marker[] = [];
for (const marker of markers) {
if (!this.markerTypes.includes(marker.type)) {
new Notice(
t(
`Marker type "%1" does not exist, using default.`,
marker.type
)
);
marker.type = "default";
let markerIcon: MarkerIcon;
let type: string;
if (typeof marker.type == "object") {
type = `custom`;
markerIcon = this.plugin.parseIcon({
type: `custom`,
iconName: marker.type.icon ?? "map-marker",
layer: marker.type.layer ?? true,
color: marker.type.color
});
} else {
if (!this.markerTypes.includes(marker.type)) {
new Notice(
t(
`Marker type "%1" does not exist, using default.`,
marker.type
)
);
marker.type = "default";
}
markerIcon = this.markerIcons.get(marker.type);
type = marker.type;
}
const markerIcon = this.markerIcons.get(marker.type);

const mapIcon = markerIcon?.icon ?? this.defaultIcon.icon;

if (!this.displaying.has(marker.type)) {
this.displaying.set(marker.type, true);
if (!this.displaying.has(type)) {
this.displaying.set(type, true);
}
const newMarker = new Marker(this, {
id: marker.id,
type: marker.type,
type: type,
loc: L.latLng(marker.loc),
link: marker.link,
icon: mapIcon,
Expand Down Expand Up @@ -1231,7 +1244,7 @@ export abstract class BaseMap extends Events implements BaseMapDefinition {
});
/** Remove Old Marker Types From Filter List */
[...this.displaying].forEach(([type]) => {
if (this.markerTypes.includes(type)) return;
if (this.markerTypes.includes(type) || type == "custom") return;

this.displaying.delete(type);

Expand All @@ -1243,8 +1256,8 @@ export abstract class BaseMap extends Events implements BaseMapDefinition {
);
}
this.currentGroup.markers[type]
.getLayers()
.forEach((layer) =>
?.getLayers()
?.forEach((layer) =>
this.currentGroup.markers.default.addLayer(layer)
);

Expand Down Expand Up @@ -1295,6 +1308,7 @@ export class RealMap extends BaseMap {
const markerGroups = Object.fromEntries(
this.markerTypes.map((type) => [type, L.layerGroup()])
);
markerGroups.custom = L.layerGroup();
const overlayGroups = {
none: L.layerGroup(),
...Object.fromEntries(
Expand Down Expand Up @@ -1478,6 +1492,8 @@ export class ImageMap extends BaseMap {
const markerGroups = Object.fromEntries(
this.markerTypes.map((type) => [type, L.layerGroup()])
);
markerGroups.custom = L.layerGroup();

const overlayGroups = {
none: L.layerGroup(),
...Object.fromEntries(
Expand Down
13 changes: 7 additions & 6 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
this.data.defaultMarker.iconName = new_value;
this.data.defaultMarker.isImage = false;

await this.plugin.saveSettings();
await this.plugin.saveMarkerTypes();

this.display();
};
Expand Down Expand Up @@ -198,6 +198,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
this.data.defaultMarker.imageUrl =
canvas.toDataURL("image/png");

this.plugin.saveMarkerTypes();
this.display();

//defaultMarkerIconSetting.settingEl.appendChild(canvas);
Expand Down Expand Up @@ -247,7 +248,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
(marker) => (marker.layer = v)
);

await this.plugin.saveSettings();
await this.plugin.saveMarkerTypes();

this.display();
return;
Expand All @@ -272,7 +273,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
await this.plugin.createNewMarkerType();
if (!newMarker) return;
this.data.markerIcons.push(newMarker);
await this.plugin.saveSettings();
await this.plugin.saveMarkerTypes();

this.display();
});
Expand Down Expand Up @@ -311,7 +312,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
});
}

await this.plugin.saveSettings();
await this.plugin.saveMarkerTypes();
this.display();
})
)
Expand All @@ -320,7 +321,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
this.data.markerIcons = this.data.markerIcons.filter(
(m) => m != marker
);
await this.plugin.saveSettings();
await this.plugin.saveMarkerTypes();
this.display();
})
);
Expand Down Expand Up @@ -704,7 +705,7 @@ export class ObsidianLeafletSettingTab extends PluginSettingTab {
({ id }) => id != marker.id
);
map.createMarker(
marker.type,
marker.type as string,
marker.loc,
undefined,
marker.link,
Expand Down
Loading

0 comments on commit d33ecd5

Please sign in to comment.