Files
mize/apps/obsidian-plugin/plugin.ts

174 lines
4.2 KiB
TypeScript

import { Plugin, ItemView, TFile, Modal } from "obsidian";
import wasmBinary from "./rust_dist/ppc_bg.wasm";
import * as wasmNamespace from "./rust_dist/ppc";
import { c2vi_obsidian_canvas_patch } from "@ppc/marts";
import { task_obsidian_otask_parse } from "@ppc/marts";
class Mize {
jsInstance: wasmNamespace.JsMize;
jsInstancePtr: number;
wasmModule: any;
constructor(wasmModule: any, jsInstance: wasmNamespace.JsMize) {
this.jsInstance = jsInstance
this.jsInstancePtr = jsInstance.__wbg_ptr;
this.wasmModule = wasmModule
}
test() {
this.wasmModule.test_fn(this.jsInstance);
}
get_part_native(name: string) {
if (name == "obsidian") {
return this.obsidianPlugin
}
}
}
async function mize_init() {
// init the wasm_bindgen module
const wasmModule = await import("./rust_dist/ppc.js");
await wasmModule.default({ module_or_path: wasmBinary });
wasmModule.initSync();
const jsInstance = await wasmModule.new_js_instance(JSON.stringify({
auth: {
client: {
issuer: "https://auth.ppc.social",
client_id: "363768685120258051",
},
},
}));
jsInstance.init();
console.log("jsInstance: ", jsInstance)
let mize = new Mize(wasmModule, jsInstance);
console.log("jsInstance: ", jsInstance)
console.log("mize.jsInstance: ", mize.jsInstance)
// set global mize
window.mize = mize;
console.log("window.mize.jsInstance: ", window.mize.jsInstance)
return mize;
}
export default class MizeObsidianPlugin extends Plugin {
async onload() {
let mize = await mize_init();
console.log("mize.jsInstance: ", mize.jsInstance)
mize.wasmModule.part_obsidian_init(mize.jsInstance, this, this.app);
console.log("mize.jsInstance: ", mize.jsInstance)
mize.obsidianPlugin = this;
console.log("mize.jsInstance: ", mize.jsInstance)
// c2vi part which patches canvas movement
c2vi_obsidian_canvas_patch(mize);
task_obsidian_otask_parse(mize);
this.registerView("test-view", (leaf) => new TestView(leaf, this));
this.addCommand({
id: "mize-test-view",
name: "Mize TestView",
callback: async () => {
const leaf = this.app.workspace.getLeaf(true);
await leaf.setViewState({
type: "test-view",
active: true,
});
this.app.workspace.revealLeaf(leaf);
},
});
}
onunload() {
this.app.workspace.detachLeavesOfType("ppc-app-view");
}
showDeviceCode(code: string, verificationUri: string) {
const modal = new Modal(this.app);
modal.onOpen = () => {
const { contentEl } = modal;
contentEl.createEl("h2", { text: "Device Login" });
// URL
contentEl.createEl("p", {
text: "Open this URL:"
});
const urlContainer = contentEl.createDiv({
cls: "device-login-row"
});
const urlInput = urlContainer.createEl("input", {
type: "text",
value: verificationUri,
});
urlInput.readOnly = true;
urlInput.style.width = "100%";
urlContainer.createEl("button", {
text: "Copy"
}).onclick = async () => {
await navigator.clipboard.writeText(verificationUri);
new Notice("URL copied to clipboard");
};
// Code
contentEl.createEl("p", {
text: "Enter this code:"
});
const codeInput = contentEl.createEl("input", {
type: "text",
value: code,
});
codeInput.readOnly = true;
codeInput.style.width = "100%";
codeInput.select();
};
modal.open();
}
}
export class TestView extends ItemView {
constructor(leaf: WorkspaceLeaf, plugin: PPCObsidianPlugin) {
super(leaf);
this.plugin = plugin;
}
getViewType(): string {
return "ppc-app-view";
}
getDisplayText(): string {
return "PPC App";
}
override async onOpen() {
this.root = document.createElement("div");
this.root.id = "ppc-app-root";
this.containerEl.children[1].appendChild(this.root);
this.plugin.mize.wasmModule.show_main_ui();
}
override async onClose() {
this.root.remove();
}
}