minor improvements
- use <p> for unstyled blocks - fix return key bug - editor placeholder
This commit is contained in:
parent
001011df27
commit
fe76eb9f73
2 changed files with 31 additions and 40 deletions
|
@ -1,6 +1,10 @@
|
|||
import {Editor, ContentState, convertFromHTML, DefaultDraftBlockRenderMap, DefaultDraftInlineStyle} from 'draft-js';
|
||||
const ReactDOM = require('react-dom');
|
||||
|
||||
const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {
|
||||
element: 'p' // draft uses <div> by default which we don't really like
|
||||
});
|
||||
|
||||
const styles = {
|
||||
BOLD: 'strong',
|
||||
CODE: 'code',
|
||||
|
@ -11,24 +15,18 @@ const styles = {
|
|||
|
||||
export function contentStateToHTML(contentState:ContentState): String {
|
||||
const elem = contentState.getBlockMap().map((block) => {
|
||||
const elem = DefaultDraftBlockRenderMap.get(block.getType()).element;
|
||||
const elem = BLOCK_RENDER_MAP.get(block.getType()).element;
|
||||
const content = [];
|
||||
block.findStyleRanges(() => true, (s, e) => {
|
||||
console.log(block.getInlineStyleAt(s));
|
||||
const tags = block.getInlineStyleAt(s).map(style => styles[style]);
|
||||
const open = tags.map(tag => `<${tag}>`).join('');
|
||||
const close = tags.map(tag => `</${tag}>`).reverse().join('');
|
||||
content.push(`${open}${block.getText().substring(s, e)}${close}`);
|
||||
});
|
||||
|
||||
return (`
|
||||
<${elem}>
|
||||
${content.join('')}
|
||||
</${elem}>
|
||||
`);
|
||||
return (`<${elem}>${content.join('')}</${elem}>`);
|
||||
}).join('');
|
||||
|
||||
|
||||
return elem;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue