Merge pull request #1164 from glacambre/pale3_xselpaste

Pale3 xselpaste
This commit is contained in:
Oliver Blanthorn 2018-11-10 17:10:35 +00:00 committed by GitHub
commit 7c690d460a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 14 deletions

View file

@ -2452,14 +2452,15 @@ import * as tri_editor from "@src/lib/editor"
// {
for (let editorfn in tri_editor) {
// Re-expose every editor function as a text.$fn excmd that will forward the call to $fn to the commandline frame if it is selected or apply $fn to the last used input if it isn't
SELF["text." + editorfn] = () => {
if ((document.activeElement as any).src === browser.extension.getURL("static/commandline.html")) return Messaging.messageOwnTab("commandline_frame", "editor_function", [editorfn])
return tri_editor[editorfn](DOM.getLastUsedInput())
SELF["text." + editorfn] = (arg) => {
if ((document.activeElement as any).src === browser.extension.getURL("static/commandline.html")) return Messaging.messageOwnTab("commandline_frame", "editor_function", [editorfn].concat(arg))
return tri_editor[editorfn](DOM.getLastUsedInput(), arg)
}
}
for (let fn in cmdframe_fns) {
SELF["ex." + fn] = () => (Messaging.messageOwnTab as any)("commandline_frame", ...cmdframe_fns[fn])
SELF["ex." + fn] = (...args) => (Messaging.messageOwnTab as any)("commandline_frame", cmdframe_fns[fn][0], cmdframe_fns[fn][1].concat(args))
}
// }
//#background_helper
@ -2467,11 +2468,11 @@ for (let fn in cmdframe_fns) {
for (let editorfn in tri_editor) {
let name = "text." + editorfn
cmd_params.set(name, new Map([]))
BGSELF[name] = () => messageActiveTab("excmd_content", name, [])
BGSELF[name] = (...args) => messageActiveTab("excmd_content", name, args)
}
for (let fn in cmdframe_fns) {
cmd_params.set("ex." + fn, new Map(cmdframe_fns[fn][1].map((a, i) => [`${i}`, typeof a] as [string, string])))
BGSELF["ex." + fn] = () => messageActiveTab("commandline_frame", ...cmdframe_fns[fn])
BGSELF["ex." + fn] = (...args) => messageActiveTab("commandline_frame", cmdframe_fns[fn][0], cmdframe_fns[fn][1].concat(args))
}
// }
@ -2525,8 +2526,10 @@ async function setclip(str) {
* @hidden
*/
//#background_helper
async function getclip() {
if ((await config.getAsync("putfrom")) == "clipboard") {
async function getclip(fromm?: "clipboard" | "selection") {
if (fromm == undefined)
fromm = (await config.getAsync("putfrom"))
if (fromm == "clipboard") {
return messageActiveTab("commandline_frame", "getClipboard")
} else {
return Native.clipboard("get", "")
@ -2555,7 +2558,7 @@ async function getclip() {
*/
//#background
export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcanon" | "yanktitle" | "yankmd" | "tabopen" = "open", ...toYank: string[]) {
export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcanon" | "yanktitle" | "yankmd" | "xselpaste" | "tabopen" = "open", ...toYank: string[]) {
let content = toYank.join(" ")
let url = ""
let urls = []
@ -2602,6 +2605,12 @@ export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcano
url = await getclip()
url && tabopen(url)
break
case "xselpaste":
content = await getclip("selection")
if (content.length > 0) {
BGSELF["text.insert_text"](content)
}
break
default:
// todo: maybe we should have some common error and error handler
throw new Error(`[clipboard] unknown excmd: ${excmd}`)

View file

@ -28,6 +28,7 @@ type editor_function = (
text: string,
start: number,
end: number,
arg?: any,
) => [string, number, number]
/** @hidden
@ -121,8 +122,8 @@ function setContentEditableValues(e, text, start, end) {
* @return boolean Whether the editor function was actually called or not
*
**/
function wrap_input(fn: editor_function): (e: HTMLElement) => boolean {
return (e: HTMLElement) => {
function wrap_input(fn: editor_function): (e: HTMLElement, arg?: any) => boolean {
return (e: HTMLElement, arg?: any) => {
let getValues = getSimpleValues
let setValues = setSimpleValues
if (e.isContentEditable) {
@ -131,7 +132,7 @@ function wrap_input(fn: editor_function): (e: HTMLElement) => boolean {
}
const [origText, origStart, origEnd] = getValues(e)
if (origText === null || origStart === null) return false
setValues(e, ...fn(origText, origStart, origEnd))
setValues(e, ...fn(origText, origStart, origEnd, arg))
return true
}
}
@ -139,8 +140,8 @@ function wrap_input(fn: editor_function): (e: HTMLElement) => boolean {
/** @hidden
* Take an editor function as parameter and wrap it in a function that will handle error conditions
*/
function needs_text(fn: editor_function): editor_function {
return (text: string, selectionStart: number, selectionEnd: number) => {
function needs_text(fn: editor_function, arg?: any): editor_function {
return (text: string, selectionStart: number, selectionEnd: number, arg?: any) => {
if (
text.length === 0 ||
selectionStart === null ||
@ -151,6 +152,7 @@ function needs_text(fn: editor_function): editor_function {
text,
selectionStart,
typeof selectionEnd == "number" ? selectionEnd : selectionStart,
arg
)
}
}
@ -605,3 +607,13 @@ export const backward_word = wrap_input(
return [null, boundaries[0], null]
},
)
/**
* Insert text in the current input.
**/
export const insert_text = wrap_input((text, selectionStart, selectionEnd, arg) => {
return [text.slice(0, selectionStart) + arg + text.slice(selectionEnd),
selectionStart + arg.length,
null
]
})