mirror of
https://dl.bcrjl.com/ghg/HaoOuBa/Joe.git
synced 2026-02-18 01:20:04 +08:00
更新
This commit is contained in:
@@ -331,6 +331,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
}
|
||||
|
||||
/* 激活OωO表情 */
|
||||
{
|
||||
new OwO({
|
||||
logo: 'OωO表情',
|
||||
container: document.querySelector('.joe_owo__container'),
|
||||
target: document.querySelector('.joe_owo__target'),
|
||||
api: 'https://cdn.jsdelivr.net/gh/HaoOuBa/Joe@master/assets/json/owo.json',
|
||||
position: 'down',
|
||||
width: '100%',
|
||||
maxHeight: '250px'
|
||||
});
|
||||
}
|
||||
|
||||
/* 懒加载 */
|
||||
new LazyLoad('.lazyload');
|
||||
|
||||
|
||||
@@ -1,145 +1 @@
|
||||
(() => {
|
||||
class OwO {
|
||||
constructor(option) {
|
||||
const defaultOption = {
|
||||
container: document.getElementsByClassName('OwO')[0],
|
||||
target: document.getElementsByTagName('textarea')[0],
|
||||
position: 'down',
|
||||
width: '100%',
|
||||
maxHeight: '250px',
|
||||
api: 'https://api.anotherhome.net/OwO/OwO.json'
|
||||
};
|
||||
for (let defaultKey in defaultOption) {
|
||||
if (defaultOption.hasOwnProperty(defaultKey) && !option.hasOwnProperty(defaultKey)) {
|
||||
option[defaultKey] = defaultOption[defaultKey];
|
||||
}
|
||||
}
|
||||
this.container = option.container;
|
||||
this.target = option.target;
|
||||
if (option.position === 'up') {
|
||||
this.container.classList.add('OwO-up');
|
||||
}
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === 4) {
|
||||
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
|
||||
this.odata = JSON.parse(xhr.responseText);
|
||||
this.init(option);
|
||||
} else {
|
||||
console.log('OwO data request was unsuccessful: ' + xhr.status);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.open('get', option.api, true);
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
init(option) {
|
||||
this.area = option.target;
|
||||
this.packages = Object.keys(this.odata);
|
||||
|
||||
// fill in HTML
|
||||
let html = `
|
||||
<div class="OwO-logo"><span><svg viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"><circle cx="12" cy="12" r="10"></circle><path d="M8 14s1.5 2 4 2 4-2 4-2"></path><line x1="9" y1="9" x2="9.01" y2="9"></line><line x1="15" y1="9" x2="15.01" y2="9"></line></svg>表情</span></div>
|
||||
<div class="OwO-body" style="width: ${option.width}"><div class="OwO-jio"></div>`;
|
||||
|
||||
for (let i = 0; i < this.packages.length; i++) {
|
||||
html += `
|
||||
<ul class="OwO-items OwO-items-${this.odata[this.packages[i]].type}" style="max-height: ${parseInt(option.maxHeight) - 53 + 'px'};">`;
|
||||
var type = this.odata[this.packages[i]].type;
|
||||
let opackage = this.odata[this.packages[i]].container;
|
||||
for (let i = 0; i < opackage.length; i++) {
|
||||
if (type == 'image') {
|
||||
html += `
|
||||
<li class="OwO-item" data-id="${opackage[i].data}" title="${opackage[i].text}">${opackage[i].icon}</li>`;
|
||||
} else {
|
||||
html += `
|
||||
<li class="OwO-item" data-id="not-given" title="${opackage[i].text}">${opackage[i].icon}</li>`;
|
||||
}
|
||||
}
|
||||
|
||||
html += `
|
||||
</ul>`;
|
||||
}
|
||||
|
||||
html += `
|
||||
<div class="OwO-bar">
|
||||
<ul class="OwO-packages">`;
|
||||
|
||||
for (let i = 0; i < this.packages.length; i++) {
|
||||
html += `
|
||||
<li><span>${this.packages[i]}</span></li>`;
|
||||
}
|
||||
|
||||
html += `
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
this.container.innerHTML = html;
|
||||
|
||||
// bind event
|
||||
this.logo = document.getElementsByClassName('OwO-logo')[0];
|
||||
this.logo.addEventListener('click', e => {
|
||||
e.stopPropagation();
|
||||
this.toggle();
|
||||
});
|
||||
|
||||
this.container.getElementsByClassName('OwO-body')[0].addEventListener('click', e => {
|
||||
let target = null;
|
||||
if (e.target.classList.contains('OwO-item')) {
|
||||
target = e.target;
|
||||
} else if (e.target.parentNode.classList.contains('OwO-item')) {
|
||||
target = e.target.parentNode;
|
||||
}
|
||||
if (target) {
|
||||
const cursorPos = this.area.selectionEnd;
|
||||
let areaValue = this.area.value;
|
||||
//this.area.value = areaValue.slice(0, cursorPos) + target.innerHTML + areaValue.slice(cursorPos);
|
||||
if (target.dataset.id == 'not-given') {
|
||||
this.area.value = areaValue.slice(0, cursorPos) + target.innerHTML + areaValue.slice(cursorPos);
|
||||
} else {
|
||||
this.area.value = areaValue.slice(0, cursorPos) + target.dataset.id + areaValue.slice(cursorPos);
|
||||
}
|
||||
this.area.focus();
|
||||
this.toggle();
|
||||
}
|
||||
});
|
||||
this.packagesEle = this.container.getElementsByClassName('OwO-packages')[0];
|
||||
for (let i = 0; i < this.packagesEle.children.length; i++) {
|
||||
(index => {
|
||||
this.packagesEle.children[i].addEventListener('click', e => {
|
||||
e.stopPropagation();
|
||||
this.tab(index);
|
||||
});
|
||||
})(i);
|
||||
}
|
||||
this.tab(0);
|
||||
}
|
||||
toggle() {
|
||||
if (this.container.classList.contains('OwO-open')) {
|
||||
this.container.classList.remove('OwO-open');
|
||||
} else {
|
||||
this.container.classList.add('OwO-open');
|
||||
}
|
||||
}
|
||||
tab(index) {
|
||||
const itemsShow = this.container.getElementsByClassName('OwO-items-show')[0];
|
||||
if (itemsShow) {
|
||||
itemsShow.classList.remove('OwO-items-show');
|
||||
}
|
||||
this.container.getElementsByClassName('OwO-items')[index].classList.add('OwO-items-show');
|
||||
|
||||
const packageActive = this.container.getElementsByClassName('OwO-package-active')[0];
|
||||
if (packageActive) {
|
||||
packageActive.classList.remove('OwO-package-active');
|
||||
}
|
||||
this.packagesEle.getElementsByTagName('li')[index].classList.add('OwO-package-active');
|
||||
}
|
||||
}
|
||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||
module.exports = OwO;
|
||||
} else {
|
||||
window.OwO = OwO;
|
||||
}
|
||||
})();
|
||||
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var a=0;a<t.length;a++){var s=t[a];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,s.key,s)}}return function(t,a,s){return a&&e(t.prototype,a),s&&e(t,s),t}}();!function(){var e=function(){function e(t){var a=this;_classCallCheck(this,e);var s={logo:"OwO表情",container:document.getElementsByClassName("OwO")[0],target:document.getElementsByTagName("textarea")[0],position:"down",width:"100%",maxHeight:"250px",api:"https://api.anotherhome.net/OwO/OwO.json"};for(var n in s)s.hasOwnProperty(n)&&!t.hasOwnProperty(n)&&(t[n]=s[n]);this.container=t.container,this.target=t.target,"up"===t.position&&this.container.classList.add("OwO-up");var i=new XMLHttpRequest;i.onreadystatechange=function(){4===i.readyState&&(i.status>=200&&i.status<300||304===i.status?(a.odata=JSON.parse(i.responseText),a.init(t)):console.log("OwO data request was unsuccessful: "+i.status))},i.open("get",t.api,!0),i.send(null)}return _createClass(e,[{key:"init",value:function(e){var t=this;this.area=e.target,this.packages=Object.keys(this.odata);for(var a='\n <div class="OwO-logo"><span>'+e.logo+'</span></div>\n <div class="OwO-body" style="width: '+e.width+'">',s=0;s<this.packages.length;s++){a+='\n <ul class="OwO-items OwO-items-'+this.odata[this.packages[s]].type+'" style="max-height: '+(parseInt(e.maxHeight)-53+"px")+';">';for(var n=this.odata[this.packages[s]].container,i=0;i<n.length;i++)a+='\n <li class="OwO-item" title="'+n[i].text+'">'+n[i].icon+"</li>";a+="\n </ul>"}a+='\n <div class="OwO-bar">\n <ul class="OwO-packages">';for(var o=0;o<this.packages.length;o++)a+="\n <li><span>"+this.packages[o]+"</span></li>";a+="\n </ul>\n </div>\n </div>\n ",this.container.innerHTML=a,this.logo=this.container.getElementsByClassName("OwO-logo")[0],this.logo.addEventListener("click",function(){t.toggle()}),this.container.getElementsByClassName("OwO-body")[0].addEventListener("click",function(e){var a=null;if(e.target.classList.contains("OwO-item")?a=e.target:e.target.parentNode.classList.contains("OwO-item")&&(a=e.target.parentNode),a){var s=t.area.selectionEnd,n=t.area.value;t.area.value=n.slice(0,s)+a.innerHTML+n.slice(s),t.area.focus(),t.toggle()}}),this.packagesEle=this.container.getElementsByClassName("OwO-packages")[0];for(var c=function(e){!function(a){t.packagesEle.children[e].addEventListener("click",function(){t.tab(a)})}(e)},l=0;l<this.packagesEle.children.length;l++)c(l);this.tab(0)}},{key:"toggle",value:function(){this.container.classList.contains("OwO-open")?this.container.classList.remove("OwO-open"):this.container.classList.add("OwO-open")}},{key:"tab",value:function(e){var t=this.container.getElementsByClassName("OwO-items-show")[0];t&&t.classList.remove("OwO-items-show"),this.container.getElementsByClassName("OwO-items")[e].classList.add("OwO-items-show");var a=this.container.getElementsByClassName("OwO-package-active")[0];a&&a.classList.remove("OwO-package-active"),this.packagesEle.getElementsByTagName("li")[e].classList.add("OwO-package-active")}}]),e}();"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=e:window.OwO=e}();
|
||||
Reference in New Issue
Block a user