mirror of
https://github.com/wassname/talk.git
synced 2026-09-12 13:01:11 +08:00
html and content editable normalizer
This commit is contained in:
@@ -4,6 +4,7 @@ import { init } from 'pell';
|
|||||||
import styles from './Editor.css';
|
import styles from './Editor.css';
|
||||||
import cn from 'classnames';
|
import cn from 'classnames';
|
||||||
import { pluginName } from '../../package.json';
|
import { pluginName } from '../../package.json';
|
||||||
|
import { htmlNormalizer } from '../utils';
|
||||||
|
|
||||||
class Editor extends React.Component {
|
class Editor extends React.Component {
|
||||||
ref = null;
|
ref = null;
|
||||||
@@ -18,7 +19,7 @@ class Editor extends React.Component {
|
|||||||
onChange: richTextBody => {
|
onChange: richTextBody => {
|
||||||
// We want to save the original comment body
|
// We want to save the original comment body
|
||||||
const originalBody = this.ref.childNodes[1].innerText;
|
const originalBody = this.ref.childNodes[1].innerText;
|
||||||
onChange(originalBody, { richTextBody });
|
onChange(originalBody, { richTextBody: htmlNormalizer(richTextBody) });
|
||||||
},
|
},
|
||||||
actions,
|
actions,
|
||||||
classes: {
|
classes: {
|
||||||
@@ -101,6 +102,7 @@ Editor.propTypes = {
|
|||||||
actions: PropTypes.array,
|
actions: PropTypes.array,
|
||||||
registerHook: PropTypes.func,
|
registerHook: PropTypes.func,
|
||||||
unregisterHook: PropTypes.func,
|
unregisterHook: PropTypes.func,
|
||||||
|
isReply: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Editor;
|
export default Editor;
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
export function htmlNormalizer(htmlInput) {
|
||||||
|
const str = htmlInput;
|
||||||
|
// We are normalizing the input from contenteditable of each browser, also removing unnecesary html tags
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content#Differences_in_markup_generation
|
||||||
|
return str
|
||||||
|
.replace('<p>', '<div>') // IE outputs <p> instead of <div>s
|
||||||
|
.replace('</p>', '</div>') // IE outputs <p> instead of <div>s
|
||||||
|
.replace('<div>', '<br>')
|
||||||
|
.replace('</div>', '');
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user