Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加serverURLs选项以便支持最新sdk;调整编辑框和表情框高度;fixed #10 #11

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/Valine.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/Valine.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valine",
"version": "1.1.8",
"version": "1.2.1",
"description": "A simple comment system based on Leancloud.",
"main": "/src/Valine.js",
"author": {
Expand Down
120 changes: 73 additions & 47 deletions src/Valine.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Valine {
constructor(option) {
let _root = this;
// version
_root.version = '1.1.8';
_root.version = '1.2.1';
getIp();
// Valine init
!!option && _root.init(option);
Expand Down Expand Up @@ -173,17 +173,38 @@ class Valine {
_root.nodata.show();

// load smiles image
let _smile_wrapper = _root.el.querySelector('.vsmile-icons');
let _smile_wrapper = _root.el.querySelector('.vsmile-icons');
let smile_names = option.emoticon_list || [];
for(let i in smile_names) {
for (let i in smile_names) {
let img = document.createElement('img');
img.setAttribute('src', `${option.emoticon_url}/${smile_names[i]}`);
_smile_wrapper.appendChild(img) ;
_smile_wrapper.appendChild(img);
}
// set serverURLs

let prefix = 'https://';
let serverURLs = '';
if (!option['serverURLs']) {
switch (option.app_id.slice(-9)) {
// TAB
case '-9Nh9j0Va':
prefix += 'tab.';
break;
// US
case '-MdYXbMMI':
prefix += 'us.';
break;
default:
break;
}
}
serverURLs = option['serverURLs'] || prefix + 'avoscloud.com';

if (!disable_av_init) {
av.init({
appId: option.app_id || option.appId,
appKey: option.app_key || option.appKey
appKey: option.app_key || option.appKey,
serverURLs: serverURLs
});
disable_av_init = true;
}
Expand Down Expand Up @@ -212,6 +233,8 @@ class Valine {
}
};

_root.loading.hide()

let vsubmitting = _root.el.querySelector('.vsubmitting');
vsubmitting.innerHTML = _spinner;
_root.submitting = {
Expand Down Expand Up @@ -241,8 +264,8 @@ class Valine {
show(o) {
_mark.innerHTML = `<div class="valert txt-center"><div class="vtext">${o.text}</div><div class="vbtns"></div></div>`;
let _vbtns = _mark.querySelector('.vbtns');
let _cBtn = `<button class="vcancel vbtn">${ o && o.ctxt || _root.i18n['cancel'] }</button>`;
let _oBtn = `<button class="vsure vbtn">${ o && o.otxt || _root.i18n['continue'] }</button>`;
let _cBtn = `<button class="vcancel vbtn">${o && o.ctxt || _root.i18n['cancel']}</button>`;
let _oBtn = `<button class="vsure vbtn">${o && o.otxt || _root.i18n['continue']}</button>`;
_vbtns.innerHTML = `${_cBtn}${o.type && _oBtn}`;
_mark.querySelector('.vcancel').addEventListener('click', function (e) {
_root.alert.hide();
Expand All @@ -261,19 +284,22 @@ class Valine {
}
}

_root.loading.show();
let query1 = new _root.v.Query('Comment');
query1.equalTo('url', defaultComment['url']);
let query2 = new _root.v.Query('Comment');
query2.equalTo('url', defaultComment['url'] + '/');
let query = AV.Query.or(query1, query2);
query.notEqualTo('isSpam', true);
query.count().then(function (count) {
_root.el.querySelector('.count').innerHTML = `${count}`;
_root.bind(option);
}, function (error) {
console.log(error);
});
query.count()
.then(count => {
_root.el.querySelector('.count').innerHTML = count;
})
.catch(ex => {
console.log(ex)
_root.el.querySelector('.count').innerHTML=0
});
_root.bind(option);

}

/**
Expand All @@ -286,7 +312,7 @@ class Valine {
Event.on('click', vsmiles, (e) => {
var textField = _root.el.querySelector('.veditor');
let imgSrc = e.target.src;
if ( typeof imgSrc == 'undefined' ) return;
if (typeof imgSrc == 'undefined') return;
// var tag = " ![](/" + imgSrc.replace(/^.*\/(.*\.gif)$/, '$1') + ") ";
var tag = "!(:" + decodeURI(imgSrc).replace(/^.*\/(.*)$/, '$1') + ":)";
if (document.selection) {
Expand Down Expand Up @@ -319,7 +345,7 @@ class Valine {
})

// cancel reply
Event.on('click', _root.el.querySelector('.vcancel-comment-reply'), (e)=>{
Event.on('click', _root.el.querySelector('.vcancel-comment-reply'), (e) => {
_root.reset();
});

Expand All @@ -340,7 +366,7 @@ class Valine {

var num = 1;
var parent_count = 0;

let parentQuery = (page_num = 1) => {
_root.loading.show();
let cq = _root.v.Query.doCloudQuery(`select nick, comment, link, rid, emailHash, isSpam
Expand Down Expand Up @@ -380,13 +406,13 @@ class Valine {
from Comment
where (rid='' or rid is not exists)
and (url='${defaultComment["url"]}' or url='${defaultComment["url"] + "/"}')
order by -createdAt`).then(data=>{
order by -createdAt`).then(data => {
parent_count = data.count;
parentQuery(1);
});

// 无限嵌套加载
let nestQuery = (vcard, level=1) => {
let nestQuery = (vcard, level = 1) => {
var _vchild = vcard.querySelector('.vcomment-children');
var _vlist = _vchild.querySelector('.vlist');
var _id = vcard.getAttribute('id');
Expand All @@ -398,7 +424,7 @@ class Valine {
from Comment
where rid='${_id}' and (url='${defaultComment["url"]}' or url='${defaultComment["url"] + "/"}')
order by -createdAt`).then(function (data) {
let count = data.count;
let count = data.count;
if (count > 0) {
var _show_children_wrapper = _vchild.querySelector('.vshow-children-wrapper');
_show_children_wrapper.setAttribute('style', 'display: block !important;');
Expand All @@ -424,9 +450,9 @@ class Valine {
let len = rets.length;
if (len) {
for (let i = 0; i < len; i++) {
if (!rets[i].get('isSpam')){
if (!rets[i].get('isSpam')) {
let vl = insertComment(rets[i], _vlist, true)
nestQuery(vl, level+1);
nestQuery(vl, level + 1);
}

}
Expand All @@ -437,7 +463,7 @@ class Valine {
})
};

let insertComment = (comment, vlist=null, top=true) => {
let insertComment = (comment, vlist = null, top = true) => {
let _vcard = document.createElement('li');
_vcard.setAttribute('class', 'vcard');
_vcard.setAttribute('id', comment.id);
Expand All @@ -449,7 +475,7 @@ class Valine {
<img class="vavatar" src="${gravatar_url}"/>
<a rid='${comment.id}' at='@${comment.get('nick')}' class="vat" id="at-${comment.id}">${_root.i18n['reply']}</a>
<div class="vmeta-info">
${comment.get('link') ? `<a class="vname" href="${ comment.get('link') }" target="_blank" rel="nofollow" > ${comment.get("nick")}</a>` : `<span class="vname">${comment.get("nick")}</span>`}
${comment.get('link') ? `<a class="vname" href="${comment.get('link')}" target="_blank" rel="nofollow" > ${comment.get("nick")}</a>` : `<span class="vname">${comment.get("nick")}</span>`}
<span class="spacer">|</span>
<span class="vtime">${timeAgo(comment.get("createdAt"), _root.i18n)}</span>
</div>
Expand Down Expand Up @@ -564,15 +590,15 @@ class Valine {
return;
}
// render markdown
defaultComment.comment = xss(md(defaultComment.comment.replace(/!\(:(.*?\.\w+):\)/g,
`<img src="${option.emoticon_url}/$1" alt="$1" class="vemoticon-img">`)),
{
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
if (name === 'class') {
return name + '="' + xss.escapeAttrValue(value) + '"';
}
}
});
defaultComment.comment = xss(md(defaultComment.comment.replace(/!\(:(.*?\.\w+):\)/g,
`<img src="${option.emoticon_url}/$1" alt="$1" class="vemoticon-img">`)),
{
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
if (name === 'class') {
return name + '="' + xss.escapeAttrValue(value) + '"';
}
}
});
let idx = defaultComment.comment.indexOf(defaultComment.at);
if (idx > -1 && defaultComment.at != '') {
let at = `<a class="at" href='#${defaultComment.rid}'>${defaultComment.at}</a>`;
Expand All @@ -597,7 +623,7 @@ class Valine {

let smile_btn = _root.el.querySelector('.vemoji-btn');
let smile_icons = _root.el.querySelector('.vsmile-icons');
Event.on('click', smile_btn, (e)=>{
Event.on('click', smile_btn, (e) => {
if (preview_text.getAttribute('triggered')) {
preview_text.setAttribute('style', 'display:none;');
preview_text.removeAttribute('triggered');
Expand All @@ -614,7 +640,7 @@ class Valine {

let preview_btn = _root.el.querySelector('.vpreview-btn');
let preview_text = _root.el.querySelector('.vpreview-text');
Event.on('click', preview_btn, (e)=>{
Event.on('click', preview_btn, (e) => {
if (smile_icons.getAttribute('triggered')) {
smile_icons.setAttribute('style', 'display:none;');
smile_icons.removeAttribute('triggered');
Expand All @@ -629,15 +655,15 @@ class Valine {
return;
}
// render markdown
preview_text.innerHTML = xss(md(defaultComment.comment.replace(/!\(:(.*?\.\w+):\)/g,
`<img src="${option.emoticon_url}/$1" alt="$1" class="vemoticon-img">`)),
{
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
if (name === 'class') {
return name + '="' + xss.escapeAttrValue(value) + '"';
}
}
});
preview_text.innerHTML = xss(md(defaultComment.comment.replace(/!\(:(.*?\.\w+):\)/g,
`<img src="${option.emoticon_url}/$1" alt="$1" class="vemoticon-img">`)),
{
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
if (name === 'class') {
return name + '="' + xss.escapeAttrValue(value) + '"';
}
}
});
preview_text.removeAttribute('style');
preview_text.setAttribute('triggered', 1);
}
Expand Down Expand Up @@ -783,7 +809,7 @@ const timeAgo = (date, i18n) => {
//计算相差秒数
var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000);
return seconds + ' ' + i18n['seconds'];
return seconds + ' ' + i18n['seconds'];
}
return minutes + ' ' + i18n['minutes'];
}
Expand Down Expand Up @@ -822,9 +848,9 @@ const loadJS = function (url, success) {
document.getElementsByTagName('head')[0].appendChild(domScript);
};

const getIp = function(){
const getIp = function () {
$.getJSON("https://api.ipify.org/?format=json",
function(json) {
function (json) {
defaultComment['ip'] = json.ip;
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/Valine.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
border-top: 1px solid #ededed;
border-radius: 0;
margin: .3em 0;
max-height: 64px;
max-height: 145px;
overflow: auto;
img {
display: inline-block;
Expand Down Expand Up @@ -125,7 +125,7 @@
background: #fff;
border: none;
resize: none;
min-height: 6em;
min-height: 8em;
margin: 0;
&:focus {
border-color: #c4c8cb;
Expand Down