({
appOption(app) {
app.openSidebarEmbed(1);
},
noteOption : {
"Embed Calendar": {
check: async function() {
return true;
},
run: async function(app, noteUUID) {
const note = await app.notes.find(noteUUID);
app.insertNoteContent({ uuid: noteUUID }, `<object data="plugin://${app.context.pluginUUID}?${noteUUID}" data-aspect-ratio="1" />`);
}
},
"Open": {
check: async function() {
return true;
},
run: async function(app, noteUUID) {
app.openSidebarEmbed(1);
}
}
},
async renderEmbed(app) {
try {
const attachments = await app.getNoteAttachments(app.context.pluginUUID);
const attachment = attachments.find(attachment => attachment.name === "build.html.json");
if (!attachment) throw new Error("build.html.json attachment not found");
return this._getAttachmentContent(app, attachment.uuid);
} catch (error) {
return `<div><em>renderEmbed error:</em> ${ error.toString() }</div>`;
}
},
async onEmbedCall(app, ...args) {
if (args[0] == "fetch_jot") {
const timestamp = Math.floor(args[1].getTime() / 1000);
const note = await app.notes.dailyJot(timestamp);
if(note.uuid) {
return true
} else {
return false
}
}
else if (args[0] == "fetch_week_jot") {
const noteHandle = await app.findNote({ name: `${args[2]}-W${args[1]}` });
if (noteHandle) {
return true
} else {
return false
}
}
else if(args[0] == "open_week_jot") {
const noteName = `${args[2]}-W${args[1]}`;
const noteHandle = await app.findNote({ name: noteName });
if(noteHandle) {
app.navigate(`https://www.amplenote.com/notes/${noteHandle.uuid}`);
} else {
const uuid = await app.createNote(noteName, [ "weekly-jot" ]);
app.navigate(`https://www.amplenote.com/notes/${uuid}`);
}
}
else if (args[0] === "open_jot") {
const inputDate = new Date(args[1]);
const timestamp = Math.floor(inputDate.getTime() / 1000);
const note = await app.notes.dailyJot(timestamp);
if (note?.uuid) {
app.navigate(`https://www.amplenote.com/notes/${note.uuid}`);
} else {
const formattedDate = this.formatDate(inputDate);
const newUuid = await app.createNote(formattedDate, ["daily-jots"]);
app.navigate(`https://www.amplenote.com/notes/${newUuid}`);
}
console.log("onEmbedCall", args);
}
},
formatDate(date) {
const months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const day = date.getDate();
const month = months[date.getMonth()];
const year = date.getFullYear();
const getOrdinalSuffix = (day) => {
if (day > 3 && day < 21) return 'th';
switch (day % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
};
return `${month} ${day}${getOrdinalSuffix(day)}, ${year}`;
},
async _getAttachmentContent(app, attachmentUUID) {
const url = await app.getAttachmentURL(attachmentUUID);
const proxyURL = new URL("https://plugins.amplenote.com/cors-proxy");
proxyURL.searchParams.set("apiurl", url);
const response = await fetch(proxyURL);
return response.text();
}
})