obsidian-paste-plugin/main.ts
baalajimaestro f4f278db6f
Allow it to be a command as well
Can keybind it this way

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
2022-12-10 19:23:46 +05:30

64 lines
1.4 KiB
TypeScript

import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
menu.addItem((item) => {
item
.setTitle("New Paste")
.setIcon("document")
.onClick(async () => {
this.createpaste();
});
});
})
);
this.addCommand({
id: "new-paste",
name: "Create a new paste",
callback: () => {
this.createpaste();
},
});
}
async createpaste(): Promise<void> {
const { vault } = this.app;
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < 6; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
const File = await vault.create("paste/"+result+".md", '');
let leaf = this.app.workspace.getLeaf(false);
await leaf.openFile(File);
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}