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

Fix rte source mode #5083

Merged
merged 5 commits into from Mar 25, 2024
Merged

Fix rte source mode #5083

merged 5 commits into from Mar 25, 2024

Conversation

whoselen
Copy link
Collaborator

ISSUE

Context

Make editor have tiptap schema for customizable source mode input

// Delete the below section once completed

PR Checklist

  • Description is clearly stated under Context section
  • Screenshots and the additional verifications are attached

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @whoselen - I've reviewed your changes and they look great!

General suggestions:

  • Ensure consistency in handling the 'style' attribute across all extensions to maintain uniformity.
  • Consider refactoring repeated logic for parsing and rendering the 'style' attribute into a shared utility function to improve code maintainability.
  • Review the implementation of dynamic height adjustments in the ProseMirrorWrapper for potential simplification.
  • Verify that the updates to tiptap dependencies do not introduce any regressions or compatibility issues with existing editor features.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 6 issues found
  • 🟡 Complexity: 1 issue found
  • 🟢 Docstrings: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines +83 to +104
addAttributes() {
return {
href: {
default: null,
},
target: {
default: this.options.HTMLAttributes.target,
},
rel: {
default: this.options.HTMLAttributes.rel,
},
style: {
parseHTML: (element) => element.getAttribute('style'),
renderHTML: (attributes) => {
if (!attributes.style) {
return {};
}
return { style: attributes.style };
},
},
};
},
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider refactoring to a shared utility function for style attribute parsing and rendering.

The logic for parsing and rendering the 'style' attribute is repeated across multiple extensions. A shared utility function could reduce redundancy and improve maintainability.

}
}
},
},
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Ensure consistency in handling 'style' attribute across all extensions.

The addition of 'style' attribute handling in the Image extension is a good improvement. Ensure similar attributes are consistently handled across all relevant extensions for uniformity.

@@ -0,0 +1,37 @@
import { Extension } from '@tiptap/core';
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding tests for GlobalAttributes extension.

It's important to ensure that the GlobalAttributes extension correctly parses and renders HTML attributes as expected. This could include testing various HTML elements with different attributes to verify that they are handled correctly.

@@ -0,0 +1,30 @@
import { Mark, mergeAttributes } from '@tiptap/core';
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding tests for SpanMark mark.

Testing the SpanMark mark is crucial to ensure that it correctly parses and renders span elements with styles. This could involve verifying that the style attributes are correctly applied and rendered in the output HTML.

@@ -0,0 +1,29 @@
import { Node } from '@tiptap/core';
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding tests for DivNode node.

Ensuring that the DivNode node behaves as expected is important, especially in terms of parsing and rendering div elements with styles and alignment attributes. Tests could validate the correct handling of these attributes.

@@ -22,9 +22,17 @@ const RichTextEditorWrapper = styled.div<{ $position: string }>`
}
`;

const ProseMirrorWrapper = styled.div`
const ProseMirrorWrapper = styled.div<{
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider simplifying the implementation by using default props in the styled component's template literals.

The introduction of conditional logic for setting height, min-height, and max-height based on props significantly increases the complexity of this component. While the added flexibility is appreciated, it also makes the component more dependent on external props and introduces additional cognitive load to understand its styling behavior.

To simplify, consider using default props directly in the styled component's template literals. This approach can reduce complexity while maintaining the intended functionality. For example:

const ProseMirrorWrapper = styled.div`
  overflow-y: auto;
  height: ${({ $height = 'unset' }) => $height};
  min-height: ${({ $minHeight = 'unset' }) => $minHeight};
  max-height: ${({ $maxHeight = 'unset' }) => $maxHeight};
  resize: vertical;
`;

This method streamlines the implementation by removing the need for conditional logic and makes the component's behavior more straightforward to understand at a glance.

Comment on lines +23 to +25
const width =
element.style.width || element.getAttribute('width') || null;
return width;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const width =
element.style.width || element.getAttribute('width') || null;
return width;
return element.style.width || element.getAttribute('width') || null;


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

{
overflow-y: auto;
height: ${(props) => (props.$height ? props.$height : 'unset')};
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Avoid unneeded ternary statements (simplify-ternary)

Suggested change
height: ${(props) => (props.$height ? props.$height : 'unset')};
height: ${(props) => (props.$height || 'unset')};


ExplanationIt is possible to simplify certain ternary statements into either use of an || or !.
This makes the code easier to read, since there is no conditional logic.

{
overflow-y: auto;
height: ${(props) => (props.$height ? props.$height : 'unset')};
min-height: ${(props) => (props.$minHeight ? props.$minHeight : 'unset')};
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Avoid unneeded ternary statements (simplify-ternary)

Suggested change
min-height: ${(props) => (props.$minHeight ? props.$minHeight : 'unset')};
min-height: ${(props) => (props.$minHeight || 'unset')};


ExplanationIt is possible to simplify certain ternary statements into either use of an || or !.
This makes the code easier to read, since there is no conditional logic.

{
overflow-y: auto;
height: ${(props) => (props.$height ? props.$height : 'unset')};
min-height: ${(props) => (props.$minHeight ? props.$minHeight : 'unset')};
max-height: ${(props) => (props.$maxHeight ? props.$maxHeight : 'unset')};
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Avoid unneeded ternary statements (simplify-ternary)

Suggested change
max-height: ${(props) => (props.$maxHeight ? props.$maxHeight : 'unset')};
max-height: ${(props) => (props.$maxHeight || 'unset')};


ExplanationIt is possible to simplify certain ternary statements into either use of an || or !.
This makes the code easier to read, since there is no conditional logic.

Copy link

sonarcloud bot commented Mar 25, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@Anu-Ujin Anu-Ujin merged commit 8ea9c65 into rc Mar 25, 2024
58 of 60 checks passed
@Anu-Ujin Anu-Ujin deleted the fix-RTE-source-mode branch March 25, 2024 04:01
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.

None yet

2 participants