-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt-view.ts
71 lines (60 loc) · 1.71 KB
/
txt-view.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
import { TextFileView, WorkspaceLeaf } from "obsidian";
import { basicSetup, EditorView, minimalSetup } from "codemirror";
import { EditorState, Extension } from "@codemirror/state";
import { VIEW_TYPE_TXT } from '../constants'
import LoaderPlugin from "../main";
import { lineNumbers } from "@codemirror/view";
import { ConvertToViewSetting } from "../helpers/view-settings-converter";
export default class TxtView extends TextFileView {
public plugin: LoaderPlugin;
private cmEditor: EditorView;
private editorEl: any;
constructor(leaf: WorkspaceLeaf, plugin: LoaderPlugin) {
super(leaf);
this.plugin = plugin;
}
onload(): void {
super.onload();
const basicExtensions = this.getExtensions();
this.editorEl = this.contentEl.createDiv("mod-cm5");
this.cmEditor = new EditorView({
state: EditorState.create({
extensions: [
minimalSetup,
...basicExtensions
],
}),
parent: this.editorEl,
})
this.app.workspace.trigger("codemirror", this.cmEditor);
}
getExtensions(): Extension[] {
const settings = this.plugin.settings;
const viewSettings = ConvertToViewSetting(settings.txtSetting, this.app.vault);
if(viewSettings.showLineNumber)
return [lineNumbers()];
else
return [];
}
// gets the title of the document
getDisplayText(): string {
if (this.file) {
return this.file.basename;
}
return "NOFILE";
}
clear(): void {
}
getViewData(): string {
return this.cmEditor.state.doc.toString();
}
getViewType(): string {
return VIEW_TYPE_TXT;
}
onClose(): Promise<void> {
return super.onClose();
}
setViewData(data: string, clear: boolean): void {
this.cmEditor.dispatch({ changes: { from: 0, to: this.cmEditor.state.doc.length, insert: data } })
}
}