-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
289 lines (233 loc) · 7.02 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
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
import { App, PluginSettingTab, Setting, Plugin } from "obsidian";
import { around } from "monkey-around";
import type moment from "moment";
declare global {
interface Window {
moment: typeof moment;
}
}
interface TimestampedCanvasSetting {
dateFormat: string;
}
const DEFAULT_SETTINGS: Partial<TimestampedCanvasSetting> = {
dateFormat: "YYYY-MM-DD HH:mm",
};
export class TimestampedCanvasSettingTab extends PluginSettingTab {
plugin: TimestampedCanvas;
constructor(app: App, plugin: TimestampedCanvas) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Date format")
.setDesc("Default date format")
.addText((text) =>
text
.setPlaceholder("YYYY-MM-DD HH:mm")
.setValue(this.plugin.settings.dateFormat)
.onChange(async (value) => {
this.plugin.settings.dateFormat = value;
await this.plugin.saveSettings();
})
);
}
}
export default class TimestampedCanvas extends Plugin {
settings: TimestampedCanvasSetting;
isHide: boolean = false;
async onload() {
await this.loadSettings();
this.patchCanvas();
this.patchCanvasNode();
this.patchCanvasEdge();
this.addSettingTab(new TimestampedCanvasSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
}
patchCanvas() {
const createTimestamp = () => {
return moment().format(this.settings.dateFormat)
}
const patchCanvas = () => {
const canvasView = app.workspace.getLeavesOfType("canvas").first()?.view;
// @ts-ignore
const canvas = canvasView?.canvas;
if (!canvasView) return false;
const plugin = this;
const canvasUninstaller = around(canvas.constructor.prototype, {
addNode(oldMethod) {
return function (...args) {
const node = args[0]
if (node['unknownData'] === undefined) {
node['unknownData'] = {}
}
node['unknownData'] = {'timestamp': createTimestamp()};
const result = oldMethod && oldMethod.apply(this, args);
return result;
}
},
addEdge(oldMethod) {
return function (...args) {
const edge = args[0]
if (edge['unknownData'] === undefined) {
edge['unknownData'] = {}
}
edge['unknownData'] = {'timestamp': createTimestamp()};
const result = oldMethod && oldMethod.apply(this, args);
return result;
}
},
showQuickSettingsMenu(oldMethod) {
return function (...args) {
const e = args[0]
e.addItem((e) => {
return e.setSection("canvas").setTitle("Hide/show timestamps").setIcon("lucide-eye-off").onClick( () => {
plugin.isHide = !plugin.isHide
this.nodes.forEach(node => {
if (plugin.isHide) {
node?.timestampEl.addClass('canvas-node-timestamp-hide')
} else {
node?.timestampEl.removeClass('canvas-node-timestamp-hide')
}
})
this.edges.forEach(edge => {
if (plugin.isHide) {
edge?.timestampEl.addClass('canvas-edge-timestamp-hide')
} else {
edge?.timestampEl.removeClass('canvas-edge-timestamp-hide')
}
})
})
})
const result = oldMethod && oldMethod.apply(this, args);
return result;
}
}
})
this.register(canvasUninstaller);
canvas?.view.leaf.rebuildView();
console.log("obsidian-timestamped-canvas: canvas view patched");
return true;
}
this.app.workspace.onLayoutReady(() => {
if (!patchCanvas()) {
const evt = app.workspace.on("layout-change", () => {
patchCanvas() && app.workspace.offref(evt);
});
this.registerEvent(evt);
}
});
}
patchCanvasEdge() {
const createTimestamp = () => {
return moment().format(this.settings.dateFormat)
}
const patchEdge = () => {
const canvasView = app.workspace.getLeavesOfType("canvas").first()?.view;
// @ts-ignore
const canvas = canvasView?.canvas;
if(!canvas) return false;
const edge = Array.from(canvas.edges).first();
if (!edge) return false;
// @ts-ignore
const edgeInstance = edge[1];
const edgeUninstaller = around(edgeInstance.constructor.prototype, {
render(oldMethod) {
return function (...args) {
const result = oldMethod && oldMethod.apply(this, args);
const coords = this.getCenter();
const div = this.timestampEl || this.canvas.canvasEl.createDiv("canvas-edge-timestamp");
div.style.transform = "translate(".concat(coords.x, "px, ").concat(coords.y, "px)")
this.timestampEl = div;
div.setText(this?.unknownData?.timestamp);
return
}
},
destroy(oldMethod) {
return function (...args) {
if (this.timestampEl) {
this.timestampEl.remove()
}
const result = oldMethod && oldMethod.apply(this, args);
}
}
})
this.register(edgeUninstaller);
console.log("timestamped-canvas: canvas edge patched");
return true;
}
this.app.workspace.onLayoutReady(() => {
if (!patchEdge()) {
const evt = app.workspace.on("layout-change", () => {
patchEdge() && app.workspace.offref(evt);
});
this.registerEvent(evt);
}
});
}
patchCanvasNode() {
const createTimestamp = () => {
return moment().format(this.settings.dateFormat)
}
const patchNode = () => {
const canvasView = app.workspace.getLeavesOfType("canvas").first()?.view;
// @ts-ignore
const canvas = canvasView?.canvas;
if(!canvas) return false;
const node = Array.from(canvas.nodes).first();
if (!node) return false;
// @ts-ignore
const nodeInstance = node[1];
const plugin = this;
const uninstaller = around(nodeInstance.constructor.prototype, {
render(oldMethod) {
return function (...args) {
const result = oldMethod && oldMethod.apply(this, args);
const div = this.timestampEl || this.nodeEl.createDiv("canvas-node-timestamp");
this.timestampEl = div;
div.setText(this?.unknownData?.timestamp);
return
}
},
showMenu(oldMethod) {
return function (...args) {
const e = args[0];
e.addItem(e => {
return e.setSection("canvas").setTitle('Clear timestamp').setIcon("lucide-alarm-minus").onClick(() => {
if (this?.unknownData?.timestamp) {
this['unknownData']['timestamp'] = ''
}
})
})
e.addItem(e => {
return e.setSection("canvas").setTitle('Update timestamp').setIcon("lucide-alarm-plus").onClick(() => {
this['unknownData']['timestamp'] = createTimestamp()
})
})
return oldMethod && oldMethod.apply(this, args);
}
}
});
this.register(uninstaller);
console.log("timestamped-canvas: canvas node patched");
return true;
}
this.app.workspace.onLayoutReady(() => {
if (!patchNode()) {
const evt = app.workspace.on("layout-change", () => {
patchNode() && app.workspace.offref(evt);
});
this.registerEvent(evt);
}
});
}
}