Skip to content

fix: Mitigate GutenbergKit text composition oddities #21883

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

Merged
merged 8 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -1947,13 +1947,13 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
)
}

private fun updateAndSavePostAsync(listener: OnPostUpdatedFromUIListener?) {
private fun updateAndSavePostAsync(listener: OnPostUpdatedFromUIListener?, isFinishing: Boolean = false) {
if (editorFragment == null) {
AppLog.e(AppLog.T.POSTS, "Fragment not initialized")
return
}
storePostViewModel.updatePostObjectWithUIAsync(
(editPostRepository), { oldContent: String -> updateFromEditor(oldContent) }
(editPostRepository), { oldContent: String -> updateFromEditor(oldContent, isFinishing) }
) { _: PostImmutableModel?, result: UpdatePostResult ->
storePostViewModel.isSavingPostOnEditorExit = false
// Ignore the result as we want to invoke the listener even when the PostModel was up-to-date
Expand All @@ -1967,20 +1967,25 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
* 2. Saves the post via [EditPostActivity.updateAndSavePostAsync];
* 3. Invokes the listener method parameter
*/
private fun updateAndSavePostAsyncOnEditorExit(listener: OnPostUpdatedFromUIListener?) {
private fun updateAndSavePostAsyncOnEditorExit(listener: OnPostUpdatedFromUIListener?,
isFinishing: Boolean = false) {
if (editorFragment == null) {
return
}
storePostViewModel.isSavingPostOnEditorExit = true
storePostViewModel.showSavingProgressDialog()
updateAndSavePostAsync(listener)
updateAndSavePostAsync(listener, isFinishing)
}

private fun updateFromEditor(oldContent: String): UpdateFromEditor {
private fun updateFromEditor(oldContent: String, isFinishing: Boolean = false): UpdateFromEditor {
editorFragment?.let {
return try {
// To reduce redundant bridge events emitted to the Gutenberg editor, we get title and content at once
val titleAndContent: Pair<CharSequence, CharSequence> = it.getTitleAndContent(oldContent)
val titleAndContent: Pair<CharSequence, CharSequence> = if (it is GutenbergKitEditorFragment) {
it.getTitleAndContent(oldContent, isFinishing)
} else {
it.getTitleAndContent(oldContent)
}
val title = titleAndContent.first as String
val content = titleAndContent.second as String
PostFields(title, content)
Expand Down Expand Up @@ -2416,7 +2421,7 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
}

// Convert the lambda to OnPostUpdatedFromUIListener and pass it to the method
updateAndSavePostAsyncOnEditorExit(lambdaToListener(lambda))
updateAndSavePostAsyncOnEditorExit(lambdaToListener(lambda), doFinish)
}

// Helper function to convert a lambda to OnPostUpdatedFromUIListener
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ google-play-services-auth = '20.4.1'
google-services = '4.4.2'
gravatar = '2.4.1'
greenrobot-eventbus = '3.3.1'
gutenberg-kit = 'trunk-fdfe788530bbff864ce7147b5a68608d7025e078'
gutenberg-kit = 'trunk-1b19fef4cb557afb9727497a441bb411cdb8895a'
gutenberg-mobile = 'v1.121.0'
indexos-media-for-mobile = '43a9026f0973a2f0a74fa813132f6a16f7499c3a'
jackson-databind = '2.12.7.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,13 @@ private void toggleHtmlMode() {
}

@Override
public Pair<CharSequence, CharSequence> getTitleAndContent(CharSequence originalContent) throws
EditorFragmentNotAddedException {
public @NonNull Pair<CharSequence, CharSequence> getTitleAndContent(@NonNull CharSequence originalContent)
throws EditorFragmentNotAddedException {
return getTitleAndContent(originalContent, false);
}

public @NonNull Pair<CharSequence, CharSequence> getTitleAndContent(@NonNull CharSequence originalContent,
boolean completeComposition) throws EditorFragmentNotAddedException {
final Pair<CharSequence, CharSequence>[] result = new Pair[1];
final CountDownLatch latch = new CountDownLatch(1);

Expand All @@ -345,7 +350,7 @@ public void onResult(@NonNull CharSequence title, @NonNull CharSequence content)
result[0] = new Pair<>(title, content);
latch.countDown();
}
}, true);
}, completeComposition);

try {
latch.await();
Expand Down