quill 富文本编辑器 @提及

使用插件quill-mention,实现在quill 富文本编辑器使用@或#提及用户。

1. 安装

npm install quill-mention --save

2. 官方给的示例quill-mention

import "quill-mention";

const atValues = [
  { id: 1, value: "Fredrik Sundqvist" },
  { id: 2, value: "Patrik Sjölin" }
];
const hashValues = [
  { id: 3, value: "Fredrik Sundqvist 2" },
  { id: 4, value: "Patrik Sjölin 2" }
];
const quill = new Quill("#editor", {
  modules: {
    mention: {
      allowedChars: /^[A-Za-z0-9_]*$/,
      mentionDenotationChars: ["@", "#"],
      source: function(searchTerm, renderList, mentionChar) {
        let values;

        if (mentionChar === "@") {
          values = atValues;
        } else {
          values = hashValues;
        }

        if (searchTerm.length === 0) {
          renderList(values, searchTerm);
        } else {
          const matches = [];
          for (let i = 0; i < values.length; i++)
            if (
              ~values[i].value.toLowerCase().indexOf(searchTerm.toLowerCase())
            )
              matches.push(values[i]);
          renderList(matches, searchTerm);
        }
      }
    }
  }
});

3. 在vue2中使用的示例,已省去无关代码。(示例来自自行封装的Quill.vue通用组件)

<template>
  <quill-editor>...</quill-editor>
</template>

<script>
import { quillEditor, Quill } from 'vue-quill-editor'
import "quill-mention"

// 工具栏配置
const toolbarOptions = [...];

export default {
  name: 'Quill',
  components: { quillEditor },
  props: {
    // 提及的用户数据
    atValues: {
      typ: Object,
      default: ()=>[]
    },
    hashValues: {
      typ: Object,
      default: ()=>[]
    }
  },
  data() {
    return {
      editorOption: {
        modules: {
          mention: {
            allowedChars: /^[a-zA-Z0-9_]*$/,
            mentionDenotationChars: ["@", "#"],
            source: (searchTerm, renderList, mentionChar) => {
              let values
              if (mentionChar === "@") {
                values = this.atValues
              } else {
                values = this.hashValues
              }

              if (searchTerm.length === 0) {
                renderList(values, searchTerm);
              } else {
                const matches = []
                for (let i = 0; i < values.length; i++)
                  if (
                    ~values[i].value.toLowerCase().indexOf(searchTerm.toLowerCase())
                  )
                    matches.push(values[i])
                renderList(matches, searchTerm)
              }
            },
            // 选中后触发
            onSelect: (item, insertItem) => {
              this.$emit('atUser', item.id)
              insertItem(item)
            }
          }
        }
      }
    }
  }

}
</script>

<style lang="less" scoped></style>

4. 无论哪个示例,记得引入css样式。在源码的 dist/quill.mention.css

.ql-mention-list-container {
  width: 270px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
  background-color: #ffffff;
  box-shadow: 0 2px 12px 0 rgba(30, 30, 30, 0.08);
  z-index: 9001;
  overflow: auto;
}

.ql-mention-loading {
  line-height: 44px;
  padding: 0 20px;
  vertical-align: middle;
  font-size: 16px;
}

.ql-mention-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.ql-mention-list-item {
  cursor: pointer;
  line-height: 44px;
  font-size: 16px;
  padding: 0 20px;
  vertical-align: middle;
}

.ql-mention-list-item.disabled {
  cursor: auto;
}

.ql-mention-list-item.selected {
  background-color: #d3e1eb;
  text-decoration: none;
}

.mention {
  height: 24px;
  width: 65px;
  border-radius: 6px;
  background-color: #d3e1eb;
  padding: 3px 0;
  margin-right: 2px;
  user-select: all;
}

.mention > span {
  margin: 0 3px;
}

5. 效果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quill 是一个基于浏览器的富文本编辑器,它提供了一种简单、灵活、强大的方式来创建和编辑富文本内容。下面是 Quill 富文本编辑器的使用教程: 1. 引入 Quill 库 首先,在你的 HTML 文件中引入 Quill 库: ```html <script src="//cdn.quilljs.com/1.3.6/quill.js"></script> <link href="//cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet"> ``` 2. 创建 Quill 实例 在页面中创建一个空的 div 元素,用于 Quill 编辑器的容器: ```html <div id="editor"></div> ``` 然后,在 JavaScript 中创建一个 Quill 实例: ```javascript var quill = new Quill('#editor', { theme: 'snow' // 使用 snow 主题 }); ``` 3. 添加编辑器内容 在 Quill 编辑器中添加内容可以通过 `setContents` 方法或 `setText` 方法实现。 ```javascript // 使用 setContents 方法添加内容 quill.setContents([ { insert: 'Hello World!' } ]); // 使用 setText 方法添加内容 quill.setText('Hello World!'); ``` 4. 监听编辑器内容变化 如果你需要在内容变化时执行一些操作,可以监听 `text-change` 事件: ```javascript quill.on('text-change', function(delta, oldDelta, source) { console.log('内容已经变化'); }); ``` 5. 获取编辑器内容 你可以使用 `getContents` 方法获取 Quill 编辑器中的内容: ```javascript var content = quill.getContents(); console.log(content); ``` 6. 获取编辑器纯文本内容 你可以使用 `getText` 方法获取 Quill 编辑器中的纯文本内容: ```javascript var text = quill.getText(); console.log(text); ``` 7. 自定义编辑器样式 你可以通过修改 CSS 样式来自定义编辑器的样式,Quill 提供了许多 CSS 类名,用于定制不同的编辑器元素。 例如,你可以修改编辑器的背景颜色: ```css .ql-editor { background-color: #f5f5f5; } ``` 以上就是 Quill 富文本编辑器的使用教程,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值