Skip to content

Commit

Permalink
add command editText
Browse files Browse the repository at this point in the history
This is for #114

Usage:
```
mapkey <a-b> <f3edit>
map <f3edit> editText sel args=["move","backward","word"]
mapkey <a-f> <f3editf>
map <f3editf> editText sel args=["move","forward","word"]
```
  • Loading branch information
gdh1995 committed Feb 1, 2020
1 parent a7b8cff commit 7609c36
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
8 changes: 7 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
"debugBackground": {
"message": "Debug the background page"
},
"editText": {
"message": "Send editing action to browser"
},
"editText_p": {
"message": "args:string[], dom=false, sel=false"
},
"enableCSTemp": {
"message": "Enable the site's CS in incognito window"
},
Expand Down Expand Up @@ -281,6 +287,7 @@
"goUp_p": {
"message": "trailingSlash=null/true/false"
},
"joinTabs": { "message": "Collect all tabs into current window" },
"lastTab": {
"message": "Go to the last N-th tab"
},
Expand All @@ -302,7 +309,6 @@
"moveTabToNextWindow_p": {
"message": "right"
},
"joinTabs": { "message": "Collect all tabs into current window" },
"nextFrame": {
"message": "Cycle forward to the next frame on the page"
},
Expand Down
5 changes: 4 additions & 1 deletion _locales/zh/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
"debugBackground": {
"message": "打开 Vimium C 扩展程序的管理页"
},
"editText": {
"message": "向浏览器发送文本操作指令"
},
"enableCSTemp": {
"message": "在无痕窗口中赋予网站对一项功能的使用权限"
},
Expand Down Expand Up @@ -194,6 +197,7 @@
"goUp": {
"message": "访问当前网址的上一层"
},
"joinTabs": { "message": "合并所有窗口" },
"lastTab": {
"message": "切换到右数指定位置的标签页"
},
Expand All @@ -212,7 +216,6 @@
"moveTabToNextWindow": {
"message": "移动当前标签页到下一个窗口"
},
"joinTabs": { "message": "合并所有窗口" },
"nextFrame": {
"message": "移动键盘焦点到下一个子页面"
},
Expand Down
6 changes: 4 additions & 2 deletions background/bg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ declare const enum kCName {
debugBackground = "debugBackground",
discardTab = "discardTab",
duplicateTab = "duplicateTab",
editText = "editText",
enableCSTemp = "enableCSTemp",
enterFindMode = "enterFindMode",
enterInsertMode = "enterInsertMode",
Expand All @@ -473,14 +474,14 @@ declare const enum kCName {
goPrevious = "goPrevious",
goToRoot = "goToRoot",
goUp = "goUp",
joinTabs = "joinTabs",
lastTab = "lastTab",
mainFrame = "mainFrame",
moveTabLeft = "moveTabLeft",
moveTabRight = "moveTabRight",
moveTabToIncognito = "moveTabToIncognito",
moveTabToNewWindow = "moveTabToNewWindow",
moveTabToNextWindow = "moveTabToNextWindow",
joinTabs = "joinTabs",
nextFrame = "nextFrame",
nextTab = "nextTab",
openCopiedUrlInCurrentTab = "openCopiedUrlInCurrentTab",
Expand Down Expand Up @@ -592,6 +593,7 @@ declare namespace CommandsNS {
[kCName.debugBackground]: kBgCmd.openUrl;
[kCName.discardTab]: kBgCmd.discardTab;
[kCName.duplicateTab]: kBgCmd.duplicateTab;
[kCName.editText]: kFgCmd.editText;
[kCName.enableCSTemp]: kBgCmd.toggleCS;
[kCName.enterFindMode]: kBgCmd.performFind;
[kCName.enterInsertMode]: kBgCmd.enterInsertMode;
Expand All @@ -606,14 +608,14 @@ declare namespace CommandsNS {
[kCName.goPrevious]: kBgCmd.goNext;
[kCName.goToRoot]: kBgCmd.goUp;
[kCName.goUp]: kBgCmd.goUp;
[kCName.joinTabs]: kBgCmd.joinTabs;
[kCName.lastTab]: kBgCmd.goToTab;
[kCName.mainFrame]: kBgCmd.mainFrame;
[kCName.moveTabLeft]: kBgCmd.moveTab;
[kCName.moveTabRight]: kBgCmd.moveTab;
[kCName.moveTabToIncognito]: kBgCmd.moveTabToNewWindow;
[kCName.moveTabToNewWindow]: kBgCmd.moveTabToNewWindow;
[kCName.moveTabToNextWindow]: kBgCmd.moveTabToNextWindow;
[kCName.joinTabs]: kBgCmd.joinTabs;
[kCName.nextFrame]: kBgCmd.nextFrame;
[kCName.nextTab]: kBgCmd.goToTab;
[kCName.openCopiedUrlInCurrentTab]: kBgCmd.openUrl;
Expand Down
3 changes: 2 additions & 1 deletion background/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ availableCommands_: <{[key: string]: CommandsNS.Description | undefined} & SafeO
}],
discardTab: [ kBgCmd.discardTab, 1, /* 20 in main.ts */ 0 ],
duplicateTab: [ kBgCmd.duplicateTab, 1, 20 as 0 ],
editText: [ kFgCmd.editText, 0, 0 ],
enableCSTemp: [ kBgCmd.toggleCS, 1, 0, { type: "images", incognito: true } ],
enterFindMode: [ kBgCmd.performFind, 1, 1, {active: true, selected: true} ],
enterInsertMode: [ kBgCmd.enterInsertMode, 1, 1 ],
Expand All @@ -425,14 +426,14 @@ availableCommands_: <{[key: string]: CommandsNS.Description | undefined} & SafeO
goPrevious: [ kBgCmd.goNext, 1, 1, { rel: "prev" } ],
goToRoot: [ kBgCmd.goUp, 1, 0 ],
goUp: [ kBgCmd.goUp, 1, 0, { count: -1 } ],
joinTabs: [ kBgCmd.joinTabs, 1, 1 ],
lastTab: [ kBgCmd.goToTab, 1, 0, { count: -1, absolute: true } ],
mainFrame: [ kBgCmd.mainFrame, 1, 1 ],
moveTabLeft: [ kBgCmd.moveTab, 1, 0, { count: -1 } ],
moveTabRight: [ kBgCmd.moveTab, 1, 0 ],
moveTabToIncognito: [ kBgCmd.moveTabToNewWindow, 1, 1, { incognito: true } ],
moveTabToNewWindow: [ kBgCmd.moveTabToNewWindow, 1, /** 30 in main.ts */ 0 ],
moveTabToNextWindow: [ kBgCmd.moveTabToNextWindow, 1, 0 ],
joinTabs: [ kBgCmd.joinTabs, 1, 1 ],
nextFrame: [ kBgCmd.nextFrame, 1, 0 ],
nextTab: [ kBgCmd.goToTab, 1, 0 ],
openCopiedUrlInCurrentTab: [ kBgCmd.openUrl, 1, 1, { reuse: ReuseType.current, copied: true } ],
Expand Down
17 changes: 16 additions & 1 deletion content/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ if (Build.BTypes & BrowserType.Chrome && Build.BTypes & ~BrowserType.Chrome) { v
let newEl = insertLock;
if (newEl) {
if ((options.act || options.action) === "backspace") {
if (D.view_(newEl)) { doc.execCommand("delete"); }
if (D.view_(newEl)) { VFind.exec_("delete", doc); }
} else {
InsertMode.last_ = newEl;
InsertMode.mutable_ = false;
Expand Down Expand Up @@ -712,6 +712,21 @@ if (Build.BTypes & BrowserType.Chrome && Build.BTypes & ~BrowserType.Chrome) { v
}
return HandlerResult.Nothing;
}, InsertMode.inputHint_);
},
/* kFgCmd.editText: */ function (count: number, options: CmdOptions[kFgCmd.editText]) {
if (insertLock || options.dom) {
const isSel = options.sel, args = options.args,
sel = isSel && VCui.getSelected_()[0];
setTimeout((): void => {
while (0 < count--) {
if (sel) {
sel.modify.apply(sel, args);
} else {
VFind.exec_(args[0], doc, args[1]);
}
}
}, 0);
}
}
],

Expand Down
17 changes: 10 additions & 7 deletions content/mode_find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ copy cut beforecopy beforecut paste".split(" ")) {
a.query_ || a.SetQuery_(query);
a.isQueryRichText_ = true;
a.notEmpty_ = !!a.query_;
a.notEmpty_ && a.innerDoc_.execCommand("selectAll", false);
a.notEmpty_ && a.exec_("selectAll");
},
init_ (adjust: AdjustType): void {
const ref = this.postMode_, UI = VCui,
Expand Down Expand Up @@ -327,7 +327,7 @@ copy cut beforecopy beforecut paste".split(" ")) {
const d = event.clipboardData, text = d && typeof d.getData === "function" ? d.getData("text/plain") : "";
VKey.prevent_(event);
if (!text) { return; }
this.document.execCommand("insertText", false, text + "");
VFind.exec_("insertText", 0, text);
} : 0 as never,
onKeydown_ (event: KeyboardEventToPrevent): void {
VKey.Stop_(event);
Expand Down Expand Up @@ -365,7 +365,7 @@ copy cut beforecopy beforecut paste".split(" ")) {
}
else { h = HandlerResult.Suppress; }
}
else if (keybody === kChar.f1) { a.innerDoc_.execCommand("delete"); }
else if (keybody === kChar.f1) { a.exec_("delete"); }
else if (keybody === kChar.f2) {
Build.BTypes & BrowserType.Firefox && a.box_.blur();
focus(); VApi.keydownEvents_()[n] = 1;
Expand All @@ -383,7 +383,7 @@ copy cut beforecopy beforecut paste".split(" ")) {
// on Chrome 79 + Win 10 / Firefox 69 + Ubuntu 18, delete a range itself
// while on Firefox 70 + Win 10 it collapses first
sel.type === "Caret" && sel.modify("extend", keybody[0] !== "d" ? "backward" : "forward", "word");
a.innerDoc_.execCommand("delete");
a.exec_("delete");
return;
}
a.deactivate_(i as FindNS.Action);
Expand Down Expand Up @@ -486,15 +486,15 @@ copy cut beforecopy beforecut paste".split(" ")) {
VApi.send_(kFgReq.findQuery, { i: ind }, this.SetQuery_);
return;
}
this.innerDoc_.execCommand("undo", false);
this.exec_("undo");
this.innerDoc_.getSelection().collapseToEnd();
},
SetQuery_ (this: void, query: string): void {
let _this = VFind;
if (query === _this.query_ || !_this.innerDoc_) { return; }
if (!query && _this.historyIndex_ > 0) { --_this.historyIndex_; return; }
_this.innerDoc_.execCommand("selectAll", false);
_this.innerDoc_.execCommand("insertText", false, query.replace(<RegExpOne> /^ /, "\xa0"));
_this.exec_("selectAll");
_this.exec_("insertText", 0, query.replace(<RegExpOne> /^ /, "\xa0"));
_this.OnInput_();
},
saveQuery_ (): void {
Expand All @@ -503,6 +503,9 @@ copy cut beforecopy beforecut paste".split(" ")) {
q: this.query0_
});
},
exec_ (cmd: string, doc?: Document | 0, value?: string) {
(doc || this.innerDoc_).execCommand(cmd, false, value);
},
postMode_: {
lock_: null as Element | null,
activate_ (): void {
Expand Down
9 changes: 7 additions & 2 deletions types/messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ declare const enum kFgCmd {
goToMarks, scroll, visualMode, vomnibar,
reset, toggle, insertMode, passNextKey, goNext,
reload, switchFocus, showHelp, autoCopy,
autoOpen, searchAs, focusInput,
autoOpen, searchAs, focusInput, editText,
END = "END",
}
type FgCmdAcrossFrames = kFgCmd.linkHints | kFgCmd.scroll | kFgCmd.vomnibar;
Expand Down Expand Up @@ -295,7 +295,12 @@ interface CmdOptions {
select?: SelectActions;
keep?: boolean;
passExitKey?: boolean;
}
};
[kFgCmd.editText]: {
sel?: boolean;
dom?: boolean;
args: [/** alert */ string, /** direction */ string, /** granularity */ string?];
};
}

declare const enum kMarkAction {
Expand Down

0 comments on commit 7609c36

Please sign in to comment.