AT protocol bookmarking platforms in obsidian
at client-cache 94 lines 2.3 kB view raw
1import { Modal, Notice } from "obsidian"; 2import type ATmarkPlugin from "../main"; 3import { createTag } from "../lib"; 4 5export class CreateTagModal extends Modal { 6 plugin: ATmarkPlugin; 7 onSuccess?: () => void; 8 9 constructor(plugin: ATmarkPlugin, onSuccess?: () => void) { 10 super(plugin.app); 11 this.plugin = plugin; 12 this.onSuccess = onSuccess; 13 } 14 15 onOpen() { 16 const { contentEl } = this; 17 contentEl.empty(); 18 contentEl.addClass("atmark-modal"); 19 20 contentEl.createEl("h2", { text: "New tag" }); 21 22 if (!this.plugin.client) { 23 contentEl.createEl("p", { text: "Not connected." }); 24 return; 25 } 26 27 const form = contentEl.createEl("form", { cls: "atmark-form" }); 28 29 const tagGroup = form.createEl("div", { cls: "atmark-form-group" }); 30 tagGroup.createEl("label", { text: "Tag", attr: { for: "tag-value" } }); 31 const tagInput = tagGroup.createEl("input", { 32 type: "text", 33 cls: "atmark-input", 34 attr: { id: "tag-value", placeholder: "Tag name", required: "true" }, 35 }); 36 37 const actions = form.createEl("div", { cls: "atmark-modal-actions" }); 38 39 const cancelBtn = actions.createEl("button", { 40 text: "Cancel", 41 cls: "atmark-btn atmark-btn-secondary", 42 type: "button", 43 }); 44 cancelBtn.addEventListener("click", () => this.close()); 45 46 const createBtn = actions.createEl("button", { 47 text: "Create", 48 cls: "atmark-btn atmark-btn-primary", 49 type: "submit", 50 }); 51 52 form.addEventListener("submit", (e) => { 53 e.preventDefault(); 54 void this.handleSubmit(tagInput, createBtn); 55 }); 56 57 tagInput.focus(); 58 } 59 60 private async handleSubmit( 61 tagInput: HTMLInputElement, 62 createBtn: HTMLButtonElement 63 ) { 64 const value = tagInput.value.trim(); 65 if (!value) { 66 new Notice("Please enter a tag name"); 67 return; 68 } 69 70 createBtn.disabled = true; 71 createBtn.textContent = "Creating..."; 72 73 try { 74 await createTag( 75 this.plugin.client, 76 this.plugin.settings.identifier, 77 value 78 ); 79 80 new Notice(`Created tag "${value}"`); 81 this.close(); 82 this.onSuccess?.(); 83 } catch (err) { 84 const message = err instanceof Error ? err.message : String(err); 85 new Notice(`Failed to create tag: ${message}`); 86 createBtn.disabled = false; 87 createBtn.textContent = "Create"; 88 } 89 } 90 91 onClose() { 92 this.contentEl.empty(); 93 } 94}