如何将CSS类添加到鹅毛笔选择中? [英] how to add css class to a quill selection?

查看:99
本文介绍了如何将CSS类添加到鹅毛笔选择中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在quilljs中添加css类的样式?

How can I add my own style with a class css in quilljs?

我有以下css类

.ql-editor spanblock {
  background-color: #F8F8F8;
    border: 1px solid #CCC;
    line-height: 19px;
    padding: 6px 10px;
    border-radius: 3px;
    margin: 15px 0;
}

和针对此CSS转换的事件

and an event targeting this CSS transformation

var toolbarOptions = [
    [{ "header": [false, 1, 2, 3, 4, 5, 6] }, "bold", "italic"],
    [{ "list": "ordered"}, { "list": "bullet" }, { "indent": "-1"}, { "indent": "+1" }],
    ["blockquote","code-block", "span-block","link", "hr"]
];
var quill = new Quill("#form_field_" + options.id + " .editor-container > .editor", {
    modules: {
        toolbar:  toolbarOptions
    },
    theme: "snow"
});

var toolbar = quill.getModule("toolbar");
toolbar.addHandler("span-block", function(){});
var spanBlockButton = document.querySelector(".ql-span-block");

spanBlockButton.addEventListener("click", () => {
    let range = quill.getSelection(true);
    // what should I add here
}   

我在quilljs文档中找不到任何内容
https://quilljs.com

I cannot find anything in the documentation of quilljs https://quilljs.com

谢谢

推荐答案

基本上,您必须扩展羊皮纸
在本教程中,我经历了此处此处

Basically you have to extend Parchment blots to have custom styled element in quill. I went through this tutorial here and here.

以下是简单的html

Following is the simple html

<link href="http://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<link href="http://cdn.quilljs.com/1.3.6/quill.bubble.css" rel="stylesheet">
<link href="http://cdn.quilljs.com/1.3.6/quill.core.css" rel="stylesheet">
<style>
.ql-spanblock:after {
  content: "<spanblock/>";
}

.spanblock {
    background-color: #F8F8F8;
    border: 1px solid #CCC;
    line-height: 19px;
    padding: 6px 10px;
    border-radius: 3px;
    margin: 15px 0;
}
</style>


<div id="editor">
</div>

这里是实际的答案,我已经扩展了 blots / inline 通过以下方式将选定的文本包装到具有所需类的div中。

Here is the actual answer,I have extended blots/inline in following way to wrap selected text into a div with desired class.

<script src="http://cdn.quilljs.com/1.3.6/quill.min.js"></script>
<script type="text/javascript">

let Inline = Quill.import('blots/inline');

class SpanBlock extends Inline{    

    static create(value){
        let node = super.create();
        node.setAttribute('class','spanblock');
        return node;    
    } 
}

SpanBlock.blotName = 'spanblock';
SpanBlock.tagName = 'div';
Quill.register(SpanBlock);


var toolbarOptions = [
    ['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 direction

    [{ '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'],
    ['spanblock']
];


var quill = new Quill('#editor', {
  modules: {
    toolbar: toolbarOptions
  },
  theme: 'snow'
});

var spanBlockButton = document.querySelector('.ql-spanblock');

//event listener to spanblock button on toolbar
spanBlockButton.addEventListener('click', function() {

        var range = quill.getSelection();
        if(range){

            quill.formatText(range,'spanblock',true);
        }else{

        }

    }
);

</script>

Codepen-demo

这篇关于如何将CSS类添加到鹅毛笔选择中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆