Files
note2any/src/widgets-modal.ts
2025-10-08 09:18:20 +08:00

60 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 文件widgets-modal.ts
* 作用:组件 / 插件片段配置弹窗。
*/
import { App, Modal, MarkdownView } from "obsidian";
import { uevent } from "./utils";
export class WidgetsModal extends Modal {
listener: any = null;
url: string = 'https://widgets.sunboshi.tech';
constructor(app: App) {
super(app);
}
insertMarkdown(markdown: string) {
const editor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
if (!editor) return;
editor.replaceSelection(markdown);
editor.exec("goRight");
uevent('insert-widgets');
}
onOpen() {
let { contentEl, modalEl } = this;
modalEl.style.width = '640px';
modalEl.style.height = '500px';
const iframe = contentEl.createEl('iframe', {
attr: {
src: this.url,
width: '100%',
height: '100%',
allow: 'clipboard-read; clipboard-write',
},
});
iframe.style.border = 'none';
this.listener = this.handleMessage.bind(this);
window.addEventListener('message', this.listener);
uevent('open-widgets');
}
handleMessage(event: MessageEvent) {
if (event.origin === this.url) {
const { type, data } = event.data;
if (type === 'cmd') {
this.insertMarkdown(data);
}
}
}
onClose() {
if (this.listener) {
window.removeEventListener('message', this.listener);
}
let { contentEl } = this;
contentEl.empty();
}
}