-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
325 lines (299 loc) · 8.71 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
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
import { MarkdownView, View, Plugin, ButtonComponent } from "obsidian";
import { addPluginCommand } from "./src/command";
import { isPreview, isSource } from "./utils";
import { ScrollToTopSettingType } from "types";
import { ScrollToTopSettingTab, scrollToTopSetting } from "./src/setting";
import {
injectSurfingComponent,
isContainSurfingWebview,
} from "plugins/surfing";
const ROOT_WORKSPACE_CLASS = ".mod-vertical.mod-root";
// solve the problem of closing always focus new tab setting
let globalMarkdownView: MarkdownView | null = null;
export default class ScrollToTopPlugin extends Plugin {
// scroll to top settings
settings: ScrollToTopSettingType;
// manage popup window
windowSet: Set<Window> = new Set();
private scrollToCursor() {
const markdownView = this.getCurrentViewOfType();
if (markdownView) {
const editor = markdownView.editor;
const anchor = editor.getCursor("anchor");
const head = editor.getCursor("head");
// setCursor or setSelection are doing a scroll (not centered) if out of view.
// so with a timer the other scroll done, this scroll is ignored and of course cursor pos is updated
setTimeout(async () => {
editor.setSelection(anchor, head);
}, 200);
editor.scrollIntoView(
{
from: anchor,
to: head,
},
true
);
// get back the focus on activeLeaf before to set the cursor (after timer)
this.app.workspace.setActiveLeaf(markdownView!.leaf, {
focus: true,
});
} else if (isContainSurfingWebview(this.settings)) {
injectSurfingComponent(false);
}
}
private scrollToTop() {
const markdownView = this.getCurrentViewOfType();
if (markdownView) {
const preview = markdownView.previewMode;
if (isSource(markdownView)) {
const editor = markdownView.editor;
// cursor set to start
setTimeout(async () => {
editor.setCursor(0, 0);
}, 200);
// not limited to the start of the editor text as with editor.exec("goStart");
editor.scrollTo(0, 0);
this.app.workspace.setActiveLeaf(markdownView!.leaf, {
focus: true,
});
} else {
isPreview(markdownView) && preview.applyScroll(0);
}
} else if (isContainSurfingWebview(this.settings)) {
injectSurfingComponent(true);
}
}
private scrollToBottom = async () => {
const markdownView = this.getCurrentViewOfType();
if (markdownView) {
const file = this.app.workspace.getActiveFile()
const content = await (this.app as any).vault.cachedRead(file);
const lines = content.split('\n');
let numberOfLines = lines.length;
//in preview mode don't count empty lines at the end
if (markdownView.getMode() === 'preview') {
while (numberOfLines > 0 && lines[numberOfLines - 1].trim() === '') {
numberOfLines--;
}
}
markdownView.currentMode.applyScroll((numberOfLines - 1))
} else if (isContainSurfingWebview(this.settings)) {
injectSurfingComponent(false);
}
}
private createScrollElement(
config: {
id: string;
className: string;
icon: string;
curWindow?: Window;
tooltipConfig: {
showTooltip: boolean;
tooltipText: string;
};
},
fn: () => void
) {
let topWidget = createEl("div");
topWidget.setAttribute("class", `div-${config.className}`);
topWidget.setAttribute("id", config.id);
document.body.style.setProperty("--size-ratio", this.settings.resizeButton.toString());
let button = new ButtonComponent(topWidget);
button.setIcon(config.icon).setClass("buttonItem").onClick(fn);
if (config.tooltipConfig.showTooltip) {
button.setTooltip(config.tooltipConfig.tooltipText);
}
let curWindow = config.curWindow || window;
const markdownView = this.getCurrentViewOfType();
curWindow.document.body
.querySelector(ROOT_WORKSPACE_CLASS)
?.insertAdjacentElement("afterbegin", topWidget);
// fix bug when the leaf was pinned caused Obsidian stuck
if (!markdownView && !isContainSurfingWebview(this.settings)) {
topWidget.style.visibility = "hidden";
}
}
public removeButton(id: string, curWindow?: Window) {
let curWin = curWindow || window;
const element = curWin.activeDocument.getElementById(id);
if (element) {
element.remove();
}
}
public getCurrentViewOfType() {
// get the current active view
let markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
// To distinguish whether the current view is hidden or not markdownView
let currentView = this.app.workspace.getActiveViewOfType(View) as MarkdownView;
// solve the problem of closing always focus new tab setting
if (markdownView !== null) {
globalMarkdownView = markdownView;
} else {
// fix the plugin shutdown problem when the current view is not exist
if (currentView == null || currentView?.file?.extension == "md") {
markdownView = globalMarkdownView
}
}
return markdownView;
}
public createButton(window?: Window) {
const {
enabledScrollToTop,
enabledScrollToBottom,
enabledScrollToCursor,
iconScrollToTop,
iconScrollToBottom,
iconScrollToCursor,
showTooltip,
scrollTopTooltipText,
scrollBottomTooltipText,
scrollCursorTooltipText,
} = this.settings;
if (enabledScrollToTop) {
// create a button
this.createScrollElement(
{
id: "__C_scrollToTop",
className: "scrollToTop",
icon: iconScrollToTop,
curWindow: window,
tooltipConfig: {
showTooltip,
tooltipText: scrollTopTooltipText,
},
},
this.scrollToTop.bind(this)
);
}
if (enabledScrollToBottom) {
this.createScrollElement(
{
id: "__C_scrollToBottom",
className: "scrollToBottom",
icon: iconScrollToBottom,
curWindow: window,
tooltipConfig: {
showTooltip,
tooltipText: scrollBottomTooltipText,
},
},
this.scrollToBottom.bind(this)
);
}
if (enabledScrollToCursor) {
this.createScrollElement(
{
id: "__C_scrollToCursor",
className: "scrollToCursor",
icon: iconScrollToCursor,
curWindow: window,
tooltipConfig: {
showTooltip,
tooltipText: scrollCursorTooltipText,
},
},
this.scrollToCursor.bind(this)
);
}
}
toggleIconView() {
let BottomButton = activeDocument.querySelector(
".div-scrollToBottom"
) as HTMLElement;
let TopButton = activeDocument.querySelector(
".div-scrollToTop"
) as HTMLElement;
let CursorButton = activeDocument.querySelector(
".div-scrollToCursor"
) as HTMLElement;
const markdownView = this.getCurrentViewOfType();
if (!markdownView && !isContainSurfingWebview(this.settings)) {
if (BottomButton) BottomButton.style.visibility = "hidden";
if (TopButton) TopButton.style.visibility = "hidden";
if (CursorButton) CursorButton.style.visibility = "hidden";
} else {
if (BottomButton) BottomButton.style.visibility = "visible";
if (TopButton) TopButton.style.visibility = "visible";
if (markdownView && isSource(markdownView)) {
if (CursorButton) CursorButton.style.visibility = "visible";
} else {
if (CursorButton) CursorButton.style.visibility = "hidden";
}
}
}
async onload() {
await this.loadSettings();
this.addSettingTab(new ScrollToTopSettingTab(this.app, this));
this.app.workspace.onLayoutReady(() => {
this.createButton();
this.registerEvent(
this.app.workspace.on("file-open", () => {
this.toggleIconView();
})
)
// add popup window support
this.registerEvent(
this.app.workspace.on("window-open", (win, window) => {
this.windowSet.add(window);
this.createButton(window);
this.toggleIconView();
})
);
this.registerEvent(
this.app.workspace.on("window-close", (win, window) => {
this.windowSet.delete(window);
})
)
this.registerEvent(
this.app.workspace.on("layout-change", () => {
this.toggleIconView();
})
);
});
// expose plugin commands
addPluginCommand(
this,
"scroll-to-top",
"Scroll to Top",
this.scrollToTop.bind(this)
);
addPluginCommand(
this,
"scroll-to-bottom",
"Scroll to Bottom",
this.scrollToBottom.bind(this)
);
addPluginCommand(
this,
"scroll-to-cursor",
"Scroll to Cursor",
this.scrollToCursor.bind(this)
);
setTimeout(() => { //linked to commands added
this.app.workspace.trigger("css-change");
}, 300);
}
async saveSettings() {
await this.saveData(this.settings);
}
async loadSettings() {
this.settings = Object.assign(
{},
scrollToTopSetting,
await this.loadData()
);
}
onunload() {
this.removeButton("__C_scrollToTop");
this.removeButton("__C_scrollToBottom");
this.removeButton("__C_scrollToCursor");
// for popup window
if (this.windowSet.size > 0) {
this.windowSet.forEach((window) => {
this.removeButton("__C_scrollToTop", window);
this.removeButton("__C_scrollToBottom", window);
this.removeButton("__C_scrollToCursor", window);
});
}
}
}