mirror of
https://github.com/vale981/tridactyl
synced 2025-03-06 10:01:39 -05:00
Update yank to use Clipboard API
This commit is contained in:
parent
26981ec0e1
commit
1d59231c21
2 changed files with 31 additions and 45 deletions
|
@ -326,6 +326,7 @@ export function fillcmdline(
|
|||
*
|
||||
* Useful for document.execCommand
|
||||
**/
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
function applyWithTmpTextArea(fn) {
|
||||
let textarea
|
||||
try {
|
||||
|
@ -342,35 +343,6 @@ function applyWithTmpTextArea(fn) {
|
|||
}
|
||||
}
|
||||
|
||||
/** @hidden **/
|
||||
export async function setClipboard(content: string) {
|
||||
await Messaging.messageOwnTab("commandline_content", "focus")
|
||||
applyWithTmpTextArea(scratchpad => {
|
||||
scratchpad.value = content
|
||||
scratchpad.select()
|
||||
// This can return false spuriously so just ignore its return value
|
||||
document.execCommand("Copy")
|
||||
logger.info("set clipboard:", scratchpad.value)
|
||||
})
|
||||
// Return focus to the document
|
||||
await Messaging.messageOwnTab("commandline_content", "hide")
|
||||
return Messaging.messageOwnTab("commandline_content", "blur")
|
||||
}
|
||||
|
||||
/** @hidden **/
|
||||
export async function getClipboard() {
|
||||
await Messaging.messageOwnTab("commandline_content", "focus")
|
||||
const result = applyWithTmpTextArea(scratchpad => {
|
||||
scratchpad.focus()
|
||||
document.execCommand("Paste")
|
||||
return scratchpad.textContent
|
||||
})
|
||||
// Return focus to the document
|
||||
await Messaging.messageOwnTab("commandline_content", "hide")
|
||||
await Messaging.messageOwnTab("commandline_content", "blur")
|
||||
return result
|
||||
}
|
||||
|
||||
/** @hidden **/
|
||||
export function getContent() {
|
||||
return commandline_state.clInput.value
|
||||
|
|
|
@ -154,7 +154,6 @@ import "@src/lib/number.mod"
|
|||
import * as BGSELF from "@src/.excmds_background.generated"
|
||||
import { CmdlineCmds as BgCmdlineCmds } from "@src/background/commandline_cmds"
|
||||
import { EditorCmds as BgEditorCmds } from "@src/background/editor"
|
||||
import { messageActiveTab } from "@src/lib/messaging"
|
||||
import { EditorCmds } from "@src/background/editor"
|
||||
import { firefoxVersionAtLeast } from "@src/lib/webext"
|
||||
import { parse_bind_args, modeMaps } from "@src/lib/binding"
|
||||
|
@ -3089,48 +3088,63 @@ export function yank(...content: string[]) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Copies a string to the clipboard/selection buffer depending on the user's preferences
|
||||
* Copies a string to the clipboard/selection buffer depending on the user's preferences.
|
||||
*
|
||||
* @hidden
|
||||
*/
|
||||
//#background_helper
|
||||
async function setclip(str) {
|
||||
// Functions to avoid retyping everything everywhere
|
||||
async function setclip(data: string) {
|
||||
// Function to avoid retyping everything everywhere
|
||||
const setclip_selection = () => Native.clipboard("set", data)
|
||||
|
||||
// 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.
|
||||
const s = () => Native.clipboard("set", str)
|
||||
const c = () => messageActiveTab("commandline_frame", "setClipboard", [str])
|
||||
|
||||
let promises = []
|
||||
let promises: Array<Promise<any>> = []
|
||||
switch (await config.getAsync("yankto")) {
|
||||
case "selection":
|
||||
promises = [s()]
|
||||
promises = [setclip_selection()]
|
||||
break
|
||||
case "clipboard":
|
||||
promises = [c()]
|
||||
promises = [setclip_api(data)]
|
||||
break
|
||||
case "both":
|
||||
promises = [s(), c()]
|
||||
promises = [setclip_selection(), setclip_api(data)]
|
||||
break
|
||||
}
|
||||
return Promise.all(promises)
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a string to the clipboard using the Clipboard API.
|
||||
* @hidden
|
||||
*/
|
||||
//#background_helper
|
||||
async function setclip_api(data: string) {
|
||||
return window.navigator.clipboard.writeText(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the content of the clipboard/selection buffer depending on user's preferences
|
||||
*
|
||||
* Exposed for use with [[composite]], e.g. `composite getclip | fillcmdline`
|
||||
*/
|
||||
//#background
|
||||
export async function getclip(fromm?: "clipboard" | "selection") {
|
||||
if (fromm === undefined) fromm = await config.getAsync("putfrom")
|
||||
if (fromm === "clipboard") {
|
||||
return messageActiveTab("commandline_frame", "getClipboard")
|
||||
export async function getclip(from?: "clipboard" | "selection") {
|
||||
if (from === undefined) from = await config.getAsync("putfrom")
|
||||
if (from === "clipboard") {
|
||||
return getclip_api()
|
||||
} else {
|
||||
return Native.clipboard("get", "")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the clipboard content using the Clipboard API.
|
||||
* @hidden
|
||||
*/
|
||||
//#background_helper
|
||||
async function getclip_api() {
|
||||
return window.navigator.clipboard.readText()
|
||||
}
|
||||
|
||||
/** Use the system clipboard.
|
||||
|
||||
If `excmd === "open"`, call [[open]] with the contents of the clipboard. Similarly for [[tabopen]].
|
||||
|
|
Loading…
Add table
Reference in a new issue