Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hi-mention

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hi-mention

纯JS聊天框,兼容原生HTML/React/Vue等各种框架;支持@提及功能、插入富文本等多功能编辑器;支持H5和PC的交互样式

  • 1.1.0
  • unpublished
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-75%
Maintainers
0
Weekly downloads
 
Created
Source

Mention 提及

  • 查看 demo 示例
  • QQ 群

安装

npm install hi-mention --save

引入

import Mention from "hi-mention";

VUE 使用示例

<template>
  <div class="editor-content"></div>
</template>

<script lang="ts" setup>
  import Mention from "hi-mention";
  import "hi-mention/index.css";
  import { onMounted } from "vue";

  onMounted(() => {
    new Mention(".editor-content", {
      users: [
        { name: "张三", id: 1 },
        { name: "李四", id: 2 },
        { name: "王五", id: 3 },
        { name: "赵六", id: 4 },
        { name: "钱七", id: 5 },
        { name: "孙八", id: 6 },
        { name: "周九", id: 7 },
        { name: "吴十", id: 8 },
      ],
    })
      .on("focus", (e) => {
        console.log("focus", e);
      })
      .on("blur", (e) => {
        console.log("blur", e);
      })
      .on("change", (d) => {
        console.log("change", d?.html);
        msg.value = d?.html || "";
      });
  });
</script>

<style>
  .editor-content {
    width: 500px;
    height: 200px;
    border: 1px solid #ccc;
    padding: 5px 10px;
  }
</style>

参数

参数说明类型默认值
trigger触发字符string@
media媒体类型(PCH5 的用户列表展示有差异)PC/H5PC
placeholder占位符string请输入
placeholderColor占位符颜色string#aaa
usersWdith用户列表宽度(media=PC时有效)string200px
usersHeight用户列表最大高度string200px
users用户列表Array<User>[]
idKeyUser对象id字段(该字段支持@搜索)stringid
nameKeyUser对象name字段(该字段支持@搜索)stringname
pingyinKeyUser对象pingyin字段(该字段支持@搜索)stringpingyin
avatarKeyUser对象avatar字段stringavatar

API

方法说明类型备注
on监听事件(key:EventType,fn:(data?:any)=>void)=>this-
updateUsers更新用户列表(users:Array<User>)=>this-
updateMedia更新媒体类型(media:"PC"/"H5")=>this-
clear清空输入框()=>this-
insertText在光标位置插入文本内容(text:string)=>this-
insertHtml在光标位置插入 HTML 内容(html:Element)=>this-
focus获取焦点、将光标移动到输入框末尾()=>this-
getData获取输入框内容()=>{html:string,text:string}-
getMentions获取输入框内提及的用户对象列表()=>Array<User>-
mentionUser提及用户(user:User)=>this-

EventType

User 类型

  • 以下字段为推荐字段,若字段不存在,请使用idKeynameKeypingyinKeyavatarKey字段进行配置
字段名必须说明类型
id键(该字段支持@查询)string
name名字(该字段支持@查询)string
pingyin拼音(该字段支持@查询)string
avatar头像string
element用户列表中展示的元素,若不提供该字段,则使用默认样式Element

允许子类继承重写方法

接口名说明类型备注
createUserElement创建用户选项Element(user:User)=>Element-
openUserList打开用户列表(query:User)=>thisquery:为用户输入@时后面跟的查询字符串
  • 继承示例
import Mention from "../../HiMention";

class MyMention extends Mention {
  constructor(props) {
    super(props);
  }

  // 打开用户列表,重写该方法会导致默认的用户列表被覆盖
  openUserList(query) {
    console.log("@后面的查询字符串", query);
  }

  // 该方法返回的元素将被展示在默认用户列表中
  createUserElement(user) {
    const { name, avatar } = user;
    const img = document.createElement("img");
    img.src = avatar;
    img.style.width = "20px";
    img.style.height = "20px";
    img.style.marginRight = "5px";
    const span = document.createElement("span");
    span.innerText = name;
    const div = document.createElement("div");
    div.appendChild(img);
    div.appendChild(span);
    // 该元素将被当成选项展示在用户列表中
    return div;
  }
}

Keywords

FAQs

Package last updated on 16 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc