-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackToHistory.js
192 lines (180 loc) · 6.6 KB
/
BackToHistory.js
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
/*
Back-to-historytory
https://github.com/SiriusXT/trilium-back-to-history
version: 0.5 for TriliumNext > 0.90.8
*/
const supportType = ['text', 'code']
const autoJump = true;
const showJumpButton = true;
const omitNoteIds=['root']
// The following code does not need to be modified
if (!autoJump && !showJumpButton) {
console.log("BackUpHistory: do nothing");
return;
}
function creatHistory() {
return new Promise((resolve, reject) => {
api.runOnBackend((noteId) => {
try {
const { note, branch } = api.createNewNote({
parentNoteId: noteId,
title: 'history',
content: '{}',
type: 'code',
mime: 'application/json'
});
resolve(note);
} catch (error) {
// reject(error);
}
}, [api.startNote.noteId]);
});
}
let historyNoteId;
async function getHistory() {
let history;
if (historyNoteId !== undefined) {
const childNote = await api.getNote(historyNoteId);
// return JSON.parse((await (await api.getNote(historyNoteId)).getNoteComplement()).content);
history = JSON.parse((await childNote.getNoteComplement()).content);
return history;
}
const children = api.startNote.children;
let haveHistory = false;
for (let i = 0; i < children.length; i++) {
const childNote = await api.getNote(children[i]);
if (childNote.type == 'code' && childNote.mime == 'application/json' && childNote.title == 'history') {
historyNoteId = childNote.noteId;
try {
history = JSON.parse((await childNote.getNoteComplement()).content);
} catch (error) {
history = {}
}
haveHistory = true;
break;
}
}
if (!haveHistory) {
const note = await creatHistory();
historyNoteId = note.noteId;
history = JSON.parse((await note.getNoteComplement()).content);
}
return history;
}
const jumpButton = `<div class="ribbon-tab-title jump-history">
<span class="ribbon-tab-title-icon bx bx-down-arrow-alt"></span>
</div><div class="ribbon-tab-spacer jump-history"></div>`
var jumpHistory = class extends api.NoteContextAwareWidget {
get position() {
return 50;
}
static get parentWidget() {
return 'note-detail-pane';
}
constructor() {
super();
}
isEnabled() {
return super.isEnabled() && supportType.includes(this.note.type) && !omitNoteIds.includes(this.noteId);
}
doRender() {
this.$widget = $('');
return this.$widget;
}
scrollHandler = () => {
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(async () => {
try {
const sc = this.$scrollingContainer[0];
const nl = this.$scrollingContainer.find('.note-list-widget')[0];
const radio = (sc.scrollTop / (sc.scrollHeight - nl.offsetHeight)).toFixed(5);
if (!isNaN(radio)) {
let history = await getHistory();
history[this.note.noteId] = radio;
saveHistory(history);
}
}
catch (error) {
const $noteSplit = $(`.note-split[data-ntx-id="${this.noteContext.ntxId}"]`);
this.$scrollingContainer = $noteSplit.children('.scrolling-container');
console.warn('jumpHistory: ', error);
}
}, 3000);
}
scrollTo = () => {
const sc = this.$scrollingContainer[0];
const nl = sc.querySelector('.note-list-widget');
const scrollHeight = sc.scrollHeight - nl.offsetHeight;
$(sc).animate({
scrollTop: this.scrollToRadio * scrollHeight,
}, 300);
}
addJumpButton() {
const $ribbonTab = $(`.note-split[data-ntx-id="${this.noteContext.ntxId}"]`).find('.ribbon-tab-container');
$ribbonTab.find('.jump-history').remove();
const $button = $(jumpButton);
$ribbonTab.append($button);
$button.on('click', this.scrollTo);
$button.attr('title', "Scorll To " + this.scrollToRadio * 100 + "%");
}
autoJumpFunc() {
this.scrollTo();
clearInterval(this.jumpInterval);
let timesRun = 0;
let preHeight = 0;
this.jumpInterval = setInterval(() => {
timesRun += 1;
if (timesRun == 5) {
clearInterval(this.jumpInterval);
}
if (this.$scrollingContainer[0].scrollHeight != preHeight) {
preHeight = this.$scrollingContainer[0].scrollHeight;
this.scrollTo();
}
}, 200);
}
async initEvent() {
this.updateTimeout = 0;
const $noteSplit = $(`.note-split[data-ntx-id="${this.noteContext.ntxId}"]`);
this.$scrollingContainer = $noteSplit.children('.scrolling-container');
this.$scrollingContainer.off('scroll', this.scrollHandler);
if (showJumpButton && this.scrollToRadio != undefined) {
this.addJumpButton();
}
setTimeout(() => {
// Automatic jump without monitoring
this.$scrollingContainer.off('scroll', this.scrollHandler);
this.$scrollingContainer.on('scroll', this.scrollHandler);
}, 1000);
}
async refreshWithNote() {
const history = await getHistory();
this.scrollToRadio = history[this.note.noteId];
await this.initEvent();
if (this.scrollToRadio != undefined && autoJump) {
this.autoJumpFunc();
}
}
async entitiesReloadedEvent({ loadResults }) {
if (loadResults.isNoteContentReloaded(this.noteId)) {
const $noteSplit = $(`.note-split[data-ntx-id="${this.noteContext.ntxId}"]`);
this.$scrollingContainer = $noteSplit.children('.scrolling-container');
this.scrollHandler();
}
if (loadResults.getAttributeRows().find(attr => attr.noteId === this.noteId)) {
this.initEvent();
}
}
}
module.exports = jumpHistory;
function saveHistory(history) {
const keys = Object.keys(history);
while (keys.length > 100) {
const oldestKey = keys.shift(); // Get the oldest key
delete history[oldestKey]; // Delete the element corresponding to the key
}
api.runAsyncOnBackendWithManualTransactionHandling(async (historyNoteId, history) => {
const historyNote = await api.getNote(historyNoteId);
historyNote.setContent(JSON.stringify(history));
}, [historyNoteId, history]);
}