-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-view.ts
60 lines (50 loc) · 1.31 KB
/
json-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
import { TextFileView, WorkspaceLeaf } from "obsidian";
import { basicSetup, EditorView } from "codemirror";
import { json } from "@codemirror/lang-json";
import { EditorState } from "@codemirror/state";
import { VIEW_TYPE_JSON } from '../constants'
import LoaderPlugin from "../main";
export default class JsonView 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();
this.editorEl = this.contentEl.createDiv("mod-cm5");
this.cmEditor = new EditorView({
state: EditorState.create({
extensions: [
basicSetup,
json()
],
}),
parent: this.editorEl,
})
this.app.workspace.trigger("codemirror", this.cmEditor);
}
// 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_JSON;
}
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 } })
}
}