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
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions src/KetcherReact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ const structServiceProvider = new StandaloneStructServiceProvider();

type Props = {
data: string;
onInit: (ketcher: Ketcher) => void;
onInit: (ketcher: Ketcher, subscriber: any) => void;
onChange: () => void;
};

const KetcherReact = ({ data, onInit }: Props) => {
const KetcherReact = ({ data, onInit, onChange }: Props) => {
return (
<div className="ketcher-container">
<Editor
errorHandler={(_message: string) => {}}
errorHandler={(_message: string) => { }}
staticResourcesUrl="./"
structServiceProvider={structServiceProvider}
onInit={(ketcher: Ketcher) => {
ketcher.setMolecule(data);
onInit(ketcher);
const subscriber = ketcher.editor.subscribe("change", operations => { onChange() })
onInit(ketcher, subscriber);
}}
/>
</div>
Expand Down
57 changes: 40 additions & 17 deletions src/ketView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,45 @@ export const VIEW_TYPE_KET = "ket-view";

export class KetView extends TextFileView {
ketcher: Ketcher;
subscriber: any;

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) {
this.ketcher?.editor.unsubscribe("change", this.subscriber);
Copy link
Contributor Author

@Acylation Acylation Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid calling unsubscribe() before editor initialize. (Will happen only when we open a new ketcher view)

ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
const container = this.containerEl.children[1];
ReactDOM.render(
<React.StrictMode>
<KetcherReact
data={this.data}
onInit={(ketcher: Ketcher, subscriber: any) => {
this.ketcher = ketcher;
// https://github.com/epam/ketcher/issues/2250
// @ts-ignore
global.ketcher = ketcher;
this.subscriber = subscriber;
}}
onChange={async () => {
this.data = await this.ketcher.getKet();
this.requestSave();
}}
/>
</React.StrictMode>,
container
);
}
// Updates the same file in other tabs/splits after `save()` is called
else {
this.ketcher.setMolecule(this.data);
}

}

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

getIcon() {
return "ketcher";
}

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

async onOpen() {
this.addAction("save", "Save", async (_eventType) => {
try {
Expand All @@ -54,6 +76,7 @@ export class KetView extends TextFileView {
}

async onClose() {
this.ketcher.editor.unsubscribe("change", this.subscriber);
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
}
}