Skip to content

Commit 716cd6c

Browse files
committed
Add auto-focus
1 parent 9c37290 commit 716cd6c

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

ios/Sources/GutenbergKit/Sources/EditorConfiguration.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public struct EditorConfiguration {
1616
public let shouldUsePlugins: Bool
1717
/// Toggles visibility of the title field
1818
public let shouldHideTitle: Bool
19+
/// If enabled, the editor automatically becomes focused when initialized
20+
/// with an empty content.
21+
public let shouldAutoFocus: Bool
1922
/// Root URL for the site
2023
public let siteURL: String
2124
/// Root URL for the site API
@@ -42,6 +45,7 @@ public struct EditorConfiguration {
4245
shouldUseThemeStyles: Bool,
4346
shouldUsePlugins: Bool,
4447
shouldHideTitle: Bool,
48+
shouldAutoFocus: Bool,
4549
siteURL: String,
4650
siteApiRoot: String,
4751
siteApiNamespace: [String],
@@ -58,6 +62,7 @@ public struct EditorConfiguration {
5862
self.shouldUseThemeStyles = shouldUseThemeStyles
5963
self.shouldUsePlugins = shouldUsePlugins
6064
self.shouldHideTitle = shouldHideTitle
65+
self.shouldAutoFocus = shouldAutoFocus
6166
self.siteURL = siteURL
6267
self.siteApiRoot = siteApiRoot
6368
self.siteApiNamespace = siteApiNamespace
@@ -77,6 +82,7 @@ public struct EditorConfiguration {
7782
shouldUseThemeStyles: shouldUseThemeStyles,
7883
shouldUsePlugins: shouldUsePlugins,
7984
shouldHideTitle: shouldHideTitle,
85+
shouldAutoFocus: shouldAutoFocus,
8086
siteURL: siteURL,
8187
siteApiRoot: siteApiRoot,
8288
siteApiNamespace: siteApiNamespace,
@@ -107,6 +113,7 @@ public struct EditorConfigurationBuilder {
107113
private var shouldUseThemeStyles: Bool
108114
private var shouldUsePlugins: Bool
109115
private var shouldHideTitle: Bool
116+
private var shouldAutoFocus: Bool
110117
private var siteURL: String
111118
private var siteApiRoot: String
112119
private var siteApiNamespace: [String]
@@ -124,6 +131,7 @@ public struct EditorConfigurationBuilder {
124131
shouldUseThemeStyles: Bool = false,
125132
shouldUsePlugins: Bool = false,
126133
shouldHideTitle: Bool = false,
134+
shouldAutoFocus: Bool = true,
127135
siteURL: String = "",
128136
siteApiRoot: String = "",
129137
siteApiNamespace: [String] = [],
@@ -140,6 +148,7 @@ public struct EditorConfigurationBuilder {
140148
self.shouldUseThemeStyles = shouldUseThemeStyles
141149
self.shouldUsePlugins = shouldUsePlugins
142150
self.shouldHideTitle = shouldHideTitle
151+
self.shouldAutoFocus = shouldAutoFocus
143152
self.siteURL = siteURL
144153
self.siteApiRoot = siteApiRoot
145154
self.siteApiNamespace = siteApiNamespace
@@ -192,6 +201,12 @@ public struct EditorConfigurationBuilder {
192201
return copy
193202
}
194203

204+
public func setShouldAutoFocus(_ shouldAutoFocus: Bool) -> EditorConfigurationBuilder {
205+
var copy = self
206+
copy.shouldAutoFocus = shouldAutoFocus
207+
return copy
208+
}
209+
195210
public func setSiteUrl(_ siteUrl: String) -> EditorConfigurationBuilder {
196211
var copy = self
197212
copy.siteURL = siteUrl
@@ -271,6 +286,7 @@ public struct EditorConfigurationBuilder {
271286
shouldUseThemeStyles: shouldUseThemeStyles,
272287
shouldUsePlugins: shouldUsePlugins,
273288
shouldHideTitle: shouldHideTitle,
289+
shouldAutoFocus: shouldAutoFocus,
274290
siteURL: siteURL,
275291
siteApiRoot: siteApiRoot,
276292
siteApiNamespace: siteApiNamespace,

ios/Sources/GutenbergKit/Sources/EditorViewController.swift

Lines changed: 18 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,24 @@ 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.shouldAutoFocus, configuration.content.isEmpty {
325+
autoFocusEditor()
326+
}
327+
}
328+
329+
// MARK: - Helpers
330+
331+
private func autoFocusEditor() {
332+
evaluate("""
333+
(function() {
334+
const editable = document.querySelector('[contenteditable="true"]');
335+
if (editable) {
336+
editable.focus();
337+
editable.click();
338+
}
339+
})();
340+
""")
350341
}
351342

352343
// MARK: - Warmup

0 commit comments

Comments
 (0)