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

Preserve whitespace in text nodes #26

Merged
merged 3 commits into from
Sep 19, 2024
Merged

Conversation

cwhitehouse
Copy link
Contributor

@cwhitehouse cwhitehouse commented Sep 4, 2024

Description

Pass the text from a text node directly to the JSON created via to_h instead of stripping out whitespace only strings.

Reason/Reference

Text nodes that just contain whitespace have a valid use case, such as separating out two text nodes with marks. This currently prevents you from having a space between two link nodes, for example.

Example

The message...

I would like a banana milkshake

Should have four JSON text nodes that look like...

...
{ type: "text", text: "I would like a " },
{ type: "text", text: "banana", marks: [LINK MARKS] },
{ type: "text", text: " " },
{ type: "text", text: "milkshake", marks: [LINK MARKS] }
...

...but the current implementation returns...

...
{ type: "text", text: "I would like a " },
{ type: "text", text: "banana", marks: [LINK MARKS] },
{ type: "text", text: "" },
{ type: "text", text: "milkshake", marks: [LINK MARKS] }
...

... which would mean if we pass the JSON to an editor we would get:

I would like a bananamilkshake

... instead of what we expected.

- Don't remove whitespace from text when converting nodes to_h
@@ -22,7 +22,7 @@ def self.from_json(json)
end

def to_h
data = {type: type_name, text: text.presence || ""}
data = {type: type_name, text: text}
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIRC I did the presence || "" bit because text was nil sometimes and TipTap JS wouldn't render the document stating that text can't be null. It turns out that text can't be an empty string either so my fix didn't actually work. I'm actually surprised that this is removing your " " string since that isn't empty?.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah but it is blank? I guess so that is why it's falling back to this. I think your fix should be fine.

Copy link
Contributor Author

@cwhitehouse cwhitehouse Sep 12, 2024

Choose a reason for hiding this comment

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

We could also do text || "" to split the difference — though I guess it doesn't matter if empty strings are also problematic — but yeah presence is being too aggressive here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea that's a nice middle ground I think.

@chadwilken
Copy link
Collaborator

@cwhitehouse can you fix the standard lint errors?

@chadwilken chadwilken merged commit 3b3b87f into CompanyCam:master Sep 19, 2024
1 check passed
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.

3 participants