Bug 697265 - Support clipboard operations in edit fields [r=mfinkle a=android-only]

This commit is contained in:
Wes Johnston 2011-12-13 19:17:56 -05:00
parent b7849b689c
commit 724b7f5edb
3 changed files with 83 additions and 18 deletions

View File

@ -205,6 +205,7 @@ var BrowserApp = {
IndexedDB.init();
XPInstallObserver.init();
ConsoleAPI.init();
ClipboardHelper.init();
// Init LoginManager
Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
@ -770,12 +771,6 @@ var NativeWindow = {
BrowserApp.addTab(url, { selected: false });
});
this.add(Strings.browser.GetStringFromName("contextmenu.changeInputMethod"),
this.textContext,
function(aTarget) {
Cc["@mozilla.org/imepicker;1"].getService(Ci.nsIIMEPicker).show();
});
this.add(Strings.browser.GetStringFromName("contextmenu.fullScreen"),
this.SelectorContext("video:not(:-moz-full-screen)"),
function(aTarget) {
@ -2915,6 +2910,83 @@ var ConsoleAPI = {
}
};
var ClipboardHelper = {
init: function() {
NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.copy"), ClipboardHelper.getCopyContext(false), ClipboardHelper.copy.bind(ClipboardHelper));
NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.copyAll"), ClipboardHelper.getCopyContext(true), ClipboardHelper.copy.bind(ClipboardHelper));
NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.selectAll"), NativeWindow.contextmenus.textContext, ClipboardHelper.select.bind(ClipboardHelper));
NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.paste"), ClipboardHelper.pasteContext, ClipboardHelper.paste.bind(ClipboardHelper));
NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.changeInputMethod"), NativeWindow.contextmenus.textContext, ClipboardHelper.inputMethod.bind(ClipboardHelper));
},
get clipboardHelper() {
delete this.clipboardHelper;
return this.clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
},
get clipboard() {
delete this.clipboard;
return this.clipboard = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
},
copy: function(aElement) {
let selectionStart = aElement.selectionStart;
let selectionEnd = aElement.selectionEnd;
if (selectionStart != selectionEnd) {
string = aElement.value.slice(selectionStart, selectionEnd);
this.clipboardHelper.copyString(string);
} else {
this.clipboardHelper.copyString(aElement.value);
}
},
select: function(aElement) {
if (!aElement || !(aElement instanceof Ci.nsIDOMNSEditableElement))
return;
let target = aElement.QueryInterface(Ci.nsIDOMNSEditableElement);
target.editor.selectAll();
target.focus();
},
paste: function(aElement) {
if (!aElement || !(aElement instanceof Ci.nsIDOMNSEditableElement))
return;
let target = aElement.QueryInterface(Ci.nsIDOMNSEditableElement);
target.editor.paste(Ci.nsIClipboard.kGlobalClipboard);
target.focus();
},
inputMethod: function(aElement) {
Cc["@mozilla.org/imepicker;1"].getService(Ci.nsIIMEPicker).show();
},
getCopyContext: function(isCopyAll) {
return {
matches: function(aElement) {
if (NativeWindow.contextmenus.textContext.matches(aElement)) {
let selectionStart = aElement.selectionStart;
let selectionEnd = aElement.selectionEnd;
if (selectionStart != selectionEnd)
return true;
else if (isCopyAll)
return true;
}
return false;
}
}
},
pasteContext: {
matches: function(aElement) {
if (NativeWindow.contextmenus.textContext.matches(aElement)) {
let flavors = ["text/unicode"];
return ClipboardHelper.clipboard.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard);
}
return false;
}
}
}
var PluginHelper = {
showDoorHanger: function(aTab) {
let message = Strings.browser.GetStringFromName("clickToPlayFlash.message");

View File

@ -11,18 +11,6 @@
<!ENTITY newtab.label "New Tab">
<!ENTITY closetab.label "Close Tab">
<!ENTITY cut.label "Cut">
<!ENTITY copy.label "Copy">
<!ENTITY copyAll.label "Copy All">
<!ENTITY copylink.label "Copy Link Location">
<!ENTITY paste.label "Paste">
<!ENTITY pasteAndGo.label "Paste &amp; Go">
<!ENTITY delete.label "Delete">
<!ENTITY selectAll.label "Select All">
<!ENTITY noSuggestions.label "(No suggestions)">
<!ENTITY addToDictionary.label "Add to Dictionary">
<!ENTITY inputMethod.label "Select Input Method">
<!ENTITY allPagesHeader.label "All Pages">
<!ENTITY bookmarksHeader.label "Bookmarks">
<!ENTITY historyHeader.label "History">

View File

@ -256,6 +256,11 @@ contextmenu.openInNewTab=Open Link in New Tab
contextmenu.changeInputMethod=Select Input Method
contextmenu.fullScreen=Full Screen
contextmenu.copy=Copy
contextmenu.copyAll=Copy All
contextmenu.selectAll=Select All
contextmenu.paste=Paste
# Select UI
selectHelper.closeMultipleSelectDialog=Done