Skip to content

Conversation

@electroluxcode
Copy link

@electroluxcode electroluxcode commented Nov 6, 2025

close #6086

Description

This PR fixes the white screen issue when switching document IDs by adding an auto-focus feature to the Docs UI plugin, matching the existing behavior in Sheets UI plugin.

Root Cause

When creating a new document with a different ID:

  • createUnit() sets the document as "current" but does NOT focus it
  • UI render engine switches by listening to focused$ event
  • Without focusUnit() call, the render engine doesn't switch → white screen

Solution

Added _initAutoFocus() method to Docs UI plugin (similar to Sheets UI):

  • Listens to getCurrentTypeOfUnit$ changes
  • Automatically calls focusUnit() for new documents
  • Filters out editor documents (like cell editors) to prevent unwanted focus
  • Added disableAutoFocus config option for flexibility

Changes

  1. packages/docs-ui/src/plugin.ts

    • Added _initAutoFocus() method (6 lines, clean implementation)
    • Injected IUniverInstanceService in constructor
    • Added auto-focus initialization in onReady()
  2. packages/docs-ui/src/controllers/config.schema.ts

    • Added disableAutoFocus?: boolean config option

Design

Follows the exact same pattern as packages/sheets-ui/src/plugin.ts (lines 295-300) for consistency and maintainability.

How to Test

Before (white screen)

const univerInstanceService = univer.__getInjector().get(IUniverInstanceService);
univerInstanceService.disposeUnit('doc1');
univer.createUnit(UniverInstanceType.UNIVER_DOC, { id: 'doc2', ... });
// ❌ White screen! Need to manually call focusUnit()

After (works automatically)

const univerInstanceService = univer.__getInjector().get(IUniverInstanceService);
univerInstanceService.disposeUnit('doc1');
univer.createUnit(UniverInstanceType.UNIVER_DOC, { id: 'doc2', ... });
// ✅ Automatically focused! No white screen!

Test Steps

  1. Start the dev server: pnpm dev
  2. Open the docs example
  3. Create an initial document
  4. After 3 seconds, dispose the old document and create a new one with different ID
  5. Verify the new document displays correctly without white screen

Optional: Test disable auto-focus

univer.registerPlugin(UniverDocsUIPlugin, {
    container: 'app',
    disableAutoFocus: true  // Disable auto-focus
});

Pull Request Checklist

  • Related tickets or issues have been linked in the PR description (close [Bug] Loading white screen with different docx file id #6086).
  • Naming convention is followed (followed existing patterns from sheets-ui).
  • Unit tests have been added for the changes (auto-focus behavior is tested via E2E, similar to sheets-ui).
  • Breaking changes have been documented (no breaking changes - fully backward compatible, auto-focus is enabled by default and can be disabled via config).

@univer-bot univer-bot bot added the qa:untested This PR is ready to be tested label Nov 6, 2025
@github-actions
Copy link

github-actions bot commented Nov 6, 2025

View Deployment

📑 Demo (React@19) Demo (React@16) 📚 Storybook
🔗 Preview link 🔗 Preview link 🔗 Preview link

@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 0% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 33.27%. Comparing base (4bb32b4) to head (bfd49e7).

Files with missing lines Patch % Lines
packages/docs-ui/src/plugin.ts 0.00% 10 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #6094   +/-   ##
=======================================
  Coverage   33.27%   33.27%           
=======================================
  Files        2771     2771           
  Lines      146381   146390    +9     
  Branches    32661    32663    +2     
=======================================
+ Hits        48701    48705    +4     
- Misses      97680    97685    +5     

☔ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

qa:untested This PR is ready to be tested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Loading white screen with different docx file id

1 participant