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

Make images block nodes #3282

Merged
merged 8 commits into from
Nov 15, 2022
Merged

Make images block nodes #3282

merged 8 commits into from
Nov 15, 2022

Conversation

mejo-
Copy link
Member

@mejo- mejo- commented Oct 19, 2022

Use markdown-it-image-figures to render standalone images inside
<figure> elements instead of <p> when markdown source gets parsed
into HTML. This allows to treat images as block elements by TipTap.

Also keep support for inline images to not break markdown files that
have inline images.

Summary

@mejo- mejo- force-pushed the fix/image_block branch 2 times, most recently from fcd9609 to 902d326 Compare October 19, 2022 11:16
@mejo-
Copy link
Member Author

mejo- commented Oct 19, 2022

Mh, seems like inserting an image as first element in a new document is broken now 😢

@mejo- mejo- force-pushed the fix/image_block branch 3 times, most recently from 3aba528 to ba7c669 Compare October 24, 2022 10:37
@mejo-
Copy link
Member Author

mejo- commented Oct 25, 2022

So @julien-nc and me spent some three more hours debugging the main remaining issue with this PR: inserting a (block node) image as first element into an empty editor breaks due to us doing this.$editor.chain().setImage().insertContent('<br />').focus().run() here.

It turns out to be a Tiptap (or maybe even ProseMirror) bug. I reported ueberdosis/tiptap#3355 upstream.

@mejo- mejo- self-assigned this Oct 25, 2022
@juliusknorr juliusknorr added the bug Something isn't working label Oct 26, 2022
@mejo- mejo- force-pushed the fix/image_block branch 3 times, most recently from 635e5af to e4d4f6b Compare October 31, 2022 14:11
@cypress
Copy link

cypress bot commented Nov 8, 2022



Test summary

106 0 0 0Flakiness 2


Run details

Project Text
Status Passed
Commit 35f1e2e ℹ️
Started Nov 15, 2022 7:30 AM
Ended Nov 15, 2022 7:36 AM
Duration 06:01 💡
OS Linux Ubuntu -
Browser Electron 106

View run in Cypress Dashboard ➡️


Flakiness

workspace.spec.js Flakiness
1 Workspace > formats text
sections.spec.js Flakiness
1 Content Sections > Heading anchors > Anchor scrolls into view

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@mejo-
Copy link
Member Author

mejo- commented Nov 8, 2022

After some further debugging, we came up with a mostly working solution that

  • Always adds a newline when inserting an image and places the cursor after that newline (like before)
  • Works well when inserting several images at once
  • Works well with dragging & dropping several images
  • Has a small issue when inserting an image in between text

@max-nextcloud and me agreed that it is better to get this merged now and follow-up by looking into the underlying Tiptap issue later on.

@max-nextcloud are you ok with merging this as is, or do you want to further look into it?

@juliusknorr
Copy link
Member

Can you quickly summarize the remaining issue, so we can file this as a follow up?

@juliusknorr juliusknorr added this to the Nextcloud 26 milestone Nov 9, 2022
@mejo-
Copy link
Member Author

mejo- commented Nov 9, 2022

Can you quickly summarize the remaining issue, so we can file this as a follow up?

Sure, sorry for not doing so in the first place 🙈

So the fix involves moving the cursor two steps further after inserting the image and before inserting the hard break (if the image is not inserted as first element, which needs other special treatment). This is necessary because per default the cursor is in front of the image after inserting it, but we want to add the hard break afterwards.

If you insert an image in between text, this results in the linebreak being inserted two characters further for the moment. I think that's an acceptable tradeoff given that images are block nodes now and most likely it will not happen that often that people insert them in the middle of text anyway.

Here's the relevant code:

const selection = this.$editor.view.state.selection
if (!selection.empty) {
// If inserted image is first element, it is selected and would get overwritten by
// subsequent editor inserts (see tiptap#3355). So unselect the image by placing
// the cursor at the end of the selection.
this.$editor.commands.focus(selection.to)
} else {
// Place the cursor after the inserted image node
this.$editor.commands.focus(selection.to + 2)
}

If you have a quick idea how to detect that the cursor (i.e. the focus, this.$editor.view.state.selection) is in the middle of some text and not at the end of a paragraph, we could add this as another special handling. But instead of waiting longer and spending another bunch of hours on this minor issue, my suggestion would be to merge this PR as is now 😉

@max-nextcloud
Copy link
Collaborator

/compile

@max-nextcloud
Copy link
Collaborator

@mejo- There's still a test failing.

I'm not sure what to make of this. I think we are adding two new lines on purpose in the toMarkdown function of the block image node.

Is this really required. I think it's unfortunate if we change the markdown syntax without any changes being made to the document. Why are those newlines needed? Can't we say every line that only contains an image is considered a block image even if no empty lines follow?

Ahh... i guess markdown parsers will then think the line is part of a larger paragraph... Hmm... difficult.

@max-nextcloud
Copy link
Collaborator

I think i will exclude 2f7ff70 for now so images still render the way they used to even if inline.

mejo- and others added 7 commits November 15, 2022 08:18
Use markdown-it-image-figures to render standalone images inside
`<figure>` elements instead of `<p>` when markdown source gets parsed
into HTML. This allows to treat images as block elements by TipTap.

Also keep support for inline images to not break markdown files that
have inline images.

Fixes: #2873

Signed-off-by: Jonas <[email protected]>
Replace `<p>` with `<figure>` now that we use markdown-it-image-figures.

Signed-off-by: Jonas <[email protected]>
Needed to prevent block images getting merged with next paragraph.

Signed-off-by: Jonas <[email protected]>
Tiptap `setImage()` marks the added image as selected when added as
first element in the document), which leads to the image being
overwritten by subsequent `insertContent()` command.

See ueberdosis/tiptap#3355 for the relevant
upstream bug.

Mitigate this bug by explicitly placing the cursor at the end of the
selection between adding the image and the line break.

Signed-off-by: Jonas <[email protected]>
Right now we add an empty line after each image so it lives in its own paragraph.

This is not needed at the end of the file
and we should preserve the number of newlines instead (#3428).

But for now it should not block merging the block images pr #3282.

Signed-off-by: Max <[email protected]>
Copy link
Collaborator

@max-nextcloud max-nextcloud left a comment

Choose a reason for hiding this comment

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

I think this can be merged now. I created issues for the remaining things and even if they do not get fixed this is an improvement over the status quo.

@max-nextcloud
Copy link
Collaborator

/compile

Signed-off-by: nextcloud-command <[email protected]>
@max-nextcloud
Copy link
Collaborator

@juliushaertl @mejo- I'm not sure if we should backport this.

Right now in 25 images and attachments are often in the same paragraph as the following content. Turning that content into say a heading then also toggles the image as it's part of that paragraph. I think that's pretty bad. At the same time this change is pretty profound and we have not really tested it so much. It will also require a bunch of follow up changes. I don't think they would need to be backported - but still.

I wonder if there would be a less intrusive fix we could apply to stable25.

@max-nextcloud max-nextcloud merged commit 4403f08 into master Nov 15, 2022
@delete-merged-branch delete-merged-branch bot deleted the fix/image_block branch November 15, 2022 12:12
@mejo-
Copy link
Member Author

mejo- commented Nov 15, 2022

/backport 0c496fa,4b63cb684897f5512d86ed00cf290b3fffa1df43,5ee1d6b7c6e9edc03222ae470c456209e90b1a7b,59b2b68337bda059a71fbf81e2748f6d7c4d647b,47a203748157f922c17eb297d495d0b247998df0,f4b660d893ca3471310ac65a0a0ea9a475913ea9,8f9a6c4d42814d20916c8bec0a4f916b282a982b to stable25

backportbot-nextcloud bot pushed a commit that referenced this pull request Nov 15, 2022
Right now we add an empty line after each image so it lives in its own paragraph.

This is not needed at the end of the file
and we should preserve the number of newlines instead (#3428).

But for now it should not block merging the block images pr #3282.

Signed-off-by: Max <[email protected]>
mejo- pushed a commit that referenced this pull request Nov 15, 2022
Right now we add an empty line after each image so it lives in its own paragraph.

This is not needed at the end of the file
and we should preserve the number of newlines instead (#3428).

But for now it should not block merging the block images pr #3282.

Signed-off-by: Max <[email protected]>
mejo- pushed a commit that referenced this pull request Nov 15, 2022
Right now we add an empty line after each image so it lives in its own paragraph.

This is not needed at the end of the file
and we should preserve the number of newlines instead (#3428).

But for now it should not block merging the block images pr #3282.

Signed-off-by: Max <[email protected]>
mejo- pushed a commit that referenced this pull request Nov 15, 2022
Right now we add an empty line after each image so it lives in its own paragraph.

This is not needed at the end of the file
and we should preserve the number of newlines instead (#3428).

But for now it should not block merging the block images pr #3282.

Signed-off-by: Max <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attachments are formatted together with the next paragraph
4 participants