Skip to content

Commit 80472b5

Browse files
committed
Add Txt view, use TxtView for .txt and .xml files
1 parent 0ef0f1c commit 80472b5

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "data-files-loader",
33
"name": "Data files loader",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"minAppVersion": "0.15.0",
66
"description": "Plugin to load data files like txt, xml, json",
77
"author": "ZukTol",

src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const VIEW_TYPE_JSON = "json";
22
export const VIEW_TYPE_XML = "xml";
3+
export const VIEW_TYPE_TXT = "txt";
34
export const VIEW_TYPE_MARKDOWN = "markdown";
45

56
export const EXT_JSON = "json";

src/main.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Plugin, TFile, WorkspaceLeaf } from 'obsidian';
22
import LoaderSettingTab from './loader-settings-tab';
3-
import { VIEW_TYPE_JSON, VIEW_TYPE_XML, VIEW_TYPE_MARKDOWN, EXT_JSON, EXT_XML, EXT_TXT } from './constants'
4-
import JsonView from "./JsonView";
3+
import * as constants from './constants'
54
import { path } from "./utils";
5+
import JsonView from "./views/JsonView";
6+
import TxtView from "./views/TxtView";
67

78
interface LoaderPluginSettings {
89
doLoadTxt: boolean;
@@ -43,29 +44,31 @@ export default class LoaderPlugin extends Plugin {
4344
}
4445

4546
private TryRegisterTxt() {
46-
if (this.settings.doLoadTxt)
47-
this.registerExtensions([EXT_TXT], VIEW_TYPE_MARKDOWN);
47+
if (this.settings.doLoadTxt) {
48+
this.registerView(constants.VIEW_TYPE_TXT, (leaf: WorkspaceLeaf) => new TxtView(leaf, this));
49+
this.registerExtensions([constants.EXT_TXT], constants.VIEW_TYPE_TXT);
50+
}
4851

4952
if (this.settings.doCreateTxt)
50-
this.registerContextMenuCommand(EXT_TXT);
53+
this.registerContextMenuCommand(constants.EXT_TXT);
5154
}
5255

5356
private tryRegisterJson() {
5457
if (this.settings.doLoadTxt) {
55-
this.registerExtensions([EXT_JSON], VIEW_TYPE_JSON);
56-
this.registerView(VIEW_TYPE_JSON, (leaf: WorkspaceLeaf) => new JsonView(leaf, this));
58+
this.registerView(constants.VIEW_TYPE_JSON, (leaf: WorkspaceLeaf) => new JsonView(leaf, this));
59+
this.registerExtensions([constants.EXT_JSON], constants.VIEW_TYPE_JSON);
5760
}
5861

5962
if(this.settings.doCreateJson)
60-
this.registerContextMenuCommand(EXT_JSON);
63+
this.registerContextMenuCommand(constants.EXT_JSON);
6164
}
6265

6366
private tryRegisterXml() {
6467
if (this.settings.doLoadXml)
65-
this.registerExtensions([EXT_XML], VIEW_TYPE_MARKDOWN);
68+
this.registerExtensions([constants.EXT_XML], constants.VIEW_TYPE_TXT);
6669

6770
if (this.settings.doCreateXml) {
68-
this.registerContextMenuCommand(EXT_XML);
71+
this.registerContextMenuCommand(constants.EXT_XML);
6972
}
7073
}
7174

src/JsonView.ts src/views/JsonView.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { TextFileView, WorkspaceLeaf } from "obsidian";
22
import { basicSetup, EditorView } from "codemirror";
33
import { json } from "@codemirror/lang-json";
44
import { EditorState } from "@codemirror/state";
5-
import { VIEW_TYPE_JSON } from './constants'
6-
import LoaderPlugin from "./main";
5+
import { VIEW_TYPE_JSON } from '../constants'
6+
import LoaderPlugin from "../main";
77

88
export default class JsonView extends TextFileView {
99

src/views/TxtView.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { TextFileView, WorkspaceLeaf } from "obsidian";
2+
import { basicSetup, EditorView } from "codemirror";
3+
import { EditorState } from "@codemirror/state";
4+
import { VIEW_TYPE_TXT } from '../constants'
5+
import LoaderPlugin from "../main";
6+
7+
export default class TxtView extends TextFileView {
8+
9+
public plugin: LoaderPlugin;
10+
private cmEditor: EditorView;
11+
private editorEl: any;
12+
13+
constructor(leaf: WorkspaceLeaf, plugin: LoaderPlugin) {
14+
super(leaf);
15+
this.plugin = plugin;
16+
}
17+
18+
onload(): void {
19+
super.onload();
20+
this.editorEl = this.contentEl.createDiv("mod-cm5");
21+
this.cmEditor = new EditorView({
22+
state: EditorState.create({
23+
extensions: [
24+
basicSetup,
25+
],
26+
}),
27+
parent: this.editorEl,
28+
})
29+
this.app.workspace.trigger("codemirror", this.cmEditor);
30+
}
31+
32+
// gets the title of the document
33+
getDisplayText(): string {
34+
if (this.file) {
35+
return this.file.basename;
36+
}
37+
return "NOFILE";
38+
}
39+
40+
clear(): void {
41+
}
42+
43+
getViewData(): string {
44+
return this.cmEditor.state.doc.toString();
45+
}
46+
47+
getViewType(): string {
48+
return VIEW_TYPE_TXT;
49+
}
50+
51+
onClose(): Promise<void> {
52+
return super.onClose();
53+
}
54+
55+
setViewData(data: string, clear: boolean): void {
56+
this.cmEditor.dispatch({ changes: { from: 0, to: this.cmEditor.state.doc.length, insert: data } })
57+
}
58+
}

versions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"1.0.0": "0.15.0",
33
"1.0.1": "0.15.0",
4-
"1.0.2": "0.15.0"
4+
"1.0.2": "0.15.0",
5+
"1.0.3": "0.15.0"
56
}

0 commit comments

Comments
 (0)