Skip to content
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

refactor: remove i18n variable in editor instance #183

Merged
merged 1 commit into from
Jan 2, 2025

Conversation

zzxming
Copy link
Collaborator

@zzxming zzxming commented Dec 30, 2024

PR

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?

make lang readonly in quill. change langText to getLangText function

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

Release Notes

  • Language & Localization

    • Enhanced language text retrieval across multiple editor components.
    • Improved method for accessing localized text dynamically.
    • Updated language handling to be more flexible and context-aware.
  • Clipboard Improvements

    • Enhanced compatibility with Internet Explorer 11.
    • Improved handling of pasted content, especially in tables.
    • Refined image extraction and error handling.
  • Bug Fixes

    • Resolved potential issues with language text access.
    • Improved robustness of clipboard functionality.

Copy link

coderabbitai bot commented Dec 30, 2024

Walkthrough

The pull request introduces a comprehensive refactoring of language text retrieval across multiple components in the Fluent Editor. The primary change involves replacing direct access to langText with a new getLangText() method in the FluentEditor class. This modification standardizes language text retrieval, making it more dynamic and centralized. The changes span several files, including counter, clipboard, toolbar, and table-related modules, ensuring a consistent approach to handling localized text throughout the editor.

Changes

File Change Summary
packages/fluent-editor/src/counter/index.ts Updated language text retrieval from direct langText access to getLangText() method.
packages/fluent-editor/src/custom-clipboard.ts Enhanced clipboard handling with improved error management and content processing, especially for pasted content.
packages/fluent-editor/src/fluent-editor.ts Added lang getter and getLangText() method for centralized language text retrieval.
packages/fluent-editor/src/i18n/index.ts Removed setLangTextToInstance() method, simplifying language management.
packages/fluent-editor/src/link/modules/tooltip.ts Updated link placeholder text retrieval method to use getLangText().
packages/fluent-editor/src/table/modules/table-operation-menu.ts Standardized language text retrieval for table operation menu items using getLangText().
packages/fluent-editor/src/toolbar/toolbar-tip.ts Modified language text retrieval in toolbar tips to utilize getLangText().

Possibly related PRs

  • fix: Fix docs error #62: The main PR modifies the Counter class to standardize language text retrieval, which is related to the changes in the CustomClipboard class that also involve language text handling.
  • feat: enable format with prettier in eslint #90: The main PR's changes to language text retrieval methods align with the new method getLangText introduced in the FluentEditor class, which enhances language handling.
  • chore: lint fix #126: The changes in the main PR regarding language text retrieval may relate to the linting fixes that ensure consistent coding practices across the codebase, including language handling.
  • feat: i18n #127: The introduction of the I18N class and its methods for managing language settings directly connects to the main PR's focus on standardizing language text retrieval.
  • fix: mathlive formula should not display edit dialog when quill disabled #138: The main PR's updates to language handling in the Counter class may relate to the bug fix in the mathlive formula functionality, which also involves language settings.
  • feat(blot): Add divider module to toolbar #167: The addition of the divider module in the toolbar may benefit from the standardized language text retrieval methods introduced in the main PR, enhancing user experience.
  • refactor: extract i18n to class module #176: The refactor to extract i18n functionality into a class module directly relates to the main PR's focus on improving language text handling in the Counter class.
  • feat: remove header-list and toolbar-tip internal dependencies #178: The removal of internal dependencies for the header-list and toolbar-tip modules may impact how language text is handled, connecting to the main PR's changes in language retrieval methods.

Suggested reviewers

  • kagol

Poem

🐰 A Rabbit's Ode to Language Delight

In code where words dance and take flight,
getLangText() brings linguistic might!
No more direct access so plain,
Our editor speaks in every refrain
With localization's clever design! 🌐✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c6028e and 14bfc9a.

📒 Files selected for processing (7)
  • packages/fluent-editor/src/counter/index.ts (3 hunks)
  • packages/fluent-editor/src/custom-clipboard.ts (2 hunks)
  • packages/fluent-editor/src/fluent-editor.ts (2 hunks)
  • packages/fluent-editor/src/i18n/index.ts (0 hunks)
  • packages/fluent-editor/src/link/modules/tooltip.ts (1 hunks)
  • packages/fluent-editor/src/table/modules/table-operation-menu.ts (11 hunks)
  • packages/fluent-editor/src/toolbar/toolbar-tip.ts (5 hunks)
💤 Files with no reviewable changes (1)
  • packages/fluent-editor/src/i18n/index.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • packages/fluent-editor/src/link/modules/tooltip.ts
  • packages/fluent-editor/src/counter/index.ts
  • packages/fluent-editor/src/fluent-editor.ts
  • packages/fluent-editor/src/toolbar/toolbar-tip.ts
  • packages/fluent-editor/src/table/modules/table-operation-menu.ts
  • packages/fluent-editor/src/custom-clipboard.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Walkthrough

This pull request refactors the fluent-editor package by removing the i18n variable from the editor instance. It introduces a new getLangText function to replace direct access to langText, ensuring that lang is now read-only in the Quill editor.

Changes

Files Summary
src/counter/index.ts, src/custom-clipboard.ts, src/link/modules/tooltip.ts, src/table/modules/table-operation-menu.ts, src/toolbar/toolbar-tip.ts Replaced direct access to langText with getLangText function calls.
src/fluent-editor.ts Added getLangText method and made lang a read-only property.
src/i18n/index.ts Removed setLangTextToInstance method.

Copy link

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.

Actionable comments posted: 1

🧹 Nitpick comments (3)
packages/fluent-editor/src/custom-clipboard.ts (2)

157-157: Consider using textContent or ensuring content is sanitized
Using innerHTML here is acceptable if this.quill.getLangText('pasting') is always trusted (e.g. from a safe dictionary). Otherwise, you might consider using textContent to avoid any potential HTML injection.


474-474: Avoid relying on string comparison for placeholder logic
Currently, the logic picks the placeholder image based on this.quill.getLangText('img-error') === 'Image Copy Error'. This approach can break if the text is changed or localized differently in the future. A more robust strategy might be to store placeholders in your language config and retrieve them accordingly, or switch using the current language code.

packages/fluent-editor/src/fluent-editor.ts (1)

45-49: New getLangText method
This is a good step for ensuring safe fallback behavior when the i18n module is missing. However, if the key is not found in the dictionary, consider logging or returning a default text to avoid undefined outputs.

🛑 Comments failed to post (1)
packages/fluent-editor/src/table/modules/table-operation-menu.ts (1)

69-69: ⚠️ Potential issue

Potential leftover call to linkplaceholder.
It seems inconsistent to switch all references to getLangText except for this line. If linkplaceholder is intentional, please clarify its role; otherwise, consider using getLangText for language consistency.

- this.DEFAULT_COLOR_SUBTITLE = this.quill.linkplaceholder('sub-title-bg-color')
+ this.DEFAULT_COLOR_SUBTITLE = this.quill.getLangText('sub-title-bg-color')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      this.DEFAULT_COLOR_SUBTITLE = this.quill.getLangText('sub-title-bg-color')

kagol
kagol previously approved these changes Dec 30, 2024
Copy link

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/fluent-editor/src/fluent-editor.ts (2)

36-39: Consider improving type safety of the i18n module retrieval.

The getter implementation correctly encapsulates the language access and provides a good fallback mechanism. However, the type casting could be made more explicit for better type safety.

Consider this improvement:

-    const i18nModule = this.getModule('i18n') as I18N
+    const i18nModule = this.getModule('i18n') as I18N | null

This makes it clearer that getModule might return null, matching the null check in the implementation.


45-49: Enhance type safety and documentation of getLangText method.

The implementation provides a good mechanism for language text retrieval with proper fallback. However, there are several opportunities for improvement:

Consider these enhancements:

-  getLangText(name: string) {
+  /**
+   * Retrieves localized text for the given key.
+   * @param name - The key to lookup in the language configuration
+   * @returns The localized text string
+   */
+  getLangText(name: string): string {
-    const i18nModule = this.getModule('i18n') as I18N
+    const i18nModule = this.getModule('i18n') as I18N | null
     if (!i18nModule) return LANG_CONF[defaultLanguage][name]
-    return i18nModule.options.langText[name]
+    return i18nModule.options.langText?.[name] ?? LANG_CONF[defaultLanguage][name]
   }

These changes:

  1. Add JSDoc documentation
  2. Specify return type
  3. Improve type safety of module retrieval
  4. Add null safety for langText access with a fallback
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 694439b and 6c6028e.

📒 Files selected for processing (7)
  • packages/fluent-editor/src/counter/index.ts (3 hunks)
  • packages/fluent-editor/src/custom-clipboard.ts (2 hunks)
  • packages/fluent-editor/src/fluent-editor.ts (2 hunks)
  • packages/fluent-editor/src/i18n/index.ts (0 hunks)
  • packages/fluent-editor/src/link/modules/tooltip.ts (1 hunks)
  • packages/fluent-editor/src/table/modules/table-operation-menu.ts (11 hunks)
  • packages/fluent-editor/src/toolbar/toolbar-tip.ts (4 hunks)
💤 Files with no reviewable changes (1)
  • packages/fluent-editor/src/i18n/index.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • packages/fluent-editor/src/toolbar/toolbar-tip.ts
  • packages/fluent-editor/src/custom-clipboard.ts
  • packages/fluent-editor/src/counter/index.ts
  • packages/fluent-editor/src/link/modules/tooltip.ts
  • packages/fluent-editor/src/table/modules/table-operation-menu.ts
🔇 Additional comments (2)
packages/fluent-editor/src/fluent-editor.ts (2)

6-6: LGTM: Import changes align with the new language handling implementation.

The additional imports for defaultLanguage and LANG_CONF are necessary for the refactored language text retrieval mechanism.


35-49: Verify the impact of language handling changes.

The refactoring successfully achieves the PR objectives by:

  1. Making lang readonly through a getter
  2. Replacing langText variable with getLangText method

Let's verify that all consumers of these language-related APIs have been updated accordingly.

✅ Verification successful

Let me check the specific usage in the i18n module to ensure all changes are properly aligned.


The language handling changes are correctly implemented

The usage of langText and lang in i18n/index.ts is correct and doesn't need modifications because:

  1. The langText access is within the I18N module itself, which owns and manages these properties
  2. The lang assignments are part of the internal language management logic of the I18N module
  3. External access is properly handled through the new getter and method in fluent-editor.ts

The refactoring successfully encapsulates the language properties while maintaining the necessary internal access in the I18N module. No further changes are required.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining direct access to langText that needs to be updated
rg -l "\.langText\b" --type ts

# Search for direct language property assignments that should now use the getter
rg -l "\.lang\s*=" --type ts

Length of output: 189


Script:

#!/bin/bash
# Check the context of langText usage in i18n module
rg "\.langText\b" packages/fluent-editor/src/i18n/index.ts -B 2 -A 2

# Check the context of lang assignments in i18n module
rg "\.lang\s*=" packages/fluent-editor/src/i18n/index.ts -B 2 -A 2

Length of output: 1223

@zzxming
Copy link
Collaborator Author

zzxming commented Dec 30, 2024

@kagol test是不是有问题?

@kagol
Copy link
Member

kagol commented Dec 30, 2024

@kagol test是不是有问题?

加了一个 PeterCat 答疑机器人之后,控制台会报错,只要控制台报错了,就会导致测试用例失败,富文本编辑器本身的功能是没问题的。

@kagol
Copy link
Member

kagol commented Jan 2, 2025

@zzxming 合入PR #178 之后有一个冲突需要解决下。

@kagol kagol merged commit c64e61e into opentiny:main Jan 2, 2025
1 of 2 checks passed
@zzxming zzxming deleted the fix-i18n branch January 27, 2025 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants