From 12388ba973097469256a84bd451909d3c6bd70ca Mon Sep 17 00:00:00 2001 From: Luis Betancourt Date: Sat, 22 Dec 2018 00:03:25 -0500 Subject: [PATCH 1/2] Update stateFromElement to add multiple inlineStyles when returning an array --- .../src/__tests__/stateFromElement-test.js | 35 +++++++++++++++++++ .../src/stateFromElement.js | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js b/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js index 079ac6fb..5a23e8d0 100644 --- a/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js +++ b/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js @@ -146,6 +146,41 @@ describe('stateFromElement', () => { }); }); + it.only('should return multiple style from customInlineFn', () => { + let element = new ElementNode('div', [], [ + new ElementNode( + 'span', + [{style: 'font-family', value: 'sans-serif'},{style: 'font-size', value: '12px'},], + [new TextNode('Hello')] + ) + ]); + let options = { + customInlineFn(el, {Style}) { + if (el.tagName === 'SPAN') { + return Style(['CUSTOM_STYLE_1', 'CUSTOM_STYLE_2']); + } + }, + }; + let contentState = stateFromElement(element, options); + let rawContentState = removeBlockKeys(convertToRaw(contentState)); + expect(rawContentState).toEqual({ + entityMap: {}, + blocks: [ + { + text: 'Hello', + type: 'unstyled', + depth: 0, + inlineStyleRanges: [ + {offset: 0, length: 5, style: 'CUSTOM_STYLE_1'}, + {offset: 0, length: 5, style: 'CUSTOM_STYLE_2'} + ], + entityRanges: [], + data: {}, + }, + ], + }); + }); + it('should support option elementStyles', () => { let textNode = new TextNode('Superscript'); let element = new ElementNode('sup', [], [textNode]); diff --git a/packages/draft-js-import-element/src/stateFromElement.js b/packages/draft-js-import-element/src/stateFromElement.js index 3fac7735..794765e2 100644 --- a/packages/draft-js-import-element/src/stateFromElement.js +++ b/packages/draft-js-import-element/src/stateFromElement.js @@ -341,7 +341,7 @@ class ContentGenerator { if (customInline != null) { switch (customInline.type) { case 'STYLE': { - style = style.add(customInline.style); + [].concat(customInline.style).forEach(customStyle => style = style.add(customStyle)); break; } case 'ENTITY': { From 8b0886d24d08963730ed5b55b515c26337abe29a Mon Sep 17 00:00:00 2001 From: Luis Betancourt Date: Sat, 22 Dec 2018 00:15:06 -0500 Subject: [PATCH 2/2] Remove .only --- .../src/__tests__/stateFromElement-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js b/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js index 5a23e8d0..61402463 100644 --- a/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js +++ b/packages/draft-js-import-element/src/__tests__/stateFromElement-test.js @@ -146,7 +146,7 @@ describe('stateFromElement', () => { }); }); - it.only('should return multiple style from customInlineFn', () => { + it('should return multiple style from customInlineFn', () => { let element = new ElementNode('div', [], [ new ElementNode( 'span',