Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Auto save #10

Merged
merged 8 commits into from
Aug 21, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions src/ketView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ketcher } from "ketcher-core";
import { Notice, TextFileView } from "obsidian";
import { Notice, TFile, TextFileView } from "obsidian";
import React from "react";
import ReactDOM from "react-dom";
import KetcherReact from "./KetcherReact";
Expand All @@ -8,31 +8,39 @@ export const VIEW_TYPE_KET = "ket-view";

export class KetView extends TextFileView {
ketcher: Ketcher;
interval: number;

getViewData() {
return this.data;
}

// If clear is set, then it means we're opening a completely different file.
setViewData(data: string, _clear: boolean) {
this.data = data;

ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
const container = this.containerEl.children[1];
ReactDOM.render(
<React.StrictMode>
<KetcherReact
data={this.data}
onInit={(ketcher: Ketcher) => {
this.ketcher = ketcher;
// https://github.com/epam/ketcher/issues/2250
// @ts-ignore
global.ketcher = ketcher;
}}
/>
</React.StrictMode>,
container
);
// If clear is set, then it means we're opening a completely different file.
if (_clear) {
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
const container = this.containerEl.children[1];
ReactDOM.render(
<React.StrictMode>
<KetcherReact
data={this.data}
onInit={(ketcher: Ketcher) => {
this.ketcher = ketcher;
// https://github.com/epam/ketcher/issues/2250
// @ts-ignore
global.ketcher = ketcher;
}}
/>
</React.StrictMode>,
container
);
}
// Update the same file in other tabs/splits after `save()` is called
else {
this.ketcher.setMolecule(this.data);
}

}

clear() {}
Expand All @@ -41,6 +49,29 @@ export class KetView extends TextFileView {
return VIEW_TYPE_KET;
}

getIcon() {
return "ketcher";
}

getDisplayText() {
return this.file?.basename ?? "ketcher";
}

async onLoadFile(file: TFile) {
window.clearInterval(this.interval);
this.interval = window.setInterval(
async () => {
try {
this.data = await this.ketcher.getKet();
await this.save(); // will call `getViewData`
} catch (error) {
new Notice(error);
}
}, 2 * 1000
)
return super.onLoadFile(file);
}

async onOpen() {
this.addAction("save", "Save", async (_eventType) => {
try {
Expand All @@ -55,5 +86,6 @@ export class KetView extends TextFileView {

async onClose() {
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
window.clearInterval(this.interval)
}
}