Skip to content

Commit

Permalink
https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8…
Browse files Browse the repository at this point in the history
…dd1e3b436235f73696eacf53/src/main.ts#L41

I don't think making static classes are necessary, you can just declare your variables and functions right in the root of the file and use them directly.

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L135
This is a bad idea to do here - you will block the app from loading and cause the app startup time to delay.
What you should do is run this asynchronously in another function, without await on the result.

Actually, ideally you can do this dynamically without having to reload the plugin

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L138
It seems that you're looking for the workspace.onLayoutReady function.

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L204
This function name is kinda misleading.

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L218
Here you should use getActiveViewOfType(MarkdownView) and test for truthiness instead of assuming that activeLeaf is not null.
Also, you should use view.editor instead of view.sourceMode.cmEditor as cmEditor is deprecated, unless you need specific APIs from CodeMirror 5 that's not available from Editor. Maybe you need to run npm update to get the latest definition files.

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L237
Result isn't returned. Why not just use await for these, instead of mixing async/await and .then()?

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/panel.ts#L29
This should just call redraw since every usage of it calls it right after.

https://github.com/cristianvasquez/obsidian-lab/blob/2497bff7046cbee8dd1e3b436235f73696eacf53/src/main.ts#L325
You're probably looking for Workspace.detachLeavesOfType (or getLeavesOfType) If not, you can just use leaf.getViewType instead of leaf.getViewState().type
  • Loading branch information
cristianvasquez committed Apr 13, 2021
1 parent 3dd3f20 commit 153fcfc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ plugin is the part that shows results in obsidian.

## Why this plugin? Why not program all in Javascript?

Sometimes, it is quicker to experiment in, for example, Python, and later, if you want, make it work in Javascript.
This is specially true when using Natural Language Processing algorithms.
Sometimes is quicker to experiment in Python and later, if you want, make it work in Javascript. This is especially true when using Natural Language Processing libraries

## Status

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-lab-plugin",
"name": "Lab plugin",
"version": "0.2.1",
"version": "0.2.2",
"minAppVersion": "0.11.6",
"description": "An interface to experiment with python scripts",
"author": "Cristian Vasquez",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-lab-plugin",
"version": "0.2.1",
"version": "0.2.2",
"description": "Obsidian lab plugin",
"main": "main.js",
"scripts": {
Expand Down
45 changes: 16 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ export default class PythonLabPlugin extends Plugin {

if (command.type=='panel') {
let viewCreator: ViewCreator = (leaf: WorkspaceLeaf) => {
let commandView = new ResultListView(
let panel = new ResultListView(
leaf,
commandId,
command.label,
);
const callbackWithView = async () => {
const data = await this.doPost(commandUrl)
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
data.label = command.label;
PythonLabPlugin.applyUpdatePanel(commandView, data);
// Update the state of the view panel
panel.setData(data);
panel.redraw();
}
this.addCommand({
id: commandId,
Expand All @@ -188,20 +189,26 @@ export default class PythonLabPlugin extends Plugin {
});

if (command.invokeOnOpen) {
commandView.registerCallback(callbackWithView);
panel.registerCallback(callbackWithView);
}
return commandView;
return panel;
};
// I would love to know if this view is already registered, but I don't know how.
this.registerView(commandId, viewCreator);
} else if (command.type=='insert-text' || command.type=='replace-text'){
const callbackWithoutView = () => {
const data = this.doPost(commandUrl)
const callbackWithoutView = async () => {
const data = await this.doPost(commandUrl)
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (command.type == 'replace-text' && activeView instanceof MarkdownView) {
PythonLabPlugin.applyReplaceText(activeView, data);
// Replaces the current selection
// const editor = activeView.sourceMode.cmEditor;
const editor = activeView.editor
editor.replaceSelection(data.contents);
} else if (command.type == 'insert-text' && activeView instanceof MarkdownView) {
PythonLabPlugin.applyInsertText(activeView, data);
// Insert content in the cursor position
let doc = activeView.editor.getDoc();
let cursor = doc.getCursor();
doc.replaceRange(data.contents, cursor);
} else {
console.error(`Cannot process: `, command);
}
Expand Down Expand Up @@ -260,26 +267,6 @@ export default class PythonLabPlugin extends Plugin {
return parameters;
}

private static applyUpdatePanel(commandView: CommandView, data: any) {
// Update the state of the view panel
commandView.setData(data);
commandView.redraw();
}

private static applyInsertText(activeView: MarkdownView, data: any) {
// Insert content in the cursor position
let doc = activeView.editor.getDoc();
let cursor = doc.getCursor();
doc.replaceRange(data.contents, cursor);
}

private static applyReplaceText(activeView: MarkdownView, data: any) {
// Replaces the current selection
// const editor = activeView.sourceMode.cmEditor;
const editor = activeView.editor
editor.replaceSelection(data.contents);
}

public async loadSettings(): Promise<void> {
this.settings = Object.assign(DEFAULT_SETTINGS, await super.loadData());
}
Expand Down
1 change: 1 addition & 0 deletions src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class LabPanel extends ItemView {
const openFile = this.app.workspace.getActiveFile();

const rootEl = createDiv({cls: 'nav-folder mod-root'});

// Label of the panel
if (this.state.label) {
const context = rootEl.createDiv({
Expand Down

0 comments on commit 153fcfc

Please sign in to comment.