Skip to content

Commit

Permalink
Update OwO.min.js
Browse files Browse the repository at this point in the history
buildUI 方法:将生成 HTML 结构的部分提取到 buildUI 方法中,使代码更易于理解和扩展。
bindEvents 方法:将事件绑定部分提取到 bindEvents 方法中,避免重复代码。
表情包初始化:通过 setInitialTab 方法明确默认选中第一个表情包。
优化事件委托:在 .OwO-body 上委托 click 事件,减少对每个表情项的事件绑定。
代码组织:优化了代码结构,便于扩展和维护。
  • Loading branch information
riceshowerX authored Nov 29, 2024
1 parent b829c83 commit 34426d1
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions js/OwO.min.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
(() => {
class OwO {
constructor(option) {
const defaultOption = {
constructor(options) {
const defaultOptions = {
logo: 'OwO表情',
container: $('.OwO')[0], // 使用jQuery选择器
target: $('textarea')[0], // 使用jQuery选择器
position: 'down',
width: '100%',
maxHeight: '250px',
api: '/usr/themes/PureSuck/js/OwO.json'
api: '/usr/themes/PureSuck/js/OwO.json',
};
Object.assign(defaultOption, option);
this.container = defaultOption.container;
this.target = defaultOption.target;

if (defaultOption.position === 'up') {
this.options = { ...defaultOptions, ...options };
this.container = this.options.container;
this.target = this.options.target;

// 如果位置是 "up",则调整样式
if (this.options.position === 'up') {
$(this.container).addClass('OwO-up');
}

this.fetchEmojiData(defaultOption.api).then(data => {
this.odata = data;
this.init(defaultOption);
}).catch(error => {
console.error('加载表情数据失败:', error);
});
this.fetchEmojiData(this.options.api)
.then((data) => {
this.odata = data;
this.init();
})
.catch((error) => {
console.error('加载表情数据失败:', error);
});
}

async fetchEmojiData(api) {
Expand All @@ -41,22 +45,28 @@
}
}

init(option) {
this.area = option.target;
init() {
this.area = this.options.target;
this.packages = Object.keys(this.odata);
this.buildUI();
this.bindEvents();
this.setInitialTab();
}

buildUI() {
let html = `
<div class="OwO-logo"><span>${option.logo}</span></div>
<div class="OwO-body" style="width: ${option.width}">`;
<div class="OwO-logo"><span>${this.options.logo}</span></div>
<div class="OwO-body" style="width: ${this.options.width}">`;

this.packages.forEach(pkg => {
// 生成表情面板内容
this.packages.forEach((pkg) => {
const packageData = this.odata[pkg];
const maxHeight = `${parseInt(option.maxHeight) - 53}px`;
const maxHeight = `${parseInt(this.options.maxHeight) - 53}px`;
const packageWidth = packageData.width || '';

html += `<ul class="OwO-items OwO-items-${packageData.type}" style="max-height: ${maxHeight};">`;

packageData.container.forEach(item => {
packageData.container.forEach((item) => {
const { text, input, icon } = item;
const isImage = packageData.type === 'image';
const imgWidth = packageWidth ? ` style="width: ${packageWidth};"` : '';
Expand All @@ -74,7 +84,8 @@
<div class="OwO-bar">
<ul class="OwO-packages">`;

this.packages.forEach(pkg => {
// 生成表情包标签
this.packages.forEach((pkg) => {
html += `<li><span>${pkg}</span></li>`;
});

Expand All @@ -85,10 +96,13 @@
</div>`;

$(this.container).html(html);
}

this.logo = $(this.container).find('.OwO-logo');
this.logo.on('click', () => this.toggle());
bindEvents() {
// logo 点击事件:切换表情面板
$(this.container).find('.OwO-logo').on('click', () => this.toggle());

// 表情项点击事件:将表情插入文本框
$(this.container).find('.OwO-body').on('click', '.OwO-item', (e) => {
const target = $(e.target);
const cursorPos = this.area.selectionEnd;
Expand All @@ -98,42 +112,52 @@
this.toggle();
});

// 切换表情包
this.packagesEle = $(this.container).find('.OwO-packages');
this.packagesEle.on('click', 'li', (e) => {
const index = $(e.target).closest('li').index();
this.tab(index);
});

this.tab(0);
}

toggle() {
$(this.container).toggleClass('OwO-open');
}

tab(index) {
// 切换表情包的显示
$(this.container).find('.OwO-items-show').removeClass('OwO-items-show');
$(this.container).find('.OwO-items').eq(index).addClass('OwO-items-show');

// 切换选中的表情包标签
$(this.container).find('.OwO-package-active').removeClass('OwO-package-active');
this.packagesEle.children().eq(index).addClass('OwO-package-active');

// 更新指示器位置
const activeLink = this.packagesEle.children().eq(index);
const indicator = $(this.container).find('.OwO-indicator');
indicator.css({
width: `${activeLink.outerWidth()}px`,
left: `${activeLink.position().left}px`
});
}

setInitialTab() {
// 默认选中第一个表情包
this.tab(0);
}
}

// 判断模块化方式
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = OwO;
} else {
window.OwO = OwO;
}

})();

// 初始化评论区的 OwO 表情选择器
function initializeCommentsOwO() {
const container = $('.OwO')[0];
const target = $('.OwO-textarea')[0];
Expand Down

0 comments on commit 34426d1

Please sign in to comment.