From 676fc929c18715343b36ef3282f4f1102fe7d445 Mon Sep 17 00:00:00 2001 From: Stephen Solka Date: Sat, 10 Apr 2021 11:34:01 -0400 Subject: [PATCH] clipboard support --- README.md | 3 ++- manifest.json | 2 +- src/lib.rs | 31 ++++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9b83ff4..5eab8a5 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,5 @@ everything below the --- will be replaced when calling extract # Commands - __Extract__: Replace url or document with readable markdown extracted from the sites html content -- __Title Only__: Replace url or document with a markdown anchor with the title extracted from the page content \ No newline at end of file +- __Title Only__: Replace url or document with a markdown anchor with the title extracted from the page content +- __Import from Clipboard__: Extract content from url that is found in your clipboard and dump it at your cursor \ No newline at end of file diff --git a/manifest.json b/manifest.json index 2b39058..6feba6d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "extract-url", "name": "Extract url content", - "version": "0.6.0", + "version": "0.7.0", "description": "Extract url converting content into markdown", "author": "Stephen Solka", "authorUrl": "https://github.com/trashhalo", diff --git a/src/lib.rs b/src/lib.rs index bf1f48b..176dd13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod fetch; mod obsidian; mod transform; +mod electron; use js_sys::{Error, JsString, Promise}; use std::rc::Rc; use thiserror::Error; @@ -14,6 +15,7 @@ use yaml_rust::scanner::ScanError; #[wasm_bindgen] pub struct ExtractCommand { title_only: bool, + use_clipboard: bool, id: JsString, name: JsString, plugin: Rc, @@ -45,8 +47,9 @@ impl ExtractCommand { pub fn callback(&self) -> Promise { let plugin = self.plugin.clone(); let title_only = self.title_only; + let use_clipboard = self.use_clipboard; future_to_promise(async move { - let res = extract_url(&plugin, title_only).await; + let res = extract_url(&plugin, title_only, use_clipboard).await; if let Err(e) = res { let msg = format!("error: {}", e); obsidian::Notice::new(&msg); @@ -61,20 +64,30 @@ impl ExtractCommand { #[wasm_bindgen] pub fn onload(plugin: obsidian::Plugin) { let p = Rc::new(plugin); - let cmd = ExtractCommand { + let cmd1 = ExtractCommand { id: JsString::from("extract-url"), name: JsString::from("Extract"), plugin: p.clone(), title_only: false, + use_clipboard: false, }; - p.addCommand(JsValue::from(cmd)); - let cmd = ExtractCommand { + p.addCommand(JsValue::from(cmd1)); + let cmd2 = ExtractCommand { id: JsString::from("extract-title-from-url"), name: JsString::from("Title Only"), plugin: p.clone(), title_only: true, + use_clipboard: false, }; - p.addCommand(JsValue::from(cmd)) + p.addCommand(JsValue::from(cmd2)); + let cmd3 = ExtractCommand { + id: JsString::from("extract-url"), + name: JsString::from("Import From Clipboard"), + plugin: p.clone(), + title_only: false, + use_clipboard: true, + }; + p.addCommand(JsValue::from(cmd3)) } #[derive(Error, Debug)] @@ -117,7 +130,7 @@ impl std::convert::From for ExtractError { } } -async fn extract_url(plugin: &obsidian::Plugin, title_only: bool) -> Result<(), ExtractError> { +async fn extract_url(plugin: &obsidian::Plugin, title_only: bool, use_clipboard: bool) -> Result<(), ExtractError> { if let Some(md_view) = plugin .app() .workspace() @@ -125,7 +138,11 @@ async fn extract_url(plugin: &obsidian::Plugin, title_only: bool) -> Result<(), { let view: obsidian::MarkdownView = md_view.dyn_into()?; let editor = view.source_mode().cm_editor(); - let url_str = editor.get_selection(); + let url_str = if use_clipboard { + electron::clipboard_read_text() + } else { + editor.get_selection() + }; if url_str == "" { let (url_str, content) = extract_link_from_yaml(&view.get_view_data())?; let md = convert_url_to_markdown(title_only, url_str).await?;