feat(editor): add optional auto text direction per block - #17931
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an optional per-block auto text-direction mode to the Protyle editor to better support mixed RTL/LTR writing, including a new config flag, runtime application hooks (render/paste/input), and UI + i18n wiring.
Changes:
- Added
autoTextDirectionto kernel editor config and frontendConfig.IEditortyping. - Introduced
app/src/protyle/util/autoDirection.tsand invoked auto-direction application during document render, paste, and input. - Added a settings switch (mutually exclusive with global RTL) and updated gutter/shortcuts to mark manual direction overrides; added new i18n keys across locales.
Reviewed changes
Copilot reviewed 30 out of 31 changed files in this pull request and generated 20 comments.
Show a summary per file
| File | Description |
|---|---|
| kernel/conf/editor.go | Adds AutoTextDirection config flag with default false. |
| app/src/types/config.d.ts | Extends Config.IEditor with autoTextDirection. |
| app/src/protyle/util/autoDirection.ts | New detection + apply/clear logic for per-block direction. |
| app/src/config/tabs/appearanceTab.ts | Adds settings toggle and RTL mutual-exclusion behavior. |
| app/src/config/tabs/editorRuntime.ts | Clears auto markers when disabling auto direction. |
| app/src/protyle/util/onGet.ts | Applies auto direction on document render when enabled. |
| app/src/protyle/util/paste.ts | Applies auto direction after paste rendering. |
| app/src/protyle/wysiwyg/input.ts | Applies auto direction after input updates. |
| app/src/protyle/gutter/index.ts | Marks manual overrides and re-applies auto direction after clearing styles. |
| app/src/protyle/wysiwyg/keydown.ts | Marks manual overrides for RTL/LTR shortcuts. |
| app/appearance/langs/zh-TW.json | Adds autoTextDirection i18n keys (needs Traditional Chinese wording fixes). |
| app/appearance/langs/zh-CN.json | Adds autoTextDirection i18n keys. |
| app/appearance/langs/en.json | Adds autoTextDirection i18n keys. |
| app/appearance/langs/uk.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/tr.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/th.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/sk.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/ru.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/pt-BR.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/pl.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/nl.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/ko.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/ja.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/it.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/id.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/hi.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/he.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/fr.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/es.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/de.json | Adds autoTextDirection i18n keys (currently English text). |
| app/appearance/langs/ar.json | Adds autoTextDirection i18n keys (currently English text). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const RTL_RE = /[\u0590-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/u; | ||
| const LTR_RE = /[A-Za-z]/u; |
| "autoTextDirection": "自动检测每个块的文字方向", | ||
| "autoTextDirectionTip": "根据首个强字符自动为每个文本块应用 RTL 或 LTR 方向。手动设置的块方向会覆盖此设置。", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
| "autoTextDirection": "Auto-detect text direction per block", | ||
| "autoTextDirectionTip": "Automatically applies RTL or LTR direction to each text block based on its first strong character. Manual block direction overrides this setting.", |
c3b4a0e to
eef1056
Compare
|
Thanks for the implementation. I found two issues that should be addressed before merging:
The existing tests, type check, ESLint checks, Go formatting, and |
4714d54 to
f9d6f06
Compare
|
Thanks for the review. Both issues have been addressed:
I also rebased the PR onto the current Please take another look when you have a chance. Thanks! |
|
Thanks, I rechecked the latest commit. The localization issue is resolved, and initialization now works for normal documents loaded through However, it still does not cover every new Protyle. When Please initialize the runtime from a common construction path after |
New edit:
Summary
Adds an optional automatic per-block text-direction mode for mixed RTL/LTR writing in the Protyle editor.
Automatic direction is applied at runtime only. It does not write detected
dir, inline styles, or custom direction markers into block data, so enabling the feature does not persist automatically detected direction into.sydocuments or affect undo/redo history.Behavior
editor.autoTextDirection, disabled by default.kbd,samp, and inline math LTR-isolated inside automatically RTL content.MutationObserverwithrequestAnimationFramebatching.Implementation
kernel/conf/editor.go— persists theAutoTextDirectionpreference.app/src/types/autoTextDirection.d.ts— extendsConfig.IEditor.app/src/protyle/util/autoDirection.ts— Unicode-aware classifier and scoped runtime.app/src/protyle/util/autoDirection.test.ts— classifier tests covering RTL/LTR scripts, mixed content, numbers, and balanced detection thresholds.app/src/config/tabs/autoDirectionSetting.ts— registers the Appearance setting and RTL mutual-exclusion behavior.app/src/protyle/util/reload.ts— synchronizes the runtime across editor reloads, including preview mode.app/src/protyle/util/destroy.ts— disposes the automatic-direction runtime.No new locale keys are introduced in this version.
Validation
autoDirection.tspassed an isolated TypeScript 5.8 + DOM type-check.pnpm buildwas not run, following the repository'sAGENTS.mdguidance.