Skip to content

Commit 84042ab

Browse files
committed
Document GutenbergView usage
1 parent 9a81f9e commit 84042ab

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ import java.util.Locale
4444

4545
const val ASSET_URL = "https://appassets.androidplatform.net/assets/index.html"
4646

47+
/**
48+
* A WebView-based Gutenberg block editor for Android.
49+
*
50+
* ## Creating a GutenbergView
51+
*
52+
* This view must be created programmatically - XML layout inflation is not supported.
53+
* Use the constructor directly or create it within a Compose `AndroidView`:
54+
*
55+
* ```kotlin
56+
* // In an Activity or Fragment:
57+
* val editor = GutenbergView(
58+
* configuration = EditorConfiguration.builder(...).build(),
59+
* dependencies = null, // or pre-fetched dependencies
60+
* coroutineScope = lifecycleScope,
61+
* context = this
62+
* )
63+
*
64+
* // In Jetpack Compose:
65+
* AndroidView(factory = { context ->
66+
* GutenbergView(configuration, dependencies, lifecycleScope, context)
67+
* })
68+
* ```
69+
*
70+
* ## Coroutine Scope Requirements
71+
*
72+
* The `coroutineScope` parameter is used for async operations like fetching editor
73+
* dependencies. The caller owns this scope and is responsible for its lifecycle:
74+
*
75+
* - **Use a lifecycle-aware scope** (e.g., `lifecycleScope`, `viewModelScope`)
76+
* to automatically cancel operations when the host is destroyed
77+
* - The view does **not** cancel the scope in `onDetachedFromWindow()`
78+
* - If using a custom scope, ensure it's cancelled when the editor is no longer needed
79+
*
80+
* ## Loading Behavior
81+
*
82+
* - If `dependencies` is provided, the editor loads immediately (fast path)
83+
* - If `dependencies` is null, dependencies are fetched asynchronously before loading
84+
*/
4785
class GutenbergView : WebView {
4886
private var isEditorLoaded = false
4987
private var didFireEditorLoaded = false
@@ -132,6 +170,17 @@ class GutenbergView : WebView {
132170
loadingListener = listener
133171
}
134172

173+
/**
174+
* Creates a new GutenbergView with the specified configuration.
175+
*
176+
* @param configuration The editor configuration specifying site details and capabilities.
177+
* @param dependencies Pre-fetched editor dependencies, or null to fetch asynchronously.
178+
* Providing dependencies enables instant editor loading.
179+
* @param coroutineScope The scope for async operations. **Caller owns this scope** -
180+
* use a lifecycle-aware scope like `lifecycleScope` to ensure
181+
* operations are cancelled when the Activity/Fragment is destroyed.
182+
* @param context The Android context.
183+
*/
135184
constructor(configuration: EditorConfiguration, dependencies: EditorDependencies?, coroutineScope: CoroutineScope, context: Context) : super(context) {
136185
this.configuration = configuration
137186
this.coroutineScope = coroutineScope

0 commit comments

Comments
 (0)