Files

42 lines
1.4 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"]);
function defaultEditorView() {
return window.matchMedia("(max-width: 760px)").matches ? "edit" : "split";
}
export function readEditorState() {
const params = new URLSearchParams(window.location.search);
const requestedView = params.get("view");
return {
view: VIEWS.has(requestedView) ? requestedView : defaultEditorView(),
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;
}