Skip to content

Commit

Permalink
add: detect synced markdown file from folder
Browse files Browse the repository at this point in the history
resolve: #975
  • Loading branch information
windingwind committed Jun 4, 2024
1 parent da03606 commit c1e76dd
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 10 deletions.
11 changes: 4 additions & 7 deletions addon/chrome/content/syncManager.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
"chrome://zotero/content/include.js",
this,
);

Services.scriptloader.loadSubScript(
"resource://zotero/require.js",
this,
);
window.arguments[0]._initPromise.resolve();
});
</script>
Expand All @@ -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;
Expand All @@ -62,6 +57,7 @@
display: flex;
flex-direction: row;
overflow: hidden;
background: var(--material-background);
}
.list-viewport {
width: 100%;
Expand Down Expand Up @@ -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>
5 changes: 4 additions & 1 deletion addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
8 changes: 7 additions & 1 deletion addon/locale/en-US/syncManager.ftl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions addon/locale/it-IT/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions addon/locale/it-IT/syncManager.ftl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions addon/locale/ru-RU/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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=Последний Синк
Expand Down
6 changes: 6 additions & 0 deletions addon/locale/ru-RU/syncManager.ftl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions addon/locale/tr-TR/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions addon/locale/tr-TR/syncManager.ftl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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=最近同步
Expand Down
6 changes: 6 additions & 0 deletions addon/locale/zh-CN/syncManager.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
title = 同步管理
refresh =
.label = 刷新
.tooltiptext = 刷新同步笔记列表
sync =
.label = 同步
.tooltiptext = 立刻同步选中的笔记
unSync =
.label = 取消同步
.tooltiptext = 不再同步选中的笔记
detect =
.label = 扫描...
.tooltiptext = 从文件夹扫描已同步的笔记
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
} from "./modules/sync/api";
import {
runTemplate,
Expand Down Expand Up @@ -92,6 +93,7 @@ const sync = {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
};

const convert = {
Expand Down
41 changes: 41 additions & 0 deletions src/modules/sync/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
getMDStatus,
getMDStatusFromContent,
getMDFileName,
findAllSyncedFiles,
};

function initSyncList() {
Expand Down Expand Up @@ -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;
}
44 changes: 43 additions & 1 deletion src/modules/sync/managerWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -127,6 +130,9 @@ export async function showSyncManager() {
});
refresh();
});
detectButton.addEventListener("click", () => {
detectSyncedNotes();
});
}
}

Expand Down Expand Up @@ -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();
}

0 comments on commit c1e76dd

Please sign in to comment.