From ca2bc803a109b038a12c2072594749f777e92219 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Wed, 11 Oct 2023 19:43:18 +0300 Subject: [PATCH] fix(paste): delay onPaste call to be sure that block is ready (#2506) --- docs/CHANGELOG.md | 1 + src/components/modules/blockManager.ts | 11 ++++++++- test/cypress/tests/copy-paste.cy.ts | 31 ++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index eaae0a56..f9de3fb1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -23,6 +23,7 @@ - `Improvement` - `blocks.update(id, data)` now can accept partial data object — it will update only passed properties, others will remain the same. - `Improvement` - `blocks.update(id, data)` now will trigger onChange with only `block-change` event. - `Improvement` - `blocks.update(id, data)` will return a promise with BlockAPI object of the changed block. +- `Fix` — Some Block were be skipped on saving after pasting them as HTML ### 2.27.2 diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index 0f5a9de8..7f350af5 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -399,7 +399,16 @@ export default class BlockManager extends Module { }); try { - block.call(BlockToolAPI.ON_PASTE, pasteEvent); + /** + * We need to call onPaste after Block will be ready + * because onPaste could change tool's root element, and we need to do that after block.watchBlockMutations() bound + * to detect tool root element change + * + * @todo make this.insert() awaitable and remove requestIdleCallback + */ + window.requestIdleCallback(() => { + block.call(BlockToolAPI.ON_PASTE, pasteEvent); + }); } catch (e) { _.log(`${toolName}: onPaste callback call is failed`, 'error', e); } diff --git a/test/cypress/tests/copy-paste.cy.ts b/test/cypress/tests/copy-paste.cy.ts index 132c1c90..4862c510 100644 --- a/test/cypress/tests/copy-paste.cy.ts +++ b/test/cypress/tests/copy-paste.cy.ts @@ -1,8 +1,10 @@ import Header from '@editorjs/header'; import Image from '@editorjs/simple-image'; import * as _ from '../../../src/components/utils'; -import { BlockTool, BlockToolData } from '../../../types'; +import { BlockTool, BlockToolData, OutputData } from '../../../types'; import $ from '../../../src/components/dom'; +import type EditorJS from '../../../types/index'; + describe('Copy pasting from Editor', function () { context('pasting', function () { @@ -111,7 +113,7 @@ describe('Copy pasting from Editor', function () { tools: { header: Header, }, - }); + }).as('editorInstance'); cy.get('[data-cy=editorjs]') .get('div.ce-block') @@ -121,6 +123,9 @@ describe('Copy pasting from Editor', function () { 'text/html': '

First block

Second block

', }); + /** + * Check inserted blocks + */ cy.get('[data-cy=editorjs]') .get('h2.ce-header') .should('contain', 'First block'); @@ -128,6 +133,28 @@ describe('Copy pasting from Editor', function () { cy.get('[data-cy=editorjs]') .get('div.ce-paragraph') .should('contain', 'Second block'); + + /** + * Check saved data as well + */ + cy.get('@editorInstance') + .then(async (editor) => { + cy.wrap(await editor.save()) + .then((data) => { + /** + *

has been correctly saved + */ + expect(data.blocks[0].type).to.eq('header'); + expect(data.blocks[0].data.text).to.eq('First block'); + expect(data.blocks[0].data.level).to.eq(2); + + /** + *

has been correctly saved + */ + expect(data.blocks[1].type).to.eq('paragraph'); + expect(data.blocks[1].data.text).to.eq('Second block'); + }); + }); }); it('should parse pattern', function () {