1.1.6 • Published 6 months ago

halo-comment-dream v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

halo-theme-dream 博客主题对应的博客评论组件,适用于 Halo 博客系统。非常完善的一个博客插件,支持图片上传、显示评论弹幕功能。

一、预览

玖涯博客

预览地址:https://blog.nineya.com

二、使用指南

  1. 进入后台 -> 系统 -> 博客设置 -> 评论设置
  2. 评论模块 JS 修改为:https://unpkg.com/halo-comment-dream@latest/dist/halo-comment.min.js

三、自定义配置

如果你需要自定义该评论组件,下面提供了一些属性:

属性说明默认值可选
autoLoad是否自动加载评论列表truetrue false
showUserAgent是否显示评论者的 UA 信息truetrue false
priorityQQAvatar是否优先展示QQ头像falsetrue false
getQQInfo昵称输入框输入QQ号自动获取QQ昵称和邮箱falsetrue false
commentHtml开启html内容,启用后有被 XSS 恶意代码注入的风险,建议同时开启评论审核。falsetrue false
loadingStyle评论加载样式defaultdefault circle balls
unfoldReplyNum评论的回复列表默认展开的回复数量10大于 0 的正整数
night评论模块以黑暗模式初始化样式localStoragenight 的值true false
replyDescSoft评论的二级回复是否采用按时间从晚到早排序falsetrue false
enableImageUpload开启评论区图片上传功能falsetrue false
enableBulletScreen开启评论弹幕falsetrue false
imageUploadApi接受图片上传的后端 api 地址undefinedurl 路径
anonymousUserName匿名评论的用户昵称,配置后允许用户匿名评论undefined用户名
avatarLoading头像加载动画assets/img/loading.svg图片路径
enableBloggerOperation如果博主已登录,允许博主直接在评论区进行操作falsetrue false
defaultAvatar默认头像,当头像加载失败时显示assets/img/avatar.svg图片路径

配置方法:

在引入评论组件的页面加上:

<script>
var configs = {
    autoLoad: true,
    showUserAgent: true
}
</script>

修改评论组件标签加上:

:configs="configs"

示例:

<halo-comment id="${post.id?c}" type="post" :configs="configs">
<halo-comment id="${sheet.id?c}" type="sheet" :configs="configs">
<halo-comment id="${journal.id?c}" type="journal" :configs="configs">

四、使用指南

4.1 Vue 方式

适用于基于 Vue 开发的主题,否则不能通过 :configs 的方式指定配置

新建 comment.ftl:

<#macro comment target,type>
    <#if !post.disallowComment!false>
        <script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
        <script src="${options.comment_internal_plugin_js!'//unpkg.com/halo-comment-dream@latest/dist/halo-comment.min.js'}"></script>
        <script>
        var configs = {
            autoLoad: true,
            showUserAgent: true
        }
        </script>
        <halo-comment id="${target.id?c}" type="${type}" :configs="configs"/>
    </#if>
</#macro>

然后在 post.ftl / sheet.ftl 中引用:

post.ftl:

<#include "comment.ftl">
<@comment target=post type="post" />

sheet.ftl:

<#include "comment.ftl">
<@comment target=sheet type="sheet" />

4.2普通方式

Vue 则需要直接将 JSON 格式的配置写入到 configs 属性。

新建 comment.ftl:

<#macro comment target,type>
    <#if !post.disallowComment!false>
        <script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
        <script src="${options.comment_internal_plugin_js!'//unpkg.com/halo-comment-dream@latest/dist/halo-comment.min.js'}"></script>
        <halo-comment id="${target.id?c}" type="${type}" configs='{"autoLoad": true,"showUserAgent": true}'/>
    </#if>
</#macro>

然后在 post.ftl / sheet.ftl 中引用:

post.ftl:

<#include "comment.ftl">
<@comment target=post type="post" />

sheet.ftl:

<#include "comment.ftl">
<@comment target=sheet type="sheet" />

五、进阶配置

5.1 通过主题进行配置

可以将 configs 中的属性通过 settings.yaml 保存数据库中,以供用户自行选择,如:

settings.yaml:

...

comment:
  label: 评论设置
  items:
    autoLoad:
      name: autoLoad
      label: 自动加载评论
      type: radio
      data-type: bool
      default: true
      options:
        - value: true
          label: 开启
        - value: false
          label: 关闭
    showUserAgent:
      name: showUserAgent
      label: 评论者 UA 信息
      type: radio
      data-type: bool
      default: true
      options:
        - value: true
          label: 显示
        - value: false
          label: 隐藏

...

那么我们需要将上面的 script 改为下面这种写法:

<script>
var configs = {
    autoLoad: ${settings.autoLoad!},
    showUserAgent: ${settings.showUserAgent!}
}
</script>

5.2 使用明亮/黑暗模式

评论模块支持明亮和黑暗两种模式,默认通过 localStoragenight 的值来初始化评论模块的模式。

动态切换模式的 js 方法:

// isNight为要切换到的模式,true表示黑暗模式
isNight = true 
$("halo-comment").each(function () {
    const shadowDom = this.shadowRoot.getElementById("halo-comment");
    $(shadowDom)[`${isNight ? "add" : "remove"}Class`]("night");
})
// 存储模式配置,用于打开网页时评论区的初始化
localStorage.setItem('night', isNight);

5.3 实现图片上传

本插件支持文件上传功能,后端文件上传接口需要基于以下报文格式实现。

  • 请求类型:POST

  • 请求报文(multipart/form-data):

参数说明
image上传图片文件的文件流
  • 响应报文(application/json):
参数说明
code/status错误状态码,响应成功的码值为 200
message错误信息
data.name文件名
data.url访问图片的url

5.4 暂停弹幕

允许主题通过 stop-bullet-screen 属性暂停弹幕,添加该属性后弹幕将被暂停,删除属性后可继续进行属性轮播。

实现示例如下:

const applyStopBulletScreen = (stopBulletScreenValue) => {
    $('halo-comment[bullet-screen]').each(function () {
    const shadowDom = this.shadowRoot.getElementById("halo-comment");
    if (stopBulletScreenValue) {
      $(shadowDom).attr('stop-bullet-screen', 'true');
    } else {
      $(shadowDom).removeAttr('stop-bullet-screen');
    }
  })
  localStorage.setItem('stop-bullet-screen', stopBulletScreenValue);
}
$bulletScreen.on('click', () => {
  let stopBulletScreen = localStorage.getItem('stop-bullet-screen') || false;
  applyStopBulletScreen(stopBulletScreen.toString() !== 'true')
});

5.5 设置主题色

评论模块支持为明亮/黑暗模式分别指定一个主题色,默认明亮模式的主题色为 #50bfff,黑暗模式的主题色为 #5d93db,你可以通过 --theme 属性在你的主题指定一个主题色。

html {
  --theme: #50bfff;
}

六、说明

  1. configs 可以不用配置。
  2. 具体主题开发文档请参考:https://halo.run/develop/theme/ready.html

七、打赏项目

感谢您对本项目的喜爱,您的打赏是对本项目最好的支持!本项目所有打赏收益将全部投入到支付宝公益项目,捐赠记录在关于我的中可见,一起为公益事业加油。

打赏项目

1.1.6

6 months ago

1.1.5

8 months ago

1.1.4

9 months ago

1.1.3

12 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.2

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago