generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
129 lines (115 loc) · 3.44 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { App, Editor, MarkdownView, Modal, Plugin } from "obsidian";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/browser";
import rubyWASM from "./assets/ruby.wasm";
export default class RubyWasmPlugin extends Plugin {
// Function to check if inside code block
isInCodeBlock = (editor: Editor, line: number) => {
const totalLines = editor.lineCount();
let inCodeBlock = true;
for (let i = line; i >= 0; i--) {
const lineText = editor.getLine(i).trim();
if (lineText.startsWith("```")) {
inCodeBlock = !inCodeBlock;
}
}
for (let i = line + 1; i < totalLines; i++) {
const lineText = editor.getLine(i).trim();
if (lineText.startsWith("```")) {
inCodeBlock = !inCodeBlock;
break;
}
}
return inCodeBlock;
};
async onload() {
const module = await WebAssembly.compile(rubyWASM);
const { vm } = await DefaultRubyVM(module);
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: "run-in-modal",
name: "Run in Modal",
editorCallback: (editor: Editor) => {
const code = editor.getSelection();
let result;
try {
result = vm.eval(code).toString();
} catch (e) {
result = e.toString();
}
// console.log(code);
new CodeModal(this.app, code, result).open();
},
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: "run-in-editor",
name: "Run in Editor",
editorCallback: (editor: Editor, view: MarkdownView) => {
const code = editor.getSelection();
let result;
try {
result = vm.eval(code).toString();
} catch (e) {
result = e.toString();
}
// console.log(code);
const cursorLine = editor.getCursor().line;
const insideCode = this.isInCodeBlock(editor, cursorLine);
if (insideCode) {
editor.replaceSelection(`${code}\n# => ${result}`);
} else {
editor.replaceSelection(
`${code}\n\`\`\`\n${result}\n\`\`\``
);
}
},
});
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
// this.registerDomEvent(document, "click", (evt: MouseEvent) => {
// console.log("click", evt);
// });
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
// this.registerInterval(
// window.setInterval(() => console.log("setInterval"), 5 * 60 * 1000)
// );
}
onunload() {}
}
class CodeModal extends Modal {
code: string;
result: string;
constructor(app: App, code: string, result: string) {
super(app);
this.code = code;
this.result = result;
}
async onOpen() {
const { contentEl } = this;
// contentEl.createEl("h1", { text: "Code" });
const codeBlock = contentEl.createEl("pre", {
cls: "language-ruby",
});
const codeElement = codeBlock.createEl("code", {
cls: "language-ruby",
});
codeElement.textContent = this.code || "code";
const resultElement = contentEl.createEl("div", {
cls: "ruby-output",
attr: { style: "white-space: pre-wrap;" },
});
resultElement.textContent = this.result || "result";
const closeButton = contentEl.createEl("button", {
cls: "modal-button",
attr: { type: "button" },
text: "Close",
});
closeButton.addEventListener("click", () => {
this.close();
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}