Skip to content

Commit

Permalink
Feat/comment typing (#431)
Browse files Browse the repository at this point in the history
* feat(comment): comment typing effect

* chore(comment): _ replace $

---------

Co-authored-by: BNDong <[email protected]>
  • Loading branch information
wangyang0210 and BNDong authored Jul 4, 2024
1 parent c17a388 commit 74b8c37
Show file tree
Hide file tree
Showing 5 changed files with 703 additions and 257 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["POWERMODE"]
}
66 changes: 38 additions & 28 deletions src/components/comment/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import defaultAvatarImg from './../../images/webp/default_avatar.webp';

export default function main(_) {

let avatarAnimate = () => {
// 头像动效
if (_.__config.animate.avatar.enable) {
let authorAvatar = $('.author_avatar');
let authorAvatar = $('.author_avatar');
let feedbackAvatar = $('.feedbackAvatar img');

if (!authorAvatar.hasClass('img-rounded')) {
Expand All @@ -23,6 +22,14 @@ export default function main(_) {
feedbackAvatar.addClass('img-rounded').css('border-radius', '50%');
}
}
};

// 评论框打字特效
if (_.__config.animate.typing.enable) {
const POWERMODE = require('./commentTyping/commentTyping');
POWERMODE.colorful = _.__config.animate.typing.options.colorful;
POWERMODE.shake = _.__config.animate.typing.options.shake;
document.body.addEventListener('input', POWERMODE);
}

let setComment = () => {
Expand All @@ -42,12 +49,18 @@ export default function main(_) {

if ($.isArray(idTmp)) id = idTmp.join('');

let op = $('#comment_' + id + '_avatar'), patch = op.length > 0 ? $.trim(op.text())
: defaultAvatarImg;
let op = $('#comment_' + id + '_avatar'),
patch = op.length > 0 ? $.trim(op.text()) : defaultAvatarImg;

let ac = $('#a_comment_author_' + id), ah = ac.length ? ac.attr('href') : 'javascropt:void(0);';
let ac = $('#a_comment_author_' + id),
ah = ac.length ? ac.attr('href') : 'javascropt:void(0);';

avatarHtml = '<div class="feedbackAvatar"><a href="' + ah + '" target="_blank"><img src="'+patch+'"/></a></div>';
avatarHtml =
'<div class="feedbackAvatar"><a href="' +
ah +
'" target="_blank"><img src="' +
patch +
'"/></a></div>';
obj.prepend(avatarHtml);
}

Expand All @@ -60,40 +73,38 @@ export default function main(_) {

avatarAnimate();
}
}

};

let addComment = () => {
let userBlogAddress = $(".comment_my_posted a").attr('href'),
userName = $(".comment_my_posted a").text(),
commentInfo = $(".bq_post_comment").text();

let userBlogAddress = $('.comment_my_posted a').attr('href'),
userName = $('.comment_my_posted a').text(),
commentInfo = $('.bq_post_comment').text();

let comment = `<div class="feedbackItem" style="padding-bottom: 0px;">
let comment = `<div class="feedbackItem" style="padding-bottom: 0px;">
<div class="feedbackAvatar">
<a href="${userBlogAddress}" target="_blank">
<img src="${defaultAvatarImg}">
</a>
</div>
<div class="feedbackListSubtitle ${ window.isBlogOwner && 'feedbackListSubtitle-louzhu'}">
${ window.isBlogOwner && `[<span class="louzhu">楼主</span>]`}
<span class="comment_date">${(new Date).toLocaleString().replace(/\//g,'-')}</span>
<div class="feedbackListSubtitle ${window.isBlogOwner && 'feedbackListSubtitle-louzhu'}">
${window.isBlogOwner && `[<span class="louzhu">楼主</span>]`}
<span class="comment_date">${new Date().toLocaleString().replace(/\//g, '-')}</span>
<a id="a_comment_author_5168811" href="${userBlogAddress}" target="_blank">${userName}</a>
</div>
<div class="feedbackCon">
<div id="comment_body_5168811" data-format-type="Markdown" class="blog_comment_body cnblogs-markdown">
<p>${commentInfo}</p>
</div>
</div>
</div>`
</div>`;

$("#blog-comments-placeholder").append(comment)
$(".comment_my_posted").remove();
$('#blog-comments-placeholder').append(comment);
$('.comment_my_posted').remove();

avatarAnimate();
}
};

_.__timeIds.commentTId = window.setInterval(() =>{
_.__timeIds.commentTId = window.setInterval(() => {
if ($('.feedbackItem').length > 0) {
setComment();
_.__tools.clearIntervalTimeId(_.__timeIds.commentTId);
Expand All @@ -103,21 +114,20 @@ export default function main(_) {
$(document).ajaxSuccess(function (event, xhr, settings) {
// 评论重新排序
if (settings.url.includes('GetComments.aspx')) {
_.__tools.clearIntervalTimeId(_.__timeIds.commentTId);
setComment()
_.__tools.clearIntervalTimeId(_.__timeIds.commentTId);
setComment();
}

// 新增评论
if (settings.url.includes('PostComment/Add.aspx')) addComment()
if (settings.url.includes('PostComment/Add.aspx')) addComment();

// 删除评论
if (settings.url.includes('comment/DeleteComment.aspx')) {
let commentId = JSON.parse(settings?.data)?.commentId;
$(`#comment_body_${commentId}`).parent().parent().remove()
$(".feedbackItem:last").css("padding-bottom", "0")
$(`#comment_body_${commentId}`).parent().parent().remove();
$('.feedbackItem:last').css('padding-bottom', '0');
}

avatarAnimate();
});

}
}
138 changes: 138 additions & 0 deletions src/components/comment/commentTyping/commentTyping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* UPDATES AND DOCS AT: https://github.com/wangyang0210
* https://www.cnblogs.com/wangyang0210/
* @author: https://github.com/disjukr/activate-power-mode
* @Date 2023-02-19 12:54
* ----------------------------------------------
* @describe: POWERMODE
*/

let canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.cssText = 'position:fixed;top:0;left:0;pointer-events:none;z-index:999999';
window.addEventListener('resize', function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
document.body.appendChild(canvas);
let context = canvas.getContext('2d');
let particles = [];
let particlePointer = 0;
let rendering = false;

POWERMODE.shake = true;

function getRandom(min, max) {
return Math.random() * (max - min) + min;
}

function getColor(el) {
if (POWERMODE.colorful) {
let u = getRandom(0, 360);
return 'hsla(' + getRandom(u - 10, u + 10) + ', 100%, ' + getRandom(50, 80) + '%, ' + 1 + ')';
} else {
return window.getComputedStyle(el).color;
}
}

function getCaret() {
let el = document.activeElement;
let bcr;
if (el.tagName === 'TEXTAREA' ||
(el.tagName === 'INPUT' && el.getAttribute('type') === 'text')) {
let offset = require('./textareaCaretPosition')(el, el.selectionEnd);
bcr = el.getBoundingClientRect();
return {
x: offset.left + bcr.left,
y: offset.top + bcr.top,
color: getColor(el)
};
}
let selection = window.getSelection();
if (selection.rangeCount) {
let range = selection.getRangeAt(0);
let startNode = range.startContainer;
if (startNode.nodeType === document.TEXT_NODE) {
startNode = startNode.parentNode;
}
bcr = range.getBoundingClientRect();
return {
x: bcr.left,
y: bcr.top,
color: getColor(startNode)
};
}
return { x: 0, y: 0, color: 'transparent' };
}

function createParticle(x, y, color) {
return {
x: x,
y: y,
alpha: 1,
color: color,
velocity: {
x: -1 + Math.random() * 2,
y: -3.5 + Math.random() * 2
}
};
}

function POWERMODE() {
{ // spawn particles
let caret = getCaret();
let numParticles = 5 + Math.round(Math.random() * 10);
while (numParticles--) {
particles[particlePointer] = createParticle(caret.x, caret.y, caret.color);
particlePointer = (particlePointer + 1) % 500;
}
}
{ // shake screen
if (POWERMODE.shake) {
let intensity = 1 + 2 * Math.random();
let x = intensity * (Math.random() > 0.5 ? -1 : 1);
let y = intensity * (Math.random() > 0.5 ? -1 : 1);
document.body.style.marginLeft = x + 'px';
document.body.style.marginTop = y + 'px';
setTimeout(function() {
document.body.style.marginLeft = '';
document.body.style.marginTop = '';
}, 75);
}
}
if(!rendering){
requestAnimationFrame(loop);
}
};
POWERMODE.colorful = false;

function loop() {
rendering = true;
context.clearRect(0, 0, canvas.width, canvas.height);
let rendered = false;
let rect = canvas.getBoundingClientRect();
for (let i = 0; i < particles.length; ++i) {
let particle = particles[i];
if (particle.alpha <= 0.1) continue;
particle.velocity.y += 0.075;
particle.x += particle.velocity.x;
particle.y += particle.velocity.y;
particle.alpha *= 0.96;
context.globalAlpha = particle.alpha;
context.fillStyle = particle.color;
context.fillRect(
Math.round(particle.x - 1.5) - rect.left,
Math.round(particle.y - 1.5) - rect.top,
3, 3
);
rendered = true;
}
if(rendered){
requestAnimationFrame(loop);
}else{
rendering = false;
}
}

module.exports = POWERMODE;
Loading

0 comments on commit 74b8c37

Please sign in to comment.