forked from Trulsaa/obsidian-icloud-contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
148 lines (136 loc) · 3.61 KB
/
Copy pathmain.ts
File metadata and controls
148 lines (136 loc) · 3.61 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
App,
DataWriteOptions,
Notice,
OpenViewState,
Plugin,
TFile,
normalizePath,
} from "obsidian";
import ICloudContactsApi, {
ICloudVCard,
OnlyRequiredFromObsidianApi,
} from "src/ICloudContactsApi";
import { fetchContacts } from "src/iCloudClient";
import {
DEFAULT_SETTINGS,
ICloudContactsSettings,
SettingTab,
} from "./src/SettingTab";
function showNotice(message: string, duration: number) {
const notice = new Notice(message, duration);
return {
setMessage: (message: string) => {
notice.setMessage(message);
},
hide: () => notice.hide(),
};
}
function createObsidianApiWrapper(app: App): OnlyRequiredFromObsidianApi {
return {
normalizePath: normalizePath,
app: {
fileManager: {
processFrontMatter: (
file: TFile,
fn: (frontmatter: any) => void,
) => app.fileManager.processFrontMatter(file, fn),
renameFile: (file: TFile, newPath: string) =>
app.fileManager.renameFile(file, newPath),
},
vault: {
adapter: {
list: (normalizedPath: string) =>
app.vault.adapter.list(normalizedPath),
exists: (normalizedPath: string, sensitive: boolean) =>
app.vault.adapter.exists(normalizedPath, sensitive),
},
append: (
file: TFile,
data: string,
_options?: DataWriteOptions,
) => app.vault.append(file, data),
create: (
path: string,
data: string,
_options?: DataWriteOptions,
) => app.vault.create(path, data),
createFolder: (path: string) => app.vault.createFolder(path),
getFileByPath: (path: string) => app.vault.getFileByPath(path),
getFolderByPath: (path: string) =>
app.vault.getFolderByPath(path),
process: (
file: TFile,
fn: (data: string) => string,
_options?: DataWriteOptions,
) => app.vault.process(file, fn),
},
workspace: {
getLeaf: () => ({
openFile: (file: TFile, _openState?: OpenViewState) =>
app.workspace.getLeaf().openFile(file),
}),
},
metadataCache: {
getCache: (path: string) => app.metadataCache.getCache(path),
},
},
};
}
export default class ICloudContacts extends Plugin {
settings: ICloudContactsSettings;
private api: ICloudContactsApi;
async onload() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
this.api = new ICloudContactsApi(
createObsidianApiWrapper(this.app),
this.settings,
fetchContacts,
showNotice,
);
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: "update-contacts",
name: "Update contacts",
callback: async () => {
const { updateData, usedSettings } =
await this.api.updateContacts();
await this.saveRunData(updateData, usedSettings);
},
});
this.addCommand({
id: "update-all-contacts",
name: "Update all contacts",
callback: async () => {
const { updateData, usedSettings } =
await this.api.updateContacts({
rewriteAll: true,
});
await this.saveRunData(updateData, usedSettings);
},
});
this.addRibbonIcon("sync", "Update contacts", () =>
this.api.updateContacts(),
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this, fetchContacts));
}
private async saveRunData(
updateData: ICloudVCard[],
usedSettings: ICloudContactsSettings,
) {
this.settings.previousUpdateData = updateData;
delete usedSettings.previousUpdateData;
delete usedSettings.previousUpdateSettings;
this.settings.previousUpdateSettings = usedSettings;
await this.saveSettings();
}
onunload() {}
async saveSettings() {
await this.saveData(this.settings);
}
}