From 05b9ae312e076aa9f26ed5dd83ef0138c40e041d Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Fri, 31 May 2024 16:59:15 +0200 Subject: [PATCH] refined migration --- src/converters/blocks.js | 11 ++++- src/converters/fromDraftjs.js | 15 ++++-- src/converters/fromDraftjs.test.js | 50 +++++++++++++++++++- src/converters/fromHtml.js | 2 +- src/converters/fromHtml.test.js | 73 +++++++++++++++++++++++++----- 5 files changed, 130 insertions(+), 21 deletions(-) diff --git a/src/converters/blocks.js b/src/converters/blocks.js index 75b1105..c59b666 100644 --- a/src/converters/blocks.js +++ b/src/converters/blocks.js @@ -97,13 +97,20 @@ const videoBlock = (elem) => { }; const calloutBlock = (elem) => { - const convertedChild = slateTextBlock(elem); + const convertedChild = slateTextBlock(elem).value[0].children; + let value = convertedChild.map((x) => { + if (!x.type) { + return { type: 'p', children: [x] }; + } + return x; + }); + const block = { '@type': 'callout_block', style: 'base', icon: 'it-info-circle', title: '', - text: convertedChild.value, + text: value, color: 'default', }; return block; diff --git a/src/converters/fromDraftjs.js b/src/converters/fromDraftjs.js index 659c1e3..90882a4 100644 --- a/src/converters/fromDraftjs.js +++ b/src/converters/fromDraftjs.js @@ -27,8 +27,9 @@ const joinRecursively = (array) => if (Array.isArray(child)) { return joinRecursively(child); } - return child; + return child.replace('\n', '
'); }) + .filter((x) => x.length > 0) .join('') : ''; @@ -72,7 +73,9 @@ const addBreaklinesInline = (children) => { if (s.split('\n').length > 1) { return s .split('\n') - .map((child, index) => (child?.length > 0 ? `${child}
` : child)); + .map((child, index) => (child?.length > 0 ? `${child}
` : child)) + .filter((x) => x.length) + .join(''); } } return joinRecursively(children); @@ -268,9 +271,13 @@ const blocks = { 'header-six': (children, { keys }) => children.map((child, i) => `
${joinRecursively(child)}
`), callout: (children, { keys }) => - children.map((child, i) => `${joinRecursively(child)}`), + children.map( + (child, i) => `

${joinRecursively(child)}

`, + ), 'callout-bg': (children, { keys }) => - children.map((child, i) => `${joinRecursively(child)}`), + children.map( + (child, i) => `

${joinRecursively(child)}

`, + ), buttons: (children, { keys }) => { let html = children[0].filter((x) => x !== undefined); const tag = parser.parseFromString(html, 'text/html'); diff --git a/src/converters/fromDraftjs.test.js b/src/converters/fromDraftjs.test.js index 9d40204..dbdbea2 100644 --- a/src/converters/fromDraftjs.test.js +++ b/src/converters/fromDraftjs.test.js @@ -130,8 +130,10 @@ describe('convertFromDraftJS parsing draftjs callout', () => { describe('with html converter', () => { const result = converFromDraftJS(draftjs); - test('will return a tag', () => { - expect(result).toBe('Callout text with bold'); + test('will return a
tag', () => { + expect(result).toBe( + '

Callout text with bold

', + ); }); }); }); @@ -448,3 +450,47 @@ describe('convertFromDraftJS parsing draftjs blockquote', () => { }); }); }); + +// describe('prova', () => { +// const draftjs = { +// blocks: [ +// { +// key: 'jrnb', +// text: 'Trasporto delle persone con difficoltà motorie ai seggi\nIl Comune di Cavriago, in collaborazione con l’Associazione “NOI CON VOI”, organizza il trasporto delle persone disabili mediante il pulmino adibito appositamente a tale servizio.\nIl servizio verrà effettuato nei seguenti orari:\nSABATO 8 GIUGNO dalle 15.00 alle 17.00\nDOMENICA 9 GIUGNO dalle 10.00 alle 12.00.\nIl trasporto dovrà essere prenotato direttamente all’associazione al numero 333 5383663 (Ines).\n', +// type: 'callout', +// depth: 0, +// inlineStyleRanges: [ +// { +// offset: 0, +// length: 56, +// style: 'BOLD', +// }, +// { +// offset: 116, +// length: 13, +// style: 'BOLD', +// }, +// { +// offset: 285, +// length: 81, +// style: 'BOLD', +// }, +// { +// offset: 443, +// length: 11, +// style: 'BOLD', +// }, +// ], +// entityRanges: [], +// data: {}, +// }, +// ], +// entityMap: {}, +// }; +// describe('with html converter', () => { +// const result = converFromDraftJS(draftjs); +// test('will return something', () => { +// expect(result).toBe(''); +// }); +// }); +// }); diff --git a/src/converters/fromHtml.js b/src/converters/fromHtml.js index 2a0148a..003f29b 100644 --- a/src/converters/fromHtml.js +++ b/src/converters/fromHtml.js @@ -74,7 +74,7 @@ const blockFromElement = (el, defaultTextBlock, href) => { case 'IFRAME': raw = iframeBlock(el); break; - case 'Q': + case 'BLOCKQUOTE': raw = calloutBlock(el); break; default: diff --git a/src/converters/fromHtml.test.js b/src/converters/fromHtml.test.js index 6b30d5f..080ba27 100644 --- a/src/converters/fromHtml.test.js +++ b/src/converters/fromHtml.test.js @@ -649,27 +649,33 @@ describe('convertFromHTML parsing nested tags', () => { describe('convertFromHTML parsing quote', () => { test('on its own', () => { - const html = 'Callout text with bold'; + const html = + '

Callout text with bold

'; const result = convertFromHTML(html, 'slate'); expect(result).toHaveLength(1); expect(result).toEqual([ { '@type': 'callout_block', - style: 'base', + color: 'default', icon: 'it-info-circle', - title: '', + style: 'base', text: [ { children: [ { - text: 'Callout text with bold', + children: [ + { + text: 'Callout text with bold', + }, + ], + type: 'strong', }, ], - type: 'strong', + type: 'p', }, ], - color: 'default', + title: '', }, ]); }); @@ -677,7 +683,8 @@ describe('convertFromHTML parsing quote', () => { describe('convertFromHTML create callout_block block from quote tag', () => { test('on its own', () => { - const html = 'Callout text with bold'; + const html = + '
Callout text with bold
'; const result = convertFromHTML(html, 'slate'); expect(result).toHaveLength(1); @@ -898,18 +905,60 @@ describe('convertFromHTML create slate block with complex ordered list', () => { }); }); -describe('convertFromHTML create slate block with blockquote', () => { +describe('convertFromHTML create callout_block', () => { test('on its own', () => { - const html = '
text
'; + const html = '

text

'; const result = convertFromHTML(html, 'slate'); expect(result).toHaveLength(1); expect(result).toEqual([ { - '@type': 'slate', - plaintext: 'text', - value: [{ children: [{ text: 'text' }], type: 'blockquote' }], + '@type': 'callout_block', + color: 'default', + icon: 'it-info-circle', + style: 'base', + text: [ + { + children: [ + { + text: 'text', + }, + ], + type: 'p', + }, + ], + title: '', + }, + ]); + }); +}); + +describe('convertFromHTML create slate block with blockquote simple', () => { + test('on its own', () => { + const html = '
Testo
'; + + const result = convertFromHTML(html, 'slate'); + expect(result).toHaveLength(1); + expect(result).toEqual([ + { + '@type': 'callout_block', + style: 'base', + icon: 'it-info-circle', + title: '', + text: [{ type: 'p', children: [{ text: 'Testo' }] }], + color: 'default', }, ]); }); }); + +// describe.only('prova', () => { +// test('on its own', () => { +// const html = +// '

Trasporto delle persone con difficoltà motorie ai seggi
Il Comune di Cavriago, in collaborazione con l’Associazione “NOI CON VOI”, organizza il trasporto delle persone disabili mediante il pulmino adibito appositamente a tale servizio. Il servizio verrà effettuato nei seguenti orari:SABATO 8 GIUGNO dalle 15.00 alle 17.00
,DOMENICA 9 GIUGNO dalle 10.00 alle 12.00.
Il trasporto dovrà essere prenotato direttamente all’associazione al numero 333 5383663 (Ines).

'; + +// const result = convertFromHTML(html, 'slate'); +// expect(result).toHaveLength(1); +// expect(result).toEqual(); +// }); +// });