Angular4 quill 富文本编辑器实现

使用quill富文本编辑器实现,angular项目中用到了ngx-quill插件。

quill的GitHub地址:https://github.com/quilljs/quill

ngx-quill的GitHub地址:https://github.com/KillerCodeMonkey/ngx-quill

 

1.Installation

npm install ngx-quill

npm install ng2-quill-editor --save

 

2.quill的配置

import { QuillModule } from 'ngx-Quill';

@NgModule({
  declarations: [
    ...routedComponents
  ],
  imports: [
  
    QuillModule,
  ],
  providers: [
    ....
  ]
})
export class HomeModule { }

在index.html中添加quill的样式 :

<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">

 

3.使用

在页面使用标签<quill-editor></quill-editor>,效果如下:

    上图中配置后的工具类,代码如下:

    <quill-editor [modules]="quillConfig" [style]="{height: '200px'}" [(ngModel)]="Content"></quill-editor>                   


    quillConfig = {
        toolbar: [
            //['bold', 'italic', 'underline', 'strike'],        // toggled buttons
            ['blockquote', 'code-block'],
            [{ 'header': 1 }, { 'header': 2 }],               // custom button values
            [{ 'list': 'ordered' }, { 'list': 'bullet' }],
            //[{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
            //[{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
            //[{ 'direction': 'rtl' }],                         // text directio
            //[{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
            //[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
            //[{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
            //[{ 'font': [] }],
            //[{ 'align': [] }],
            //['clean'],                                         // remove formatting button
            ['link', 'image', 'video']                         // link and image, video
        ]
    }; 
                     

自定义工具栏:

<quill-editor [style]="{height: '200px'}" [(ngModel)]="data.content" placeholder="在这里详细描述一个推荐原因,\n">
            <div quill-editor-toolbar>
              <span class="ql-formats">
                <button type="button" class="ql-header" value="1"></button>
                <button type="button" class="ql-header" value="2"></button>
                <button type="button" class="ql-blockquote"></button>
                <button type="button" class="ql-code-block ql-active"></button>
                <button type="button" class="ql-list" value="ordered"></button>
              </span>
              <span class="ql-formats">
                  <button type="button" class="ql-link"></button>
                  <button type="button" style="outline:none">
                    <div class="upload_img">
                        <svg class="camera" viewBox="0 0 18 18"> <rect class="ql-stroke" height="10" width="12" x="3" y="4"></rect> <circle class="ql-fill" cx="6" cy="7" r="1"></circle> <polyline class="ql-even ql-fill" points="5 12 5 11 7 9 8 10 11 7 13 9 13 12 5 12"></polyline> </svg>
                        <input type="file" class="file_input" ng2FileSelect [uploader]="uploader2" />
                    </div>
                </button>
              </span>
              <span class="ql-formats">
                  <p style="font-size: 9px;" *ngIf="uploader2?.queue.length > 0">图片已上传:{{ uploader2?.queue.length > 0 ? uploader2?.queue[uploader2?.queue.length-1]?.progress : 0 }}%</p>
              </span>
            </div>
          </quill-editor>

上传图片代码(使用了ng2-file-upload插件):

public uploader2: FileUploader;

this.uploader2 = new FileUploader({
      url: this.URL
      , method: "POST"
      , itemAlias: "upfile"
      , autoUpload: true
    });
    this.uploader2.onSuccessItem = function (item, response, status, headers) {
      if(status == 200){
        let rsp = JSON.parse(response);
        let img = '<img class="camera" src="'+"http://您的域名"+  rsp.url+'" alt="">';
        that.data.content += img;
      }
    };

 

 

 

转载于:https://www.cnblogs.com/qingfeng3keke/p/9804718.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值