-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.ts
More file actions
294 lines (234 loc) · 8.96 KB
/
Copy pathview.ts
File metadata and controls
294 lines (234 loc) · 8.96 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
import { ItemView, TFile, WorkspaceLeaf, MarkdownRenderer } from 'obsidian';
import RecentNotesTimelinePlugin from './main';
import RecentNotesTimelinePlugin from './main';
export const VIEW_TYPE_TIMELINE = 'recent-notes-timeline-view';
interface NoteInfo {
file: TFile;
timestamp: number;
isCreated: boolean; // true if creation date is more recent than modification date
}
export class TimelineView extends ItemView {
private plugin: RecentNotesTimelinePlugin;
private contentEl: HTMLElement;
private refreshInterval: number;
// This icon will be shown in the tab header
icon = 'clock';
constructor(leaf: WorkspaceLeaf, plugin: RecentNotesTimelinePlugin) {
super(leaf);
this.plugin = plugin;
this.refreshInterval = -1;
}
getViewType(): string {
return VIEW_TYPE_TIMELINE;
}
getDisplayText(): string {
return 'Recent Notes Timeline';
}
async onOpen() {
// Use the entire container for the content
const container = this.containerEl.children[1];
container.empty();
// Add the timeline class to the main container
container.addClass('recent-notes-timeline');
// Create a content wrapper with proper width constraints
const contentWrapper = container.createDiv({ cls: 'timeline-wrapper' });
// Create a header with a refresh button
const header = contentWrapper.createDiv({ cls: 'timeline-header' });
// Add a title
const titleContainer = header.createDiv({ cls: 'timeline-title-container' });
const title = titleContainer.createEl('h2', {
cls: 'timeline-title',
text: 'Recent Notes Timeline'
});
const subtitle = titleContainer.createEl('p', {
cls: 'timeline-subtitle',
text: 'Your latest updates and creations'
});
// Add stats container
const statsContainer = header.createDiv({ cls: 'timeline-stats' });
// Add refresh button
const refreshButton = header.createEl('button', {
cls: 'timeline-refresh-button',
attr: { 'aria-label': 'Refresh timeline' }
});
refreshButton.innerHTML = '↻';
refreshButton.addEventListener('click', () => this.refreshTimeline());
// Create the timeline content container
this.contentEl = contentWrapper.createDiv({ cls: 'timeline-content' });
// Load the initial timeline data
await this.refreshTimeline();
// Update stats after loading data
this.updateStats(statsContainer);
// Set up auto-refresh every 5 minutes
this.refreshInterval = window.setInterval(() => {
this.refreshTimeline().then(() => {
this.updateStats(statsContainer);
});
}, 5 * 60 * 1000);
// Register an event handler to refresh when files change
this.registerEvent(
this.app.vault.on('modify', () => {
this.refreshTimeline().then(() => {
this.updateStats(statsContainer);
});
})
);
this.registerEvent(
this.app.vault.on('create', () => {
this.refreshTimeline().then(() => {
this.updateStats(statsContainer);
});
})
);
}
private updateStats(container: HTMLElement) {
container.empty();
const files = this.app.vault.getMarkdownFiles();
const now = Date.now();
const dayInMs = 24 * 60 * 60 * 1000;
// Count files modified in the last day, week, and month
const lastDay = files.filter(file => (now - file.stat.mtime) < dayInMs).length;
const lastWeek = files.filter(file => (now - file.stat.mtime) < 7 * dayInMs).length;
const lastMonth = files.filter(file => (now - file.stat.mtime) < 30 * dayInMs).length;
const daySpan = container.createSpan({ cls: 'timeline-stat' });
daySpan.createSpan({ cls: 'timeline-stat-value', text: lastDay.toString() });
daySpan.createSpan({ cls: 'timeline-stat-label', text: ' today' });
const weekSpan = container.createSpan({ cls: 'timeline-stat' });
weekSpan.createSpan({ cls: 'timeline-stat-value', text: lastWeek.toString() });
weekSpan.createSpan({ cls: 'timeline-stat-label', text: ' this week' });
const monthSpan = container.createSpan({ cls: 'timeline-stat' });
monthSpan.createSpan({ cls: 'timeline-stat-value', text: lastMonth.toString() });
monthSpan.createSpan({ cls: 'timeline-stat-label', text: ' this month' });
}
async onClose() {
// Clear the auto-refresh interval when the view is closed
if (this.refreshInterval !== -1) {
clearInterval(this.refreshInterval);
this.refreshInterval = -1;
}
}
async refreshTimeline() {
// Clear the current content
this.contentEl.empty();
// Get all notes from the vault
const notes = this.getNoteInfos();
if (notes.length === 0) {
this.contentEl.createEl('p', {
text: 'No recent notes found.',
cls: 'timeline-empty-message'
});
return;
}
// Build the timeline UI
this.buildTimelineUI(notes);
}
private getNoteInfos(): NoteInfo[] {
const notes: NoteInfo[] = [];
// Get all markdown files from the vault
const files = this.app.vault.getMarkdownFiles();
for (const file of files) {
const createdTime = file.stat.ctime;
const modifiedTime = file.stat.mtime;
// Use the most recent timestamp (created or modified)
const timestamp = Math.max(createdTime, modifiedTime);
const isCreated = createdTime >= modifiedTime;
notes.push({
file,
timestamp,
isCreated
});
}
// Sort by timestamp in descending order (newest first)
return notes.sort((a, b) => b.timestamp - a.timestamp);
}
private buildTimelineUI(notes: NoteInfo[]) {
let currentDate = '';
const currentYear = new Date().getFullYear();
for (const note of notes) {
const date = new Date(note.timestamp);
// Format the date as "Month Day" or "Month Day, Year" if not current year
const month = date.toLocaleString('en-US', { month: 'long' });
const day = date.getDate();
const year = date.getFullYear();
let dateString = `${month} ${day}`;
if (year !== currentYear) {
dateString += `, ${year}`;
}
// Add date separator if we've moved to a new date
if (dateString !== currentDate) {
currentDate = dateString;
this.contentEl.createEl('div', {
cls: 'timeline-date-separator',
text: currentDate
});
}
// Create the timeline entry
const entry = this.contentEl.createDiv({ cls: 'timeline-entry' });
// Create the note info section
const infoSection = entry.createDiv({ cls: 'timeline-entry-info' });
// Add main content container
const contentSection = infoSection.createDiv({ cls: 'timeline-entry-content' });
// Add file name as a link
const titleEl = contentSection.createEl('a', {
cls: 'timeline-entry-title',
text: note.file.basename,
attr: {
'data-path': note.file.path,
'aria-label': `Open note: ${note.file.basename}`
}
});
// Open the note when clicked
titleEl.addEventListener('click', (event) => {
event.preventDefault();
this.app.workspace.openLinkText(note.file.path, '', false);
});
// Add action indicator and timestamp
const metaInfo = contentSection.createDiv({ cls: 'timeline-entry-meta' });
// Action text
const actionEl = metaInfo.createEl('span', {
cls: 'timeline-entry-action',
text: note.isCreated ? 'Created' : 'Modified'
});
// Add a timestamp
const timeEl = metaInfo.createEl('span', {
cls: 'timeline-entry-time',
text: date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
});
// Try to get a preview of the content
this.getFilePreview(note.file).then(preview => {
if (preview) {
const previewEl = entry.createDiv({
cls: 'timeline-entry-preview'
});
// Use Obsidian's Markdown renderer
MarkdownRenderer.renderMarkdown(
preview,
previewEl,
note.file.path,
this
);
}
});
// We're removing the file path display as requested
// Open button removed as requested
}
}
// Helper method to get a preview of the file content for markdown rendering
private async getFilePreview(file: TFile): Promise<string | null> {
try {
// Read the entire file content
const content = await this.app.vault.read(file);
// Get the lines of content (up to 7 lines)
const lines = content.split('\n').slice(0, 7);
// Join the lines back together
let preview = lines.join('\n');
// If there are more lines, add an indicator
if (content.split('\n').length > 7) {
preview += '\n\n*...*';
}
return preview;
} catch (error) {
return null;
}
}
}