Refactorize some code and add links from hotkeys to encrypt modal

This commit is contained in:
Luis Alberto Jaramillo Gonzalez 2024-01-20 15:43:40 -05:00
parent b9a0f6f638
commit 6382c40430
No known key found for this signature in database
GPG key ID: 0444CB0073EDFBB5
5 changed files with 101 additions and 66 deletions

54
main.ts
View file

@ -1,19 +1,17 @@
import { App, Modal, Notice, Plugin } from 'obsidian';
import { GpgSettingsTab } from 'src/SettingsTab';
import { HotKeys } from 'src/HotKeys';
import { Plugin } from 'obsidian';
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
interface GpgEncryptSettings {
pgpExecPath: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
const DEFAULT_SETTINGS: GpgEncryptSettings = {
pgpExecPath: '/usr/local/bin/gpg'
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
export default class GpgEncryptPlugin extends Plugin {
settings: GpgEncryptSettings;
async onload() {
// Load settings variables
@ -23,29 +21,9 @@ export default class MyPlugin extends Plugin {
this.addSettingTab(new GpgSettingsTab(this.app, this));
// Add hotkeys
this.addCommand(HotKeys.GpgEncryptInline);
this.addCommand(HotKeys.GpgEncryptDocument);
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('This is a notice!');
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');
// 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));
let hotKeys: HotKeys = new HotKeys(this.app, this);
this.addCommand(hotKeys.GpgEncryptInline);
this.addCommand(hotKeys.GpgEncryptDocument);
}
onunload() {
@ -60,19 +38,3 @@ export default class MyPlugin extends Plugin {
await this.saveData(this.settings);
}
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}

67
src/EncryptModal.ts Normal file
View file

@ -0,0 +1,67 @@
import { getListPublicKey } from "gpg";
import { App, Editor, MarkdownView, Modal, Notice } from "obsidian";
import GpgEncryptPlugin from 'main';
// Enum to identify encrypt modal mode
export enum EncryptModalMode {
INLINE = "Inline",
DOCUMENT = "Document"
}
// Encrypt modal (Works for inline and document encryption)
export class EncryptModal extends Modal {
// Encrypt modal mode
private encryptMode: EncryptModalMode;
// Editor
private editor: Editor;
// Markdown View
private view: MarkdownView;
// Current plugin instance
plugin: GpgEncryptPlugin;
// Constructor of modal encrypt
constructor(app: App, plugin: GpgEncryptPlugin, mode: EncryptModalMode, editor: Editor, view: MarkdownView) {
super(app);
this.plugin = plugin;
this.encryptMode = mode;
this.editor = editor;
this.view = view;
}
// OnOpen Method
async onOpen() {
// Get an instance of this Element
const {contentEl} = this;
// A title div is created
contentEl.createEl("h1", { text: "Encrypt " + this.encryptMode });
// Check if encryption mode is InLine and Check if some text is not selected
if (this.encryptMode == EncryptModalMode.INLINE && !this.editor.somethingSelected()) {
// Show a user message that is mandatory select a text before
new Notice('❌ Select some text to encrypt');
// Close this modal
this.close();
}
// Check if encryption mode is Document and Check if editor is null
if (this.encryptMode == EncryptModalMode.DOCUMENT && !this.editor) {
// Show a user message that is mandatory have a document
new Notice('❌ Open a file to encrypt');
// Close this modal
this.close();
}
// Help text is created to select GPG keys
contentEl.createEl("p", { text: "Select which Public GPG key(s) you want to be able to decrypt the text:" });
// Get list of GPG public Keys
let gpgPublicKeys: { keyID: string; userID: string }[] = await getListPublicKey(this.plugin.settings.pgpExecPath);
// Iterate over each public key
gpgPublicKeys.forEach((gpgPublicKey) => {
// Add public key as new element in list
});
}
// OnClose Method
onClose() {
// Get an instance of this Element
const {contentEl} = this;
// Clear element
contentEl.empty();
}
}

View file

@ -1,21 +1,36 @@
import { Command } from "obsidian";
import { EncryptModal, EncryptModalMode } from "./EncryptModal";
import { App, Command, Editor, MarkdownView } from "obsidian";
import GpgEncryptPlugin from 'main';
// HotKeys class
export class HotKeys {
// Current app instance
private app: App;
// Current plugin instance
plugin: GpgEncryptPlugin;
// Constructor with App and GpgEncryptPlugin
constructor(app: App, plugin: GpgEncryptPlugin) {
this.app = app;
this.plugin = plugin;
}
// HotKey to Encrypt Inline
public static GpgEncryptInline: Command = {
public GpgEncryptInline: Command = {
id: 'gpg-encrypt-inline',
name: 'GPG encrypt inline',
callback: () => {
// todo: link modal to encrypt inline
icon: 'lock',
editorCallback: (editor: Editor, view: MarkdownView) => {
// Open Encrypt Modal in mode InLine
new EncryptModal(this.app, this.plugin, EncryptModalMode.INLINE, editor, view).open();
}
};
// HotKey to Encrypt Document
public static GpgEncryptDocument: Command = {
public GpgEncryptDocument: Command = {
id: 'gpg-encrypt-document',
name: 'GPG encrypt document',
callback: () => {
// todo: link modal to encrypt document
icon: 'lock',
editorCallback: (editor: Editor, view: MarkdownView) => {
// Open Encrypt Modal in mode Document
new EncryptModal(this.app, this.plugin, EncryptModalMode.DOCUMENT, editor, view).open();
}
};
}

View file

@ -1,6 +1,6 @@
import spawnGPG, { GpgResult, getListPublicKey } from 'gpg';
import { App, PluginSettingTab, Setting } from 'obsidian';
import MyPlugin from 'main';
import GpgEncryptPlugin from 'main';
let fs = require('fs');
// Enum of types of GPG executable path status
@ -17,9 +17,9 @@ enum GpgExecPathStatus {
// GPG Settings Tab Class
export class GpgSettingsTab extends PluginSettingTab {
// Current plugin instance
plugin: MyPlugin;
// Constructor with App and MyPlugin
constructor(app: App, plugin: MyPlugin) {
plugin: GpgEncryptPlugin;
// Constructor with App and GpgEncryptPlugin
constructor(app: App, plugin: GpgEncryptPlugin) {
super(app, plugin);
this.plugin = plugin;
}

View file

@ -1,12 +1,3 @@
/*
This CSS file will be included with your plugin, and
available in the app when your plugin is enabled.
If your plugin does not need CSS, delete this file.
*/
.text-color-orange {
color: orange;
}