Minor code cleanups

This commit makes error messages when the native messenger is
unavailable easier to read. Since they're easier to read, there's no
need for custom errors in setclip/getclip anymore, provided that the
errors they throw are correctly logged. In order to make sure of that,
we remove the try/catch in excmds.ts:clipboard(), which should let
errors bubble up as needed.

I also noticed that while {set,get}Clipboard relied on the command line
being focused in order to work, they didn't do that themselves and
instead expected their callers to have set things up. This didn't make
sense to me so I moved the focusing code inside of {set,get}Clipboard.

This was all done while chasing the elusive #1135 but probably doesn't
change anything about it.
This commit is contained in:
glacambre 2018-11-07 06:13:43 +01:00
parent 9cf2c2f205
commit 7b5f7ef298
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
3 changed files with 58 additions and 72 deletions

View file

@ -51,7 +51,7 @@ async function sendNativeMsg(
return resp as MessageResp return resp as MessageResp
} catch (e) { } catch (e) {
if (!quiet) { if (!quiet) {
throw e throw new Error("Failed to send message to native messenger. If it is correctly installed (run `:native`), please report this bug on https://github.com/tridactyl/tridactyl/issues .")
} }
} }
} }

View file

@ -385,6 +385,7 @@ function applyWithTmpTextArea(fn) {
/** @hidden **/ /** @hidden **/
export async function setClipboard(content: string) { export async function setClipboard(content: string) {
await Messaging.messageOwnTab("commandline_content", "focus")
applyWithTmpTextArea(scratchpad => { applyWithTmpTextArea(scratchpad => {
scratchpad.value = content scratchpad.value = content
scratchpad.select() scratchpad.select()
@ -394,20 +395,21 @@ export async function setClipboard(content: string) {
} else throw "Failed to copy!" } else throw "Failed to copy!"
}) })
// Return focus to the document // Return focus to the document
Messaging.messageOwnTab("commandline_content", "hide") await Messaging.messageOwnTab("commandline_content", "hide")
Messaging.messageOwnTab("commandline_content", "blur") return Messaging.messageOwnTab("commandline_content", "blur")
} }
/** @hidden **/ /** @hidden **/
export function getClipboard() { export async function getClipboard() {
await Messaging.messageOwnTab("commandline_content", "focus")
const result = applyWithTmpTextArea(scratchpad => { const result = applyWithTmpTextArea(scratchpad => {
scratchpad.focus() scratchpad.focus()
document.execCommand("Paste") document.execCommand("Paste")
return scratchpad.textContent return scratchpad.textContent
}) })
// Return focus to the document // Return focus to the document
Messaging.messageOwnTab("commandline_content", "hide") await Messaging.messageOwnTab("commandline_content", "hide")
Messaging.messageOwnTab("commandline_content", "blur") await Messaging.messageOwnTab("commandline_content", "blur")
return result return result
} }

View file

@ -2487,8 +2487,8 @@ export async function get_current_url() {
* Copy content to clipboard without feedback. Use `clipboard yank` for interactive use. * Copy content to clipboard without feedback. Use `clipboard yank` for interactive use.
*/ */
//#background //#background
export async function yank(...content: string[]) { export function yank(...content: string[]) {
await setclip(content.join(" ")) return setclip(content.join(" "))
} }
/** /**
@ -2501,16 +2501,8 @@ async function setclip(str) {
// Functions to avoid retyping everything everywhere // Functions to avoid retyping everything everywhere
// Note: We're using fillcmdline here because exceptions are somehow not caught. We're rethrowing because otherwise the error message will be overwritten with the "yank successful" message. // Note: We're using fillcmdline here because exceptions are somehow not caught. We're rethrowing because otherwise the error message will be overwritten with the "yank successful" message.
let s = () => let s = () => Native.clipboard("set", str)
Native.clipboard("set", str).catch(e => { let c = () => messageActiveTab("commandline_frame", "setClipboard", [str])
let msg = "# Failed to set X selection. Is the native messenger installed and is it >=v.0.1.7?"
fillcmdline(msg)
throw msg
})
let c = async () => {
await messageActiveTab("commandline_content", "focus")
await messageActiveTab("commandline_frame", "setClipboard", [str])
}
let promises = [] let promises = []
switch (await config.getAsync("yankto")) { switch (await config.getAsync("yankto")) {
@ -2524,7 +2516,7 @@ async function setclip(str) {
promises = [s(), c()] promises = [s(), c()]
break break
} }
return await Promise.all(promises) return Promise.all(promises)
} }
/** /**
@ -2537,11 +2529,7 @@ async function getclip() {
if ((await config.getAsync("putfrom")) == "clipboard") { if ((await config.getAsync("putfrom")) == "clipboard") {
return messageActiveTab("commandline_frame", "getClipboard") return messageActiveTab("commandline_frame", "getClipboard")
} else { } else {
return Native.clipboard("get", "").catch(e => { return Native.clipboard("get", "")
let msg = "# Failed to get X selection. Is the native messenger installed?"
fillcmdline(msg)
throw msg
})
} }
} }
@ -2571,7 +2559,6 @@ export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcano
let content = toYank.join(" ") let content = toYank.join(" ")
let url = "" let url = ""
let urls = [] let urls = []
try {
switch (excmd) { switch (excmd) {
case "yankshort": case "yankshort":
urls = await geturlsforlinks("rel", "shortlink") urls = await geturlsforlinks("rel", "shortlink")
@ -2619,9 +2606,6 @@ export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcano
// todo: maybe we should have some common error and error handler // todo: maybe we should have some common error and error handler
throw new Error(`[clipboard] unknown excmd: ${excmd}`) throw new Error(`[clipboard] unknown excmd: ${excmd}`)
} }
} catch (e) {
logger.error(e)
}
} }
/** Change active tab. /** Change active tab.