-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbaseDataviewSourceQuery.ts
More file actions
413 lines (389 loc) · 9.43 KB
/
Copy pathbaseDataviewSourceQuery.ts
File metadata and controls
413 lines (389 loc) · 9.43 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { App, Platform } from "obsidian";
import { DataviewApi, getAPI, DataArray, Literal } from "obsidian-dataview";
import { GraphProcessError } from "src/processor/graphProcessError";
import {
CountFieldType,
Data,
DataSource,
DateFieldType,
PropertySource,
} from "./types";
import { DateTime } from "luxon";
import { Contribution, ContributionItem } from "src/types";
import { isLuxonDateTime } from "src/util/dateTimeUtils";
import { parseNumberOption } from "src/util/utils";
import { dataviewDataFilterChain } from "./filter/dataviewDataFilter";
export abstract class BaseDataviewDataSourceQuery {
abstract accept(source: DataSource): boolean;
async query(source: DataSource, app: App): Promise<Contribution[]> {
this.reconcileSourceValueIfNotExists(source);
const dv = this.checkAndGetApi(app);
const data = this.doQuery(dv, source);
let queryData = this.mapToQueryData(data, source);
if (typeof (this as any).enrichQueryData === "function") {
queryData = await (this as any).enrichQueryData(
queryData,
source,
app
);
}
const unsatisfiedData = queryData.filter((item) => !item.date);
if (unsatisfiedData.length > 0) {
console.warn(
unsatisfiedData.length +
" data can't be converted to date, please check the date field format",
unsatisfiedData
);
}
const result = queryData
.filter((d) => d.date != undefined)
.groupBy((d) => d.date?.toFormat("yyyy-MM-dd"))
.map((entry) => {
// performance optimization
const value = this.countSumValueByCustomizeProperty(
entry.rows,
source.countField?.type,
source.countField?.value
);
const items = entry.rows
.map((item) => {
let label;
if (source.type == "PAGE") {
// @ts-ignore
label = item.raw.file.name;
} else {
label = item.raw.text;
}
const itemValue =
this.getAndConvertValueByCustomizeProperty(
item,
source.countField?.type,
source.countField?.value
);
if (source.countField?.type == "PAGE_PROPERTY") {
label += ` [${source.countField?.value}:${itemValue}]`;
}
if (source.countField?.type == "QUERY_INSTANCES") {
label += ` [instances: ${itemValue}]`;
}
return {
label: label,
value: itemValue,
link: {
// @ts-ignore
href: item.raw.file.path,
className: "internal-link",
rel: 'noopener'
},
open: (e) => jump(e, source, item, app),
} as ContributionItem;
})
.array();
return {
date: entry.key,
value: value,
items: items,
} as Contribution;
})
.array();
return result;
}
reconcileSourceValueIfNotExists(source: DataSource) {
if (!source.value) {
source.value = '""';
}
}
checkAndGetApi(app: App): DataviewApi {
const dv = getAPI(app);
if (!dv) {
throw new GraphProcessError({
summary: "Initialize Dataview failed",
recommends: ["Please install Dataview plugin"],
});
}
return dv;
}
abstract doQuery(
dv: DataviewApi,
source: DataSource
): DataArray<Record<string, Literal>>;
mapToQueryData(
data: DataArray<Record<string, Literal>>,
source: DataSource
): DataArray<Data<Record<string, Literal>>> {
if (source.dateField && source.dateField.type) {
const dateFieldName = source.dateField.value;
const dateFieldType = source.dateField.type;
const dateFieldFormat = source.dateField.format;
return data
.filter((item) => {
return dataviewDataFilterChain.every((filter) =>
filter.filter(source, item, this)
);
})
.map((item) => {
// @ts-ignore
const fileName = item.file.name;
if (dateFieldType == "FILE_CTIME") {
// @ts-ignore
return new Data(item, item.file.ctime);
} else if (dateFieldType == "FILE_MTIME") {
// @ts-ignore
return new Data(item, item.file.mtime);
} else if (dateFieldType == "FILE_NAME") {
const dateTime = this.toDateTime(
fileName,
fileName,
dateFieldFormat
);
if (dateTime) {
return new Data(item, dateTime);
} else {
return new Data(item);
}
} else {
const propertySource =
this.getPropertySourceByDateFieldType(
source.dateField?.type
);
const fieldValue = this.getValueByCustomizeProperty(
item,
propertySource,
dateFieldName || ""
);
if (isLuxonDateTime(fieldValue)) {
return new Data(item, fieldValue as DateTime);
} else {
const dateTime = this.toDateTime(
fileName,
fieldValue as string,
dateFieldFormat
);
return new Data(item, dateTime);
}
}
});
} else {
return data.map((item) => {
// @ts-ignore
return new Data(item, item.file.ctime);
});
}
}
toDateTime(
page: string,
date: string,
dateFieldFormat?: string
): DateTime | undefined {
if (typeof date !== "string") {
console.warn(
"can't parse date, it's a valid format? " +
date +
" in page " +
page
);
return undefined;
}
try {
let dateTime = null;
if (dateFieldFormat) {
dateTime = DateTime.fromFormat(date, dateFieldFormat);
if (dateTime.isValid) {
return dateTime;
}
}
dateTime = DateTime.fromISO(date);
if (dateTime.isValid) {
return dateTime;
}
dateTime = DateTime.fromRFC2822(date);
if (dateTime.isValid) {
return dateTime;
}
dateTime = DateTime.fromHTTP(date);
if (dateTime.isValid) {
return dateTime;
}
dateTime = DateTime.fromSQL(date);
if (dateTime.isValid) {
return dateTime;
}
dateTime = DateTime.fromFormat(date, "yyyy-MM-dd HH:mm");
if (dateTime.isValid) {
return dateTime;
}
dateTime = DateTime.fromFormat(date, "yyyy-MM-dd'T'HH:mm");
if (dateTime.isValid) {
return dateTime;
}
} catch (e) {
console.warn(
"can't parse date, it's a valid format? " +
date +
" in page " +
page
);
}
return undefined;
}
countSumValueByCustomizeProperty(
groupData: DataArray<Data<Record<string, Literal>>>,
propertyType?: CountFieldType,
propertyName?: string
): number {
if (!propertyType || propertyType == "DEFAULT") {
return groupData.length;
}
if (propertyType === "QUERY_INSTANCES") {
return groupData
.map((item) =>
this.getAndConvertValueByCustomizeProperty(
item,
"QUERY_INSTANCES",
"_instanceCount"
)
)
.array()
.reduce((a, b) => a + b, 0);
}
if (propertyName) {
return groupData
.map((item) => {
return this.getAndConvertValueByCustomizeProperty(
item,
propertyType,
propertyName
);
})
.array()
.reduce((a, b) => a + b, 0);
}
return groupData.length;
}
getAndConvertValueByCustomizeProperty(
item: Data<Record<string, Literal>>,
propertyType?: CountFieldType,
propertyName?: string
): number {
const effectiveName =
propertyName ||
(propertyType === "QUERY_INSTANCES" ? "_instanceCount" : undefined);
if (effectiveName) {
let propertySource: PropertySource;
switch (propertyType) {
case "PAGE_PROPERTY":
propertySource = "PAGE";
break;
case "TASK_PROPERTY":
propertySource = "TASK";
break;
case "QUERY_INSTANCES":
propertySource = "PAGE";
break;
default:
propertySource = "UNKNOWN";
break;
}
const r = this.getValueByCustomizeProperty(
item.raw,
propertySource,
effectiveName
);
if (r == undefined || r == null) {
return 0;
}
if (r instanceof Array) {
return r.length;
}
if (typeof r === "number" || r instanceof Number) {
return r as number;
}
if (typeof r === "string" || r instanceof String) {
const n = parseNumberOption(r as string);
if (n != null) {
return n;
}
return r.trim() === "" ? 0 : 1;
}
if (typeof r === "boolean" || r instanceof Boolean) {
return r ? 1 : 0;
}
return 1;
} else {
return 1;
}
}
abstract getValueByCustomizeProperty(
data: Record<string, Literal>,
propertyType: PropertySource,
propertyName: string
): any;
getPropertySourceByCountFieldType(type: CountFieldType): PropertySource {
switch (type) {
case "PAGE_PROPERTY":
return "PAGE";
case "TASK_PROPERTY":
return "TASK";
case "QUERY_INSTANCES":
return "PAGE";
default:
return "UNKNOWN";
}
}
getPropertySourceByDateFieldType(type?: DateFieldType): PropertySource {
switch (type) {
case "FILE_CTIME":
case "FILE_MTIME":
case "FILE_NAME":
case "PAGE_PROPERTY":
return "PAGE";
case "TASK_PROPERTY":
return "TASK";
default:
return "UNKNOWN";
}
}
}
function jump(
evt: MouseEvent,
source: DataSource,
item: Data<Record<string, Literal>>,
app: App
) {
if (source.type != "PAGE") {
const selectionState = {
eState: {
cursor: {
from: {
line: item.raw.line,
// @ts-ignore
ch: item.raw.position.start.col,
},
to: {
// @ts-ignore
line: item.raw.line + item.raw.lineCount - 1,
// @ts-ignore
ch: item.raw.position.end.col,
},
},
line: item.raw.line,
},
};
// MacOS interprets the Command key as Meta.
app.workspace.openLinkText(
// @ts-ignore
item.raw.link.toFile().obsidianLink(),
// @ts-ignore
item.raw.path,
evt.ctrlKey || (evt.metaKey && Platform.isMacOS),
selectionState as any
);
} else {
app.workspace.openLinkText(
// @ts-ignore
item.raw.file?.path,
"",
evt.ctrlKey || (evt.metaKey && Platform.isMacOS)
);
}
}