Skip to content

Commit

Permalink
support import clipboard on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo committed May 25, 2022
1 parent 8e9b45c commit 9f7707f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 25 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ version = "0.1.0"
authors = ["Stephen Solka <[email protected]>"]
edition = "2018"

[build]
rustflags = ["--cfg=web_sys_unstable_apis"]

[target.wasm32-unknown-unknown]
rustflags = ["--cfg=web_sys_unstable_apis"]

[lib]
crate-type = ["cdylib"]

Expand Down Expand Up @@ -32,8 +38,10 @@ version = "^0"
default-features = false

[dependencies.web-sys]
version = "0.3.49"
version = "0.3.57"
features = [
'Window',
'console'
'console',
'Navigator',
'Clipboard'
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ everything below the --- will be replaced when calling extract

- **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
- **Import from Clipboard**: Extract content from url that is found in your clipboard and dump it at your cursor. Desktop only
- **Import from Clipboard**: Extract content from url that is found in your clipboard and dump it at your cursor.

# Youtube

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "extract-url",
"name": "Extract url content",
"version": "0.10.0",
"version": "0.11.0",
"description": "Extract url converting content into markdown",
"author": "Stephen Solka",
"authorUrl": "https://github.com/trashhalo",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"esbuild-dev": "esbuild index.js --platform=node --external:obsidian --external:electron --loader:.wasm=base64 --bundle --outfile=main.js",
"esbuild-release": "esbuild index.js --platform=node --external:obsidian --external:electron --loader:.wasm=base64 --bundle --outfile=main.js --minify",
"build": "wasm-pack build --target web && yarn esbuild-release",
"dev": "wasm-pack build --dev --target web && yarn esbuild-dev"
"build": "export RUSTFLAGS=--cfg=web_sys_unstable_apis && wasm-pack build --target web && yarn esbuild-release",
"dev": "export RUSTFLAGS=--cfg=web_sys_unstable_apis && wasm-pack build --dev --target web && yarn esbuild-dev"
},
"keywords": [],
"author": "",
Expand Down
24 changes: 22 additions & 2 deletions src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::obsidian;
use crate::request;
use crate::shim;
use crate::transform;
use js_sys::{Error, JsString, Promise};
use thiserror::Error;
Expand All @@ -9,6 +8,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::{future_to_promise, JsFuture};
use web_sys::console;
use web_sys::window;
use yaml_rust::emitter::{EmitError, YamlEmitter};
use yaml_rust::scanner::ScanError;

Expand Down Expand Up @@ -109,6 +109,12 @@ pub enum ExtractError {

#[error("error transforming content. {0}")]
Transform(#[from] transform::TransformError),

#[error("no clipboard available")]
NoClipboard,

#[error("no url in clipboard")]
NoClipboardContent,
}

impl std::convert::From<JsValue> for ExtractError {
Expand All @@ -127,6 +133,20 @@ impl std::convert::From<obsidian::View> for ExtractError {
}
}

async fn read_clipboard() -> Result<String, ExtractError> {
Ok(JsFuture::from(
window()
.ok_or(ExtractError::NoClipboard)?
.navigator()
.clipboard()
.ok_or(ExtractError::NoClipboard)?
.read_text(),
)
.await?
.as_string()
.ok_or(ExtractError::NoClipboardContent)?)
}

async fn extract_url(
plugin: &obsidian::Plugin,
title_only: bool,
Expand All @@ -140,7 +160,7 @@ async fn extract_url(
let view: obsidian::MarkdownView = md_view.dyn_into()?;
let editor = view.source_mode().cm_editor();
let url_str = if use_clipboard {
shim::clipboard_read_text()
read_clipboard().await?
} else {
editor.get_selection()
};
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use wasm_bindgen::prelude::*;
pub async fn onload(plugin: obsidian::Plugin) {
plugin.addCommand(JsValue::from(extract::command_extract_url()));
plugin.addCommand(JsValue::from(extract::command_extract_url_from_url()));
if *obsidian::DESKTOP {
plugin.addCommand(JsValue::from(extract::command_import_url()));
}
plugin.addCommand(JsValue::from(extract::command_import_url()));
plugin.addCommand(JsValue::from(archive::command_archive()));
}

Expand Down

0 comments on commit 9f7707f

Please sign in to comment.