Bug 1540037 - part 36: Make EditorBase handle Delete, Backspace and Tab of plaintext editor mode r=m_kato

`Delete` and `Backspace` keys are handled by same code.  So, the code should
be in `EditorBase` instead of `TextEditor`.

If `HTMLEditor` is in the plaintext editing mode of mail composer, `Tab` key
is also handled by the same code as `TextEditor`.  So, the code in `TextEditor`
should be moved to `EditorBase` too and `HTMLEditor` should call `EditorBase`'s
method only when it's in the plaintext mode.

Depends on D116352

Differential Revision: https://phabricator.services.mozilla.com/D116353
This commit is contained in:
Masayuki Nakano 2021-06-01 08:51:25 +00:00
parent 4b29ca0eb3
commit 11211e435b
3 changed files with 61 additions and 52 deletions

View File

@ -4715,6 +4715,56 @@ nsresult EditorBase::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
MOZ_ASSERT_UNREACHABLE(
"eKeyPress event shouldn't be fired for modifier keys");
return NS_ERROR_UNEXPECTED;
case NS_VK_BACK: {
if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
return NS_OK;
}
DebugOnly<nsresult> rvIgnored =
DeleteSelectionAsAction(nsIEditor::ePrevious, nsIEditor::eStrip);
aKeyboardEvent->PreventDefault();
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"EditorBase::DeleteSelectionAsAction() failed, but ignored");
return NS_OK;
}
case NS_VK_DELETE: {
// on certain platforms (such as windows) the shift key
// modifies what delete does (cmd_cut in this case).
// bailing here to allow the keybindings to do the cut.
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
aKeyboardEvent->IsOS()) {
return NS_OK;
}
DebugOnly<nsresult> rvIgnored =
DeleteSelectionAsAction(nsIEditor::eNext, nsIEditor::eStrip);
aKeyboardEvent->PreventDefault();
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"EditorBase::DeleteSelectionAsAction() failed, but ignored");
return NS_OK;
}
case NS_VK_TAB: {
MOZ_ASSERT_IF(IsHTMLEditor(), IsPlaintextEditor());
if (IsTabbable()) {
return NS_OK; // let it be used for focus switching
}
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
aKeyboardEvent->IsOS()) {
return NS_OK;
}
// else we insert the tab straight through
aKeyboardEvent->PreventDefault();
nsresult rv = OnInputText(u"\t"_ns);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::OnInputText(\\t) failed");
return rv;
}
}
return NS_OK;
}

View File

@ -891,19 +891,18 @@ nsresult HTMLEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
case NS_VK_BACK:
case NS_VK_DELETE: {
// These keys are handled on TextEditor.
nsresult rv = TextEditor::HandleKeyPressEvent(aKeyboardEvent);
nsresult rv = EditorBase::HandleKeyPressEvent(aKeyboardEvent);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"TextEditor::HandleKeyPressEvent() failed");
"EditorBase::HandleKeyPressEvent() failed");
return rv;
}
case NS_VK_TAB: {
if (IsPlaintextEditor()) {
// If this works as plain text editor, e.g., mail editor for plain
// text, should be handled on TextEditor.
nsresult rv = TextEditor::HandleKeyPressEvent(aKeyboardEvent);
// text, should be handled with common logic with TextEditor.
nsresult rv = EditorBase::HandleKeyPressEvent(aKeyboardEvent);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"TextEditor::HandleKeyPressEvent() failed");
"EditorBase::HandleKeyPressEvent() failed");
return rv;
}
@ -978,7 +977,7 @@ nsresult HTMLEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
aKeyboardEvent->PreventDefault();
nsresult rv = OnInputText(u"\t"_ns);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"TextEditor::OnInputText(\\t) failed");
"EditorBase::OnInputText(\\t) failed");
return EditorBase::ToGenericNSResult(rv);
}
case NS_VK_RETURN:
@ -1008,7 +1007,7 @@ nsresult HTMLEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
aKeyboardEvent->PreventDefault();
nsAutoString str(aKeyboardEvent->mCharCode);
nsresult rv = OnInputText(str);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "TextEditor::OnInputText() failed");
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::OnInputText() failed");
return rv;
}

View File

@ -181,52 +181,12 @@ nsresult TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {
aKeyboardEvent->PreventDefault();
return NS_OK;
case NS_VK_BACK: {
if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
return NS_OK;
}
DebugOnly<nsresult> rvIgnored =
DeleteSelectionAsAction(nsIEditor::ePrevious, nsIEditor::eStrip);
aKeyboardEvent->PreventDefault();
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"EditorBase::DeleteSelectionAsAction() failed, but ignored");
return NS_OK;
}
case NS_VK_DELETE: {
// on certain platforms (such as windows) the shift key
// modifies what delete does (cmd_cut in this case).
// bailing here to allow the keybindings to do the cut.
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
aKeyboardEvent->IsOS()) {
return NS_OK;
}
DebugOnly<nsresult> rvIgnored =
DeleteSelectionAsAction(nsIEditor::eNext, nsIEditor::eStrip);
aKeyboardEvent->PreventDefault();
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"EditorBase::DeleteSelectionAsAction() failed, but ignored");
return NS_OK;
}
case NS_VK_BACK:
case NS_VK_DELETE:
case NS_VK_TAB: {
if (IsTabbable()) {
return NS_OK; // let it be used for focus switching
}
if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
aKeyboardEvent->IsOS()) {
return NS_OK;
}
// else we insert the tab straight through
aKeyboardEvent->PreventDefault();
nsresult rv = OnInputText(u"\t"_ns);
nsresult rv = EditorBase::HandleKeyPressEvent(aKeyboardEvent);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::OnInputText(\\t) failed");
"EditorBase::HandleKeyPressEvent() failed");
return rv;
}
case NS_VK_RETURN: {