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

QR

Supun Lakmale8dde01da3aa
diff --git a/index.html b/index.html
index a8fdfc5..a0c9d8b 100644
--- a/index.html
+++ b/index.html
@@ -66,6 +66,7 @@
<button id="focus-btn" type="button" class="ghost">Focus</button>
<button id="add-event" type="button" class="primary">Add event</button>
<button id="copy-link" type="button">Copy link</button>
+ <button id="share-qr" type="button">Share QR</button>
<button id="lock-btn" type="button">Lock</button>
<a class="action-link" href="https://github.com/supunlakmal/hash-calendar" target="_blank" rel="noreferrer">
<i class="fa-brands fa-github" aria-hidden="true"></i>
@@ -258,9 +259,28 @@
</div>
</div>
+ <div id="qr-modal" class="modal hidden" role="dialog" aria-modal="true" aria-labelledby="qr-modal-title">
+ <div class="modal-backdrop"></div>
+ <div class="modal-card qr-modal-card">
+ <header class="modal-header">
+ <h3 id="qr-modal-title">Scan to open on mobile</h3>
+ <button id="qr-close" type="button" class="icon-btn" aria-label="Close">x</button>
+ </header>
+ <div class="qr-modal-body">
+ <p id="qr-warning" class="qr-warning hidden"></p>
+ <div id="qrcode-container" class="qr-code"></div>
+ <p class="qr-helper">Point your phone camera at this code to open your calendar.</p>
+ </div>
+ <div class="modal-actions qr-modal-actions">
+ <button id="qr-copy-link" type="button" class="ghost">Copy text link instead</button>
+ </div>
+ </div>
+ </div>
+
<div id="toast-container" class="toast-container" aria-live="polite" aria-atomic="true"></div>
-
+
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script src="modules/lz-string.min.js"></script>
- <script type="module" src="script.js"></script>
-</body>
-</html>
+ <script type="module" src="script.js"></script>
+</body>
+</html>
diff --git a/modules/qrCodeManager.js b/modules/qrCodeManager.js
new file mode 100644
index 0000000..34c142c
--- /dev/null
+++ b/modules/qrCodeManager.js
@@ -0,0 +1,87 @@
+const MAX_QR_URL_LENGTH = 2000;
+const DEFAULT_QR_SIZE = 256;
+
+export function initQRCodeManager({
+ onBeforeOpen = null,
+ onCopyLink = null,
+ showToast = null,
+ maxLength = MAX_QR_URL_LENGTH,
+ size = DEFAULT_QR_SIZE,
+} = {}) {
+ const modal = document.getElementById("qr-modal");
+ const container = document.getElementById("qrcode-container");
+ const warning = document.getElementById("qr-warning");
+ const closeBtn = document.getElementById("qr-close");
+ const copyBtn = document.getElementById("qr-copy-link");
+ const backdrop = modal ? modal.querySelector(".modal-backdrop") : null;
+
+ function hide() {
+ if (modal) modal.classList.add("hidden");
+ }
+
+ function showWarning(message) {
+ if (!warning) return;
+ warning.textContent = message;
+ warning.classList.remove("hidden");
+ }
+
+ function clearWarning() {
+ if (!warning) return;
+ warning.textContent = "";
+ warning.classList.add("hidden");
+ }
+
+ async function show() {
+ if (!modal || !container) return;
+
+ if (typeof onBeforeOpen === "function") {
+ await onBeforeOpen();
+ }
+
+ await new Promise((resolve) => setTimeout(resolve, 50));
+
+ const url = window.location.href;
+ container.innerHTML = "";
+ clearWarning();
+
+ if (typeof window.QRCode === "undefined") {
+ if (typeof showToast === "function") {
+ showToast("QR library failed to load", "error");
+ } else {
+ window.alert("QR library failed to load.");
+ }
+ return;
+ }
+
+ if (url.length > maxLength) {
+ showWarning(`Calendar data too long for QR transfer (${url.length} chars). Try deleting old events.`);
+ modal.classList.remove("hidden");
+ return;
+ }
+
+ try {
+ new window.QRCode(container, {
+ text: url,
+ width: size,
+ height: size,
+ colorDark: "#000000",
+ colorLight: "#ffffff",
+ correctLevel: window.QRCode.CorrectLevel.L,
+ });
+ modal.classList.remove("hidden");
+ } catch (error) {
+ console.error("QR generation error", error);
+ if (typeof showToast === "function") {
+ showToast("Error creating QR code", "error");
+ } else {
+ window.alert("Error creating QR code.");
+ }
+ }
+ }
+
+ if (closeBtn) closeBtn.addEventListener("click", hide);
+ if (backdrop) backdrop.addEventListener("click", hide);
+ if (copyBtn && typeof onCopyLink === "function") copyBtn.addEventListener("click", onCopyLink);
+
+ return { show, hide };
+}
diff --git a/script.js b/script.js
index afe53b0..113bb24 100644
--- a/script.js
+++ b/script.js
@@ -1,10 +1,11 @@
import { readStateFromHash, writeStateToHash, isEncryptedHash, clearHash } from "./modules/hashcalUrlManager.js";
import { expandEvents } from "./modules/recurrenceEngine.js";
import { FocusMode } from "./modules/focusMode.js";
-import {
- formatDateKey,
- getMonthGridRange,
- getWeekRange,
+import { initQRCodeManager } from "./modules/qrCodeManager.js";
+import {
+ formatDateKey,
+ getMonthGridRange,
+ getWeekRange,
renderCalendar,
renderTimeGrid,
renderWeekdayHeaders,
@@ -38,6 +39,7 @@ let editingIndex = null;
let passwordResolver = null;
let passwordMode = "unlock";
let focusMode = null;
+let qrManager = null;
const ui = {};
@@ -146,9 +148,10 @@ function cacheElements() {
ui.titleInput = document.getElementById("calendar-title");
ui.prevMonth = document.getElementById("prev-month");
ui.nextMonth = document.getElementById("next-month");
- ui.todayBtn = document.getElementById("today-btn");
+ ui.todayBtn = document.getElementById("today-btn");
ui.addEventBtn = document.getElementById("add-event");
ui.copyLinkBtn = document.getElementById("copy-link");
+ ui.shareQrBtn = document.getElementById("share-qr");
ui.lockBtn = document.getElementById("lock-btn");
ui.focusBtn = document.getElementById("focus-btn");
ui.viewButtons = Array.from(document.querySelectorAll(".view-toggle button"));
@@ -207,33 +210,50 @@ function cacheElements() {
ui.toastContainer = document.getElementById("toast-container");
}
-function showToast(message, type = "info") {
- if (!ui.toastContainer) return;
- const toast = document.createElement("div");
- toast.className = `toast ${type}`.trim();
- toast.textContent = message;
- ui.toastContainer.appendChild(toast);
- setTimeout(() => {
- toast.remove();
- }, 3200);
-}
-
-function scheduleSave() {
- if (lockState.encrypted && !lockState.unlocked) return;
- if (!state.e.length) {
- if (window.location.hash) clearHash();
- updateUrlLength();
- if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
- updateJsonModal();
- }
- return;
- }
- if (saveTimer) window.clearTimeout(saveTimer);
- saveTimer = window.setTimeout(async () => {
- await writeStateToHash(state, password);
- updateUrlLength();
- }, DEBOUNCE_MS);
-}
+function showToast(message, type = "info") {
+ if (!ui.toastContainer) return;
+ const toast = document.createElement("div");
+ toast.className = `toast ${type}`.trim();
+ toast.textContent = message;
+ ui.toastContainer.appendChild(toast);
+ setTimeout(() => {
+ toast.remove();
+ }, 3200);
+}
+
+async function persistStateToHash() {
+ if (lockState.encrypted && !lockState.unlocked) return;
+ if (!state.e.length) {
+ if (window.location.hash) clearHash();
+ updateUrlLength();
+ if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
+ updateJsonModal();
+ }
+ return;
+ }
+ await writeStateToHash(state, password);
+ updateUrlLength();
+ if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
+ updateJsonModal();
+ }
+}
+
+function scheduleSave() {
+ if (lockState.encrypted && !lockState.unlocked) return;
+ if (!state.e.length) {
+ if (saveTimer) {
+ window.clearTimeout(saveTimer);
+ saveTimer = null;
+ }
+ persistStateToHash();
+ return;
+ }
+ if (saveTimer) window.clearTimeout(saveTimer);
+ saveTimer = window.setTimeout(() => {
+ saveTimer = null;
+ persistStateToHash();
+ }, DEBOUNCE_MS);
+}
function updateUrlLength() {
const length = window.location.hash.length;
@@ -293,11 +313,11 @@ function updateLockUI() {
if (ui.lockedOverlay) {
ui.lockedOverlay.classList.toggle("hidden", !isLocked);
}
- const disabled = isLocked;
- [ui.addEventBtn, ui.addEventInline, ui.copyLinkBtn].forEach((btn) => {
- if (btn) btn.disabled = disabled;
- });
-}
+ const disabled = isLocked;
+ [ui.addEventBtn, ui.addEventInline, ui.copyLinkBtn, ui.shareQrBtn].forEach((btn) => {
+ if (btn) btn.disabled = disabled;
+ });
+}
function groupOccurrences(occurrences) {
const map = new Map();
@@ -778,22 +798,56 @@ function handleNextMonth() {
shiftView(1);
}
-function handleToday() {
- const today = startOfDay(new Date());
- viewDate = today;
- selectedDate = today;
- render();
-}
-
-async function handleCopyLink() {
+function handleToday() {
+ const today = startOfDay(new Date());
+ viewDate = today;
+ selectedDate = today;
+ render();
+}
+
+function fallbackCopyText(text) {
+ const textArea = document.createElement("textarea");
+ textArea.value = text;
+ textArea.setAttribute("readonly", "");
+ textArea.style.position = "absolute";
+ textArea.style.left = "-9999px";
+ document.body.appendChild(textArea);
+ textArea.select();
+ let success = false;
try {
- await navigator.clipboard.writeText(window.location.href);
- showToast("Link copied", "success");
+ success = document.execCommand("copy");
} catch (error) {
+ success = false;
+ }
+ document.body.removeChild(textArea);
+ return success;
+}
+
+async function handleCopyLink() {
+ await persistStateToHash();
+ const url = window.location.href;
+ if (navigator.clipboard && navigator.clipboard.writeText) {
+ try {
+ await navigator.clipboard.writeText(url);
+ showToast("Link copied", "success");
+ return;
+ } catch (error) {
+ // Fallback below
+ }
+ }
+ if (fallbackCopyText(url)) {
+ showToast("Link copied", "success");
+ } else {
showToast("Unable to copy link", "error");
}
}
+function handleShareQr() {
+ if (qrManager) {
+ qrManager.show();
+ }
+}
+
function handleFocusToggle() {
if (!focusMode) return;
if (focusMode.isActive()) {
@@ -912,6 +966,7 @@ function bindEvents() {
if (ui.addEventBtn) ui.addEventBtn.addEventListener("click", () => openEventModal({ date: selectedDate }));
if (ui.addEventInline) ui.addEventInline.addEventListener("click", () => openEventModal({ date: selectedDate }));
if (ui.copyLinkBtn) ui.copyLinkBtn.addEventListener("click", handleCopyLink);
+ if (ui.shareQrBtn) ui.shareQrBtn.addEventListener("click", handleShareQr);
if (ui.lockBtn) ui.lockBtn.addEventListener("click", handleLockAction);
if (ui.focusBtn) ui.focusBtn.addEventListener("click", handleFocusToggle);
if (ui.weekstartToggle) ui.weekstartToggle.addEventListener("click", handleWeekStartToggle);
@@ -942,6 +997,11 @@ function bindEvents() {
async function init() {
cacheElements();
+ qrManager = initQRCodeManager({
+ onBeforeOpen: persistStateToHash,
+ onCopyLink: handleCopyLink,
+ showToast,
+ });
focusMode = new FocusMode({
getState: () => state,
fallbackColors: DEFAULT_COLORS,
diff --git a/styles.css b/styles.css
index 3a0d50b..fae9be8 100644
--- a/styles.css
+++ b/styles.css
@@ -788,6 +788,56 @@ button:disabled {
.json-modal-card {
width: min(720px, 94vw);
}
+
+.qr-modal-card {
+ width: min(420px, 92vw);
+ text-align: center;
+}
+
+.qr-modal-body {
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+ align-items: center;
+}
+
+.qr-code {
+ padding: 12px;
+ background: #ffffff;
+ border-radius: 10px;
+ border: 1px solid var(--border);
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+}
+
+.qr-code canvas,
+.qr-code img {
+ display: block;
+}
+
+.qr-warning {
+ margin: 0;
+ font-size: 0.8rem;
+ color: var(--danger);
+ background: rgba(217, 48, 37, 0.08);
+ border: 1px solid rgba(217, 48, 37, 0.2);
+ padding: 10px 12px;
+ border-radius: 8px;
+ text-align: left;
+ align-self: stretch;
+ word-break: break-word;
+}
+
+.qr-helper {
+ margin: 0;
+ font-size: 0.8rem;
+ color: var(--muted);
+}
+
+.qr-modal-actions {
+ justify-content: center;
+}
.modal-header {
display: flex;