margox / braft-editor

美观易用的React富文本编辑器,基于draft-js开发
MIT License
4.61k stars 593 forks source link

无法粘贴html内容 #247

Closed wangzhipeng404 closed 6 years ago

wangzhipeng404 commented 6 years ago

以下是,粘贴的时候console出来的错误

Cannot read property 'fontFamilies' of undefined
    at configs.js:457
    at genFragment (convertFromHTML.js:360)
    at genFragment (convertFromHTML.js:427)
    at getChunkForHTML (convertFromHTML.js:481)
    at convertFromHTMLtoContentBlocks (convertFromHTML.js:521)
    at convertFromHTML.js:612
    at convertHTMLToRaw (index.js:28)
    at Object.insertHTML (content.js:265)
    at BraftEditor.<anonymous> (index.js:3931)
    at callCallback (react-dom.development.js:11132)
    at commitUpdateEffects (react-dom.development.js:11171)
    at commitUpdateQueue (react-dom.development.js:11159)
    at commitLifeCycles (react-dom.development.js:14700)
    at commitAllLifeCycles (react-dom.development.js:15904)
    at HTMLUnknownElement.callCallback (react-dom.development.js:145)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:195)
    at invokeGuardedCallback (react-dom.development.js:248)
    at commitRoot (react-dom.development.js:16074)
    at completeRoot (react-dom.development.js:17462)
    at performWorkOnRoot (react-dom.development.js:17390)
    at performWork (react-dom.development.js:17294)
    at performSyncWork (react-dom.development.js:17266)
    at interactiveUpdates$1 (react-dom.development.js:17557)
    at interactiveUpdates (react-dom.development.js:2208)
    at dispatchInteractiveEvent (react-dom.development.js:4913)
convertHTMLToRaw @ index.js:31
insertHTML @ content.js:265
(anonymous) @ index.js:3931
callCallback @ react-dom.development.js:11132
commitUpdateEffects @ react-dom.development.js:11171
commitUpdateQueue @ react-dom.development.js:11159
commitLifeCycles @ react-dom.development.js:14700
commitAllLifeCycles @ react-dom.development.js:15904
callCallback @ react-dom.development.js:145
invokeGuardedCallbackDev @ react-dom.development.js:195
invokeGuardedCallback @ react-dom.development.js:248
commitRoot @ react-dom.development.js:16074
completeRoot @ react-dom.development.js:17462
performWorkOnRoot @ react-dom.development.js:17390
performWork @ react-dom.development.js:17294
performSyncWork @ react-dom.development.js:17266
interactiveUpdates$1 @ react-dom.development.js:17557
interactiveUpdates @ react-dom.development.js:2208
dispatchInteractiveEvent @ react-dom.development.js:4913
content.js:270 Error: invalid RawDraftContentState
    at invariant (invariant.js:42)
    at convertFromRawToDraftState (convertFromRawToDraftState.js:209)
    at Object.insertHTML (content.js:265)
    at BraftEditor.<anonymous> (index.js:3931)
    at callCallback (react-dom.development.js:11132)
    at commitUpdateEffects (react-dom.development.js:11171)
    at commitUpdateQueue (react-dom.development.js:11159)
    at commitLifeCycles (react-dom.development.js:14700)
    at commitAllLifeCycles (react-dom.development.js:15904)
    at HTMLUnknownElement.callCallback (react-dom.development.js:145)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:195)
    at invokeGuardedCallback (react-dom.development.js:248)
    at commitRoot (react-dom.development.js:16074)
    at completeRoot (react-dom.development.js:17462)
    at performWorkOnRoot (react-dom.development.js:17390)
    at performWork (react-dom.development.js:17294)
    at performSyncWork (react-dom.development.js:17266)
    at interactiveUpdates$1 (react-dom.development.js:17557)
    at interactiveUpdates (react-dom.development.js:2208)
    at dispatchInteractiveEvent (react-dom.development.js:4913)
margox commented 6 years ago

你好,你用的是什么版本? 使用代码贴一下看看

margox commented 6 years ago

如果用的1.x版本的braft-editor,那看看node_modules下面的braft-convert的版本,如果是2.x,就npm install一下1.9.8版本的braft-convert试试

wangzhipeng404 commented 6 years ago

以下为源码,版本为2.x

import React from 'react';
import { Form } from 'antd';
import BraftEditor, { EditorState } from 'braft-editor';
import 'braft-editor/dist/index.css';
import { BaseImgUrl, getQiniuToken } from '../../common/config';
import { htmlIsEmpty } from '../../utils/utils';

const FormItem = Form.Item;

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 7 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 12 },
    md: { span: 10 },
  },
};

const handleEditorupload = param => {
  getQiniuToken().then(token => {
    const serverURL = 'http://up-z2.qiniu.com';
    const xhr = new XMLHttpRequest();
    const fd = new FormData();
    const successFn = () => {
      const blkRet = JSON.parse(xhr.responseText);
      param.success({
        url: BaseImgUrl + blkRet.key,
      });
    };

    const progressFn = event => {
      param.progress(event.loaded / event.total * 100);
    };

    const errorFn = () => {
      param.error({ msg: '上传失败' });
    };

    xhr.upload.addEventListener('progress', progressFn, false);
    xhr.addEventListener('load', successFn, false);
    xhr.addEventListener('error', errorFn, false);
    xhr.addEventListener('abort', errorFn, false);

    fd.append('file', param.file);
    fd.append('token', token);
    xhr.open('POST', serverURL, true);
    xhr.send(fd);
  });
};

export const editorProps = {
  height: 500,
  media: {
    uploadFn: handleEditorupload,
  },
  imageControls: [],
  fontFamilies: [],
};

export const Editor = props => {
  const { form, initialValue, name, label, required = true, editorOptions = {} } = props;
  const { getFieldDecorator } = form;
  const newProps = {
    ...editorProps,
    ...editorOptions,
  };
  const layout = props.formItemLayout || formItemLayout;
  const defaultValue = EditorState.createFrom(initialValue);
  const validateEditor = (rule, value, callback) => {
    if (required && htmlIsEmpty(value)) {
      callback(`${label}不能为空,请输入`);
    } else {
      callback();
    }
  };
  return (
    <FormItem {...layout} label={label}>
      {getFieldDecorator(name, {
        getValueFromEvent: editorState => editorState.toHTML(),
        rules: [
          {
            validator: validateEditor,
          },
        ],
        initialValue: defaultValue,
      })(<BraftEditor {...newProps} style={{ border: '1px solid #e8e8e8' }} />)}
    </FormItem>
  );
};
wangzhipeng404 commented 6 years ago

早上我看了一下你的源码,估计也是这一块的原因,但是装成了draft-convert,所以没成功 我刚才尝试了npm install braft-convert@^2.1.4,问题解决了,谢谢

margox commented 6 years ago

好的,在antd form中使用的话,还可以用上一些别的特性,例如通过editorState.isEmpty()来判断内容是否为空,最近新增了很多功能但是还没来得及更新文档

参考: https://braft.margox.cn/demos/antd-form 这个是今天刚更新的新官网

wangzhipeng404 commented 6 years ago

isEmpty还是只判断

吗,我记得isEmpty这个需求是我提的哈哈

超能刚哥 notifications@github.com于2018年10月12日 周五14:46写道:

好的,在antd form中使用的话,还可以用上一些别的特性,例如通过editorState.isEmpty()来判断内容是否为空,最近新增了很多功能但是还没来得及更新文档

参考: https://braft.margox.cn/demos/antd-form 这个是今天刚更新的新官网

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/margox/braft-editor/issues/247#issuecomment-429222241, or mute the thread https://github.com/notifications/unsubscribe-auth/AFtjKDW0dzMRlIRQRa2Xm1YoQ82U3y7Zks5ukDregaJpZM4XXQfJ .

margox commented 6 years ago

这个isEmpty方法,其实判断的就是editorState.getCurrentContent().getPlainText().length,和之前判断

相比,性能会好一些,毕竟不用进行一次toHTML的转换

wangzhipeng404 commented 6 years ago

那应该比我的快很多,我的是正则获取全部表签内的内容来判断的,之后我改成自带的这个就行了

超能刚哥 notifications@github.com于2018年10月12日 周五14:52写道:

这个isEmpty方法,其实判断的就是editorState.getCurrentContent().getPlainText().length,和之前判断

相比,性能会好一些,毕竟不用进行一次toHTML的转换

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/margox/braft-editor/issues/247#issuecomment-429223585, or mute the thread https://github.com/notifications/unsubscribe-auth/AFtjKJ3py8IEGvTnYD_VA2bEEmsxHENjks5ukDxAgaJpZM4XXQfJ .

rendongsc commented 5 years ago

@margox 不开新问题,直接在这里问了。 希望通过粘贴html内容,来自动插入a标签,html代码如下。 PS:stripPastedStyles={true}加了这个也不行。

<a style="color: #fff; text-decoration: none; display: inline; padding: 15px 40px; background: #53b778; border-radius: 5px; margin-right: 50px;" href="{{disAgreeUrl}}" target="_blank" rel="noopener">填写登记表</a>

谢谢!