Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

feat(world-planner): move timezone state to mp and always include UTC

Supun Lakmalc441786ca128
diff --git a/.gitignore b/.gitignore
index 6759ac4..ba1cc2e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,2 @@
-task.md
-.env
+.env
.vscode
\ No newline at end of file
diff --git a/modules/constants.js b/modules/constants.js
index 71850fe..200f73b 100644
--- a/modules/constants.js
+++ b/modules/constants.js
@@ -19,8 +19,7 @@ export const DEFAULT_STATE = {
l: "en",
r: 0,
},
- timezones: [],
- mp: { h: null, z: [], s: null, d: null },
+ mp: { h: null, z: [], s: null, d: null, f24: false },
};
// Timing Constants
@@ -48,7 +47,7 @@ export const PLANNER_BUSINESS_HOUR_START = 8;
export const PLANNER_BUSINESS_HOUR_END = 17;
export const PLANNER_ACTIVE_HOUR_START = 18;
export const PLANNER_ACTIVE_HOUR_END = 22;
-export const PLANNER_DEFAULT_ZONES = ["UTC", "America/New_York", "Asia/Tokyo", "Europe/London"];
+export const PLANNER_DEFAULT_ZONES = ["UTC"];
export const PLANNER_MAX_ZONES = 3;
// Time Constants
diff --git a/modules/hashcalUrlManager.js b/modules/hashcalUrlManager.js
index 117d078..aec607c 100644
--- a/modules/hashcalUrlManager.js
+++ b/modules/hashcalUrlManager.js
@@ -1,10 +1,11 @@
import { decryptString, encryptString } from "./cryptoManager.js";
+import { getLocalZone } from "./timezoneManager.js";
const HASH_PREFIX = "ENC:";
const DEFAULT_TITLE = "hash-calendar";
const DEFAULT_COLORS = ["#ff6b6b", "#ffd43b", "#4dabf7", "#63e6be", "#9775fa"];
-const DEFAULT_SETTINGS = { d: 0, m: 0, v: "month", r: 0 };
+const DEFAULT_SETTINGS = { d: 0, m: 0, v: "month", l: "en", r: 0 };
function arraysEqual(a, b) {
return a.length === b.length && a.every((v, i) => v === b[i]);
@@ -43,26 +44,33 @@ function compactState(state) {
});
}
- if (state.s) {
- const hasView = typeof state.s.v === "string" && state.s.v !== DEFAULT_SETTINGS.v;
- const hasLang = typeof state.s.l === "string" && state.s.l !== "en";
- const hasReadOnly = Number(state.s.r || 0) !== DEFAULT_SETTINGS.r;
- if (state.s.d !== DEFAULT_SETTINGS.d || state.s.m !== DEFAULT_SETTINGS.m || hasView || hasLang || hasReadOnly) {
- out.s = state.s;
- }
- }
-
- if (Array.isArray(state.timezones) && state.timezones.length) {
- out.z = state.timezones;
+ if (state.s) {
+ const mini = {};
+ if (state.s.d !== DEFAULT_SETTINGS.d) mini.d = state.s.d;
+ if (state.s.m !== DEFAULT_SETTINGS.m) mini.m = state.s.m;
+ if (typeof state.s.v === "string" && state.s.v !== DEFAULT_SETTINGS.v) mini.v = state.s.v;
+ if (typeof state.s.l === "string" && state.s.l !== DEFAULT_SETTINGS.l) mini.l = state.s.l;
+ if (Number(state.s.r || 0) !== DEFAULT_SETTINGS.r) mini.r = state.s.r;
+ if (Object.keys(mini).length) out.s = mini;
}
if (state.mp) {
const mp = state.mp;
const mini = {};
- if (mp.home) mini.h = mp.home;
- if (mp.zones && mp.zones.length) mini.z = mp.zones;
- if (mp.sel) mini.s = mp.sel;
- if (mp.date) mini.d = mp.date;
+ const localZone = getLocalZone();
+
+ // Only serialize mp.h if it's different from local timezone
+ if (mp.h && mp.h !== localZone) mini.h = mp.h;
+
+ // Only serialize mp.z if user added custom zones beyond UTC
+ if (mp.z && mp.z.length) {
+ const hasCustomZones = mp.z.length > 1 || (mp.z.length === 1 && mp.z[0] !== "UTC");
+ if (hasCustomZones) mini.z = mp.z;
+ }
+
+ if (mp.s) mini.s = mp.s;
+ if (mp.d) mini.d = mp.d;
+ if (mp.f24) mini.f24 = 1;
if (Object.keys(mini).length) out.mp = mini;
}
diff --git a/modules/worldPlannerModule.js b/modules/worldPlannerModule.js
index 07c7561..bddf791 100644
--- a/modules/worldPlannerModule.js
+++ b/modules/worldPlannerModule.js
@@ -78,11 +78,11 @@ export class WorldPlanner {
const state = this.getState();
// Initialize state if empty
- if ((!state.mp || (!state.mp.h && !state.mp.z.length)) && canEdit) {
+ if (!state.mp || (!state.mp.h && !state.mp.z.length)) {
const local = getLocalZone();
const newMp = {
h: local,
- z: PLANNER_DEFAULT_ZONES.filter((z) => z !== local).slice(0, PLANNER_MAX_ZONES),
+ z: ["UTC"],
s: null,
d: new Date().toISOString().split("T")[0],
f24: false,
diff --git a/script.js b/script.js
index fdf945d..501ee76 100644
--- a/script.js
+++ b/script.js
@@ -216,22 +216,41 @@ function normalizeState(raw) {
}
}
- if (Array.isArray(raw.timezones) || Array.isArray(raw.z) || Array.isArray(raw.tz)) {
- const zones = Array.isArray(raw.timezones) ? raw.timezones : Array.isArray(raw.z) ? raw.z : raw.tz;
- next.timezones = normalizeTimezones(zones);
- }
-
+ // Initialize mp object
if (raw.mp && typeof raw.mp === "object") {
next.mp = {
h: typeof raw.mp.h === "string" ? raw.mp.h : null,
z: Array.isArray(raw.mp.z) ? normalizeTimezones(raw.mp.z) : [],
s: Number(raw.mp.s) || null,
d: typeof raw.mp.d === "string" ? raw.mp.d : null,
+ f24: !!raw.mp.f24,
};
} else {
next.mp = cloneState(DEFAULT_STATE.mp);
}
+ // Migration: Merge old 'z' or 'timezones' into mp.z
+ if (Array.isArray(raw.timezones) || Array.isArray(raw.z) || Array.isArray(raw.tz)) {
+ const oldZones = Array.isArray(raw.timezones) ? raw.timezones : Array.isArray(raw.z) ? raw.z : raw.tz;
+ const normalized = normalizeTimezones(oldZones);
+
+ // Merge into mp.z, avoiding duplicates
+ const combined = [...next.mp.z];
+ normalized.forEach(zone => {
+ if (!combined.includes(zone)) {
+ combined.push(zone);
+ }
+ });
+ next.mp.z = combined;
+ }
+
+ // Ensure mp.z has at least UTC if not empty
+ if (next.mp.z.length === 0) {
+ next.mp.z = ["UTC"];
+ } else if (!next.mp.z.includes("UTC")) {
+ next.mp.z.unshift("UTC");
+ }
+
return next;
}
@@ -342,15 +361,15 @@ function cacheElements() {
ui.mobileUrlWarning = document.getElementById("mobile-url-warning");
ui.mobileViewJson = document.getElementById("mobile-view-json");
ui.mobileExportJson = document.getElementById("mobile-export-json");
- ui.mobileImportIcs = document.getElementById("mobile-import-ics");
- ui.mobileClearAll = document.getElementById("mobile-clear-all");
- ui.mobileTzList = document.getElementById("mobile-tz-list");
- ui.mobileAddTzBtn = document.getElementById("mobile-add-tz-btn");
- ui.shareExportSection = document.getElementById("share-export-section");
- ui.dangerZoneSection = document.getElementById("danger-zone-section");
- ui.mobileShareExportSection = document.getElementById("mobile-share-export-section");
- ui.mobileDangerZoneSection = document.getElementById("mobile-danger-zone-section");
- ui.mobileDrawerViewButtons = Array.from(document.querySelectorAll(".mobile-drawer-view-toggle [data-view]"));
+ ui.mobileImportIcs = document.getElementById("mobile-import-ics");
+ ui.mobileClearAll = document.getElementById("mobile-clear-all");
+ ui.mobileTzList = document.getElementById("mobile-tz-list");
+ ui.mobileAddTzBtn = document.getElementById("mobile-add-tz-btn");
+ ui.shareExportSection = document.getElementById("share-export-section");
+ ui.dangerZoneSection = document.getElementById("danger-zone-section");
+ ui.mobileShareExportSection = document.getElementById("mobile-share-export-section");
+ ui.mobileDangerZoneSection = document.getElementById("mobile-danger-zone-section");
+ ui.mobileDrawerViewButtons = Array.from(document.querySelectorAll(".mobile-drawer-view-toggle [data-view]"));
ui.mobileWeekstartToggle = document.getElementById("mobile-weekstart-toggle");
ui.mobileThemeToggle = document.getElementById("mobile-theme-toggle");
ui.mobileReadOnlyBtn = document.getElementById("mobile-readonly-btn");
@@ -374,14 +393,14 @@ function showToast(message, type = "info") {
}, TOAST_TIMEOUT_MS);
}
-function hasStoredData() {
- return !!((state.e && state.e.length) || (state.timezones && state.timezones.length) || isReadOnlyMode());
-}
+function hasStoredData() {
+ return !!((state.e && state.e.length) || (state.mp && state.mp.z && state.mp.z.length > 1) || isReadOnlyMode());
+}
-function createTzCard(zoneId, isLocal) {
+function createTzCard(zoneId, isLocal, isUTC = false) {
const data = getZoneInfo(zoneId);
const div = document.createElement("div");
- div.className = `tz-card${isLocal ? " is-local" : ""}`;
+ div.className = `tz-card${isLocal ? " is-local" : ""}${isUTC ? " is-utc" : ""}`;
div.innerHTML = `
<div class="tz-name">
@@ -390,7 +409,7 @@ function createTzCard(zoneId, isLocal) {
</div>
<div class="tz-time">${data.time}</div>
${data.dayDiff ? `<div class="tz-diff">${data.dayDiff}</div>` : ""}
- ${!isLocal ? `<button class="tz-remove" type="button" data-zone="${data.fullZone}" aria-label="Remove timezone">x</button>` : ""}
+ ${!isLocal && !isUTC ? `<button class="tz-remove" type="button" data-zone="${data.fullZone}" aria-label="Remove timezone">x</button>` : ""}
`;
return div;
@@ -401,12 +420,13 @@ function renderTimezones() {
if (!targets.length) return;
const localZone = getLocalZone();
- const zones = Array.isArray(state.timezones) ? state.timezones : [];
+ const zones = Array.isArray(state.mp.z) ? state.mp.z : [];
const savedZones = [];
const seen = new Set();
zones.forEach((zone) => {
if (zone === localZone) return;
+ if (zone === "UTC") return; // Still filter from savedZones (handled separately)
if (seen.has(zone)) return;
if (!isValidZone(zone)) return;
seen.add(zone);
@@ -415,8 +435,16 @@ function renderTimezones() {
targets.forEach((target) => {
target.innerHTML = "";
- target.appendChild(createTzCard(localZone, true));
+ // 1. Always show local timezone
+ target.appendChild(createTzCard(localZone, true, false));
+
+ // 2. Always show UTC (unless it's the same as local timezone)
+ if (localZone !== "UTC") {
+ target.appendChild(createTzCard("UTC", false, true));
+ }
+
+ // 3. Show additional custom timezones or empty message
if (!savedZones.length) {
const empty = document.createElement("p");
empty.className = "tz-empty";
@@ -426,7 +454,7 @@ function renderTimezones() {
}
savedZones.forEach((zone) => {
- target.appendChild(createTzCard(zone, false));
+ target.appendChild(createTzCard(zone, false, false));
});
});
}
@@ -436,17 +464,17 @@ function addTimezone(zoneStr) {
if (!zoneStr || !isValidZone(zoneStr)) return;
const localZone = getLocalZone();
if (zoneStr === localZone) return;
- if (!Array.isArray(state.timezones)) state.timezones = [];
- if (state.timezones.includes(zoneStr)) return;
- state.timezones.push(zoneStr);
+ if (!Array.isArray(state.mp.z)) state.mp.z = [];
+ if (state.mp.z.includes(zoneStr)) return;
+ state.mp.z.push(zoneStr);
scheduleSave();
renderTimezones();
}
function removeTimezone(zoneStr) {
if (!ensureEditable()) return;
- if (!Array.isArray(state.timezones) || !zoneStr) return;
- state.timezones = state.timezones.filter((zone) => zone !== zoneStr);
+ if (!Array.isArray(state.mp.z) || !zoneStr) return;
+ state.mp.z = state.mp.z.filter((zone) => zone !== zoneStr);
scheduleSave();
renderTimezones();
}
@@ -491,7 +519,7 @@ function handleTzSearch() {
return;
}
const localZone = getLocalZone();
- const existing = new Set([localZone, ...(state.timezones || [])]);
+ const existing = new Set([localZone, ...(state.mp.z || [])]);
const matches = AVAILABLE_ZONES.filter((zone) => {
if (existing.has(zone)) return false;
const zoneLower = zone.toLowerCase();
@@ -761,28 +789,28 @@ function updateLockUI() {
ui.mobileClearAll,
ui.tzAddBtn,
ui.mobileAddTzBtn,
- ].forEach((btn) => {
- if (btn) btn.disabled = editDisabled;
- });
-
- [
- ui.addEventBtn,
- ui.addEventInline,
- ui.mobileAddEventInline,
- ui.mobileAddEvent,
- ui.tzAddBtn,
- ui.mobileAddTzBtn,
- ui.shareExportSection,
- ui.dangerZoneSection,
- ui.mobileShareExportSection,
- ui.mobileDangerZoneSection,
- ].forEach((item) => {
- if (item) item.classList.toggle(CSS_CLASSES.HIDDEN, isReadOnly);
- });
-
- [ui.copyLinkBtn, ui.shareQrBtn, ui.mobileCopyLink, ui.mobileShareQr].forEach((btn) => {
- if (btn) btn.disabled = isLocked;
- });
+ ].forEach((btn) => {
+ if (btn) btn.disabled = editDisabled;
+ });
+
+ [
+ ui.addEventBtn,
+ ui.addEventInline,
+ ui.mobileAddEventInline,
+ ui.mobileAddEvent,
+ ui.tzAddBtn,
+ ui.mobileAddTzBtn,
+ ui.shareExportSection,
+ ui.dangerZoneSection,
+ ui.mobileShareExportSection,
+ ui.mobileDangerZoneSection,
+ ].forEach((item) => {
+ if (item) item.classList.toggle(CSS_CLASSES.HIDDEN, isReadOnly);
+ });
+
+ [ui.copyLinkBtn, ui.shareQrBtn, ui.mobileCopyLink, ui.mobileShareQr].forEach((btn) => {
+ if (btn) btn.disabled = isLocked;
+ });
// Mobile lock/unlock button visibility
if (ui.mobileLockBtn && ui.mobileUnlockBtn) {