From b4d7acf823d73cfc2942a75bf219620257eb0c13 Mon Sep 17 00:00:00 2001 From: Michael Haglund Date: Thu, 30 May 2019 15:16:46 -0500 Subject: [PATCH] fix(DraftableState return types): Fix DraftableState return types --- src/lib/DraftableState.js | 10 +++++----- stories/index.stories.js | 8 +++++++- test/DraftableState.test.js | 8 ++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/lib/DraftableState.js b/src/lib/DraftableState.js index 40a8ff1..7800ff1 100644 --- a/src/lib/DraftableState.js +++ b/src/lib/DraftableState.js @@ -11,12 +11,12 @@ export const FORMAT_MARKDOWN = 'markdown'; type FormatType = typeof FORMAT_HTML | typeof FORMAT_MARKDOWN; export default class DraftableState { - static createFromString(markup: string, format: FormatType) { + static createFromString(markup: string, format: FormatType):EditorState { switch (format) { case FORMAT_HTML: - return stateFromHTML(markup); + return EditorState.createWithContent(stateFromHTML(markup)); case FORMAT_MARKDOWN: - return stateFromMarkdown(markup); + return EditorState.createWithContent(stateFromMarkdown(markup)); default: throw new Error(`Format not supported: ${format}`); } @@ -25,9 +25,9 @@ export default class DraftableState { static toString(editorState:EditorState, format: FormatType):string { switch (format) { case FORMAT_HTML: - return stateToHTML(editorState); + return stateToHTML(editorState.getCurrentContent()); case FORMAT_MARKDOWN: - return stateToMarkdown(editorState); + return stateToMarkdown(editorState.getCurrentContent()); default: throw new Error(`Format not supported: ${format}`); } diff --git a/stories/index.stories.js b/stories/index.stories.js index c4ee8c3..e07d8b7 100644 --- a/stories/index.stories.js +++ b/stories/index.stories.js @@ -1,6 +1,6 @@ import React from 'react'; import { storiesOf } from '@storybook/react'; -import { Draftable } from '../src'; +import { Draftable, DraftableState } from '../src'; import BoldIcon from '../src/icons/TextBold'; storiesOf('Draftable', module) @@ -27,4 +27,10 @@ storiesOf('Draftable', module) return ( ); + }) + .add('with initial state', () => { + const initialState = DraftableState.createFromString('

Test bolded

', 'html'); + return ( + + ); }); diff --git a/test/DraftableState.test.js b/test/DraftableState.test.js index a9cf333..8b94cff 100644 --- a/test/DraftableState.test.js +++ b/test/DraftableState.test.js @@ -7,8 +7,8 @@ describe('DraftableState', () => { test('Converts HTML to DraftJS state', () => { const convertedHTML = DraftableState.createFromString(HTML, FORMAT_HTML); - expect(convertedHTML.hasText()).toBeTruthy(); - expect(convertedHTML.getPlainText()).toEqual('Test Document\nThis is the text I would like to initialize state with.'); + expect(convertedHTML.getCurrentContent().hasText()).toBeTruthy(); + expect(convertedHTML.getCurrentContent().getPlainText()).toEqual('Test Document\nThis is the text I would like to initialize state with.'); }); test('Converts DraftJS state to HTML', () => { @@ -18,8 +18,8 @@ describe('DraftableState', () => { test('Converts Markdown to DraftJS state', () => { const convertedHTML = DraftableState.createFromString(MARKDOWN, FORMAT_MARKDOWN); - expect(convertedHTML.hasText()).toBeTruthy(); - expect(convertedHTML.getPlainText()).toEqual('Test Document\nThis is the text I would like to initialize state with.'); + expect(convertedHTML.getCurrentContent().hasText()).toBeTruthy(); + expect(convertedHTML.getCurrentContent().getPlainText()).toEqual('Test Document\nThis is the text I would like to initialize state with.'); }); test('Converts DraftJS state to Markdown', () => {