generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
178 lines (169 loc) · 4.8 KB
/
main.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
Editor,
MarkdownFileInfo,
MarkdownView,
Notice,
Plugin,
} from "obsidian";
import { moment } from "obsidian";
enum ClassNames {
SECTION_TOTAL = "timediff-section-total",
TEXT_ACCENT = "timediff-accent",
SECTION_VALUE_TOTAL = "timediff-section-value-total",
}
enum Attributes {
TOTAL_VALUE_IN_MINUTES = "data-timediff-section-total-in-minutes",
}
const timeRegex = /\d{2}:\d{2} - \d{2}:\d{2}/g;
export default class TimeDiffPlugin extends Plugin {
async onload() {
this.addCommand({
id: "timediff-total",
name: "Show total time diff count in current file",
checkCallback: (checking: boolean) => {
if (!checking) {
const file = this.app.workspace.getActiveFile();
if (file) {
const fileCache =
this.app.metadataCache.getFileCache(file);
const cacheRead = this.app.vault
.cachedRead(file)
.then((data) => {
let totalSumInMinutes = 0;
const codeSections =
fileCache?.sections?.filter(
(section) => section.type === "code"
) || [];
for (const section of codeSections) {
const start = section.position.start.offset;
const end = section.position.end.offset;
const extracted = data.substring(
start,
end
);
if (!extracted.startsWith("```timediff")) {
// code block without ```timediff, skip it
continue;
}
const extractedWithoutCodeblocks = extracted
.replace("```timediff", "")
.replace("````", "");
const rows = extractedWithoutCodeblocks
.split("\n")
.filter((row) => row.length > 0);
for (const row of rows) {
const match = row.match(timeRegex);
if (!match) {
continue;
}
const timeElements = match[0]
.trim()
.split(" - ");
const [left, right] = timeElements.map(
(timeElement) => {
const [hours, minutes] =
timeElement.split(":");
return moment()
.hours(Number(hours))
.minutes(Number(minutes));
}
);
const totalDiffInMinutes = right.diff(
left,
"minutes"
);
totalSumInMinutes += totalDiffInMinutes;
}
}
const { readableDiff, diffInMinutes } =
calculateTimeDiffs(totalSumInMinutes);
new Notice(
`Total: ${totalSumInMinutes}min - ${readableDiff}`
);
});
}
}
return true;
},
});
this.addCommand({
id: "timediff-add-timediff-block",
name: "Insert timediff block",
editorCallback: (editor: Editor) => {
editor.replaceSelection("```timediff\n\n````");
},
});
this.addCommand({
id: "timediff-add-current-time",
name: "Insert current time",
editorCallback: (editor: Editor) => {
const currentTIme = moment();
editor.replaceSelection(
`${currentTIme.hours()}:${currentTIme.minutes()}`
);
},
});
this.registerMarkdownCodeBlockProcessor(
"timediff",
(source, el, _ctx) => {
let totalSumInMinutes = 0;
const rows = source.split("\n").filter((row) => row.length > 0);
for (const row of rows) {
const match = row.match(timeRegex);
if (!match) {
const div = el.createEl("div");
div.createEl("span", {
text: `${row}`,
});
continue;
}
const timeElements = match[0].trim().split(" - ");
const [left, right] = timeElements.map((timeElement) => {
const [hours, minutes] = timeElement.split(":");
return moment()
.hours(Number(hours))
.minutes(Number(minutes));
});
const totalDiffInMinutes = right.diff(left, "minutes");
totalSumInMinutes += totalDiffInMinutes;
const { readableDiff } =
calculateTimeDiffs(totalDiffInMinutes);
const div = el.createEl("div");
div.createEl("span", {
text: `${row}`,
});
div.createEl("span", {
text: `\t${readableDiff}`,
cls: ClassNames.TEXT_ACCENT,
});
}
const div = el.createEl("div", {
cls: ClassNames.SECTION_TOTAL,
});
div.createEl("span", {
text: `Total: `,
});
div.createEl("span", {
text: `${
calculateTimeDiffs(totalSumInMinutes).readableDiff
}`,
cls: `${ClassNames.TEXT_ACCENT} ${ClassNames.SECTION_VALUE_TOTAL}`,
attr: {
"data-timediff-section-total-in-minutes": `${totalSumInMinutes}`,
},
});
}
);
}
onunload() {}
}
function calculateTimeDiffs(totalDiffInMinutes: number): {
diffInHours: number;
diffInMinutes: number;
readableDiff: string;
} {
const diffInHours = Math.floor(totalDiffInMinutes / 60);
const diffInMinutes = totalDiffInMinutes % 60;
const readableDiff = `${diffInHours}h${diffInMinutes}min`;
return { diffInHours, diffInMinutes, readableDiff };
}