Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Add Edit commands #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ async function handlePopupMessage(message) {
let result = {
disable: [
"*",
"edit*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should stay here, because it ensures that the edit menu isn’t enabled when the content script can’t start (i.e. the page is privileged (about:, chrome:// or resource://)).

Yes, I do realize that this is contained in the * selector above, but that one will be removed once the menu is fully implemented, while the edit* selector and any other selector dependant on the content script will be kept.

"dev*",
"workOffline",
"openHelpHealthReport",
Expand Down
46 changes: 35 additions & 11 deletions src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ var amLoaded = amLoaded || false;
if (!amLoaded) {
amLoaded = true;
browser.runtime.onMessage.addListener(async (message, sender) => {
let focusedElement = document.activeElement;
let isFocusable = focusedElement && focusedElement !== document.body;
let method = String(message.method);
let execCommand = (command) => {
window.focus();
if (isFocusable) {
focusedElement.focus();
}
document.execCommand(command);
};

switch (method) {
case "init": {
let result = {
Expand All @@ -31,27 +41,41 @@ if (!amLoaded) {
"saveAs"
]
}
if (document.documentElement.requestFullScreen || document.documentElement.mozRequestFullScreen) {
//result.enable.push("fullscreen");
}
if (document.querySelector(":focus")) {

if (isFocusable && focusedElement.isContentEditable) {
result.enable.push("edit*");
} else if (isFocusable) {
result.enable.push("editCut", "editCopy", "editSelectAll");
} else {
result.enable.push("editCopy", "editSelectAll");
Copy link
Member

@ExE-Boss ExE-Boss Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy is still broken in this context on Windows. That’s why I implemented commit ad96f85, so that this can be disabled on Windows and enabled on MacOS.

Do note that I only tested this on Stable and Developer Edition, but not on Nightly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use Nightly, which may have a different behaviour.

Copy link
Contributor Author

@nt1m nt1m Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually, it doesn't work for me either in this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it does work, it seems like your latest commit breaks this though.

}
return result;
} case "editCut": {
return document.execCommand("cut");
return execCommand("cut");
} case "editCopy": {
return document.execCommand("copy");
return execCommand("copy");
} case "editPaste": {
return document.execCommand("paste");
return execCommand("paste");
} case "editUndo": {
return document.execCommand("undo");
return execCommand("undo");
} case "editRedo": {
return document.execCommand("redo");
return execCommand("redo");
} case "editSelectAll": {
return document.execCommand("selectAll");
// Select all doesn't work reliably on inputs yet
Copy link
Contributor Author

@nt1m nt1m Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: This comment needs to updated, select all works properly on inputs.

if (isFocusable) {
window.focus();
focusedElement.focus();
return focusedElement.select();
}

// ...so we select everything on the page
let selection = window.getSelection();
let range = document.createRange();
range.selectNodeContents(document.documentElement);
selection.removeAllRanges();
return selection.addRange(range);
} case "editDelete": {
return document.execCommand("delete");
return execCommand("delete");
} case "print": {
return window.print();
} default: {
Expand Down
6 changes: 3 additions & 3 deletions src/popup/popup.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<div class="panel-section">
<i class="eb-icon-placeholder"/>
<i class="panel-list-item text" data-i18n="popup_edit"/>
<div class="panel-list-item icon" data-ipc-message="editCut" data-remain-open="true">
<div class="panel-list-item icon" data-ipc-message="editCut">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data-remain-open="true" is needed here to accurately implement how the Application Menu worked in Firefox 4-28 and CTR for FF 29-56.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work with data-remain-open=true

<i class="eb-icon-placeholder" data-icon="clipboard-cut"/>
</div>
<div class="panel-list-item icon" data-ipc-message="editCopy" data-remain-open="true">
<div class="panel-list-item icon" data-ipc-message="editCopy">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

<i class="eb-icon-placeholder" data-icon="clipboard-copy"/>
</div>
<div class="panel-list-item icon" data-ipc-message="editPaste" data-remain-open="true">
<div class="panel-list-item icon" data-ipc-message="editPaste">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

<i class="eb-icon-placeholder" data-icon="clipboard-paste"/>
</div>
<div class="panel-list-item icon" data-sub-menu="edit">
Expand Down