Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

[MOC-63] Re edit feature #77

Merged
merged 6 commits into from
Jun 26, 2024
Merged

[MOC-63] Re edit feature #77

merged 6 commits into from
Jun 26, 2024

Conversation

NicoMorenoSirius
Copy link
Contributor

@NicoMorenoSirius NicoMorenoSirius commented Jun 26, 2024

Screen.Recording.2024-06-26.at.13.34.56.mov

Summary by CodeRabbit

  • New Features

    • Added onSubmit and onCancel functionality to text areas in edit mode.
    • Introduced methods to manage highlights, including removal and visibility toggle.
    • Implemented double-click text replacement with decorators.
    • Added random string generation utility.
  • Improvements

    • Enhanced highlighter functions to support additional elements and actions.
    • Optimized modification persistence and loading mechanisms for better performance.
  • Bug Fixes

    • Ensured highlights are properly removed before node replacements in the DOM.

Copy link

linear bot commented Jun 26, 2024

Copy link

coderabbitai bot commented Jun 26, 2024

Warning

Review failed

The pull request is closed.

Walkthrough

Walkthrough

Recent updates across several files in the codebase enhance the functionality of text area decoration, highlighting, and modification persistence. In summary, these changes improve handling of text area interactions, expand the capabilities of highlighting elements, refine the storage of modifications, and integrate new methods within the DOM manipulation processes. Additionally, a utility function to generate random strings has been added.

Changes

Files Modified Summary of Changes
apps/mocksi-lite/content/EditMode/decorator.ts Enhanced decorate function to accept onSubmit and onCancel parameters; updated injectTextArea.
apps/mocksi-lite/content/EditMode/highlighter.ts Updated Highlighter class with methods for handling highlights; modified highlight function to handle elements.
apps/mocksi-lite/utils.ts Adjusted several functions related to modifications; added generateRandomString function.
packages/dodom/receivers/DOMManipulator.ts Added removeHighlightNode method to ContentHighlighterInterface and integrated it into DOMManipulator.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Decorator as EditMode/decorator.ts
    participant Highlighter as EditMode/highlighter.ts
    participant Utils as utils.ts
    participant DOMManipulator as DOMManipulator.ts

    User->>Decorator: Trigger decorate()
    Decorator->>Highlighter: Call injectTextArea()
    Highlighter-->>Decorator: Return with updated text area

    User->>Highlighter: Double-click to highlight
    Highlighter->>Highlighter: Trigger highlightNode()
    Highlighter->>Highlighter: Set highlightId
    
    User->>DOMManipulator: Replace node
    DOMManipulator->>Highlighter: Call removeHighlightNode()
    Highlighter-->>DOMManipulator: Return after removing highlight

    User->>Utils: Persist modifications
    Utils->>Utils: Call generateRandomString()
    Utils-->>User: Return with random string
Loading

Poem

In code's vast sea, we made a change,
Enhancing texts in a rearrange.
Highlights glimmer, then they fade,
With submit and cancel, a new parade.
Now strings can randomize,
In Mocksi's world, a bright new prize.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

@NicoMorenoSirius NicoMorenoSirius marked this pull request as ready for review June 26, 2024 16:42
@NicoMorenoSirius NicoMorenoSirius requested a review from elg0nz June 26, 2024 16:42
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

Outside diff range and nitpick comments (1)
apps/mocksi-lite/content/EditMode/highlighter.ts (1)

[!TIP]
Codebase Verification

Ensure robust error handling around event.target usage in highlighter.ts.

The highlight function in highlighter.ts lacks error handling or conditional checks around event.target. This could lead to runtime errors if event.target is null or undefined. Consider adding appropriate error handling to enhance robustness.

  • File: apps/mocksi-lite/content/EditMode/highlighter.ts
  • Lines: 98-124
Analysis chain

Line range hint 83-122: Refactored highlight function to handle double-clicks and replace text.

The function now handles double-click events to replace highlighted text with a decorator, which enhances user interaction by allowing direct editing of highlighted text. Ensure that event handling and element replacement are robust and error-free.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify robustness of event handling and element replacement in `highlight`.

# Test: Search for error handling in event interactions. Expect: Robust error handling and element replacement.
rg --type python $'event\\.target'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify robustness of event handling and element replacement in `highlight`.

# Test: Search for error handling in event interactions. Expect: Robust error handling and element replacement.
rg --type ts 'event\.target'

Length of output: 661



Script:

#!/bin/bash
# Description: Verify robustness of event handling and element replacement in `highlight`.

# Test: Search for try-catch blocks or conditional checks around event.target usages. Expect: Robust error handling.
rg --type ts -A 3 -B 3 'event\.target' | rg -A 3 -B 3 'try|catch|if|else'

Length of output: 2965

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d9351e2 and 9b96235.

Files selected for processing (5)
  • apps/mocksi-lite/commands/Command.ts (2 hunks)
  • apps/mocksi-lite/content/EditMode/decorator.ts (2 hunks)
  • apps/mocksi-lite/content/EditMode/highlighter.ts (3 hunks)
  • apps/mocksi-lite/utils.ts (6 hunks)
  • packages/dodom/receivers/DOMManipulator.ts (2 hunks)
Additional comments not posted (19)
apps/mocksi-lite/commands/Command.ts (3)

7-7: Added previousKey to DOMModification interface.

This addition is crucial for tracking the relationship between current and previous modifications, which is essential for undo functionality and modification history tracking.


58-64: Updated execute method in SaveModificationCommand.

The method now handles the previousKey property effectively, clearing the previous modification entry if it exists. This change is aligned with the PR goals of enhancing modification tracking and management.


46-52: Adjustment to prevModifications to allow undefined values.

Allowing undefined values can help in clearing modifications without deleting keys, potentially simplifying state management. Ensure that all uses of prevModifications properly handle undefined values to avoid runtime errors.

Verification successful

Adjustment to prevModifications to allow undefined values is properly handled.

The code ensures that prevModifications handles undefined values correctly by checking for undefined before accessing properties and setting values to undefined where necessary.

  • apps/mocksi-lite/commands/Command.ts:
    • Lines around the usage of prevModifications show proper handling of undefined values.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all usages of prevModifications handle undefined values.

# Test: Search for usages of prevModifications. Expect: Proper handling of undefined values.
rg --type python $'prevModifications'

Length of output: 67



Script:

#!/bin/bash
# Description: Verify all usages of prevModifications handle undefined values.

# Test: Search for usages of prevModifications. Expect: Proper handling of undefined values.
rg --type typescript 'prevModifications'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify all usages of prevModifications handle undefined values.

# Test: Search for usages of prevModifications. Expect: Proper handling of undefined values.
rg --type ts 'prevModifications'

Length of output: 534



Script:

#!/bin/bash
# Extract lines around the occurrences of prevModifications to examine the context.
rg --type ts --context 5 'prevModifications' apps/mocksi-lite/commands/Command.ts

Length of output: 974

apps/mocksi-lite/content/EditMode/decorator.ts (5)

9-12: Enhanced decorate function to accept callback functions.

The addition of onSubmit and onCancel callbacks in the decorate function allows for more flexible and interactive text area decorations. This is a positive change, enhancing user interaction capabilities.


18-23: Updated injectTextArea call in decorate.

The injectTextArea function is now called with the new callback parameters. This change is correctly implemented and matches the updated function signature.


28-33: Refactored injectTextArea to include optional callbacks.

By adding optional onSubmit and onCancel parameters, the function now supports executing custom logic on these events, which is useful for handling user interactions more effectively.


62-62: Handling of Escape key in injectTextArea.

The addition of an onCancel callback when the Escape key is pressed is a thoughtful inclusion, allowing users to cancel edits intuitively.


70-70: Submit event handling in injectTextArea.

The function now triggers the onSubmit callback upon form submission, which is essential for capturing and handling user input correctly. Ensure that newValue is properly sanitized before use to prevent XSS vulnerabilities.

apps/mocksi-lite/content/EditMode/highlighter.ts (4)

7-8: Added highlightedNodes array to track highlighted elements.

This new property helps manage highlighted elements more effectively, allowing for easier addition and removal of highlights. This is a crucial feature for dynamic content highlighting.


14-27: Updated highlightNode method to include ID generation and tracking.

The method now generates a unique ID for each highlight and tracks it, which is essential for managing multiple highlights and their lifecycle.


30-39: Added removeHighlightNode method.

This new method provides a way to remove highlights based on the element, which is necessary for managing dynamic content where elements might be updated or removed.


41-49: Enhanced showHideHighlight method for individual elements.

The method now allows showing or hiding a specific highlighted element, which is useful for interactive applications where user actions might trigger visibility changes.

packages/dodom/receivers/DOMManipulator.ts (2)

10-10: Added removeHighlightNode method to ContentHighlighterInterface.

This addition allows for a more modular and flexible handling of node manipulations, particularly in scenarios where nodes need to be unhighlighted before being replaced or removed.


87-87: Usage of removeHighlightNode in iterateAndReplace.

The method is now used to remove highlights from nodes before replacing them, which is a good practice to ensure that the UI remains consistent and no stale highlights are left behind.

apps/mocksi-lite/utils.ts (5)

67-67: Updated saveModification to include previousKey.

The function now correctly tracks the relationship between new and old values, which is crucial for undo functionality and history tracking.


79-79: Refactored persistModifications to use a new alterations building method.

This change simplifies the process of persisting modifications by using a centralized method to build alterations, enhancing maintainability and reducing redundancy.


138-141: Updated loadPreviousModifications to use an iterable for modifications.

This update improves the efficiency and readability of the code by using an iterable to process modifications, which is a more modern and efficient approach compared to the previous index-based iteration.


Line range hint 211-225: Introduced modificationsIterable and updated buildAlterations.

These changes provide a more functional and streamlined approach to handling DOM modifications, making the code cleaner and more maintainable.


331-334: Added generateRandomString function.

This utility function generates a random string of length 5, which can be useful for creating unique identifiers or tokens. Ensure that the random string generation meets the necessary security requirements if used in sensitive contexts.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9b96235 and d8c721a.

Files selected for processing (1)
  • apps/mocksi-lite/content/EditMode/decorator.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/mocksi-lite/content/EditMode/decorator.ts

Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

Just a couple small changes

highlightDiv.style.cursor = "text";
highlightDiv.ondblclick = (event: MouseEvent) => {
(event.target as HTMLElement).style.display = "none";
if (highlightedElement.parentElement) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do an early return instead.

if (!highlightedElement?.parentElement) {
return
}

@@ -323,3 +327,8 @@ export const recordingLabel = (currentStatus: RecordingState) => {
return "Start recording";
}
};

// Generates a random string of length 5
export const generateRandomString = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use uuids instead.

import { v4 as uuidv4 } from "uuid";
return uuidv4()

Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

lgtm

@NicoMorenoSirius NicoMorenoSirius merged commit 541b21b into main Jun 26, 2024
3 checks passed
@NicoMorenoSirius NicoMorenoSirius deleted the MOC-63-re-edit-feature branch June 26, 2024 18:28
elg0nz pushed a commit that referenced this pull request Jun 28, 2024
* WIP re-editing DOM changes

* DOMManipulator now removes changed highlighted modifications

* Feature re-editing modifications

* run lint

* typo fix

* changed to uuid generated id
@elg0nz elg0nz mentioned this pull request Jul 3, 2024
elg0nz pushed a commit that referenced this pull request Jul 5, 2024
* WIP re-editing DOM changes

* DOMManipulator now removes changed highlighted modifications

* Feature re-editing modifications

* run lint

* typo fix

* changed to uuid generated id
elg0nz pushed a commit that referenced this pull request Jul 5, 2024
* WIP re-editing DOM changes

* DOMManipulator now removes changed highlighted modifications

* Feature re-editing modifications

* run lint

* typo fix

* changed to uuid generated id
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants