generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functionsYear.ts
183 lines (126 loc) · 6.81 KB
/
functionsYear.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
179
180
181
182
183
import ReleaseTimeline from "main";
import { getAPI, isPluginEnabled, DataviewAPI } from "obsidian-dataview";
import { moment } from "obsidian";
import { create } from "domain";
import { createErrorMsg, createRowSeparator, createRowSeparatorYearMonth, createRowYear, createRowItem, createNewRow, parseQuerySortOrder, replaceThisInQuery } from "helperFunctions";
export default class YearTimeline {
plugin: ReleaseTimeline;
constructor(plugin: ReleaseTimeline) {
this.plugin = plugin;
}
async renderTimeline(content: string) {
const dv = getAPI();
if ( typeof dv == 'undefined' ) { return createErrorMsg('Dataview is not installed. The Release Timeline plugin requires Dataview to properly function.'); }
var sortOrder = parseQuerySortOrder(content, this.plugin);
content = replaceThisInQuery(content, this.plugin.app);
//get results from dataview
try {
var results;
var results0 = await dv.query(content);
let a = results0.value.values;
//filter out null years
let b = a.filter(x => typeof x[1] !== 'undefined' && x[1] !== null);
//convert date values to years, get rid of non-date values like character strings
b.forEach(x => x[1] = moment( x[1].toString(), 'Y' ).format('Y'))
b = b.filter(x => x[1] != "Invalid date")
//parse file name from path, replace path, insert to notes with empty alias
b.forEach(x => x[0] = x[0].path.match(/([^\/]+(?=\.)).md/)[1]);
b.forEach(x => x[2]==null ? x[2]=x[0] : 1);
//group by year
results = dv.array(b);
results = results.groupBy(x => x[1]);
//sort by sort order
results = results.sort(k => k.key, sortOrder);
}
catch(error) {
return createErrorMsg("Error from dataview: " + error.message)
}
if (results.length == 0) {
return createErrorMsg("No results");
}
else {
return this.createTimelineTable(results);
}
};
createTimelineTable(timeline) {
const newTbl = document.createElement("table");
newTbl.classList.add("release-timeline")
//create table body
const newTbody = document.createElement("tbody");
//check to create an empty row separator
let isLongRow = 0;
// check if too many years are selected
let minYear = Math.min(...timeline.key.values);
let maxYear = Math.max(...timeline.key.values);
if (maxYear - minYear > 5000 && this.plugin.settings.collapseEmptyYears == false) {
let errorTbl = createErrorMsg("Error: More than 5000 years in selection and \"Collapse years\" option is not enabled. Enable this option in plugin settings to build the timeline.");
return errorTbl;
};
//create rows for table
let prevYear = Number(timeline[0].key);
timeline.forEach(item => {
//year
let key = Number(item.key);
//array of titles, sorted by name
//let value = item.rows.values.map(k => k.file.name).sort();
//[[filename1, alias1], [filename2, alias2], ..]
//let value = item.rows.values.map(k => [k.file.name, typeof k[aliasName] !== 'undefined' ? k[aliasName] : k.file.name]);//.sort((a, b) => b[0] - a[0]);
let value = item.rows.values.map(k => [k[0], k[2]]);
//create separator if previous row was long
if (isLongRow == 1) { newTbody.appendChild(createRowSeparator()) };
//create empty rows
let yearDiff = Math.abs(key - prevYear);
let collapseRows = this.plugin.settings.collapseEmptyYears;
let collapseLimit = Number(this.plugin.settings.collapseLimit) || 2;
if ( yearDiff > 1) {
//if collapse rows is on - create 1 row
if ( collapseRows && yearDiff > 2 && yearDiff > collapseLimit ) {
let yearRange = key > prevYear ? `${prevYear + 1} - ${key - 1}` : `${key + 1} - ${prevYear - 1}`;
const rowYear = createRowYear( { val: yearRange, cls: 'year-nonexisting' } );
const rowItem = createRowItem( { fileName: "", fileAlias: "" } );
const newRow = createNewRow(rowYear, rowItem);
newTbody.appendChild(newRow);
//if collapse rows is off - create all rows
} else {
for (let j = 1; j < yearDiff; j++) {
let i = (key > prevYear) ? prevYear + j : prevYear - j;
const rowYear = createRowYear( { val: i, cls: 'year-nonexisting' } );
const rowItem = createRowItem( { fileName: "", fileAlias: "" } );
const newRow = createNewRow(rowYear, rowItem);
newTbody.appendChild(newRow);
};
};
isLongRow = 0;
};
//create real rows
//create row with 1 element
if ( value.length == 1 ) {
isLongRow = 0;
const rowYear = createRowYear( { val: key, cls: 'year-existing' } );
const rowItem = createRowItem( { fileName: value[0][0], fileAlias: value[0][1] } );
const newRow = createNewRow(rowYear, rowItem);
newTbody.appendChild(newRow);
//create rows with multiple elements
} else {
//create separator if prev row was short, but this one is long
if (isLongRow == 0) { newTbody.appendChild(createRowSeparator()); };
isLongRow = 1;
//create 1st row
const rowYear = createRowYear( { val: key, cls: 'year-existing', rowspanNb: value.length } );
const rowItem = createRowItem( { fileName: value[0][0], fileAlias: value[0][1], cls: "td-first" } );
const newRow = createNewRow(rowYear, rowItem);
newTbody.appendChild(newRow);
//create 2nd+ rows
for (let i = 1; i < value.length; i++) {
const rowItem = createRowItem( { fileName: value[i][0], fileAlias: value[i][1], cls: "td-next" } );
const newRow = createNewRow(rowItem);
newTbody.appendChild(newRow);
};
};
prevYear = key;
});
//append table body to table
newTbl.appendChild(newTbody);
return newTbl;
};
}