Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Fix UBE Text Selection Bug on Android #34668

Merged
merged 22 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
83a65b5
Load new custom JS file in UBE
Sep 8, 2021
4681438
Detect when dropdown menus are active
Sep 8, 2021
ab5425e
Remove toolbar by resetting text selection
Sep 8, 2021
112f535
Merge branch 'trunk' into rnmobile/fix/android-ube-text-selection
Sep 19, 2021
b8fffc6
Rename 'custom-js' file for clarity
Sep 19, 2021
b4debe5
Add clarifying comment to new JS file
Sep 20, 2021
7e52ac4
Improve if/else logic by adding 'selected' check
Sep 20, 2021
3cd73f2
Access 'activeElement' via 'document'
Sep 20, 2021
3c4eb82
Add back 'activeElement' selector
Sep 20, 2021
8c24a8d
Update CHANGELOG
Sep 20, 2021
3294f70
Merge branch 'trunk' into rnmobile/fix/android-ube-text-selection
SiobhyB Sep 20, 2021
ca63ef9
Notify Android code when certain menus are tapped
Sep 28, 2021
3fadc5e
Update comment to clarify new code changes
Sep 28, 2021
35c6414
Hide text selection toolbar when selecting certain items
Sep 28, 2021
bae5472
Merge branch 'trunk' into rnmobile/fix/android-ube-text-selection
Sep 29, 2021
afbed69
Add missing variable
Sep 29, 2021
4f1ca31
Add listener for hide text selection context menu event
fluiddot Oct 4, 2021
4b76002
Merge branch 'trunk' into rnmobile/fix/android-ube-text-selection
fluiddot Oct 4, 2021
3ac0c7a
Listen to click events instead of focus
fluiddot Oct 6, 2021
2074054
Add click listeners for context menu visibility
fluiddot Oct 6, 2021
b7c55e4
Merge branch 'trunk' into rnmobile/fix/android-ube-text-selection
SiobhyB Oct 8, 2021
1456ae9
Remove commented line
fluiddot Oct 8, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -24,7 +25,7 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.wordpress.android.util.AppLog;;
import org.wordpress.android.util.AppLog;
import org.wordpress.mobile.FileUtils;

import java.util.ArrayList;
Expand All @@ -41,6 +42,8 @@ public class GutenbergWebViewActivity extends AppCompatActivity {
private static final String INJECT_LOCAL_STORAGE_SCRIPT_TEMPLATE = "localStorage.setItem('WP_DATA_USER_%d','%s')";
private static final String INJECT_CSS_SCRIPT_TEMPLATE = "window.injectCss('%s')";
private static final String INJECT_GET_HTML_POST_CONTENT_SCRIPT = "window.getHTMLPostContent();";
private static final String INJECT_ON_SHOW_CONTEXT_MENU_SCRIPT = "window.onShowContextMenu();";
private static final String INJECT_ON_HIDE_CONTEXT_MENU_SCRIPT = "window.onHideContextMenu();";
private static final String JAVA_SCRIPT_INTERFACE_NAME = "wpwebkit";

protected WebView mWebView;
Expand All @@ -49,6 +52,7 @@ public class GutenbergWebViewActivity extends AppCompatActivity {
protected TextView mForegroundViewTitle;
protected TextView mForegroundViewSubtitle;
protected boolean mIsRedirected;
protected ActionMode mActionMode = null;

private ProgressBar mProgressBar;
private boolean mIsGutenbergReady;
Expand Down Expand Up @@ -85,6 +89,24 @@ private void showTroubleshootingInstructions() {
mForegroundViewImage.setVisibility(ImageView.VISIBLE);
}

@Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
}
mWebView.evaluateJavascript(INJECT_ON_SHOW_CONTEXT_MENU_SCRIPT,
value -> AppLog.e(AppLog.T.EDITOR, value));
super.onActionModeStarted(mode);
}

@Override
public void onActionModeFinished(ActionMode mode) {
mActionMode = null;
mWebView.evaluateJavascript(INJECT_ON_HIDE_CONTEXT_MENU_SCRIPT,
value -> AppLog.e(AppLog.T.EDITOR, value));
super.onActionModeFinished(mode);
}

@SuppressLint("SetJavaScriptEnabled")
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -267,6 +289,9 @@ public void onPageFinished(WebView view, String url) {

String injectGutenbergObserver = getFileContentFromAssets("gutenberg-web-single-block/gutenberg-observer.js");
evaluateJavaScript(injectGutenbergObserver);

String behaviorOverrides = getFileContentFromAssets("gutenberg-web-single-block/editor-behavior-overrides.js");
evaluateJavaScript(behaviorOverrides);
}
});
}
Expand Down Expand Up @@ -400,5 +425,14 @@ public void postMessage(String content) {
public void gutenbergReady() {
GutenbergWebViewActivity.this.runOnUiThread(() -> onGutenbergReady());
}

@JavascriptInterface
public void hideTextSelectionContextMenu() {
if (mActionMode != null) {
GutenbergWebViewActivity.this.runOnUiThread(() -> {
GutenbergWebViewActivity.this.mActionMode.finish();
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Listeners for native context menu visibility changes
let isContextMenuVisible = false;
const hideContextMenuListeners = [];

window.onShowContextMenu = () => {
isContextMenuVisible = true;
};
window.onHideContextMenu = () => {
isContextMenuVisible = false;
while ( hideContextMenuListeners.length > 0 ) {
const listener = hideContextMenuListeners.pop();
listener();
}
};

/*
This is a fix for a text selection quirk in the UBE.
It notifies the Android app to dismiss the text selection
context menu when certain menu items are tapped. This is
done via the 'hideTextSelectionContextMenu' method, which
is sent back to the Android app, where the dismissal is
then handle. See PR for further details:
https://github.com/WordPress/gutenberg/pull/34668
*/
window.addEventListener(
'click',
( event ) => {
const selected = document.getSelection();
if ( ! isContextMenuVisible || ! selected || ! selected.toString() ) {
return;
}

// Check if the event is triggered by a dropdown
// toggle button.
const dropdownToggles = document.querySelectorAll(
'.components-dropdown-menu > button'
);
let currentToggle;
for ( const node of dropdownToggles.values() ) {
if ( node.contains( event.target ) ) {
currentToggle = node;
break;
}
}

// Hide text selection context menu when the click
// is triggered by a dropdown toggle.
//
// NOTE: The event propagation is prevented because
// it will be dispatched after the context menu
// is hidden.
if ( currentToggle ) {
event.stopPropagation();
hideContextMenuListeners.push( () => currentToggle.click() );
window.wpwebkit.hideTextSelectionContextMenu();
}
},
true
);
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For each user feature we should also add a importance categorization label to i

## Unreleased
- [*] [Embed block] Fix inline preview cut-off when editing URL [#35321]
- [*] [Unsupported Block Editor] Fix text selection bug for Android [#34668]

## 1.63.0
- [**] [Embed block] Add the top 5 specific embed blocks to the Block inserter list [#34967]
Expand Down