Skip to content

Commit c63dc55

Browse files
keandcalhoun
andauthored
feature: Auto-focus when creating a new post (#191)
* Add auto-focus * Remove from configuration and add focus to JS * feat: Auto-focus Android editor when content is empty Match the iOS UX. --------- Co-authored-by: David Calhoun <[email protected]>
1 parent 6b3a9cd commit c63dc55

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.os.Looper
1010
import android.util.AttributeSet
1111
import android.util.Log
1212
import android.view.View
13+
import android.view.inputmethod.InputMethodManager
1314
import android.webkit.ConsoleMessage
1415
import android.webkit.CookieManager
1516
import android.webkit.JavascriptInterface
@@ -477,6 +478,18 @@ class GutenbergView : WebView {
477478
.alpha(1f)
478479
.setDuration(300)
479480
.start()
481+
482+
if (configuration.content.isEmpty()) {
483+
// Focus the editor content
484+
this.evaluateJavascript("editor.focus();", null)
485+
486+
// Request focus on the WebView and show the soft keyboard
487+
handler.postDelayed({
488+
this.requestFocus()
489+
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
490+
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
491+
}, 100)
492+
}
480493
}
481494
}
482495
}

ios/Sources/GutenbergKit/Sources/EditorViewController.swift

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,6 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
8686
setUpEditor()
8787
loadEditor()
8888
}
89-
90-
// TODO: register it when editor is loaded
91-
// service.$rawBlockTypesResponseData.compactMap({ $0 }).sink { [weak self] data in
92-
// guard let self else { return }
93-
// assert(Thread.isMainThread)
94-
//
95-
// }.store(in: &cancellables)
96-
}
97-
98-
// TODO: move
99-
private func registerBlockTypes(data: Data) async {
100-
guard let string = String(data: data, encoding: .utf8),
101-
let escapedString = string.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
102-
assertionFailure("invalid block types")
103-
return
104-
}
105-
do {
106-
// TODO: simplify this
107-
try await webView.evaluateJavaScript("""
108-
const blockTypes = JSON.parse(decodeURIComponent('\(escapedString)'));
109-
editor.registerBlocks(blockTypes);
110-
"done";
111-
""")
112-
} catch {
113-
NSLog("failed to register blocks \(error)")
114-
// TOOD: relay to the client
115-
}
11689
}
11790

11891
private func setUpEditor() {
@@ -347,6 +320,10 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
347320
let duration = CFAbsoluteTimeGetCurrent() - timestampInit
348321
print("gutenbergkit-measure_editor-first-render:", duration)
349322
delegate?.editorDidLoad(self)
323+
324+
if configuration.content.isEmpty {
325+
evaluate("editor.focus();")
326+
}
350327
}
351328

352329
// MARK: - Warmup

src/components/editor/use-host-bridge.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ export function useHostBridge( post, editorRef ) {
108108
);
109109
};
110110

111+
window.editor.focus = () => {
112+
const editable = document.querySelector(
113+
'[contenteditable="true"]'
114+
);
115+
if ( editable ) {
116+
editable.focus();
117+
editable.click();
118+
}
119+
};
120+
111121
window.editor.appendTextAtCursor = ( text ) => {
112122
const selectedBlockClientId = getSelectedBlockClientId();
113123

0 commit comments

Comments
 (0)