Files
rustpad/static/js/note-api.js
T

111 lines
4.5 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.
*/
import { api } from "@rustpad/api";
import { askConfirm } from "@rustpad/modal";
import { NoteSocket, PadSocket } from "@rustpad/socket";
function encode(value) {
return encodeURIComponent(value);
}
export function createPadAdapter() {
const slug = location.pathname.split("/").filter(Boolean)[1];
const base = `/api/pads/${encode(slug)}`;
return {
access: { kind: "pad", key: slug },
addressSelector: "#document-url",
title: info => `${info.title} · RustPad`,
loadInfo: headers => api(base, { headers }),
loadColor: headers => api(`${base}/editor-color`, { headers }),
saveColor: (headers, color) => api(`${base}/editor-color`, { method: "POST", headers, body: JSON.stringify({ color }) }),
saveEditorSettings: (headers, settings) => api(`${base}/editor-settings`, { method: "POST", headers, body: JSON.stringify(settings) }),
fileEndpoints: {
list: `${base}/files`,
upload: `${base}/files`,
remove: fileId => `${base}/files/${encode(fileId)}`,
},
createSocket: options => new PadSocket({ slug, ...options }),
requestAccess: password => api("/api/access-token", {
method: "POST",
body: JSON.stringify({ kind: "pad", slug, password }),
}),
publish: (accessToken, allowTaskUpdates, unprotectPage, enabled = true) => api(`${base}/publish`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null, allow_task_updates: allowTaskUpdates, unprotect_page: unprotectPage, enabled }),
}),
loadHistory: accessToken => api(`${base}/history`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null }),
}),
restoreRevision: (revisionId, accessToken) => api(`${base}/restore`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null, revision_id: revisionId }),
}),
configureView() { },
deleteNote: null,
};
}
export function createWorkspaceNoteAdapter() {
const parts = location.pathname.split("/").filter(Boolean);
const workspaceSlug = parts[1];
const noteSlug = parts[3];
const base = `/api/workspaces/${encode(workspaceSlug)}/notes/${encode(noteSlug)}`;
return {
access: { kind: "workspace", key: workspaceSlug },
addressSelector: "#document-url",
title: info => `${info.title} · ${info.workspace_title}`,
loadInfo: headers => api(base, { headers }),
loadColor: headers => api(`${base}/editor-color`, { headers }),
saveColor: (headers, color) => api(`${base}/editor-color`, { method: "POST", headers, body: JSON.stringify({ color }) }),
saveEditorSettings: (headers, settings) => api(`${base}/editor-settings`, { method: "POST", headers, body: JSON.stringify(settings) }),
fileEndpoints: {
list: `${base}/files`,
upload: `${base}/files`,
remove: fileId => `${base}/files/${encode(fileId)}`,
},
createSocket: options => new NoteSocket({ workspaceSlug, noteSlug, ...options }),
requestAccess: password => api("/api/access-token", {
method: "POST",
body: JSON.stringify({ kind: "workspace", slug: workspaceSlug, password }),
}),
publish: (accessToken, allowTaskUpdates, unprotectPage, enabled = true) => api(`${base}/publish`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null, allow_task_updates: allowTaskUpdates, unprotect_page: unprotectPage, enabled }),
}),
loadHistory: accessToken => api(`${base}/history`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null }),
}),
restoreRevision: (revisionId, accessToken) => api(`${base}/restore`, {
method: "POST",
body: JSON.stringify({ access_token: accessToken || null, revision_id: revisionId }),
}),
configureView(info) {
const button = document.querySelector("#delete-note");
if (button) button.hidden = info.note_protected;
},
async deleteNote(info, accessToken) {
if (!await askConfirm(`Delete note “${info.title}”? This cannot be undone.`, {
title: "Delete note",
confirmText: "Delete",
danger: true,
})) return;
await api(base, {
method: "DELETE",
body: JSON.stringify({ access_token: accessToken || null }),
});
location.assign(`/w/${encode(workspaceSlug)}`);
},
};
}