Files
rustpad/static/js/url-state.js
T

37 lines
1.3 KiB
JavaScript

/*
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
const VIEWS = new Set(["edit", "split", "preview"]);
const MODES = new Set(["markdown", "text"]);
export function readEditorState() {
const params = new URLSearchParams(window.location.search);
return {
view: VIEWS.has(params.get("view")) ? params.get("view") : "split",
mode: MODES.has(params.get("mode")) ? params.get("mode") : "markdown",
};
}
export function writeEditorState(state, { replace = false } = {}) {
const url = new URL(window.location.href);
url.searchParams.set("view", state.view);
url.searchParams.set("mode", state.mode);
const method = replace ? "replaceState" : "pushState";
window.history[method]({ ...state }, "", `${url.pathname}${url.search}${url.hash}`);
window.dispatchEvent(new CustomEvent("rustpad:urlchange", { detail: { url: url.href } }));
return url.href;
}
export function currentShareUrl(state) {
const url = new URL(window.location.href);
url.searchParams.set("view", state.view);
url.searchParams.set("mode", state.mode);
return url.href;
}