Skip to content

Highlight formula#9

Merged
hackerwins merged 2 commits into
mainfrom
formula-highlight
Sep 21, 2024
Merged

Highlight formula#9
hackerwins merged 2 commits into
mainfrom
formula-highlight

Conversation

@hackerwins

@hackerwins hackerwins commented Aug 24, 2024

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

Highlight formula

Screenshot 2024-08-24 at 9 29 07 PM

Any background context you want to provide?

What are the relevant tickets?

Fixes #

Checklist

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Introduced new functionality for managing text selections within editable content.
    • Added a new type Token and a method for processing formula strings into tokens.
    • Introduced a method to sanitize input text for secure HTML rendering.
    • Enhanced cell input handling with a content-editable area and support for rendering tokens with colors.
  • Bug Fixes

    • Improved handling of input and rendering in the worksheet component.

@coderabbitai

coderabbitai Bot commented Aug 24, 2024

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduce new types and functions in the src/formula/formula.ts, src/spreadsheet/htmlutils.ts, and src/spreadsheet/textrange.ts files, enhancing formula processing, HTML sanitization, and text selection management within contenteditable elements. Additionally, the src/spreadsheet/worksheet.ts file modifies input handling by changing the cellInput property from an HTMLInputElement to an HTMLDivElement, allowing for a more flexible input area and introducing methods for rendering tokens.

Changes

File Change Summary
src/formula/formula.ts Added type Token, method extractTokens(formula: string): Array<Token>, and retained extractReferences().
src/spreadsheet/htmlutils.ts Introduced method escapeHTML(text: string): string for sanitizing HTML input.
src/spreadsheet/textrange.ts Introduced type TextRange, and functions `toTextRange(container: HTMLDivElement): TextRange
src/spreadsheet/worksheet.ts Updated cellInput from HTMLInputElement to HTMLDivElement, added TokenColorMap, and method paintCellInput.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FormulaProcessor
    participant HTMLUtils
    participant TextRangeManager
    participant Worksheet

    User->>FormulaProcessor: Input formula
    FormulaProcessor->>FormulaProcessor: Extract tokens
    FormulaProcessor->>Worksheet: Render tokens
    User->>HTMLUtils: Input text for HTML
    HTMLUtils->>HTMLUtils: Sanitize input
    User->>TextRangeManager: Select text
    TextRangeManager->>TextRangeManager: Create TextRange
    TextRangeManager->>Worksheet: Set text range
Loading

Poem

🐇 In the meadow where formulas play,
New tokens dance in a joyful array.
Text ranges leap with a flick of a paw,
Rendering colors that leave us in awe.
With each little change, we hop and we cheer,
For the magic of code brings us all near! 🌼


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

@hackerwins
hackerwins force-pushed the formula-highlight branch 2 times, most recently from 2f4977f to 4c5455d Compare September 4, 2024 14:55
@hackerwins
hackerwins marked this pull request as ready for review September 21, 2024 11:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c0570bd and d0ba059.

Files selected for processing (3)
  • src/formula/formula.ts (2 hunks)
  • src/spreadsheet/textrange.ts (1 hunks)
  • src/spreadsheet/worksheet.ts (11 hunks)
Additional comments not posted (12)
src/spreadsheet/textrange.ts (3)

17-57: LGTM!

The toTextRange function correctly converts the current selection in a contenteditable element to a TextRange object. It handles edge cases, accurately calculates offsets, and uses a recursive traversal to visit all nodes. The implementation looks good.


62-99: LGTM!

The setTextRange function correctly sets the current selection in a contenteditable element based on a given TextRange object. It accurately finds the start and end nodes, sets the range, removes any previous selection, and focuses the container element. The implementation looks good.


1-8: JSDoc comments look good!

The JSDoc comments provide clear and accurate documentation for the TextRange type and the toTextRange and setTextRange functions. The example in the TextRange comment is helpful in understanding the concept. The comments effectively convey the purpose and behavior of the type and functions.

Also applies to: 14-16, 59-61

src/formula/formula.ts (2)

20-28: LGTM!

The Token type definition is clear and captures the relevant information for representing a token in the formula. The included properties type, start, stop, and text provide a comprehensive representation of each token.


50-74: LGTM!

The extractTokens function is implemented correctly and effectively tokenizes the input formula. Here's a breakdown of the implementation:

  • The function correctly slices the formula to remove the leading '=' character.
  • It sets up the lexer and token stream properly, ensuring all tokens are available for processing.
  • The lexer's error listeners are removed to avoid unwanted behavior.
  • The function iterates over the tokens and constructs Token objects with the correct properties, capturing the token type, start and stop indices, and the token text.
  • The token type is mapped from the numeric value to the symbolic name using the lexer's vocabulary, providing a readable representation of the token type.
  • Finally, the function returns the array of Token objects, representing the tokenized formula.

The implementation is clear, concise, and follows the expected behavior for extracting tokens from a formula.

src/spreadsheet/worksheet.ts (7)

1-2: Imports are correctly added for new functionalities

The import statements for setTextRange, toTextRange, extractTokens, and Token are appropriately included to support the new features related to text range manipulation and token extraction.


287-289: LGTM!

Adding an input event listener to cellInput to trigger paintCellInput ensures that the syntax highlighting is updated as the user types.


305-308: Verify the synchronization between cellInput and formulaInput

The code updates cellInput.innerText and formulaInput.value on keyup events to keep them in sync. Ensure this bidirectional synchronization is necessary and does not cause any unintended side effects, such as cursor position jumps or performance issues due to excessive updates.


536-536: LGTM!

The hasFormulaInCellInput method correctly checks if the cell input starts with '=' to identify formulas.


550-550: LGTM!

Setting cellInput.innerText appropriately based on withoutValue flag ensures correct initialization of the cell input during editing.


554-554: LGTM!

Calling paintCellInput after setting cellInput.innerText ensures that the syntax highlighting is applied to the initial content.


569-569: LGTM!

Clearing cellInput.innerText when hiding the cell input prevents residual content from affecting subsequent edits.

Comment thread src/spreadsheet/worksheet.ts
Comment thread src/spreadsheet/worksheet.ts
Comment thread src/spreadsheet/worksheet.ts
Comment thread src/spreadsheet/worksheet.ts
@hackerwins
hackerwins merged commit d56a25b into main Sep 21, 2024
@hackerwins
hackerwins deleted the formula-highlight branch September 21, 2024 11:42
hackerwins added a commit that referenced this pull request Apr 18, 2026
- Add Multer fileSize/fileFilter to FileInterceptor (#3)
- Move external input guard before all keyboard shortcuts (#5)
- Re-anchor image to cell under top-left after drag move (#6)
- Use drop coordinates for image insertion position (#7)
- Move file input outside desktop branch for mobile (#8)
- Gate context menu delete on onDeleteImage handler (#9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant