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 issue of duplicated blocks upon pasting an image into the Slate E… #5818

Merged
merged 9 commits into from
Jun 19, 2024

Conversation

aryan7081
Copy link
Contributor

Fixes Issue

Inside the onImageLoad function in withDeserializers extension of Volto Slate.
uploadContent adds the block using Redux and FormData, but the .then promise callback of uploadContent also adds a new Node in the Slate Editor using Transforms.insertNodes(editor, image); which eventually leads to adding multiple images in the editor.

Copy link

netlify bot commented Mar 1, 2024

Deploy Preview for volto canceled.

Name Link
🔨 Latest commit 138a634
🔍 Latest deploy log https://app.netlify.com/sites/volto/deploys/65e9ee11d325e500086f471f

Copy link

netlify bot commented Mar 1, 2024

Deploy Preview for plone-components canceled.

Name Link
🔨 Latest commit b36387a
🔍 Latest deploy log https://app.netlify.com/sites/plone-components/deploys/666d49be95110e00089759de

packages/volto-slate/news/5818.bugfix Outdated Show resolved Hide resolved
Co-authored-by: Steve Piercy <web@stevepiercy.com>
Copy link
Collaborator

@stevepiercy stevepiercy left a comment

Choose a reason for hiding this comment

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

News looks good, PR needs review by a maintainer.

@stevepiercy
Copy link
Collaborator

@plone/volto-team review requested.

Copy link
Collaborator

@stevepiercy stevepiercy left a comment

Choose a reason for hiding this comment

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

Change log is OK. Code needs review by a maintainer. @plone/volto-team

@ichim-david ichim-david self-requested a review March 2, 2024 08:24
Copy link
Member

@ichim-david ichim-david left a comment

Choose a reason for hiding this comment

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

@aryan7081 I wonder how did you test that this code change fixes the issue?
I will attach a video where I record testing your fix and the issue is still present
exactly as it was flagged into the issue so I don't understand what made you belive that this issue would be fixed by your change.

slate-image-pasting.mp4

If you still want to try to work on this issue, your pull request needs to contain
a cypress test that uploads an image, you copy the image to the clipboard and you paste it in a page as you can see from this video:

copy-uploaded-image.mp4

EDIT:
Then your test should show that when you paste the image inside the empty slate block, the image is pasted inside that
block instead of leaving that initial slate block empty.

@ichim-david
Copy link
Member

ichim-david commented Mar 2, 2024

@aryan7081 A couple of extra context as upon further testing I noticed the following:

  1. copy and pasting an image from the website or from the internet results in the correct behavior regardless of your change
  2. The code that you disabled indeed prevents 2 images from being uploaded when pasting from the os (MAC OS Sonoma in my case)
  3. That is not enough to prevent the slate empty block from being added or from the image being inserted in the first slate block when pasting from the os clipboard for an image copied from Finder app.
    It should be tested also in other os systems such as Linux or Windows

@aryan7081
Copy link
Contributor Author

@ichim-david Sorry for the delay. Working on this and adding tests as well. Can I test this on linux and windows vm?

@ichim-david
Copy link
Member

@ichim-david Sorry for the delay. Working on this and adding tests as well. Can I test this on linux and windows vm?

@aryan7081 do the minimum test first on whatever platform you're working on and if you can reproduce the issue there then try to fix it, there is no point in doing cross-testing until necessary.

@aryan7081
Copy link
Contributor Author

aryan7081 commented Mar 7, 2024

The createImageBlock function within packages/volto-slate/src/utils/volto-blocks.js currently utilizes addBlock to insert a new block at the index position plus one. However, the addBlock function adds multiple blocks: one for the block being added and another empty block if the type is not 'slate'.
To address this, I've attempted to resolve the issue by modifying the createImageBlock function as follows:

const currBlockId = properties.blocks_layout.items[index];
const currBlockHasValue = blockHasValue(properties.blocks[currBlockId]);
let id, newFormData;
if (currBlockHasValue) {
  [id, newFormData] = addBlock(properties, 'image', index + 1);
  newFormData = changeBlock(newFormData, id, { '@type': 'image', url });
} else {
  [id, newFormData] = insertBlock(properties, currBlockId, { '@type': 'image', url });
}

This change ensures that a block is inserted at the (index - 1) position if the current(index) block is empty. Otherwise, it proceeds to create another block as usual.
Please let me know your thoughts or any further adjustments needed.

Will push cypress tests once this approach is validated.

Screen.Recording.2024-03-07.at.9.01.39.PM.MOV

@stevepiercy
Copy link
Collaborator

@aryan7081 please run code quality checks locally before pushing commits. See Linting.

However our documentation lacks the specific commands that need to be run, so I don't blame you for not running undocumented commands. See #5341

The CI jobs call https://github.com/plone/volto/blob/main/.github/workflows/code-analysis.yml. You can look at it for the commands that do not pass the checks, and try running them locally.

@aryan7081
Copy link
Contributor Author

@stevepiercy Working on cypress tests.

Copy link
Member

@sneridagh sneridagh left a comment

Choose a reason for hiding this comment

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

@aryan7081 I tried it out, and it does work well indeed.

We would need the tests if possible. We improved the documentation lately:
https://6.docs.plone.org/volto/contributing/acceptance-tests.html

Also, the code analysis checks are failing.

Could you imagine in work on this?

@sneridagh sneridagh added this to the 18.x.x milestone May 27, 2024
* main: (42 commits)
  Release 18.0.0-alpha.35
  Release @plone/types 1.0.0-alpha.16
  (fix): export getFieldURL from Url.js in helpers (plone#6100)
  Improve container detection, `config.settings.containerBlockTypes` is no longer needed (plone#6099)
  Support nested directories in public folder add-on sync folders both … (plone#6098)
  Release 18.0.0-alpha.34
  Add generated new declarations for a34, just in case (plone#6097)
  Release @plone/slate 18.0.0-alpha.13
  Release @plone/registry 1.6.0
  Release @plone/types 1.0.0-alpha.15
  Add support for reading the add-ons `tsconfig.json` paths (plone#6096)
  Fixes plone#6046 pass proper defaults to align and size fields of Image block (plone#6093)
  bug fix. relations control panel. Restrict eglible relation targets a… (plone#6092)
  Fix Uncaught RangeError: date value is not finite in DateTimeFormat.format (plone#6088)
  Add optional `token` parameter to ploneClient initialization (plone#6077)
  Blocks Layout Navigator (plone#5642)
  Fix internalUrl  Widget to Reflect Prop Changes via onChangeBlock (plone#6036)
  [types] Improve styleClassNameExtenders types, Icon component JSDoc t… (plone#6095)
  Fix link in pop-up RelationsMatrix.jsx (plone#6085)
  Fix deselecting lists. (plone#6080)
  ...
@sneridagh
Copy link
Member

@plone/volto-team This works fine for me, pity is that we don't have any Cypress test for it. I don't even know if it's possible to model that interaction I've investigated it a bit this morning, we have clipboard mocks for pasting text but I have no clue how to mock the image data. I've tried though, if someone wants to take over:

https://github.com/plone/volto/tree/testingpasteimages

For the rest I'd merge it.

@ichim-david I'd appreciate another review from you!

@sneridagh sneridagh requested a review from ichim-david June 15, 2024 09:44
@sneridagh sneridagh requested a review from stevepiercy June 15, 2024 09:44
@ichim-david
Copy link
Member

@sneridagh will have a look at this tonight.

@ichim-david
Copy link
Member

@sneridagh Tonight ended up this morning :)
Tested it again and there is no more duplication of images but I still find some strange behavior
or some differences in behavior at least.

Pasting from os clipboard will upload and use an image block.
Pasting from the same website or other websites will add an img tag with the link to that source.
Ex:
image-block
img-tag-external
same-web-image

Not saying that it's the fault of this ticket. It just seems to me less useful this img tag linking since you don't have
control over size or alignment like you have when it's uploaded through the image block.

@sneridagh
Copy link
Member

@ichim-david paste from a website you mean copy the html with along text or only the image? Is this different than before?

@ichim-david
Copy link
Member

@ichim-david paste from a website you mean copy the html with along text or only the image? Is this different than before?

@sneridagh right click copy image only. It's not different from before.
I guess it would be useful to copy paste alongside text otherwise it's better to drag and image from desktop to get
proper image block usage alongside upload and image options.

Ready for merging from my part.

@sneridagh sneridagh merged commit 3311686 into plone:main Jun 19, 2024
44 checks passed
sneridagh added a commit that referenced this pull request Jun 20, 2024
* main:
  Make dynamic/live teaser without additional requests (#6024)
  Fix issue of duplicated blocks upon pasting an image into the Slate E… (#5818)
  Remove `api` folder. Add `backend` folder using latest backend best practices (#6110)
  Rename files with wrong extension `js->jsx` when they contain JSX. (#6114)
  Improve shadowing by including the support for `js->jsx` extensions in… (#6113)
  issue-5452 markdown shortcut (#5495)
sneridagh added a commit that referenced this pull request Jun 20, 2024
* main:
  Make dynamic/live teaser without additional requests (#6024)
  Fix issue of duplicated blocks upon pasting an image into the Slate E… (#5818)
  Remove `api` folder. Add `backend` folder using latest backend best practices (#6110)
  Rename files with wrong extension `js->jsx` when they contain JSX. (#6114)
  Improve shadowing by including the support for `js->jsx` extensions in… (#6113)
  issue-5452 markdown shortcut (#5495)
sneridagh added a commit that referenced this pull request Jun 26, 2024
* main: (73 commits)
  Release 18.0.0-alpha.36
  Rename missing command
  Image widget PR as breaking (#6125)
  Release @plone/slate 18.0.0-alpha.14
  Release @plone/registry 1.7.0
  Rename Makefile targets (#6104)
  fix: nonContentRoutes diff path (#6102)
  Automatically set the label to `03 type: feature (plip)` for PLIPs (#6122)
  Add ImageWidget with upload/drop/external and inline/widget capabilities (#5607)
  Ensure that sidebar field will not steal focus when metadata is edited (#5983)
  Prevent duplicated UUUIDs in inner blocks when copying container blocks (#6112)
  feat: handle breakList in detached TextBlockEditor (#6106)
  Make dynamic/live teaser without additional requests (#6024)
  Fix issue of duplicated blocks upon pasting an image into the Slate E… (#5818)
  Remove `api` folder. Add `backend` folder using latest backend best practices (#6110)
  Rename files with wrong extension `js->jsx` when they contain JSX. (#6114)
  Improve shadowing by including the support for `js->jsx` extensions in… (#6113)
  issue-5452 markdown shortcut (#5495)
  Release 18.0.0-alpha.35
  Release @plone/types 1.0.0-alpha.16
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
4 participants