Skip to content

Commit

Permalink
feat: 自定义表情前缀以及后缀
Browse files Browse the repository at this point in the history
  • Loading branch information
Lete114 committed Jun 9, 2022
1 parent b537a4e commit 48bd975
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ npm install emot --save
<script src="https://cdn.jsdelivr.net/npm/emot"></script>
<script>
const emot = new Emot({
el: 'body', // 挂载dom (支持css选择器获取dom)
target: 'textarea', // 目标输入容器 (支持css选择器获取dom)
emotMaps: 'emot.json' // 自定义表情,可以是一个url,或一个对象
el: 'body', // 挂载dom (支持css选择器获取dom)
target: 'textarea', // 目标输入容器 (支持css选择器获取dom)
emotMaps: 'emot.json', // 自定义表情,可以是一个url,或一个对象
before: ':', // 自定义标签前缀,默认[
after: ':' // 自定义标签后缀,默认]
})
// 通过调用get()方法获取处理后的数据
const result = emot.get()
Expand Down
13 changes: 10 additions & 3 deletions dist/emot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ var Emot = /*#__PURE__*/function () {

_classPrivateFieldGet(this, _options).el = document.querySelector(options.el);
_classPrivateFieldGet(this, _options).target = document.querySelector(options.target);
_classPrivateFieldGet(this, _options).before = _classPrivateFieldGet(this, _options).before || '[';
_classPrivateFieldGet(this, _options).after = _classPrivateFieldGet(this, _options).after || ']';

_classPrivateFieldSet(this, _output, {
content: '',
Expand Down Expand Up @@ -225,7 +227,7 @@ function _createDOM2() {

var Start = ctx.substring(0, cursorStart);
var Ent = ctx.substring(cursorEnd);
if (emotValue.type === 'text') _classPrivateFieldGet(self, _output).content = Start + iValue + Ent;else _classPrivateFieldGet(self, _output).content = Start + '[' + iKey + ']' + Ent;
if (emotValue.type === 'text') _classPrivateFieldGet(self, _output).content = Start + iValue + Ent;else _classPrivateFieldGet(self, _output).content = Start + _classPrivateFieldGet(self, _options).before + iKey + _classPrivateFieldGet(self, _options).after + Ent;

_classPrivateFieldGet(self, _options).target.focus();

Expand Down Expand Up @@ -291,11 +293,16 @@ function _createDOM2() {
}

function _parseEmot2() {
var before = _classPrivateFieldGet(this, _options).before;

var after = _classPrivateFieldGet(this, _options).after;

var ctx = _classPrivateFieldGet(this, _output).content;

var emots = []; // 匹配所有[]格式的内容,并存储到emots数组中

ctx.replace(/\[(.*?)\]/g, function ($0, $1) {
var reg = new RegExp('\\' + before + '(.*?)' + '\\' + after, 'g');
ctx.replace(reg, function ($0, $1) {
emots.push($1);
}); // 遍历匹配到的所有[]格式的表情

Expand All @@ -308,7 +315,7 @@ function _parseEmot2() {

if (!link) continue;
var img = '<img src=' + link + ' alt=' + emot + '/>';
ctx = ctx.replace('[' + emot + ']', img);
ctx = ctx.replace(before + emot + after, img);
}

_classPrivateFieldGet(this, _output).contentHTML = ctx;
Expand Down
13 changes: 10 additions & 3 deletions dist/emot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ var Emot = /*#__PURE__*/function () {

_classPrivateFieldGet(this, _options).el = document.querySelector(options.el);
_classPrivateFieldGet(this, _options).target = document.querySelector(options.target);
_classPrivateFieldGet(this, _options).before = _classPrivateFieldGet(this, _options).before || '[';
_classPrivateFieldGet(this, _options).after = _classPrivateFieldGet(this, _options).after || ']';

_classPrivateFieldSet(this, _output, {
content: '',
Expand Down Expand Up @@ -223,7 +225,7 @@ function _createDOM2() {

var Start = ctx.substring(0, cursorStart);
var Ent = ctx.substring(cursorEnd);
if (emotValue.type === 'text') _classPrivateFieldGet(self, _output).content = Start + iValue + Ent;else _classPrivateFieldGet(self, _output).content = Start + '[' + iKey + ']' + Ent;
if (emotValue.type === 'text') _classPrivateFieldGet(self, _output).content = Start + iValue + Ent;else _classPrivateFieldGet(self, _output).content = Start + _classPrivateFieldGet(self, _options).before + iKey + _classPrivateFieldGet(self, _options).after + Ent;

_classPrivateFieldGet(self, _options).target.focus();

Expand Down Expand Up @@ -289,11 +291,16 @@ function _createDOM2() {
}

function _parseEmot2() {
var before = _classPrivateFieldGet(this, _options).before;

var after = _classPrivateFieldGet(this, _options).after;

var ctx = _classPrivateFieldGet(this, _output).content;

var emots = []; // 匹配所有[]格式的内容,并存储到emots数组中

ctx.replace(/\[(.*?)\]/g, function ($0, $1) {
var reg = new RegExp('\\' + before + '(.*?)' + '\\' + after, 'g');
ctx.replace(reg, function ($0, $1) {
emots.push($1);
}); // 遍历匹配到的所有[]格式的表情

Expand All @@ -306,7 +313,7 @@ function _parseEmot2() {

if (!link) continue;
var img = '<img src=' + link + ' alt=' + emot + '/>';
ctx = ctx.replace('[' + emot + ']', img);
ctx = ctx.replace(before + emot + after, img);
}

_classPrivateFieldGet(this, _output).contentHTML = ctx;
Expand Down
2 changes: 1 addition & 1 deletion dist/emot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 48bd975

Please sign in to comment.