Skip to content

Fix GutenbergKit editor issues during dark mode toggle#22135

Merged
oguzkocer merged 2 commits intorelease/26.1from
try/fixing-gutenbergkit-darkmode-toggle
Aug 15, 2025
Merged

Fix GutenbergKit editor issues during dark mode toggle#22135
oguzkocer merged 2 commits intorelease/26.1from
try/fixing-gutenbergkit-darkmode-toggle

Conversation

@oguzkocer
Copy link
Contributor

Summary

Fixes content loss and WebView loading issues when toggling dark mode while using the GutenbergKit editor.

Problem

  • Content loss: Editor content disappeared when toggling dark mode
  • WebView loading issues: Blank WebView after dark mode toggle on private sites
  • Root cause: Double editor initialization due to duplicate auth state handling and isEditorStarted flag preventing proper reinitialization

Solution

  • Prevent duplicate fetchWpComCookies() calls by checking existing auth state
  • Add ViewPager duplicate setup protection with error logging
  • Use distinctUntilChanged() to prevent duplicate LiveData emissions
  • Remove isEditorStarted flag that was preventing proper editor reinitialization
  • Consolidate auth success handling logic

Demo

dark_mode_toggle_fixed.webm

Testing Instructions

Prerequisites

  • Test with both private WordPress.com sites and simple sites
  • Ensure GutenbergKit editor is enabled

Test Cases

  1. Dark Mode Toggle Test

    • Open a post in GutenbergKit editor
    • Add some content to the editor
    • Toggle device dark mode
    • Verify: Content is preserved and editor loads correctly
    • Toggle device dark mode again
    • Verify: Content is preserved and editor loads correctly
  2. Device Rotation Test (Regression Test)

    • Open a post in GutenbergKit editor
    • Add some content to the editor
    • Rotate device (portrait ↔ landscape)
    • Verify: Content is preserved and editor loads correctly (ensure no regressions)

Expected Results

  • ✅ Editor content is preserved during dark mode toggle
  • ✅ WebView loads correctly (no blank screen)
  • ✅ No duplicate editor initialization warnings in logs
  • ✅ Smooth transitions without content loss

The isEditorStarted flag was preventing editor reinitialization during
activity restarts, but this caused content loss because:

1. Activity restart triggers new fragment creation
2. isEditorStarted gets reset to false in onDestroy()
3. startWithEditorSettings() gets called but always rebuilds from original data
4. Current editor content is lost instead of being preserved

Root cause: The flag was designed to prevent duplicate initialization within
a single fragment instance, but during activity restarts, we WANT the editor
to reinitialize with the current content, not block reinitialization entirely.

Changes:
- Remove isEditorStarted field and related state management
- Remove isEditorStarted checks in startWithEditorSettings()
- Allow editor to reinitialize after activity restart
- Fixes content loss during dark mode toggle
Prevent duplicate editor initialization by:
1. Only calling fetchWpComCookies() if not already in Success state
2. Adding ViewPager state check to prevent duplicate setup
3. Using distinctUntilChanged() to prevent duplicate LiveData emissions
4. Consolidating Success handling logic in handleSuccessfulAuth()

Changes:
- Check auth state before calling fetchWpComCookies()
- Add ViewPager adapter check with error logging for duplicate setup attempts
- Add distinctUntilChanged() to auth state observer
- Extract handleSuccessfulAuth() for consistent Success state handling
- Remove debug logging from production code
@oguzkocer oguzkocer added this to the 26.1 milestone Aug 14, 2025
@oguzkocer oguzkocer added Posting/Editing Gutenberg Editing and display of Gutenberg blocks. labels Aug 14, 2025
@dangermattic
Copy link
Collaborator

dangermattic commented Aug 14, 2025

1 Warning
⚠️ This PR is assigned to the milestone 26.1. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger

@oguzkocer oguzkocer requested a review from dcalhoun August 14, 2025 23:50
@oguzkocer oguzkocer marked this pull request as ready for review August 14, 2025 23:50
@sonarqubecloud
Copy link

@wpmobilebot
Copy link
Contributor

WordPress📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress WordPress
FlavorJalapeno
Build TypeDebug
Versionpr22135-c010176
Commitc010176
Direct Downloadwordpress-prototype-build-pr22135-c010176.apk
Note: Google Login is not supported on these builds.

@wpmobilebot
Copy link
Contributor

Jetpack📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack Jetpack
FlavorJalapeno
Build TypeDebug
Versionpr22135-c010176
Commitc010176
Direct Downloadjetpack-prototype-build-pr22135-c010176.apk
Note: Google Login is not supported on these builds.

@codecov
Copy link

codecov bot commented Aug 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.74%. Comparing base (205b7da) to head (c010176).
⚠️ Report is 1 commits behind head on release/26.1.

Additional details and impacted files
@@               Coverage Diff                @@
##           release/26.1   #22135      +/-   ##
================================================
+ Coverage         39.43%   39.74%   +0.30%     
================================================
  Files              2149     1572     -577     
  Lines            101872    73737   -28135     
  Branches          15604    11925    -3679     
================================================
- Hits              40177    29306   -10871     
+ Misses            58126    41912   -16214     
+ Partials           3569     2519    -1050     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@oguzkocer oguzkocer enabled auto-merge (squash) August 15, 2025 01:56
Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for helping to identify and land this fix. 🙇🏻‍♂️

I tested Simple and Atomic sites, private and public sites, appearance changes, orientation changes, and smoke tested editor functionality. The changes function well overall.

I did note an apparent race condition or memory leak may remain. I am able to discard the editor content after several OS appearance toggles.

Discard editor contents w/ multiple appearance toggles
Screen_Recording_20250815_074141_Jetpack.mp4

However, this appears present in 26.0 as well. Given that, I believe we should move forward with this fix; it's a definite improvement for the release branch. We can then spend time later seeking to address the core issue.

In the future, I envision us addressing two separate things:

  • Optimize editor create/destroy logic to mitigate race conditions and memory leaks
  • Manage uiMode configuration changes similar to how we handle other configuration changes (e.g., device orientation change)

The second will be easier once GBK has its own separate activity, where we can make changes without impacting the other editors.

@oguzkocer oguzkocer merged commit ebeaac9 into release/26.1 Aug 15, 2025
30 checks passed
@oguzkocer oguzkocer deleted the try/fixing-gutenbergkit-darkmode-toggle branch August 15, 2025 12:00
oguzkocer added a commit that referenced this pull request Aug 15, 2025
* Remove isEditorStarted flag to fix content loss during activity restart

The isEditorStarted flag was preventing editor reinitialization during
activity restarts, but this caused content loss because:

1. Activity restart triggers new fragment creation
2. isEditorStarted gets reset to false in onDestroy()
3. startWithEditorSettings() gets called but always rebuilds from original data
4. Current editor content is lost instead of being preserved

Root cause: The flag was designed to prevent duplicate initialization within
a single fragment instance, but during activity restarts, we WANT the editor
to reinitialize with the current content, not block reinitialization entirely.

Changes:
- Remove isEditorStarted field and related state management
- Remove isEditorStarted checks in startWithEditorSettings()
- Allow editor to reinitialize after activity restart
- Fixes content loss during dark mode toggle

* Fix double ViewPager setup during auth state changes

Prevent duplicate editor initialization by:
1. Only calling fetchWpComCookies() if not already in Success state
2. Adding ViewPager state check to prevent duplicate setup
3. Using distinctUntilChanged() to prevent duplicate LiveData emissions
4. Consolidating Success handling logic in handleSuccessfulAuth()

Changes:
- Check auth state before calling fetchWpComCookies()
- Add ViewPager adapter check with error logging for duplicate setup attempts
- Add distinctUntilChanged() to auth state observer
- Extract handleSuccessfulAuth() for consistent Success state handling
- Remove debug logging from production code
oguzkocer added a commit that referenced this pull request Aug 15, 2025
* Revert "Implement GutenbergKit ViewModel Architecture (#22087)" (#22127)

This reverts commit 890d484.

* Fix `GutenbergKit` editor issues during dark mode toggle (#22135)

* Remove isEditorStarted flag to fix content loss during activity restart

The isEditorStarted flag was preventing editor reinitialization during
activity restarts, but this caused content loss because:

1. Activity restart triggers new fragment creation
2. isEditorStarted gets reset to false in onDestroy()
3. startWithEditorSettings() gets called but always rebuilds from original data
4. Current editor content is lost instead of being preserved

Root cause: The flag was designed to prevent duplicate initialization within
a single fragment instance, but during activity restarts, we WANT the editor
to reinitialize with the current content, not block reinitialization entirely.

Changes:
- Remove isEditorStarted field and related state management
- Remove isEditorStarted checks in startWithEditorSettings()
- Allow editor to reinitialize after activity restart
- Fixes content loss during dark mode toggle

* Fix double ViewPager setup during auth state changes

Prevent duplicate editor initialization by:
1. Only calling fetchWpComCookies() if not already in Success state
2. Adding ViewPager state check to prevent duplicate setup
3. Using distinctUntilChanged() to prevent duplicate LiveData emissions
4. Consolidating Success handling logic in handleSuccessfulAuth()

Changes:
- Check auth state before calling fetchWpComCookies()
- Add ViewPager adapter check with error logging for duplicate setup attempts
- Add distinctUntilChanged() to auth state observer
- Extract handleSuccessfulAuth() for consistent Success state handling
- Remove debug logging from production code

* Update translations

* Update translations

* Bump version number

* Update WordPress metadata translations for 26.1

* Update Jetpack metadata translations for 26.1

---------

Co-authored-by: Oguz Kocer <oguzkocer@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gutenberg Editing and display of Gutenberg blocks. Posting/Editing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants