Skip to content

Commit

Permalink
fix(DraftableState return types): Fix DraftableState return types
Browse files Browse the repository at this point in the history
  • Loading branch information
hagmic committed May 30, 2019
1 parent 3646740 commit b4d7acf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/lib/DraftableState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand All @@ -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}`);
}
Expand Down
8 changes: 7 additions & 1 deletion stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -27,4 +27,10 @@ storiesOf('Draftable', module)
return (
<Draftable toolbarConfig={toolbarConfig} />
);
})
.add('with initial state', () => {
const initialState = DraftableState.createFromString('<p>Test <b>bolded</b></p>', 'html');
return (
<Draftable initialState={initialState} />
);
});
8 changes: 4 additions & 4 deletions test/DraftableState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit b4d7acf

Please sign in to comment.