mirror of
https://github.com/andrii256/ops_obsidian_smart-day-night-switcher.git
synced 2026-07-22 05:42:31 +00:00
rewrite schedule using internal obsidian API
This commit is contained in:
parent
bbcd9c1bda
commit
d20b69fb20
1 changed files with 94 additions and 55 deletions
149
main.ts
149
main.ts
|
|
@ -17,8 +17,6 @@ const DEFAULT_SETTINGS: SDNSPluginSettings = {
|
|||
longitude: "-0.127758",
|
||||
};
|
||||
|
||||
const DYNAMIC_DIV_ID = "a440b9a8-80d9-4b4b-b7a3-0265c8964997"; // unique ID to make sure it will not clash with other plugins
|
||||
|
||||
export default class SDNSPlugin extends Plugin {
|
||||
private timeout: ReturnType<typeof setTimeout> | null;
|
||||
settings: SDNSPluginSettings;
|
||||
|
|
@ -52,52 +50,6 @@ export default class SDNSPlugin extends Plugin {
|
|||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
this.checkAndSwitchColorScheme();
|
||||
this.updateScheduleHTML();
|
||||
}
|
||||
|
||||
updateScheduleHTML() {
|
||||
const container = document.getElementById(DYNAMIC_DIV_ID);
|
||||
|
||||
if (container) {
|
||||
container.innerHTML = this.generateScheduleHTML();
|
||||
}
|
||||
}
|
||||
|
||||
generateScheduleHTML() {
|
||||
const date = new Date();
|
||||
const rows = [];
|
||||
|
||||
for (let i = 0; i <= 30; i++) {
|
||||
const { dawn, sunsetStart: dusk } = SunCalc.getTimes(
|
||||
date,
|
||||
+this.settings.latitude,
|
||||
+this.settings.longitude
|
||||
);
|
||||
|
||||
const dateStr = `${date.getDate()} ${date.toLocaleString("en-US", {
|
||||
month: "short",
|
||||
})} ${date.getFullYear()}`; // date 'DD MMM YYYY'
|
||||
const dawnStr = `${String(dawn.getHours()).padStart(
|
||||
2,
|
||||
"0"
|
||||
)}:${String(dawn.getMinutes()).padStart(2, "0")}`; // dawn 'HH:MM'
|
||||
const duskStr = `${String(dusk.getHours()).padStart(
|
||||
2,
|
||||
"0"
|
||||
)}:${String(dusk.getMinutes()).padStart(2, "0")}`; // dusk 'HH:MM'
|
||||
|
||||
rows.push(
|
||||
`<tr><td>${dateStr}</td><td>${dawnStr}</td><td>${duskStr}</td></tr>`
|
||||
);
|
||||
|
||||
date.setDate(date.getDate() + 1); // to increase date before next call
|
||||
}
|
||||
|
||||
const html = `<div id="a440b9a8-80d9-4b4b-b7a3-0265c8964997"><h4 style=" border-top: 1px solid var(--background-modifier-border); margin: 1em auto 1em; padding-top: 1em;">Schedule:</h4><table style="width: 100%;text-align: center;"> <tbody> <tr> <th>Date</th> <th>Dawn<br><small>(Light mode will be enabled)</small></th> <th>Dusk<br><small>(Dark mode will be enabled)</small></th> ${rows.join(
|
||||
""
|
||||
)}</tbody></table>`;
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
checkAndSwitchColorScheme() {
|
||||
|
|
@ -177,9 +129,9 @@ class SDNSPluginSettingTab extends PluginSettingTab {
|
|||
const { containerEl } = this;
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl("h3", { text: "Starting Point Coordinates" });
|
||||
|
||||
containerEl.createDiv().innerHTML = `<p>Please enter the latitude and longitude of your approximate location (coords of any city within ±200 km)</p>`;
|
||||
containerEl.createEl("p", {
|
||||
text: "Please enter the latitude and longitude of your approximate location (coords of any city within ±200 km)",
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Latitude")
|
||||
|
|
@ -215,10 +167,97 @@ class SDNSPluginSettingTab extends PluginSettingTab {
|
|||
)
|
||||
);
|
||||
|
||||
containerEl.createDiv().innerHTML = `<p><small >To easily find your latitude and longitude, you can use any simple online service, such as <a href="https://www.latlong.net/">latlong.net</a>, <a href="https://www.gps-coordinates.net/">gps-coordinates.net</a>, or any other similar tools available on the web.</small ><br/><br /><small >* Obsidian does not provide developers with access to geolocation, so this plugin cannot automatically determine your coordinates.</small > <br /> <small style="display: inline-block; margin-top: 0.4em;" > However, <strong>to accurately calculate sunrise and sunset times in your location</strong>, the formula needs an approximate location (within ±200 km) of where you are.</small ></p>`;
|
||||
const addAdditionalDescription = () => {
|
||||
const additionalDescription = containerEl.createEl("p");
|
||||
|
||||
const dynamicContent = containerEl.createDiv();
|
||||
dynamicContent.id = DYNAMIC_DIV_ID;
|
||||
dynamicContent.innerHTML = this.plugin.generateScheduleHTML();
|
||||
additionalDescription.createEl("small", {
|
||||
text: "To easily find your latitude and longitude, you can use any simple online service, such as ",
|
||||
});
|
||||
additionalDescription.createEl("small").createEl("a", {
|
||||
href: "https://www.latlong.net/",
|
||||
text: "latlong.net",
|
||||
});
|
||||
additionalDescription.createEl("small", { text: ", " });
|
||||
additionalDescription.createEl("small").createEl("a", {
|
||||
href: "https://www.gps-coordinates.net/",
|
||||
text: "gps-coordinates.net",
|
||||
});
|
||||
additionalDescription.createEl("small", {
|
||||
text: ", or any other similar tools available on the web.",
|
||||
});
|
||||
additionalDescription.createEl("br");
|
||||
additionalDescription.createEl("br");
|
||||
additionalDescription.createEl("small", {
|
||||
text: "* Obsidian does not provide developers with access to geolocation, so this plugin cannot automatically determine your coordinates.",
|
||||
});
|
||||
additionalDescription.createEl("br");
|
||||
additionalDescription.createEl("small", { text: "However, " });
|
||||
additionalDescription.createEl("small").createEl("strong", {
|
||||
text: "to accurately calculate sunrise and sunset times in your location",
|
||||
});
|
||||
additionalDescription.createEl("small", {
|
||||
text: ", the formula needs an approximate location (within ±200 km) of where you are.",
|
||||
});
|
||||
};
|
||||
addAdditionalDescription();
|
||||
|
||||
const addSchedule = () => {
|
||||
const dynamicContent = containerEl.createEl("div");
|
||||
const scheduleHeader = dynamicContent.createEl("h4", {
|
||||
text: "Schedule:",
|
||||
});
|
||||
scheduleHeader.setCssStyles({
|
||||
borderTop: "1px solid var(--background-modifier-border)",
|
||||
margin: "1em auto 1em",
|
||||
paddingTop: "1em",
|
||||
});
|
||||
|
||||
const table = dynamicContent.createEl("table", {
|
||||
attr: { style: "width: 100%; text-align: center;" },
|
||||
});
|
||||
const tbody = table.createEl("tbody");
|
||||
const headerRow = tbody.createEl("tr");
|
||||
headerRow.createEl("th", { text: "Date" });
|
||||
const dawnTh = headerRow.createEl("th");
|
||||
dawnTh.createEl("span", { text: "Dawn" });
|
||||
dawnTh.createEl("br");
|
||||
dawnTh.createEl("small", { text: "(Light mode will be enabled)" });
|
||||
const duskTh = headerRow.createEl("th");
|
||||
duskTh.createEl("span", { text: "Dusk" });
|
||||
duskTh.createEl("br");
|
||||
duskTh.createEl("small", { text: "(Dark mode will be enabled)" });
|
||||
|
||||
const date = new Date();
|
||||
for (let i = 0; i <= 30; i++) {
|
||||
const { dawn, sunsetStart: dusk } = SunCalc.getTimes(
|
||||
date,
|
||||
+this.plugin.settings.latitude,
|
||||
+this.plugin.settings.longitude
|
||||
);
|
||||
|
||||
const dateStr = `${date.getDate()} ${date.toLocaleString(
|
||||
"en-US",
|
||||
{
|
||||
month: "short",
|
||||
}
|
||||
)} ${date.getFullYear()}`;
|
||||
const dawnStr = `${String(dawn.getHours()).padStart(
|
||||
2,
|
||||
"0"
|
||||
)}:${String(dawn.getMinutes()).padStart(2, "0")}`;
|
||||
const duskStr = `${String(dusk.getHours()).padStart(
|
||||
2,
|
||||
"0"
|
||||
)}:${String(dusk.getMinutes()).padStart(2, "0")}`;
|
||||
|
||||
const row = tbody.createEl("tr");
|
||||
row.createEl("td", { text: dateStr });
|
||||
row.createEl("td", { text: dawnStr });
|
||||
row.createEl("td", { text: duskStr });
|
||||
|
||||
date.setDate(date.getDate() + 1);
|
||||
}
|
||||
};
|
||||
addSchedule();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue