add: detect synced markdown file from folder

resolve: #975
This commit is contained in:
windingwind 2024-06-04 23:10:10 +08:00
parent da03606906
commit c1e76ddbc3
14 changed files with 137 additions and 10 deletions

View File

@ -32,11 +32,6 @@
"chrome://zotero/content/include.js",
this,
);
Services.scriptloader.loadSubScript(
"resource://zotero/require.js",
this,
);
window.arguments[0]._initPromise.resolve();
});
</script>
@ -47,12 +42,12 @@
padding: 0;
margin: 0;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
overflow: hidden;
word-wrap: break-word;
}
.viewport {
margin: 0 5px 0 5px;
overflow-y: auto;
}
.viewport-container {
padding: 0;
@ -62,6 +57,7 @@
display: flex;
flex-direction: row;
overflow: hidden;
background: var(--material-background);
}
.list-viewport {
width: 100%;
@ -89,6 +85,7 @@
<button id="refresh" data-l10n-id="refresh"></button>
<button id="sync" data-l10n-id="sync"></button>
<button id="unSync" data-l10n-id="unSync"></button>
<button id="detect" data-l10n-id="detect"></button>
</div>
</body>
</html>

View File

@ -26,11 +26,14 @@ editor-previewImage-title = Preview Image
syncManager-noteName = Note Name
syncManager-lastSync = Last Sync
syncManager-filePath = MarkDown Path
syncManager-detectConfirmInfo = {$total} synced markdown files detected in {$dir}
{$new} new, {$current} will be updated.
Do you want to apply the changes?
syncInfo-syncTo = MarkDown Path
syncInfo-lastSync = Last Sync
syncInfo-sync = Sync
syncInfo-unSync = UnSync
syncInfo-unSync = Unsync
syncInfo-reveal = Show in Folder
syncInfo-manager = Sync Manager
syncInfo-export = Export As...

View File

@ -1,7 +1,13 @@
title = Sync Manager
refresh =
.label = Refresh
.tooltiptext = Refresh the list of notes
sync =
.label = Sync
.tooltiptext = Sync the selected notes immediately
unSync =
.label = UnSync
.label = Unsync
.tooltiptext = Unsync the selected notes
detect =
.label = Detect...
.tooltiptext = Detect synced notes from folder

View File

@ -22,6 +22,9 @@ templateEditor-templateName = Nome template
syncManager-noteName = Nome nota
syncManager-lastSync = Ultima sincronizzazione
syncManager-filePath = Percorso MarkDown
syncManager-detectConfirmInfo = {$total} synced markdown files detected in {$dir}
{$new} new, {$current} will be updated.
Do you want to apply the changes?
syncInfo-syncTo = Percorso MarkDown
syncInfo-lastSync = Ultima sincronizzazione

View File

@ -1,7 +1,13 @@
title = Manager di sincronizzazione
refresh =
.label = Aggiorna
.tooltiptext = Refresh the list of notes
sync =
.label = Sincronizza
.tooltiptext = Sync the selected notes immediately
unSync =
.label = Interrompi sincronizzazione
.tooltiptext = Unsync the selected notes
detect =
.label = Detect...
.tooltiptext = Detect synced notes from folder

View File

@ -26,6 +26,9 @@ editor-previewImage-title = Preview Image
syncManager-noteName=Имя заметки
syncManager-lastSync=Последний Синк
syncManager-filePath=MarkDown путь
syncManager-detectConfirmInfo = {$total} synced markdown files detected in {$dir}
{$new} new, {$current} will be updated.
Do you want to apply the changes?
syncInfo-syncTo=MarkDown путь
syncInfo-lastSync=Последний Синк

View File

@ -1,7 +1,13 @@
title = Синк Менеджер
refresh =
.label = Обновить
.tooltiptext = Refresh the list of notes
sync =
.label = Синк
.tooltiptext = Sync the selected notes immediately
unSync =
.label = ДеСинк
.tooltiptext = Unsync the selected notes
detect =
.label = Detect...
.tooltiptext = Detect synced notes from folder

View File

@ -26,6 +26,9 @@ editor-previewImage-title = Resmi Önizle
syncManager-noteName = Not Adı
syncManager-lastSync = Son Senkronizasyon
syncManager-filePath = MarkDown Yolu
syncManager-detectConfirmInfo = {$total} synced markdown files detected in {$dir}
{$new} new, {$current} will be updated.
Do you want to apply the changes?
syncInfo-syncTo = MarkDown Yolu
syncInfo-lastSync = Son Senkronizasyon

View File

@ -1,7 +1,13 @@
title = Sync Manager
refresh =
.label = Yenile
.tooltiptext = Refresh the list of notes
sync =
.label = Senkronize Et
.tooltiptext = Sync the selected notes immediately
unSync =
.label = Senkronizasyonu Geri Al
.tooltiptext = Unsync the selected notes
detect =
.label = Detect...
.tooltiptext = Detect synced notes from folder

View File

@ -26,6 +26,9 @@ editor-previewImage-title = 预览图片
syncManager-noteName=笔记名称
syncManager-lastSync=最近同步
syncManager-filePath=MarkDown路径
syncManager-detectConfirmInfo = 扫描到{$total}项由本插件同步的markdown文件 (来自{$dir})
将要新同步{$new}项, 将要覆盖{$current}项
确定应用这些更改吗?
syncInfo-syncTo=MarkDown路径
syncInfo-lastSync=最近同步

View File

@ -1,7 +1,13 @@
title = 同步管理
refresh =
.label = 刷新
.tooltiptext = 刷新同步笔记列表
sync =
.label = 同步
.tooltiptext = 立刻同步选中的笔记
unSync =
.label = 取消同步
.tooltiptext = 不再同步选中的笔记
detect =
.label = 扫描...
.tooltiptext = 从文件夹扫描已同步的笔记

View File

@ -28,6 +28,7 @@ import {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
} from "./modules/sync/api";
import {
runTemplate,
@ -92,6 +93,7 @@ const sync = {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
};
const convert = {

View File

@ -15,6 +15,7 @@ export {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
};
function initSyncList() {
@ -210,3 +211,43 @@ async function getMDFileName(noteId: number, searchDir?: string) {
[noteItem],
);
}
async function findAllSyncedFiles(searchDir: string) {
const results: SyncStatus[] = [];
const mdRegex = /\.(md|MD|Md|mD)$/;
await Zotero.File.iterateDirectory(
searchDir,
async (entry: OS.File.Entry) => {
if (entry.isDir) {
const subDirResults = await findAllSyncedFiles(entry.path);
results.push(...subDirResults);
return;
}
if (mdRegex.test(entry.name)) {
const MDStatus = await getMDStatus(entry.path);
if (!MDStatus.meta?.$libraryID || !MDStatus.meta?.$itemKey) {
return;
}
const item = await Zotero.Items.getByLibraryAndKeyAsync(
MDStatus.meta.$libraryID,
MDStatus.meta.$itemKey,
);
if (!item || !(item as Zotero.Item).isNote()) {
return;
}
results.push({
path: MDStatus.filedir,
filename: MDStatus.filename,
md5: Zotero.Utilities.Internal.md5(MDStatus.content, false),
noteMd5: Zotero.Utilities.Internal.md5(
(item as Zotero.Item).getNote(),
false,
),
lastsync: MDStatus.lastmodify.getTime(),
itemID: item.id,
});
}
},
);
return results;
}

View File

@ -110,6 +110,9 @@ export async function showSyncManager() {
const unSyncButton = win.document.querySelector(
"#unSync",
) as HTMLButtonElement;
const detectButton = win.document.querySelector(
"#detect",
) as HTMLButtonElement;
refreshButton.addEventListener("click", (ev) => {
refresh();
});
@ -127,6 +130,9 @@ export async function showSyncManager() {
});
refresh();
});
detectButton.addEventListener("click", () => {
detectSyncedNotes();
});
}
}
@ -218,5 +224,41 @@ async function unSyncNotes(itemIds: number[]) {
for (const itemId of itemIds) {
await addon.api.sync.removeSyncNote(itemId);
}
refresh();
await refresh();
}
async function detectSyncedNotes() {
const dir = await new addon.data.ztoolkit.FilePicker(
"Select folder to detect",
"folder",
).open();
if (!dir) return;
const statusList = await addon.api.sync.findAllSyncedFiles(dir);
let current = 0;
for (const status of statusList) {
if (addon.api.sync.isSyncNote(status.itemID)) {
current++;
}
}
const total = statusList.length;
const newCount = total - current;
if (
!addon.data.sync.manager.window?.confirm(
getString("syncManager-detectConfirmInfo", {
args: {
total,
new: newCount,
current,
dir,
},
}),
)
)
return;
for (const status of statusList) {
addon.api.sync.updateSyncStatus(status.itemID, status);
}
await addon.hooks.onSyncing();
await refresh();
}