Skip to content

Commit

Permalink
支持快速导入模板
Browse files Browse the repository at this point in the history
  • Loading branch information
loonghfut committed Jan 13, 2025
1 parent fefaa83 commit 82e9662
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
Binary file added public/asset/日程.sy.zip
Binary file not shown.
34 changes: 33 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,36 @@ export async function testSync(myurl: string, token: string) {
return true;
}
return false;
}
}

//导入思源文件.zip
export async function importSY(notebookId: string, file: Blob) {
const formData = new FormData();
formData.append('file', file, '日程.sy.zip');
formData.append('notebook', notebookId);
formData.append('toPath', '/');

const response = await fetch('/api/import/importSY', {
method: 'POST',
body: formData
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
return data.code === 0;
}

//触发网络下载
export const downloadFile = (file: Blob) => {
const url = URL.createObjectURL(file);
const link = document.createElement('a');
link.href = url;
link.download = '日程.sy.zip';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
42 changes: 32 additions & 10 deletions src/calendar/module-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,31 @@ export class M_calendar {
}
}

async importMoBan() {
let notebookId = getSTCalendarNotebookId((await api.lsNotebooks()).notebooks);
console.log("ceshi1", notebookId);
if (!notebookId) {
showMessage("已创建名为“ST日程管理”的笔记本", 3000, "info");
await api.createNotebook("ST日程管理");
notebookId = getSTCalendarNotebookId((await api.lsNotebooks()).notebooks);
}
//获取模板zip文件
try{
const file = await api.getFileBlob("/data/plugins/siyuan-steve-tools/asset/日程.sy.zip");
await api.importSY(notebookId, file);
}catch(e){
console.log(e);
}
showMessage("已导入日程模板", 3000, "info");
}

}


function getSTCalendarNotebookId(notebooks): string | null {
const stCalendarNotebook = notebooks.find(notebook => notebook.name === "ST日程管理");
return stCalendarNotebook ? stCalendarNotebook.id : null;
}

function extractDataAvId(markdown: string): string | null {
const regex = /data-av-id="([^"]+)"/;
Expand All @@ -560,16 +582,16 @@ function convertTimestampToArray(timestamp: number): [number, number, number, nu
];
}

type FrequencyType = 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY';
const mapFrequency = (chinese: string): FrequencyType => {
const frequencyMap: Record<string, FrequencyType> = {
'每天': 'DAILY',
'每周': 'WEEKLY',
'每月': 'MONTHLY',
'每年': 'YEARLY'
};
return frequencyMap[chinese] || 'WEEKLY';
};
// type FrequencyType = 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY';
// const mapFrequency = (chinese: string): FrequencyType => {
// const frequencyMap: Record<string, FrequencyType> = {
// '每天': 'DAILY',
// '每周': 'WEEKLY',
// '每月': 'MONTHLY',
// '每年': 'YEARLY'
// };
// return frequencyMap[chinese] || 'WEEKLY';
// };


// const targetElement = document.querySelector('div[contenteditable="false"][data-av-id="20241213113357-m9b143e"][data-av-type="table"][data-node-id="20241003141312-30yk3cr"][data-type="NodeAttributeView"][class="av"][custom-sy-av-view="20241213113357-tuugpcw"][name="日程"]');
Expand Down
26 changes: 13 additions & 13 deletions src/setting-example.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@
key: "cal-show-view",
value: settings["cal-show-view"],
},
// {
// type: "button",
// title: "日程周期规则说明",
// description: "日程周期规则说明",
// key: "cal-rule",
// value: settings["cal-rule"],
// button: {
// label: "查看",
// callback: () => {
// moduleInstances['M_calendar'].showhelp();
// },
// },
// },
{
type: "button",
title: "日程周期模板",
description: "生成日程周期模板(注意:会创建一个笔记本)",
key: "cal-rule",
value: settings["cal-rule"],
button: {
label: "生成",
callback: () => {
moduleInstances['M_calendar'].importMoBan();
},
},
},
{
type: "button",
title: "恢复默认配置",
Expand Down

0 comments on commit 82e9662

Please sign in to comment.