reactjs - 如何在 react-quill 中注册对齐样式

标签 reactjs next.js server-side-rendering react-quill

我正在使用 react-quill npm 包并在 nextjs 中动态导入它,我也在使用 create-next-app 样板。 我能够让 react-quill 编辑器工作但是 我无法从工具栏获取使用对齐按钮设置的图像样式/段落样式并重新显示内容 - 图像/段落。

用例:

  1. 从编辑器的工具栏添加图像/段落并添加对齐方式。
  2. 将编辑器内容保存在数据库中
  3. 使用 npm 包重新显示来自数据库的 react-quill 编辑器的内容 htmr

预期:图像/段落内容仍应右对齐/居中/对齐。

实际:图像/段落内容已删除所有样式属性。

下面是我的代码,我的问题是如何在nextjs中为react-quill的图片/段落注册样式

import { useState,useEffect } from 'react'
import Router from 'next/router'
import dynamic from 'next/dynamic' 
import { withRouter } from 'next/router'   // used to get access to props of react-dom
import { getCookie,isAuth } from '../../actions/auth';
import { createBlog }  from '../../actions/blog'

// dynamically importing react-quill  
const ReactQuill = dynamic( ()=> import('react-quill'), {ssr:false} )
import '../../node_modules/react-quill/dist/quill.snow.css'

const CreateBlog  = ( {router} ) => {


    const [ body,setBody ] = useState( blogFromLS() )
    const [ values,setValues ] = useState({
        error : '',
        sizeError : '',
        success : '',
        formData : '',
        title : '',
        hidePublishButton : false
    })


    const  { error, sizeError, success, formData, title, hidePublishButton } = values;
    const token = getCookie('token')

    useEffect(()=>{
            setValues({...values, formData: new FormData()})
            initCategories()
            intiTags()
    },[router])

    
    const handleChange = name => e => {
        //console.log(e.target.value)
        const value = name === 'photo' ? e.target.files[0] : e.target.value
        formData.set(name,value)
        setValues({...values, [name]:value, formData : formData , error:''})
    }; 

    const handleBody =  e => {
        //console.log(e)
        setBody(e)
        formData.set('body',e)
        if(typeof window !== 'undefined'){
            localStorage.setItem('blog',JSON.stringify(e))
        }
    }

    const publishBlog =(e) => {
            e.preventDefault();
           // console.log('ready to publish')
           createBlog(formData, token).then(data => {
            if(data.error){
                setValues({...values,error:data.error})
                // console.log('error macha')
            }
                else{
                        setValues({...values,title:'' ,error:'', success:' Blog was Published 
                        successfully '})
                        setBody('')
                        setCategories([]);
                        setTags([])
                    }
            })
    }



    const createBlogForm = () => {
        return <form onSubmit= { publishBlog }>
                <div className="form-group">
                        <label className="text-muted"> Title </label>
                        <input type="text" className="form-control" 
                             value= { title }   onChange={handleChange('title')} ></input>
                </div>

            <div className="form-group">
                <ReactQuill style={{height:'30rem',marginBottom:'8rem'}} value={body} 
                        placeholder="Write here, minimum of 200 charaters is required"
                 modules={CreateBlog.modules} formats={ CreateBlog.formats }  onChange={ handleBody } >
                </ReactQuill>
            </div>

            <div className="form-group">
                <button type="submit" className="btn btn-primary" > Publish </button>
            </div><br></br>

        </form>
    }

    const showError = () => (
        <div className="alert alert-danger" style={{display : error ? '' : 'none'}}> {error} </div>
    )

    const showSuccess = () => (
        <div className="alert alert-success" style={{display : success ? '' : 'none'}}> {success} </div>
    )


    return (
        <div className="container-fluid">
              <div className="row">
                    <div className="col-md-8">
                    { createBlogForm() }
                    <div>
                        {showError()}
                        {showSuccess()}
                    </div>
                    </div>
          
              <div className="col-md-4">
                    <div className="form-group pb-2">
                          <h5>Featured Image</h5>
                                <hr></hr>
                        <small className="text-muted">Max.size upto 2mb</small><br></br>
                         <label className="btn btn-outline-info">
                                    Upload Featured Image
                         <input onChange={handleChange('photo')} type="file" accept="image/*" hidden></input>
                        </label>

                 </div>

                    </div>
              </div>
        </div>
        
    )
}


CreateBlog.modules = {
    toolbar : [
            [{ header:'1' }, {header:'2'}, {header:[3,4,5,6] } , {font:[]} ],
            [{ size:[] }],
            ['bold','italic','underline','strike','blockquote'],
            [{ list:'ordered' }, {list:'bullet'},{'indent': '-1'}, {'indent': '+1'} ],
            [{ align: '' }, { align: 'center' }, { align: 'right' }, { align: 'justify' }],
            ['link','image','video'],
            ['clean'],
            ['code-block']
    ]
};


CreateBlog.formats = [
      'header',
      'font',
      'size',
      'bold',
      'italic',
      'underline',
      'strike',
      'blockquote',
      'list',
      'bullet',
      'indent',
      'align',
      'link',
      'image',
      'video',
      'code-block',
];




export default withRouter(CreateBlog);

最佳答案

据我所试,Image resize Module不会与 Nextjs 的样板一起工作,并且在显示内容时样式本身不会注册。您需要弹出样板或使用 webpack。

我更喜欢你使用 SunEditor 作为 React 富文本编辑器,它与 Nextjs 配合得非常好。 SunEditor github Link .您只需在 _document.js 或 _app.js 中全局导入样式表。

你可以看到 demo这里

关于reactjs - 如何在 react-quill 中注册对齐样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63479292/

相关文章:

Angular Universal 不在路由器导出内呈现内容

visual-studio - 如何使这个简单的 TypeScript + React 应用程序与 Visual Studio 2015 一起工作?

javascript - 对显示的选择更改选项值使用react

javascript - Socket.io 多次更新

docker - nginx 代理后面的 nextjs 多阶段 docker 构建

reactjs - NextJS - 链接到缩短的网址

带有 Jade 模板的 Angular 2

reactjs - 使用 lodash/fp 映射和第二个参数重构小函数

kubernetes - Kubernetes 入口路由到 Nextjs 应用程序的问题