generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
317 lines (257 loc) · 9.1 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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting,
WorkspaceLeaf, TFile, SplitDirection, ObsidianProtocolData, View } from 'obsidian';
// Remember to rename these classes and interfaces!
declare module "obsidian" {
interface View {
file: TFile;
}
}
interface ParallelMarkdownSettings {
mySetting: string;
DIRECTION: SplitDirection;
CLOSE_OTHER: boolean;
}
const DEFAULT_SETTINGS: ParallelMarkdownSettings = {
mySetting: 'default',
DIRECTION: 'vertical',
CLOSE_OTHER: true
}
export default class ParalleMarkdownPlugin extends Plugin {
settings: ParallelMarkdownSettings;
leafRight: WorkspaceLeaf;
leafLeft: WorkspaceLeaf;
async onload() {
await this.loadSettings();
// add Parallel trigger icon
const ribbonIconEl = this.addRibbonIcon('check-check', 'Parallel', (evt: MouseEvent) => {
// Called when the user clicks the icon.
var ctx = this.app.workspace.getActiveViewOfType(MarkdownView) as MarkdownView;
if (!ctx) {
ctx = <MarkdownView>this.leafLeft?.view ?? null;
}
this.parallelLeftToRight(ctx.editor, ctx);
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Parallel Markdown editor plugin');
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new SampleModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
this.addCommand({
id: "translate-and-sync",
name: "Translate and sync Selection to right side",
editorCallback: (editor: Editor) => {
const selectedText = editor.getSelection();
const onSubmit = (text: string, url: string) => {
editor.replaceSelection(`[${text}](${url})`);
};
//new InsertLinkModal(this.app, selectedText, onSubmit).open();
},
});
this.addCommand({
id: "parallel-position",
name: "Parallel position",
editorCallback: (editor: Editor, ctx: MarkdownView) => {
this.parallelLeftToRight(editor, ctx);
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});
this.app.workspace.onLayoutReady(() => {
this.openSplitPaneView();
});
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
//this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
this.registerObsidianProtocolHandler(
'file-open',
this.actionHandler,
);
this.app.workspace.on("file-open", async (file) => {
if (!file) {
return
}
if(file.basename === "witte-en") {
console.log('Open file: ', file)
// await this.leafLeft.openFile(file, { state: { mode: 'source', active: true, focus: false } });
// this.app.workspace.setActiveLeaf(this.leafLeft);
// console.log("try to open parallel file");
// const parallelFile = this.app.vault.getAbstractFileByPath('witte-cn.md');
// if (parallelFile instanceof TFile) {
// console.log('parallel file is: ', parallelFile)
// await this.leafRight.openFile(parallelFile, { state: { mode: 'source', active: true, focus: false } });
// } else {
// // fileOrFolder is null or a TFolder... handle accordingly
// }
}
})
console.log('loading parallel markdown plugin finished.')
} // end of on load
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async actionHandler(params: ObsidianProtocolData): Promise<void> {
console.log("get a message:");
console.log(params.action);
if (params.action == 'file-open') {
console.log("try to open file");
console.log(params)
}
}
getMostRecentMDLeaf(): WorkspaceLeaf | null {
const mrl = this.app.workspace.getMostRecentLeaf();
if (mrl && mrl.view.getViewType() === 'markdown') {
return mrl;
} else {
return null;
}
}
//if it is left & right side panels, return
//else change the layout to left&right side panels
async openSplitPaneView() {
//TODO: temparally method to do panels check, is there a better way?
console.log("try to iterate all leaves");
var i: number = 0;
this.app.workspace.iterateAllLeaves((leaf) => {
//console.log("read a leaf: ", leaf.getViewState().type);
if (leaf.getViewState().type === "markdown") {
if (i==0) {
this.leafLeft = leaf;
} else {
this.leafRight = leaf;
}
i = i + 1;
}
});
if (i>=2) {
console.log("Already have left&right leaf, do nothing");
this.openParallelFiles();
return;
}
// split view
const srcLeaf: WorkspaceLeaf | null = this.getMostRecentMDLeaf();
//const srcView = <MarkdownView>srcLeaf?.view ?? null;
const newLeaf = this.app.workspace.getLeaf('split', this.settings.DIRECTION); // open a split window
this.app.workspace.setActiveLeaf(newLeaf);
if (srcLeaf) {
//srcLeaf.setGroupMember(newLeaf);// don't need to open the same file together
this.app.workspace.setActiveLeaf(srcLeaf);
}
if (srcLeaf) {
this.leafLeft = srcLeaf;
}
this.leafRight = newLeaf;
this.openParallelFiles();
}
async openParallelFiles() {
// choose parallel files
const files = await this.app.vault.getMarkdownFiles();
//const selectedEN = files.filter(file => file.name === "witte-en.md")[0];
//const selectedCN = files.filter(file => file.name === "witte-cn.md")[0];
//random choose two files to open
//TODO: should choose files according to history or file name pattern
const selectedEN = files[0];
const selectedCN = files[1];
console.log('selected file is: ', selectedEN.name)
await this.leafLeft.openFile(selectedEN, { state: { mode: 'source', active: true, focus: false } });
this.app.workspace.setActiveLeaf(this.leafLeft);
console.log('selected file is: ', selectedCN.name)
await this.leafRight.openFile(selectedCN, { state: { mode: 'source', active: true, focus: false } });
//this.app.workspace.setActiveLeaf(this.leafRight);
}
private parallelLeftToRight(editor: Editor, ctx: MarkdownView) {
console.log('command callback: parallelLeftToRight')
var sourceView: MarkdownView = <MarkdownView>this.leafLeft?.view ?? null;
var targetView: MarkdownView = <MarkdownView>this.leafRight?.view ?? null;
if (ctx === <MarkdownView>this.leafRight?.view) {
console.log("exchange source and target:");
targetView = sourceView;
sourceView = ctx;
}
//const sourceView = <MarkdownView>this.leafLeft?.view ?? null;
//const targetView = <MarkdownView>this.leafRight?.view ?? null;
if (sourceView && targetView) {
const file = this.app.workspace.getActiveFile();
console.log("TFile:", file);
// sync method 1
// const numberOfLines = sourceView.editor.getCursor().line;
// console.log("file length is:", numberOfLines);
// targetView.currentMode.applyScroll((numberOfLines - 1));
// sync method 2
var target = sourceView.editor.getCursor();
targetView.editor.setCursor(target);
} else {
console.log("This is not left & right view layout");
}
new Notice('success parallel markdown files');
}
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: ParalleMarkdownPlugin;
constructor(app: App, plugin: ParalleMarkdownPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Google Translate API key:')
.setDesc('Use for google translation:')
.addText(text => text
.setPlaceholder('Enter your secret API key')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Pdf to markdown API key:')
.setDesc('Translate pdf to markdown:')
.addText(text => text
.setPlaceholder('Enter your secret API key')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
}
}