From 38550ae7c1afeab87cb287015733e6d25d9ba939 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Thu, 30 May 2024 18:12:30 +0200 Subject: [PATCH] chore: copy to clipboard util --- src/views/TemplateBuilderModal.ts | 3 ++- src/views/TemplateBuilderView.ts | 14 ++------------ src/views/copyToClipboard.ts | 12 ++++++++++++ 3 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 src/views/copyToClipboard.ts diff --git a/src/views/TemplateBuilderModal.ts b/src/views/TemplateBuilderModal.ts index dbbaffbe..2e1df1c1 100644 --- a/src/views/TemplateBuilderModal.ts +++ b/src/views/TemplateBuilderModal.ts @@ -1,6 +1,7 @@ import { App, Modal } from "obsidian"; import { TemplateBuilderModel } from "./components/TemplateBuilder"; import TemplateBuilder from "./components/TemplateBuilder.svelte"; +import { copyToClipboard } from "./copyToClipboard"; /** * This class is just the minimum glue code to bind our core logic * with the svelte UI and obsidian API modal. @@ -23,7 +24,7 @@ export class TemplateBuilderModal extends Modal { const { contentEl } = this; this._component = new TemplateBuilder({ target: contentEl, - props: { model: this.deps }, + props: { model: this.deps, copyToClipboard }, }); } } diff --git a/src/views/TemplateBuilderView.ts b/src/views/TemplateBuilderView.ts index ad9ea33b..9c76a41b 100644 --- a/src/views/TemplateBuilderView.ts +++ b/src/views/TemplateBuilderView.ts @@ -1,19 +1,9 @@ -import { ItemView, Notice, ViewStateResult, WorkspaceLeaf } from "obsidian"; +import { ItemView, ViewStateResult, WorkspaceLeaf } from "obsidian"; import { FormDefinition } from "src/core/formDefinition"; import ModalFormPlugin from "src/main"; import { makeModel } from "./components/TemplateBuilder"; import TemplateBuilder from "./components/TemplateBuilder.svelte"; - -function copyToClipboard(text: string) { - navigator.clipboard.writeText(text).then( - () => { - new Notice("Template has been copied to the clipboard"); - }, - (err) => { - console.error("Could not copy text: ", err); - }, - ); -} +import { copyToClipboard } from "./copyToClipboard"; export const TEMPLATE_BUILDER_VIEW = "modal-form-template-builder-view"; /** diff --git a/src/views/copyToClipboard.ts b/src/views/copyToClipboard.ts new file mode 100644 index 00000000..053405c1 --- /dev/null +++ b/src/views/copyToClipboard.ts @@ -0,0 +1,12 @@ +import { Notice } from "obsidian"; + +export function copyToClipboard(text: string) { + navigator.clipboard.writeText(text).then( + () => { + new Notice("Template has been copied to the clipboard"); + }, + (err) => { + console.error("Could not copy text: ", err); + }, + ); +}