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

desktop: fix issues with codeblock toggling #6435

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

seanwynwins
Copy link
Contributor

Addresses #6321

Copy link
Contributor

@thecodrr thecodrr left a comment

Choose a reason for hiding this comment

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

Thank you so much for fixing this!

I noticed a bug as well where toggling the code block added an empty line above it. Not sure why that's happening.

Comment on lines 240 to 241
let hasParagraph = false;

state.doc.nodesBetween(from, to, (node) => {
if (node.type.name === "paragraph") {
hasParagraph = true;
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of checking whether selection has paragraph, we can just check whether a codeblock node is active using isNodeActive function.

Copy link
Contributor Author

@seanwynwins seanwynwins Aug 29, 2024

Choose a reason for hiding this comment

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

Are you saying that the selected text should only be converted if there are no active codeblock nodes?

I wonder if the behavior should be to convert all selected text to a codeblock, if any one of the selected nodes is of a type other than codeblock?

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you saying that the selected text should only be converted if there are no active codeblock nodes?

Yes. Because if the cursor is inside a codeblock we'll just convert it into a paragraph. Otherwise, everything gets converted into a codeblock.

Comment on lines 249 to 254
return commands.insertContent({
type: this.name,
attrs: {
...attributes,
id: createCodeblockId()
},
content: [
{
type: "text",
text: state.doc.textBetween(from, to, "\n")
}
]
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be simplified like so:

return commands.insertContent(
  this.type.create(
    { ...attributes, id: createCodeblockId() },
    state.schema.text(state.doc.textBetween(from, to, "\n"))
  )
);

However, I don't recommend depending on insertContent for replacing the selection. Instead we can directly use the replaceSelectionWith method on Transaction:

          const { from, to } = tr.selection;
          const text = tr.doc.textBetween(from, to, "\n");
          tr.replaceSelectionWith(
            this.type.create(
              { ...attributes, id: createCodeblockId() },
              state.schema.text(text)
            )
          );

Not to mention, that after replacing the node we also need to set the selection like so:

          commands.setTextSelection({ from, to: tr.mapping.map(to) });

This is all very rough so you might need to make adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, just changed this

@seanwynwins
Copy link
Contributor Author

seanwynwins commented Aug 29, 2024

Thank you so much for fixing this!

I noticed a bug as well where toggling the code block added an empty line above it. Not sure why that's happening.

No problem! I think using the transactions directly solved this issue. Also made the tests a bit more thorough to make sure there's no other nodes other than the codeblock

@thecodrr
Copy link
Contributor

thecodrr commented Sep 2, 2024

@seanwynwins I think you didn't push the changes.

@seanwynwins
Copy link
Contributor Author

seanwynwins commented Sep 3, 2024

@seanwynwins I think you didn't push the changes.

Oh I didn't push yet because I wanted to check on this-did you have any thoughts?:

#6435 (comment)

@seanwynwins
Copy link
Contributor Author

@seanwynwins I think you didn't push the changes.

Just got back from vacation, will push tomorrow

@seanwynwins
Copy link
Contributor Author

seanwynwins commented Sep 16, 2024

Sorry for the delay, can you review? @thecodrr

@seanwynwins seanwynwins force-pushed the togglecodeblockrevision branch 3 times, most recently from 8e0774e to 4822005 Compare September 16, 2024 06:54
@thecodrr thecodrr merged commit 76bfbc7 into streetwriters:master Sep 16, 2024
@thecodrr
Copy link
Contributor

Thanks!

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.

2 participants