96 lines
3.4 KiB
JavaScript
96 lines
3.4 KiB
JavaScript
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: "#pad-url",
|
|
title: info => `${info.title} · RustPad`,
|
|
loadInfo: headers => api(base, { headers }),
|
|
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) => api(`${base}/publish`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ access_token: accessToken || null, allow_task_updates: allowTaskUpdates }),
|
|
}),
|
|
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: "#note-url",
|
|
title: info => `${info.title} · ${info.workspace_title}`,
|
|
loadInfo: headers => api(base, { headers }),
|
|
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) => api(`${base}/publish`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ access_token: accessToken || null, allow_task_updates: allowTaskUpdates }),
|
|
}),
|
|
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)}`);
|
|
},
|
|
};
|
|
}
|