margox / braft-editor Goto Github PK
View Code? Open in Web Editor NEW美观易用的React富文本编辑器,基于draft-js开发
License: MIT License
美观易用的React富文本编辑器,基于draft-js开发
License: MIT License
npm安装完成后运行的时候convertFromHTML模块提示can't not resolve 'draft-js', draft-js写在了devDependency中了
预览时丢失了编辑时li标签上设置的text-align属性
如题...
<FormItem
{...formItemLayout}
label="正文"
>
{getFieldDecorator('content', {
rules: [{
required: true, message: '请输入正文',
}],
})(
<BraftEditor {...editorProps} />
)}
</FormItem>
我有一个需求是需要将所有的粘贴进来的内容都处理成纯文本(或者说移除原来所有的格式),想问下有哪个api能够处理类似的需求么(比如监听粘贴事件,或者onHtmlBeforeChange之类的)?
在使用initialContent
属性时
如果参数中有图片则使用图片排版功能会报错
DraftModifier.js:144 Uncaught TypeError: Cannot read property 'merge' of undefined at DraftModifier.js:144 at immutable.js:3004 at immutable.js:3376 at immutable.js:1365 at ArrayMapNode.iterate.HashCollisionNode.iterate (immutable.js:1714) at Map.__iterate (immutable.js:1363) at flatDeep (immutable.js:3373) at immutable.js:3375 at immutable.js:2822 at ArraySeq.__iterate (immutable.js:375)
不管是使用html 还是raw作为初始值都会出错,但是如果是新增的图片就不会有这个错误
我代码
import React from 'react';
import { upload } from '@hlj/ajax/src/ajax';
import Popup from '@hlj/widget/Popup';
import { convertFromHTML } from 'draft-js';
import { isString } from 'lodash';
import BraftEditor from 'braft-editor';
import 'braft-editor/dist/braft.css';
import './style.scss';
class Marked extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
content: this.props.source,
editing: false
};
}
componentDidMount() {
const { source } = this.props;
if (isString(source)) {
const content = convertFromHTML(source);
this.setState({ content })
}
}
render() {
const { title } = this.props;
const { editing, content } = this.state;
return (
<div className="marked field">
<label>{title}</label>
<div>
<button type="button" className="ui button"
onClick={() => this.setState({ editing: true })}>
<i className="edit icon"></i>
</button>
</div>
{
editing &&
<Popup className="draft-editor">
<div className="bg" onClick={::this.onClose}></div>
<div className="wrapper">
<BraftEditor
initialContent={content}
controls={[
'undo', 'redo', 'split', 'font-size', 'font-family', 'text-color',
'bold', 'italic', 'underline', 'strike-through', 'superscript',
'subscript', 'text-align', 'split', 'headings', 'list_ul', 'list_ol',
'split', 'link', 'split', 'media'
]}
media={{
image: true, // 开启图片插入功能
video: false, // 开启视频插入功能
audio: false, // 开启音频插入功能
uploadFn: ::this.uploadImg // 指定上传函数
}}
onChange={::this.onChange}
onHTMLChange={::this.onHTMLChange}
/>
</div>
</Popup>
}
</div>
);
}
onHTMLChange(html) {
this.setState({ html });
}
onChange(raw) {
this.setState({ content: raw });
}
onClose() {
const { content, html } = this.state;
const { update } = this.props;
console.log(content);
debugger
update && update({ source: content, html });
this.setState({ editing: false });
}
uploadImg(e) {
if (!e || !e.file) {
return Promise.reject();
}
return upload(e.file, '/images').then(({ url }) => (
e.success({ url })
)).catch(ex => {
window.console.error(ex);
e.error({ msg: 'unable to upload.' });
});
}
}
export default Marked;
不知道算不算是问题哦,我想段落缩进,然后在每个段落首行敲了两个字符的空格,但预览的时候没有缩进效果
上传图片的时候,选择拖拽文件上传,第一次拖拽有效,第二次拖拽变成浏览器打开图片了
chrome浏览器每次插入引用内容后无法显示光标,就不能输入其他东西了 @margox
先占个坑…
用webpack打包时不进行JS压缩可以正常使用。压缩后发布到线上想要传图片,点击工具栏时报了一个错: xx is not a function
,怀疑是压缩后变量名重复?
import React from 'react'
import PropTypes from 'prop-types'
import BraftEditor from 'braft-editor'
import 'braft-editor/dist/braft.css'
const controls = [
'bold',
'italic',
'underline',
'strike-through',
'text-color',
'split',
'text-align',
'split',
'list_ul',
'list_ol',
'split',
'link',
'media',
]
const colors = [
'#000000',
'#333333',
'#999999',
'#ffffff',
'#1C89EA',
'#13ce66',
'#f7ba2a',
'#ff4949',
]
const media = upload => ({
image: true,
video: true,
audio: true,
uploadFn: upload,
})
const RichTextEditor = ({ value, onChange, upload }) => (
<BraftEditor
contentFormat="html"
initialContent={value}
onHTMLChange={onChange}
controls={controls}
colors={colors}
media={media(upload)}
/>
)
RichTextEditor.propTypes = {
value: PropTypes.string,
onChange: PropTypes.func,
upload: PropTypes.func,
}
export default RichTextEditor
const fontFamilies = [
{
name: '黑',
family: 'SimHei, Helvetica, sans-serif'
}, {
name: '宋',
family: 'SimSun, Helvetica, sans-serif'
}, {
name: '楷体',
family: 'KaiTi, Helvetica, sans-serif'
}, {
name: '微软雅黑',
family: 'Microsoft YaHei , Helvetica, sans-serif'
}, {
name: 'Araial',
family: 'Arial, Helvetica, sans-serif'
}, {
name: 'Georgia',
family: 'Georgia, serif'
}, {
name: 'Impact',
family: 'Impact, serif'
}, {
name: 'Monospace',
family: '"Courier New", Courier, monospace'
}, {
name: 'Tahoma',
family: "tahoma, arial, 'Hiragino Sans GB', 宋体, sans-serif"
}
]
braft-editor/example/index.jsx
Line 20 in 0edf31a
在这后面加上
window.previewWindow.document.close()
应该可以解决
如何添加自己的语言文件,有无API提供,谢谢。
你好,在使用富文本上传文件的时候,如何指定携带的header头?
有没有本地上传图片或者视频功能?
转换成代码的时候,在代码尾部回车键无效,无法换行,也无法从代码块中切换出来继续在下面输入,在代码块下面点击无效
当我上传了一个视频,回到编辑器,鼠标点击刚上传的视频(意思是将光标移到刚上传的视频),然后再点击媒体选择一个视频,按下确定控制台报“Uncaught TypeError: Cannot read property 'getKey' of undefined at t.a.selectNextBlock ”错误。
音频和视频有这种问题。如果编辑器只有图片,就不会报错, 图片会自动向下插入一张图片。音频和视频需要将光标移到空白处才能插入, 麻烦帮看一下!
保存成html上传,取回来时将其转换成raw后,赋值给editorProps的initialContent,但是里面的图片消失了;
这个是保存成功后取回的数据
// 转换成raw
initEditorState = () => {
// content是html字符串
const { content } = this.props;
const blockHTML = convertFromHTML(content);
const contentState = ContentState.createFromBlockArray(blockHTML.contentBlocks, blockHTML.entityMap);
return convertToRaw(contentState);
}
// 编辑器的配置
const initialContent = this.initEditorState();
const editorProps = {
initialContent,
}
初始化后的编辑器,应该是要有图片的(取回的数据是有图片的):
还请帮忙看下,谢谢
immutable
状态,期望是一次性删除,而不是和删除普通文本一样尝试了很多次,toggleSelectionBlockType这个方法好像是改变光标所在区块的区块类型,而不是选中内容的区块类型。。。
有什么方法能做到让选中的内容独立一行么?
react-dom.js:17896 Uncaught Error: _registerComponent(...): Target container is not a DOM element.
上传图片到服务器拿到存到阿里云的 图片地址后,正常展示在资源库里之后,点击插入图片,图片没有添加 src 属性,图片无法显示
Cannot read property 'getBoundingClientRect' of null
从在线demo拷贝的代码 http://margox.github.io/braft-editor/
本地运行时,在文本区域输入文本会报错:
Uncaught Error: block is not a BlockNode
不知道是不是我改什么东西导致的,有人遇到类似的情况吗
`import React from 'react'
import { connect } from 'dva'
import { routerRedux } from 'dva/router'
import BraftEditor from 'braft-editor'
import 'braft-editor/dist/braft.css'
class Editortest extends React.Component {
state = {
htmlContent: ''
}
render() {
const editorProps = {
placeholder: 'Hello World!',
initialContent: '',
onHTMLChange: this.handleHTMLChange,
viewWrapper: '.demo',
// 增加自定义预览按钮
extendControls: [
{
type: 'split',
},
{
type: 'button',
text: '预览',
className: 'preview-button',
onClick: () => {
window.open().document.write(this.state.htmlContent)
}
}, {
type: 'dropdown',
text: <span>下拉菜单</span>,
component: <h1 style={{width: 200, color: '#ffffff', padding: 10, margin: 0}}>Hello World!</h1>
}, {
type: 'modal',
text: <span style={{paddingRight: 10,paddingLeft: 10}}>弹窗</span>,
className: 'modal-button',
modal: {
title: '这是一个弹窗',
showClose: true,
showCancel: true,
showConfirm: true,
confirmable: true,
onConfirm: () => console.log(1),
onCancel: () => console.log(2),
onClose: () => console.log(3),
children: (
<div style={{width: 480, height: 320, padding: 30}}>
<span>Hello World!</span>
</div>
)
}
}
]
}
return (
<div className="demo">
<BraftEditor {...editorProps} />
</div>
)
}
handleHTMLChange = (htmlContent) => {
this.setState({ htmlContent })
}
}
export default connect(({ editortest }) => ({ editortest }))(Editortest)`
使用过程中感觉这个编辑器还可以做得更加精细点,主要有下面几个点:
非常感谢了,很好用的编辑器
1.不能缩进,即使使用空格,也不会生效
2.换行后保存,并没有生效
ps:我是直接使用html的
今天看到版本更新到V1.3.0,支持tab键缩进 就更新了本地的包, 按tab键依旧没有效果
1.1.9版本,只是简单的引入了尝试后发现点击居中 居左都无效
超链接输入字符无作用,控制台并未报错
initialContent为HTML内容时,并且存在img标签时会报错,如下
const editorProps = {
height: 500,
contentFormat: 'html',
initialContent: '<div><img /></div><p></p>',
onHTMLChange: this.handleHTMLChange,
media: {
image: true, // 开启图片插入功能
video: false, // 开启视频插入功能
audio: false, // 开启音频插入功能
validateFn: null, // 指定本地校验函数,说明见下文
uploadFn: this.uploadFn.bind(this) // 指定上传函数,说明见下文
},
controls: [
'bold', 'italic', 'underline', 'strike-through', 'text-align', 'split',
'headings', 'list_ul', 'list_ol', 'blockquote', 'split',
'link', 'media'
]
}
vendor.bundle.js:392 Uncaught Error: Unknown DraftEntity key: 1.
at invariant (vendor.bundle.js:392)
at Object.__get (app.bundle.js:7263)
at ContentState.getEntity (app.bundle.js:7933)
at app.bundle.js:51105
at findRangesImmutable (app.bundle.js:7004)
at ContentBlock.findEntityRanges (app.bundle.js:6281)
at r (app.bundle.js:51104)
at app.bundle.js:53940
at Array.forEach ()
at CompositeDraftDecorator.getDecorations (app.bundle.js:53928)
你好,能否增加视频上传功能, 像上传图片一样? 这样更加完美!!!
如题 上传是否支持禁用进度条
在代码块内能否处理下tab键,现在一tab就跳出编辑器了,其实我只是想在代码块内加个tab
请问怎么设置初值,我看到content是个对象
{
blocks: [],
entityMap: {}
}
但是有没有提供一个吧html转换成这种content的方法用以初始化呢?
const editorProps = {
height: 500,
initialContent: '
hello world!
',报的错误:
Uncaught TypeError: Cannot convert undefined or null to object
at Function.keys ()
at convertFromRawToDraftState (convertFromRawToDraftState.js:40)
at t.N.setContent (braft.js:1)
at t.value (braft.js:1)
at eval (ReactCompositeComponent.js:262)
at measureLifeCyclePerf (ReactCompositeComponent.js:73)
at eval (ReactCompositeComponent.js:261)
at CallbackQueue.notifyAll (CallbackQueue.js:74)
at ReactReconcileTransaction.close (ReactReconcileTransaction.js:78)
at ReactReconcileTransaction.closeAll (Transaction.js:207)
图片上传后不能设置尺寸,觉得这个功能还是蛮重要的,而且readme里面开发计划也提到了修改图片宽度,不知道作者大大是否已经着手开发这个功能?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.