File size: 3,219 Bytes
740e55f adb34c2 740e55f adb34c2 740e55f adb34c2 740e55f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import { hydrateAttachment } from "/chatclient/media.js";
const DRAFT_KEY = "oapix.chatclient.draft";
export function loadDraft() {
try {
const saved = JSON.parse(window.localStorage.getItem(DRAFT_KEY) || "{}");
return {
editorHtml: typeof saved.editorHtml === "string" ? saved.editorHtml : "",
attachmentMode: saved.attachmentMode === "link" ? "link" : "upload",
attachmentLinkType: saved.attachmentLinkType === "audio" ? "audio" : "image",
attachmentLinkUrl: typeof saved.attachmentLinkUrl === "string" ? saved.attachmentLinkUrl : "",
attachments: sanitizeAttachments(saved.attachments)
};
} catch (_error) {
window.localStorage.removeItem(DRAFT_KEY);
return emptyDraft();
}
}
export function saveDraft({ editor, attachmentMode, attachmentLinkType, attachmentLinkUrl, attachments }) {
try {
window.localStorage.setItem(DRAFT_KEY, JSON.stringify({
editorHtml: editor.innerHTML,
attachmentMode,
attachmentLinkType,
attachmentLinkUrl,
attachments: attachments
.map((attachment) => serializeAttachment(attachment))
.filter(Boolean)
}));
} catch (_error) {
window.localStorage.setItem(DRAFT_KEY, JSON.stringify({
editorHtml: editor.innerHTML,
attachmentMode,
attachmentLinkType,
attachmentLinkUrl,
attachments: attachments
.filter((attachment) => attachment.sourceType === "link")
.map((attachment) => serializeAttachment(attachment))
.filter(Boolean)
}));
}
}
export function clearDraft() {
window.localStorage.removeItem(DRAFT_KEY);
}
function sanitizeAttachments(value) {
if (!Array.isArray(value)) {
return [];
}
return value
.map((attachment) => sanitizeAttachment(attachment))
.filter(Boolean);
}
function emptyDraft() {
return {
editorHtml: "",
attachmentMode: "upload",
attachmentLinkType: "image",
attachmentLinkUrl: "",
attachments: []
};
}
function serializeAttachment(attachment) {
if (!attachment || (attachment.kind !== "image" && attachment.kind !== "audio")) {
return null;
}
if (attachment.sourceType === "link") {
return {
id: attachment.id,
kind: attachment.kind,
name: attachment.name,
sizeLabel: attachment.sizeLabel,
previewUrl: attachment.previewUrl,
sourceType: "link",
url: attachment.url
};
}
if (attachment.sourceType === "file" && typeof attachment.dataUrl === "string") {
return {
id: attachment.id,
kind: attachment.kind,
name: attachment.name,
sizeLabel: attachment.sizeLabel,
sourceType: "file",
dataUrl: attachment.dataUrl,
mimeType: attachment.mimeType
};
}
return null;
}
function sanitizeAttachment(attachment) {
if (!attachment || (attachment.kind !== "image" && attachment.kind !== "audio") || typeof attachment.name !== "string") {
return null;
}
if (attachment.sourceType === "link" && typeof attachment.url === "string") {
return hydrateAttachment(attachment);
}
if (attachment.sourceType === "file" && typeof attachment.dataUrl === "string") {
return hydrateAttachment(attachment);
}
return null;
}
|