123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <template>
- <div v-clickoutside="clickoutside">
- <div class="biaoqingContent" @click.stop>
- <img class="expression" @click.stop="show = !show" src="./expression.png" />
- <!-- 表情选择框-->
- <div class="box" v-if="show" @click.stop>
- <div class="left">
- <ul>
- <li v-for="(item, index) in biaoqingList" :key="index" :class="['cursor', biaoqingActive === index ? 'active' : '']">
- <div @click="bqNameChange(index, item.iconArr)">
- {{ item.name }}
- </div>
- </li>
- </ul>
- </div>
- <div class="right">
- <button v-for="(i, index) in rightList" :key="index" class="emoji" @click.stop="insertHtmlAtCaret(i.icon)">
- <span>{{ i.icon }}</span>
- </button>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script>
- // 定义最后光标对象
- import { biaoqingArr } from "./biaoqing.js";
- export default {
- props: {
- //表情框宽度
- width: {
- type: String,
- default: "50%",
- },
- //表情框高度
- height: {
- type: String,
- default: "200px",
- },
- //表情框ID
- id: {
- type: String,
- default: "text",
- },
- //内容
- value: {
- type: String,
- default: "",
- },
- },
- data() {
- return {
- lastEditRange: "",
- content: "",
- show: false,
- biaoqingList: biaoqingArr,
- biaoqingActive: 0,
- rightList: [],
- allList: [],
- };
- },
- watch: {
- value() {
- this.valueChange();
- },
- },
- methods: {
- clickoutside() {
- console.log("clickoutside", 111111);
- this.show = false;
- },
- valueChange() {
- if (!this.value) return;
- let str = this.value;
- let newStr = this.forArr(this.forArr(str), "className");
- document.getElementById(this.id).innerHTML = newStr;
- },
- //替换emoji 表情图片
- forArr(str, _type) {
- for (let j = 0; j < this.allList.length; j++) {
- let val = this.allList[j];
- let reg = _type === "className" ? `alt${val.className}` : val.icon;
- if (_type === "className") {
- str = str.replace(new RegExp(reg, "g"), val.icon);
- } else {
- str = str.replace(
- new RegExp(reg, "g"),
- `<img style="vertical-align: sub;" src="http://tkeasyemoji.oss-cn-shanghai.aliyuncs.com/images/placeholder.png" class="${val.className}" alt="alt${val.className}">`
- );
- }
- }
- return str;
- },
- // 表情title点击事件
- bqNameChange(index, arr) {
- this.biaoqingActive = index;
- this.rightList = arr;
- },
- contentClick() {
- // 获取选定对象
- var selection = getSelection();
- // 设置最后光标对象
- this.lastEditRange = selection.getRangeAt(0);
- },
- //表情插入文本框
- insertHtmlAtCaret(icon) {
- this.$emit("select", icon);
- },
- },
- created() {
- this.rightList = biaoqingArr[0].iconArr;
- biaoqingArr.forEach((e) => {
- this.allList = this.allList.concat(e.iconArr);
- });
- },
- mounted() {
- this.valueChange();
- window.addEventListener("message", (res) => {
- if (Object.prototype.toString.call(res.data) == "[object Object]") {
- if (res.data.source === "clickScene") {
- if (this.show) {
- this.show = false;
- }
- }
- }
- });
- },
- components: {},
- };
- </script>
- <style>
- @import "./emoji_sorites.css";
- </style>
- <style lang="less" scoped>
- .contentBox {
- border: 1px solid #ccc;
- border-radius: 5px;
- padding: 10px;
- font-size: 16px;
- position: relative;
- }
- .biaoqingContent {
- padding: 5px;
- width: 30px;
- margin: 5px auto;
- position: relative;
- .expression {
- width: 20px;
- margin: 5px 0;
- cursor: pointer;
- }
- .box {
- border: 1px solid #73a8f9;
- width: 400px;
- min-height: 400px;
- box-shadow: 0px 0px 16px rgba(0, 0, 0, 0.2);
- background: #fff;
- bottom: 100%;
- z-index: 999;
- left: 0;
- display: flex;
- position: absolute;
- }
- .left {
- width: 90px;
- height: 100%;
- display: table-cell;
- border-right: 1px solid #ebebeb;
- padding: 4px;
- ul {
- padding: 0;
- margin: 0;
- text-align: center;
- list-style-type: none;
- li {
- height: 30px;
- line-height: 30px;
- cursor: pointer;
- font-size: 14px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- color: #000;
- button {
- border: none;
- background: none;
- width: 100%;
- height: 100%;
- }
- }
- }
- .active {
- background-color: #3171d1;
- color: #000;
- border-radius: 4px;
- button {
- color: #000;
- }
- }
- }
- .right {
- flex: 1;
- padding-left: 10px;
- padding-top: 15px;
- overflow: auto;
- text-align: left;
- button {
- border: none;
- padding: 0;
- cursor: pointer;
- width: 32px;
- height: 32px;
- }
- .emoji {
- display: inline-block;
- padding: 3px;
- border: 1px solid transparent;
- cursor: pointer;
- background: #fff;
- color: #000;
- &:hover {
- height: 32px;
- background-color: #ddded8;
- border: 1px solid #b3c1fd;
- border-radius: 4px;
- }
- }
- }
- //样式一
- .box0 {
- }
- }
- @media screen and (max-width: 600px) {
- .biaoqingContent {
- .box {
- width: 80vw;
- right: -10vw;
- max-height: 26vh;
- min-height: unset;
- overflow-y: auto;
- }
- }
- }
- </style>
|