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) { 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 // 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] = () => { SELF["text." + editorfn] = (arg) => {
if ((document.activeElement as any).src === browser.extension.getURL("static/commandline.html")) return Messaging.messageOwnTab("commandline_frame", "editor_function", [editorfn]) 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()) return tri_editor[editorfn](DOM.getLastUsedInput(), arg)
} }
} }
for (let fn in cmdframe_fns) { 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 //#background_helper
@ -2467,11 +2468,11 @@ for (let fn in cmdframe_fns) {
for (let editorfn in tri_editor) { for (let editorfn in tri_editor) {
let name = "text." + editorfn let name = "text." + editorfn
cmd_params.set(name, new Map([])) 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) { 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]))) 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 * @hidden
*/ */
//#background_helper //#background_helper
async function getclip() { async function getclip(fromm?: "clipboard" | "selection") {
if ((await config.getAsync("putfrom")) == "clipboard") { if (fromm == undefined)
fromm = (await config.getAsync("putfrom"))
if (fromm == "clipboard") {
return messageActiveTab("commandline_frame", "getClipboard") return messageActiveTab("commandline_frame", "getClipboard")
} else { } else {
return Native.clipboard("get", "") return Native.clipboard("get", "")
@ -2555,7 +2558,7 @@ async function getclip() {
*/ */
//#background //#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 content = toYank.join(" ")
let url = "" let url = ""
let urls = [] let urls = []
@ -2602,6 +2605,12 @@ export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcano
url = await getclip() url = await getclip()
url && tabopen(url) url && tabopen(url)
break break
case "xselpaste":
content = await getclip("selection")
if (content.length > 0) {
BGSELF["text.insert_text"](content)
}
break
default: default:
// 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}`)

View file

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