Skip to content

feat: output continuous spaces as &nbsp#471

Open
wuyiping0628 wants to merge 1 commit into
devfrom
wyp/space-0515
Open

feat: output continuous spaces as &nbsp#471
wuyiping0628 wants to merge 1 commit into
devfrom
wyp/space-0515

Conversation

@wuyiping0628
Copy link
Copy Markdown
Collaborator

@wuyiping0628 wuyiping0628 commented May 21, 2026

将连续空格转为不间断空格,以便 innerHTML 输出   并在预览中保留间距
修改前:
空格2

修改后:
空格1

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features
    • Enhanced whitespace handling in the editor with context-aware automatic conversion of spaces to non-breaking spaces for improved HTML formatting consistency.
    • Better spacing preservation during typing to ensure consistent formatting output across various content scenarios.

Review Change Stack

@github-actions github-actions Bot added the enhancement New feature or request label May 21, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

Walkthrough

This PR introduces whitespace preservation utilities for the editor to maintain consistent spacing in HTML output. Two utility functions are added to handle non-breaking space conversion and decision logic, integrated into keyboard handling via a new space-key binding, and consolidated with existing clipboard processing by removing a duplicate implementation.

Changes

Whitespace Preservation Feature

Layer / File(s) Summary
Whitespace preservation utilities
packages/fluent-editor/src/config/editor.utils.ts
Introduces replaceStrWhiteSpace to convert regular spaces into non-breaking spaces based on position, and shouldInsertNbspOnSpace to determine whether a space keypress should preserve whitespace based on the preceding character.
Keyboard binding and clipboard consolidation
packages/fluent-editor/src/themes/snow.ts, packages/fluent-editor/src/modules/custom-clipboard.ts
snow.ts imports the utility and adds a keyboard binding for the space key that conditionally inserts non-breaking spaces. custom-clipboard removes its local replaceStrWhiteSpace implementation and imports the shared version from editor.utils.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit's delight, spacing made right! ✨
Where spaces transform to nbsp with might,
Keyboard bindings dance, clipboard takes flight—
HTML preserved, no more formatting blight! 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: converting consecutive spaces into non-breaking space entities ( ) in the editor output.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch wyp/space-0515

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/fluent-editor/src/config/editor.utils.ts (1)

116-149: ⚡ Quick win

Add focused unit tests for whitespace edge cases.

These rules are easy to regress; please add direct tests for line-start spaces, consecutive in-text spaces, full-width spaces (\u3000), and prefixes ending with \u00A0.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/fluent-editor/src/config/editor.utils.ts` around lines 116 - 149,
Add focused unit tests for replaceStrWhiteSpace and shouldInsertNbspOnSpace
covering edge cases: assert replaceStrWhiteSpace turns leading spaces at
line-start into '\u00A0', handles consecutive in-text spaces by alternating
between normal space and '\u00A0' per the algorithm, correctly processes
full-width space '\u3000' as whitespace, and preserves/outputs '\u00A0' where
expected; also add tests for shouldInsertNbspOnSpace with empty prefix, prefixes
ending in regular space, prefixes ending in '\u00A0', and prefixes ending in
'\u3000' to verify true/false returns match the WHITE_SPACE_OR_NBSP_RE logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/fluent-editor/src/config/editor.utils.ts`:
- Around line 116-149: Add focused unit tests for replaceStrWhiteSpace and
shouldInsertNbspOnSpace covering edge cases: assert replaceStrWhiteSpace turns
leading spaces at line-start into '\u00A0', handles consecutive in-text spaces
by alternating between normal space and '\u00A0' per the algorithm, correctly
processes full-width space '\u3000' as whitespace, and preserves/outputs
'\u00A0' where expected; also add tests for shouldInsertNbspOnSpace with empty
prefix, prefixes ending in regular space, prefixes ending in '\u00A0', and
prefixes ending in '\u3000' to verify true/false returns match the
WHITE_SPACE_OR_NBSP_RE logic.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 09a53e87-f240-4058-8f64-547afd7b6531

📥 Commits

Reviewing files that changed from the base of the PR and between c7f8992 and 22a6cf6.

📒 Files selected for processing (3)
  • packages/fluent-editor/src/config/editor.utils.ts
  • packages/fluent-editor/src/modules/custom-clipboard.ts
  • packages/fluent-editor/src/themes/snow.ts

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant