Comments (16)
For ones who are wondering how to initialize it with HTML content instead of empty here is the way you need to generate editorState with the content you have, to init editorState there is a function in draft-js EditorState component and I am using a converter draft-js-import-html which allows to generate contentState out of HTML content, see below
import {EditorState, ContentState, convertFromHTML, convertToRaw} from 'draft-js';
import {stateFromHTML} from 'draft-js-import-html';
constructor(props) {
super(props);
var contentState = stateFromHTML(props.input.value);
editorState = EditorState.createWithContent(contentState);
this.state = {editorState: editorState};
}
Above works for me.
from react-draft-wysiwyg.
@jpuri & @iphonic , I tried statefromHTML and also convertFromHTML from draft-js. both were working fine, but when the content has underline/subscript/superscript content, it was rendered as normal text. Is there any work-around available?
Note: Bold and italic styles working fine.
from react-draft-wysiwyg.
@iphonic's result worked for me perfectly. I am using functional components though so here is my solution:
const [editorState, setEditorState] = useState(EditorState.createEmpty());
useEffect(() => {
// automatedMessage is the html string I am fetching from my server
if (automatedMessage) {
let contentState = stateFromHTML(automatedMessage.message); //automatedMessage.message being "<p>Test</p>"
setEditorState(EditorState.createWithContent(contentState));
}
}, [automatedMessage]);
from react-draft-wysiwyg.
I ran into the same problem where my editorContents
was null the first time I rendered, then was populated on a second render. A quick and dirty fix of not rendering until I know the data is there worked for me.
` render() {
const { editorContents } = this.state;
if (!editorContents) {
return <div />;
}
return (
<div>
<div className="demo-label">
Editor with output generated in HTML.
</div>
<div className="demo-editorSection">
<Editor
toolbarClassName="demo-toolbar"
wrapperClassName="demo-wrapper"
editorClassName="demo-editor"
rawContentState={editorContents}
onChange={this.onEditorChange.bind(this)}
toolbar={{
options: ['inline', 'blockType', 'textAlign', 'link', 'remove', 'history'],
inline: {
options: ['bold', 'italic', 'underline', 'strikethrough'],
},
}}
/>
</div>
</div>
);
}`
from react-draft-wysiwyg.
Hi @alagos ,
Check similar example here: https://github.com/jpuri/react-draft-wysiwyg/blob/master/docs/src/components/Demo/index.js#L140
from react-draft-wysiwyg.
@jpuri yes, actually, that's the specific example I'm basing of. Instead of using sampleEditorContent
to load the content in rawContentState={sampleEditorContent}
I loaded it from the state and I almost copy/pasted the content from https://github.com/jpuri/react-draft-wysiwyg/blob/master/docs/src/util/sampleEditorContent.js to my _getInitialHTML
function to load the initial data for the state, but is not working.
Not sure if has something to do, but I'm using 0.2.1
version.
from react-draft-wysiwyg.
ok try to do a console log and check whats coming in editorContent
after this line const { editorContent } = this.state;
.
from react-draft-wysiwyg.
That's the result with console.log(JSON.stringify(editorContent));
:
{
"entityMap": {},
"blocks": [
{
"key": "1n9j8",
"text": "Hello world",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
}
]
}
and after I click the editor to write something and render
is reevaluated, the result is:
{
"entityMap": {},
"blocks": [
{
"key": "a5b87",
"text": "",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
}
]
}
from react-draft-wysiwyg.
Its looks like editor is re-mounting in each render - which I do not expect to be the case, it may be an issue with Modal
component.
Ideally editor should have been initialized only once but apparently its content are reset second time also.
from react-draft-wysiwyg.
@ytwater: you can also check new contentState
property (detailed in docs) it can be used not only for initializing but also changing editor content.
from react-draft-wysiwyg.
@alagos: is your issue resolved, can I do something more to help it ?
from react-draft-wysiwyg.
@jpuri at the end I moved to another editor as I couldn't figure out what was going on, sorry about that. Anyway thanks for the help :)
from react-draft-wysiwyg.
same issue here using react-bootstrap modal
from react-draft-wysiwyg.
@iphonic Thanks for posting, was having problems loading the content stored in the DB back into the editor and your post solved it.
from react-draft-wysiwyg.
@iphonic Thanks for your answer
from react-draft-wysiwyg.
when the content has underline/subscript/superscript content, it was rendered as normal text
The same problem. Is there any solution?
from react-draft-wysiwyg.
Related Issues (20)
- nbsp not working when adding the HTML back into Wysiwyg
- Uncaught Error: Got unexpected null or undefined
- https://registry.npmjs.org 【淘宝镜像不维护了】
- Cursor selector jump on start on each update of content if try to make component controlled HOT 2
- TypeScript Error: Argument Type Mismatch in dynamic Import of Editor Component in NextJS project. HOT 1
- How to upload video by toolbarCustomButtons? HOT 1
- Unable to paste multiple files on firefox using react-draft-wysiwyg library
- hey, I also have a question, can I change the look and feel of the bind link popup or customize it?
- First character bug in Firefox HOT 1
- emdedd link are refused to connect
- Issue with Mention Functionality Removing Text in React-Draft-Wysiwyg HOT 2
- How to render the HTML template with background-color and styles in editor
- react-draft-wysiwyg toolbar options not opening HOT 1
- when using unorderlist in editor and and making it center is not working
- Type error: 'Editor' cannot be used as a JSX component.
- its dont work with React 16.13.1 and vite HOT 1
- Locale translations
- SyntaxError: Identifier '__vite__injectQuery' has already been declared (at ${mod.id}:57205:1)
- Bug when trying to align text with justify, center, left and right
- Tool bar will be scrolled out of frame
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-draft-wysiwyg.