feat: notify document updates over websocket

This commit is contained in:
2026-04-29 09:14:49 -04:00
parent 9b8f2968e8
commit 6e244d55db
12 changed files with 358 additions and 19 deletions

View File

@@ -0,0 +1,35 @@
(function () {
const notice = document.querySelector("[data-version-notice]");
const reload = document.querySelector("[data-version-reload]");
const documentShell = document.querySelector("[data-document-path][data-document-hash]");
if (!notice || !reload || !documentShell || !window.WebSocket) {
return;
}
const currentPath = documentShell.getAttribute("data-document-path");
const currentHash = documentShell.getAttribute("data-document-hash");
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(protocol + "//" + window.location.host + "/ws");
socket.addEventListener("message", function (event) {
let payload;
try {
payload = JSON.parse(event.data);
} catch {
return;
}
if (payload.type !== "document_version" || !payload.data) {
return;
}
if (payload.data.path === currentPath && payload.data.hash !== currentHash) {
notice.hidden = false;
}
});
reload.addEventListener("click", function () {
window.location.reload();
});
})();