Skip to content

Commit

Permalink
some update 2019-04-01 14:26
Browse files Browse the repository at this point in the history
  • Loading branch information
xCss committed Apr 1, 2019
1 parent a0d23d1 commit beeaeec
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 60 deletions.
7 changes: 4 additions & 3 deletions dist/Valine.Pure.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions dist/Valine.min.js

Large diffs are not rendered by default.

Binary file added dist/Valine.min.zip
Binary file not shown.
58 changes: 38 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ ValineFactory.prototype.init = function (option) {
avatar_cdn,
notify,
verify,
visitor,
pageSize,
recordIP
} = option;
Expand All @@ -143,16 +144,16 @@ ValineFactory.prototype.init = function (option) {
root.notify = notify || false;
root.verify = verify || false;

if(recordIP){
let ipScript = Utils.create('script','src','//api.ip.sb/jsonip?callback=getIP');
let s = document.getElementsByTagName("script")[0];
if (recordIP) {
let ipScript = Utils.create('script', 'src', '//api.ip.sb/jsonip?callback=getIP');
let s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(ipScript, s);
// 获取IP
window.getIP = function(json){
window.getIP = function (json) {
defaultComment['ip'] = json.ip;
}
}

_avatarSetting['params'] = `?d=${(ds.indexOf(avatar) > -1 ? avatar : 'mp')}&v=${VERSION}${force}`;
_avatarSetting['hide'] = avatar === 'hide' ? true : false;
_avatarSetting['cdn'] = LINKREG.test(avatar_cdn) ? avatar_cdn : _avatarSetting['cdn']
Expand Down Expand Up @@ -182,6 +183,9 @@ ValineFactory.prototype.init = function (option) {
}
let id = option.app_id || option.appId;
let key = option.app_key || option.appKey;
if (!id || !key) throw 99;
AV.applicationId && delete AV._config.applicationId || (AV.applicationId = null);
AV.applicationKey && delete AV._config.applicationKey || (AV.applicationKey = null);
AV.init({
appId: id,
appKey: key
Expand All @@ -204,7 +208,7 @@ ValineFactory.prototype.init = function (option) {
}

// Counter
option.visitor && CounterFactory.add(AV.Object.extend('Counter'));
visitor && CounterFactory.add(AV.Object.extend('Counter'));

let el = option.el || null;
let _el = Utils.findAll(document, el);
Expand Down Expand Up @@ -504,12 +508,14 @@ ValineFactory.prototype.bind = function (option) {
}
root.preview = {
show() {
root.emoji.hide();
Utils.attr(_vpreviewCtrl, 'v', 1);
Utils.removeAttr(_emojiCtrl, 'v');
_vpreview.innerHTML = defaultComment['comment'];
Utils.attr(_vpreview, 'style', 'display:block');
_activeMathJax()
if (defaultComment['comment']) {
root.emoji.hide();
Utils.attr(_vpreviewCtrl, 'v', 1);
Utils.removeAttr(_emojiCtrl, 'v');
_vpreview.innerHTML = defaultComment['comment'];
Utils.attr(_vpreview, 'style', 'display:block');
_activeOtherFn()
}
return root.preview
},
hide() {
Expand Down Expand Up @@ -725,7 +731,7 @@ ValineFactory.prototype.bind = function (option) {
let _as = Utils.findAll(_vcard, 'a');
for (let i = 0, len = _as.length; i < len; i++) {
let _a = _as[i];
if (_a && (Utils.attr(_a, 'data-at') || '').indexOf('at') == -1) {
if (_a && (Utils.attr(_a, 'class') || '').indexOf('at') == -1) {
Utils.attr(_a, {
'target': '_blank',
'rel': 'nofollow'
Expand All @@ -737,18 +743,30 @@ ValineFactory.prototype.bind = function (option) {
else node.insertBefore(_vcard, _vlis[0]);
let _vcontent = Utils.find(_vcard, '.vcontent');
if (_vcontent) expandEvt(_vcontent);
if (_vat) bindAtEvt(_vat,rt);
_activeMathJax()
if (_vat) bindAtEvt(_vat, rt);
_activeOtherFn()
}


let _activeMathJax = () => {
let _activeOtherFn = () => {
setTimeout(function () {
let MathJax = MathJax || '';
MathJax && MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
try {
let MathJax = MathJax || '';
MathJax && MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
$('pre code').each(function (i, block) {
hljs.highlightBlock(block);
})
$('code.hljs').each(function (i, block) {
hljs.lineNumbersBlock(block);
});
} catch (error) {

}
}, 20)
}

let _activeHLJS = () => {}

// expand event
let expandEvt = (el) => {
setTimeout(function () {
Expand All @@ -763,7 +781,7 @@ ValineFactory.prototype.bind = function (option) {

let atData = {}
// at event
let bindAtEvt = (el,rt) => {
let bindAtEvt = (el, rt) => {
Utils.on('click', el, (e) => {
let at = `@${Utils.escape(rt.get('nick'))}`;
atData = {
Expand Down Expand Up @@ -848,7 +866,7 @@ ValineFactory.prototype.bind = function (option) {
let pid = atData['pid'] || atData['rid'];
comment.set('rid', atData['rid']);
comment.set('pid', pid);
defaultComment['comment'] = defaultComment['comment'].replace('<p>',`<p><a href="#${pid}">${atData['at']}</a> , `);
defaultComment['comment'] = defaultComment['comment'].replace('<p>', `<p><a class="at" href="#${pid}">${atData['at']}</a> , `);
}
for (let i in defaultComment) {
if (defaultComment.hasOwnProperty(i)) {
Expand Down
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.3.5",
"version": "1.3.6",
"description": "A simple comment system based on Leancloud.",
"main": "/dist/Valine.min.js",
"author": "xCss <[email protected]> (https://github.com/xCss)",
Expand Down
75 changes: 44 additions & 31 deletions utils/htmlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const reHasEscapedHtml = RegExp(reEscapedHtml.source)

const utils = {
on(type, el, handler, capture) {
type=type.split(' ')
for(let i=0,len=type.length;i<len;i++){
utils.off(type[i],el,handler,capture)
type = type.split(' ')
for (let i = 0, len = type.length; i < len; i++) {
utils.off(type[i], el, handler, capture)
if (el.addEventListener) el.addEventListener(type[i], handler, capture || false);
else if (el.attachEvent) el.attachEvent(`on${type[i]}`, handler);
else el[`on${type[i]}`] = handler;
Expand Down Expand Up @@ -51,9 +51,9 @@ const utils = {
* @param {Object} attrName
* @param {Object} attrVal
*/
create(name,attrName,attrVal){
create(name, attrName, attrVal) {
let el = document.createElement(name)
utils.attr(el,attrName,attrVal)
utils.attr(el, attrName, attrVal)
return el
},
/**
Expand Down Expand Up @@ -85,11 +85,11 @@ const utils = {
if (value !== undefined) {
if (value === null) utils.removeAttr(el, name)
else el.setAttribute(name, value)
}else if(({}).toString.call(name) === '[object Object]' ){
utils.each(name,(k,v)=>{
el.setAttribute(k,v)
} else if (({}).toString.call(name) === '[object Object]') {
utils.each(name, (k, v) => {
el.setAttribute(k, v)
})
}else return el.getAttribute(name)
} else return el.getAttribute(name)
},
/**
* get prop or set prop
Expand All @@ -98,13 +98,13 @@ const utils = {
* @param {String} value
*/
prop(el, name, value) {
if(value !== undefined){
if (value !== undefined) {
return el[name] = value
}else if(({}).toString.call(name) === '[object Object]' ){
utils.each(name,(k,v)=>{
} else if (({}).toString.call(name) === '[object Object]') {
utils.each(name, (k, v) => {
el[k] = v
})
}else return el[name]
} else return el[name]
},
/**
* Remove el attribute
Expand All @@ -129,23 +129,37 @@ const utils = {
* Clear element attributes
* @param {HTMLElement} el
*/
clearAttr(el){
clearAttr(el) {
let attrs = el.attributes
let ignoreAttrs = ['align','alt','disabled','href','id','target','title','type','src']
utils.each(attrs,(idx,attr)=>{
let ignoreAttrs = ['align', 'alt', 'disabled', 'href', 'id', 'target', 'title', 'type', 'src', 'class', 'style']
utils.each(attrs, (idx, attr) => {
let name = attr.name
if('style' === name){
let style = attr.value
utils.each(style.split(';'),(idx,item)=>{
if(item.indexOf('color') > -1) {
utils.attr(el,'style',item);
switch (attr.name.toLowerCase()) {
case 'style':
let style = attr.value
utils.each(style.split(';'), (idx, item) => {
if (item.indexOf('color') > -1) {
utils.attr(el, 'style', item);
return false
} else utils.removeAttr(el, 'style');
})
break;
case 'class':
if (el.nodeName == 'CODE') return false
let clazz = attr.value
if (clazz.indexOf('at') > -1) {
utils.attr(el, 'class', 'at');
return false
}else utils.removeAttr(el,'style');
})
}
break;
default:
// utils.removeAttr(el,'class');
break;

}

if(ignoreAttrs.indexOf(name)>-1) return
else utils.removeAttr(el,name)
if (ignoreAttrs.indexOf(name) > -1) return
else utils.removeAttr(el, name)
})
return el
},
Expand All @@ -155,9 +169,8 @@ const utils = {
*/
remove(child) {
try {
if(child.parentNode) child.parentNode.removeChild(child)
} catch (error) {
}
if (child.parentNode) child.parentNode.removeChild(child)
} catch (error) {}
},

/**
Expand All @@ -171,17 +184,17 @@ const utils = {
let value,
i = 0,
length = collection.length,
likeArray = ["[object Array]","[object NodeList]"],
likeArray = ["[object Array]", "[object NodeList]"],
type = ({}).toString.call(collection)
if (likeArray.indexOf(type) > -1) {
for (; i < length; i++) {
value = callback && callback.call(collection[i],i, collection[i])
value = callback && callback.call(collection[i], i, collection[i])
if (value === false) break
}
} else {
for (i in collection) {
if (collection.hasOwnProperty(i)) {
value = callback && callback.call(collection[i],i, collection[i])
value = callback && callback.call(collection[i], i, collection[i])
if (value === false) break
}
}
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const plugins = [];
const banner =
'Valine v' + version + '\n' +
'(c) 2017-' + new Date().getFullYear() + ' xCss\n' +
'Released under the GPL-2.0 License.' +
'Update: ' + new Date();
'Released under the GPL-2.0 License.\n' +
'Last Update: ' + (new Date()).toLocaleString();

const TARGET = process.env
if(TARGET.NODE_ENV == 'production'){
Expand Down

0 comments on commit beeaeec

Please sign in to comment.