generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
233 lines (197 loc) · 6.8 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
import {App, MarkdownView, Plugin, PluginSettingTab, Setting} from 'obsidian';
interface MyPluginSettings {
timoutduration: number;
rememberscroll: boolean;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
timoutduration: 10,
rememberscroll: true
}
// https://raw.githubusercontent.com/derwish-pro/obsidian-remember-cursor-position/master/main.ts
interface EphemeralState {
cursor?: {from: {ch: number
line: number
},
to: {
ch: number
line: number
}
},
scroll?: number
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
db: {[file_path: string]: EphemeralState;};
lastTime: number;
async onload() {
console.log('loading plugin autoview');
this.lastTime = 1;
this.db = {};
await this.loadSettings();
this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerCodeMirror((cm: CodeMirror.Editor) => {
console.log('codemirror', cm);
});
this.registerDomEvent(
document, 'keydown',
(evt: KeyboardEvent) => this.modeHandler(null, evt));
this.registerDomEvent(
document, 'mousedown',
this.modeHandler); // TODO: find better way to find interaction with
// editor
}
modeHandler(mouseevt?: MouseEvent, keyevt?: KeyboardEvent) {
var markdownLeaves2 = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownLeaves2.getMode() == 'preview') {
var curState = markdownLeaves2.getState();
curState.mode = 'source';
markdownLeaves2.setState(curState, theresult);
this.restoreEphemeralState();
}
this.lastTime++;
this.backtopreview(this.lastTime, markdownLeaves2);
}
checkEphemeralStateChanged() {
let fileName = this.app.workspace.getActiveFile()?.path;
// waiting for load new file
if (!fileName) return;
let st = this.getEphemeralState();
this.saveEphemeralState(st);
}
getEphemeralState(): EphemeralState {
// let state: EphemeralState =
// this.app.workspace.getActiveViewOfType(MarkdownView)?.getEphemeralState();
// //doesnt work properly
let state: EphemeralState = {};
state.scroll = Number(this.app.workspace.getActiveViewOfType(MarkdownView)
?.currentMode?.getScroll()
?.toFixed(4));
let editor = this.getEditor();
if (editor) {
let from = editor.getCursor('anchor');
let to = editor.getCursor('head');
if (from && to) {
state.cursor = {
from: {ch: from.ch, line: from.line},
to: {ch: to.ch, line: to.line}
}
}
}
return state;
}
private getEditor(): CodeMirror.Editor {
return this.app.workspace.getActiveViewOfType(MarkdownView)
?.sourceMode.cmEditor;
}
async saveEphemeralState(st: EphemeralState) {
let fileName = this.app.workspace.getActiveFile()?.path;
this.db[fileName] = st;
}
async backtopreview(a: number, markdownLeave: MarkdownView) {
await this.delay(this.settings.timoutduration * 1000);
if (a == this.lastTime) {
if (markdownLeave.getMode() == 'source') {
this.checkEphemeralStateChanged();
var curState = markdownLeave.getState();
curState.mode = 'preview';
markdownLeave.setState(curState, theresult);
}
}
}
onunload() {
console.log('unloading plugin');
}
setEphemeralState(state: EphemeralState) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (state.cursor) {
let editor = this.getEditor();
if (this.settings.rememberscroll) {
if (editor) {
editor.setSelection(
state.cursor.from, state.cursor.to, {scroll: false});
editor.focus();
}
if (view && state.scroll) {
view.sourceMode.applyScroll(state.scroll);
}
} else {
if (editor) {
editor.setSelection(
state.cursor.from, state.cursor.to, {scroll: true});
editor.focus();
}
}
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async delay(ms: number) {
if (ms > 10) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
async restoreEphemeralState() {
let fileName = this.app.workspace.getActiveFile()?.path;
if (fileName) {
let st = this.db[fileName];
if (st) {
// waiting for load note
let scroll: number;
for (let i = 0; i < 20; i++) {
scroll = this.app.workspace.getActiveViewOfType(MarkdownView)
?.currentMode?.getScroll();
if (scroll !== null) break;
await this.delay(10);
}
this.setEphemeralState(st);
}
}
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for AutoView.'});
new Setting(containerEl)
.setName('Timeout Duration')
.setDesc(
'The amount of time in seconds that the plugin waits after the last keystroke before switching over to preview mode. ')
.addText(
text =>
text.setPlaceholder('10')
.setValue(this.plugin.settings.timoutduration.toString())
.onChange(async (value) => {
if (Number(value) > 0) {
this.plugin.settings.timoutduration = Number(value);
await this.plugin.saveSettings();
} else {
this.plugin.settings.timoutduration = 10;
text.setValue('');
await this.plugin.saveSettings();
}
}));
new Setting(containerEl)
.setName('Scroll back to previous editing location')
.setDesc(
'Should the editor scroll back to it\'s previous location when switching from edit to preview mode? ')
.addToggle(
toggle => toggle.setValue(this.plugin.settings.rememberscroll)
.onChange(async (value) => {
this.plugin.settings.rememberscroll = value;
await this.plugin.saveSettings();
}));
}
}
function theresult(curState: any, tresult: any) {
// console.log(tresult);
}