Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set default value #357

Closed
ok713 opened this issue Jun 15, 2017 · 26 comments
Closed

how to set default value #357

ok713 opened this issue Jun 15, 2017 · 26 comments

Comments

@ok713
Copy link

ok713 commented Jun 15, 2017

No description provided.

@ddelrio1986
Copy link

import React, { Component } from 'react'
import { EditorState, ContentState, convertFromHTML } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

class MyEditor extends Component {
  constructor(props) {
    super(props)
    this.state = {
      editorState: EditorState.createWithContent(
        ContentState.createFromBlockArray(
          convertFromHTML('<p>My initial content.</p>')
        )
      ),
    }
  }

  render() {
    return <Editor editorState={this.state.editorState} />
  }
}

export default MyEditor

@jpuri
Copy link
Owner

jpuri commented Jul 3, 2017

Thanks @ddelrio1986 ,

@ok713 I hope that helps, I am closing the issue - plz feel free to re-open if you need more information.

@jpuri jpuri closed this as completed Jul 3, 2017
@revoltaxz
Copy link

in the case how do I get the last value from the database and enter in the editor?

@frankFree
Copy link

just use defaultEditorState
image

@justinlazaro-ubidy
Copy link

textAlign center not working on defaultEditorState? anyone have an idea about this? thanks.

@justinlazaro-ubidy
Copy link

@ddelrio1986 cant type on wysiwyg

import React, { Component } from 'react'
import { EditorState, ContentState, convertFromHTML } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

class MyEditor extends Component {
  constructor(props) {
    super(props)
    this.state = {
      editorState: EditorState.createWithContent(
        ContentState.createFromBlockArray(
          convertFromHTML('<p>My initial content.</p>')
        )
      ),
    }
  }

  render() {
    return <Editor editorState={this.state.editorState} />
  }
}

export default MyEditor

@jpuri
Copy link
Owner

jpuri commented Feb 4, 2018

@justinlazaro-ubidy: to make editor an un-controlled component you need to use defaultEditorState. Here you have made it a controlled component.

@ddelrio1986
Copy link

@jpuri is right. You need to still add a normal onChange event handler to handle typing within the component just like with any other controlled input component.

@wdse005
Copy link

wdse005 commented Oct 14, 2020

It works and I can get data into editor, but when I type something then it auto undo all content and not able to write something else.

Please someone help me......

import React, { Component } from 'react'
import { EditorState, ContentState, convertFromHTML } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

class MyEditor extends Component {
constructor(props) {
super(props)
this.state = {
editorState: EditorState.createWithContent(
ContentState.createFromBlockArray(
convertFromHTML('

My initial content.

')
)
),
}
}

render() {
return
}
}

export default MyEditor

@waheedsaifi
Copy link

sdfdsfdsfsdfsdfdsfsdfsd

@raihan-muhammad
Copy link

how about set default value in react hooks? please help

@rizkisetyawan
Copy link

import React, { Component } from 'react'
import { EditorState, ContentState, convertFromHTML } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

class MyEditor extends Component {
  constructor(props) {
    super(props)
    this.state = {
      editorState: EditorState.createWithContent(
        ContentState.createFromBlockArray(
          convertFromHTML('<p>My initial content.</p>')
        )
      ),
    }
  }

  render() {
    return <Editor editorState={this.state.editorState} />
  }
}

export default MyEditor

How with typescript ?
Screen Shot 2021-05-10 at 06 20 18

@ghazale-javaheri
Copy link

ghazale-javaheri commented Jul 19, 2021

is there any way to set default value without <p></p> , i want it would be just an empty string ''?

@WelltonQ
Copy link

existe alguma maneira de definir o valor padrão sem <p></p>, eu quero que seja apenas uma string vazia ''?

value.replace(/(<([^>]+)>)/gi, "");

@buskerone
Copy link

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

@buskerone
Copy link

buskerone commented Nov 25, 2021

@raihan-muhammad

how about set default value in react hooks? please help

Please check if the solution above works for you.

@naserexe
Copy link

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

This solution works for me. Thank you.

@Saqib92
Copy link

Saqib92 commented Dec 28, 2021

Not able to Edit by doing:

EditorState.createWithContent(
     ContentState.createFromBlockArray(
          convertFromHTML(res.data.page_content)
     )
)

@ghost
Copy link

ghost commented Jan 13, 2022

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

Many thanks

@friskomayufid
Copy link

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

Thankyou so much!

@hfabio
Copy link

hfabio commented Jan 30, 2022

Works, but lost all formatting :(

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

@Deepak9811
Copy link

import React, { Component } from 'react';
import { EditorState, convertToRaw, ContentState, convertFromHTML } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import dynamic from 'next/dynamic';
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";

const Editor = dynamic(
() => import('react-draft-wysiwyg').then(mod => mod.Editor),
{ ssr: false }
)

let htmlToDraft = null;
if (typeof window === 'object') {
htmlToDraft = require('html-to-draftjs').default;
}

export default class EditorConvertToHTML extends Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
contentState: ""
}
}

componentDidMount() {

    const html = '<p>Hey this <strong>editor</strong> rocks d😀</p>';
    const contentBlock = htmlToDraft(html);
    console.log(contentBlock)
    if (contentBlock) {
        const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
        const editorState = EditorState.createWithContent(contentState);
        console.log(editorState)
        this.setState(
            {
                editorState: EditorState.createWithContent(
                    ContentState.createFromBlockArray(
                        convertFromHTML(html)
                    )
                )
            }
        )
    }



}

onEditorStateChange = (editorState) => {
    this.setState({
        editorState,
    });
};

render() {
    const { editorState } = this.state;
    console.log(this.state.editorState.getCurrentContent())
    return (
        <div>
            <Editor
                editorState={editorState}
                wrapperClassName="demo-wrapper"
                editorClassName="demo-editor"
                onEditorStateChange={this.onEditorStateChange}
            />
            <textarea
                disabled
                value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}
            />
        </div>
    );
}

}

@rexconsul
Copy link

rexconsul commented Apr 7, 2022

@hfabio

Works, but lost all formatting :(

Found a solution with hooks:

import {
  EditorState,
  convertToRaw,
  ContentState,
  convertFromHTML
} from "draft-js"

const YourComponent = ({ props }: YourComponentProps) => {
  const [editorState, setEditorState] = useState(() => {
    const blocksFromHTML = convertFromHTML(data)
    const contentState = ContentState.createFromBlockArray(
      blocksFromHTML.contentBlocks,
      blocksFromHTML.entityMap
    )

    return EditorState.createWithContent(contentState)
  }
)
 ...

Did you find a workaround on this to also keep the formatting? Thanks in advance

@siddheeq93
Copy link

how to add inline styles to the html code
image
cant we do like this?

@Anikets08
Copy link

Putting it here in case anyone who wants to have default value coming from the backend ( to mock db I've used the localStorage )

  const [editorContent, setEditorContent] = React.useState(
    EditorState.createEmpty()
  );

  useEffect(() => {
    let rawContent = localStorage.getItem("editorRawContent");
    let parsedRawContent = JSON.parse(rawContent);
    if (parsedRawContent) {
      setEditorContent(
        EditorState.createWithContent(convertFromRaw(parsedRawContent))
      );
    }
  }, []);

 <Editor         
       editorState={editorContent}
       onEditorStateChange={(e) => {
       setEditorContent(e);
       localStorage.setItem(
       "editorRawContent",
        JSON.stringify(convertToRaw(e.getCurrentContent()))
            );
       }}
  />

@pejman-hkh
Copy link

how to add inline styles to the html code image cant we do like this?

Also, I have this problem. Did you find any solutions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests