GithubHelp home page GithubHelp logo

rishiqing / vue-emoji Goto Github PK

View Code? Open in Web Editor NEW
38.0 7.0 7.0 8.47 MB

An emoji component for vue2.0

Home Page: https://rishiqing.github.io/vue-emoji/examples/

License: MIT License

Shell 0.23% JavaScript 41.64% CSS 51.48% Vue 6.65%
vue2 vue-components emoji

vue-emoji's Introduction

vue-emoji

Build Status Downloads Version License

基于 Vue2.0 的emoji插件

English

Vue-emoji

安装

yarn add rui-vue-emoji

使用示例

<link rel="stylesheet" type="text/css" href="./node_modules/vue-emoji-component/dist/vue-emoji.css">
<script src = './vue.min.js'></script>
<script src = './node_modules/vue-emoji-component/dist/vue-emoji.js'></script>
<div id="app">
  <div>
    <h1>Vue-Emoji</h1>
    <div contenteditable="" ref = 'edit' focus></div>
    <button ref = 'btn' @click = 'showEmoji = !showEmoji'>emoji</button>
    <vue-emoji
      v-show = 'showEmoji'
      ref = 'emoji'
      @select = 'showEmoji = false'
      @hide = 'handleHide'
    ></vue-emoji>
  </div>
</div>
new Vue({
  el: '#app',
  components: {
    VueEmoji
  },
  data () {
    return {
      showEmoji : false
    }
  },
  mounted () {
    this.$refs.emoji.appendTo({
      area: this.$refs.edit,
      btn: this.$refs.btn,
      position: 'top left'
    });
  },
  methods: {
    hide () {
      this.showEmoji = false;
    },
    handleHide (e) {
      this.hide();
    }
  }
});

webpack中引入

// styles.js
import 'rui-vue-emoji/dist/vue-emoji.css';

你也可以考虑将雪碧图和单个图标上传到CDN, 那么需要改动一下jscss文件。

[class*="sprite-"] {
  background-image: url("https://your.cdn.path/")!important;
}
mounted () {
  this.$refs.emoji.appendTo({
    area: this.$refs.edit,
    btn: this.$refs.btn,
    position: 'top left'
  }).setPath('https://your.cdn.path/');
},

需要注意的是, 在css中控制雪碧图的路径, 在js中控制最后引用的单个表情图片的路径。

选项

事件

  • select: 选中表情时会触发此事件, 事件参数为选中的表情对应的图片
  • hide: 触发隐藏事件, 当发生这个事件后, 需要在父组件中对弹窗进行关闭, 含有一个事件对象, 表示点击的元素。

方法

appendTo

这个方法需要在父组件mounted的是时候进行调用, 以便初始化参数。 至少需要传递两个参数area以及btn, area表示需要将表情插入的地方, btn表示触发表情弹窗显示的按钮。 可选的position选项用于设置表情框的位置, 默认为top center, 表示在位于按钮的上方, 居中显示。 当第一个参数不为top的时候, 将会置于按钮下方。 第二个参数表示弹窗相对于按钮的位置, 可选的有left, center, right三个选项。

calcPosition

用于重新计算弹窗的位置, 当[contenteditable]内容增加的情况下, 通常其高度也会变化, 这个时候需要重新进行计算以更新弹窗的位置。 遗憾的是, 我没能找到一种自动监听其变化的方法, 所以需要手动监听, 然后再进行调用。

app.vue

watch: {
  showEmoji (value) { // showEmoji 为控制弹窗隐藏显示的属性。
    if (value) {
      this.$refs.emoji.calcPosition();
    }
  }
}

getImgPathByUnicode

该方法接收一个表情编码, 如果能够找到对应的图片, 则会返回基于前面设定的路径的图片, 如果没有找到, 则会返回null。

<vue-emoji
  v-show = 'showEmoji'
  ref = 'emoji'
  :unicode='true'
  @select = 'handleSelect'
  @hide = 'hide()'
></vue-emoji>
handleSelect (img) {
  if (img.nodeType === 3) {
    var $img = new Image();
    $img.src =  this.$refs.emoji.getImgPathByUnicode(img.textContent);
    $app1.appendChild($img);
  }
  this.hide();
}

getUnicodeByImgPath

该方法接受一个图片地址, 返回对应的unicode编码的表情

setPath

用于指定所需使用的图片的地址路径。 默认为当前根目录下的images/, 推荐使用CDN.

属性

unicode

默认情况下当选中一个表情的时候会以图片的方式插入对应区域。你可以通过配置unicode选项来开启Unicode支持, 也就是说, 在这种情况下, 选中一个表情的时候, 会插入对应的Unicode字符。需要注意的是, 相比于图片模式, 使用Unicode的时候会缺少一些表情。

<vue-emoji
  v-show = 'showEmoji'
  ref = 'emoji'
  :unicode='true'
  @select = 'hide()'
  @hide = 'hide()'
></vue-emoji>

captions

默认情况下, 会使用表情、自然、物品、地点、符号来作为每个emoji栏目的标题, 可以通过使用组件的时候传入captions参数, 来改变标题文字。

<vue-emoji
  v-show = 'showEmoji'
  ref = 'emoji'
  :captions = 'captions'
  @select = 'showEmoji = false'
  @hide = 'handleHide'
></vue-emoji>
data () {
  return {
    captions: ['Expressions', 'Nature', 'Goods', 'Lacation', 'Symbol']
  }
}

开发

git clone [email protected]:rishiqing/vue-emoji.git
cd vue-emoji
yarn
npm start

一些小问题

因为接触Vue的时间比较短, 仓促之下做的这个东西也有太多不完善的地方。 比如我希望的是, 能够在template中就将元素的一些属性传递过去, 从而取代在mounted方法里面手动调用appendTo 的方式, 因为在我看来我现在的这种实现就是一个丑陋的hack, 然而当我多次尝试之后, 也没有找到一个能够在父组件中渲染子组件的时候, 向子组件里面传递父组件其他DOM节点的引用的方法。 当然, 也许更大的原因还是在于我设计上的失误。所以, 如果你有更好的方法, 欢迎告诉我, 在下不胜感激。

vue-emoji's People

Contributors

374632897 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-emoji's Issues

建议支持Unicode模式

非常喜欢这个组件,网上找了一大圈,感觉只有这个是比较实用的
不过目前使用起来只支持纯图片的模式,还必须要手动配置表情路径
Emoji表情的输出本身应该是由系统自身实现的,属于系统的一种字体
详见:http://www.cnblogs.com/batsing/p/emoji_iconfont.html
希望这一块能够优化一下,同时能够支持Unicode模式,直接插入Unicode码来显示表情

可以请教你一下你的实现思路吗

看了下你的API 发现并没有将表情数据传送到数据库的说明,请问这个东西如何存到数据库里面去(一般emoji是存字符编码),还有能提供一下你的实现思路么

在vue文件里面用,无效

把组件注册到export default里面,代码也写在vue文件里面,能弹出表情框,选择了之后,无法把表情插到div为contenteditable的里面,也无报错信息。

emoji控制button

控制emoji显示与否的只能是button吗?换其他的html元素(比如iview的Button)会导致抛出的hide事件完全乱套了。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.