From 64b9a61276d6cb4bfe1b44a6e2e8ff294d0f6266 Mon Sep 17 00:00:00 2001 From: xiaozzzi <42293085+xiaozzzi@users.noreply.github.com> Date: Wed, 17 Apr 2024 00:41:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=B4=E6=97=B6=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B0=B4=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../article/draft/ArticleController.java | 16 +- .../backend/server/utils/ArticleUtil.java | 316 +++++++++++++++--- .../src/main/resources/exportTemplate.html | 16 +- 3 files changed, 295 insertions(+), 53 deletions(-) diff --git a/blossom-backend/backend/src/main/java/com/blossom/backend/server/article/draft/ArticleController.java b/blossom-backend/backend/src/main/java/com/blossom/backend/server/article/draft/ArticleController.java index 813b899..21c3d8d 100644 --- a/blossom-backend/backend/src/main/java/com/blossom/backend/server/article/draft/ArticleController.java +++ b/blossom-backend/backend/src/main/java/com/blossom/backend/server/article/draft/ArticleController.java @@ -265,7 +265,13 @@ public class ArticleController { } String reportHtml = ArticleUtil.toHtml(article, userService.selectById(AuthContext.getUserId()), - userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_COLOR).getParamValue()); + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_COLOR).getParamValue(), + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_ENABLED).getParamValue(), + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_CONTENT).getParamValue(), + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_FONTSIZE).getParamValue(), + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_COLOR).getParamValue(), + userParamService.getValue(AuthContext.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_GAP).getParamValue() + ); try (InputStream is = new ByteArrayInputStream(reportHtml.getBytes(StandardCharsets.UTF_8)); BufferedInputStream bis = new BufferedInputStream(is)) { String filename = URLEncodeUtil.encode(article.getName() + ".html"); @@ -339,6 +345,12 @@ public class ArticleController { return ArticleUtil.toHtml( article, userService.selectById(visit.getUserId()), - userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_COLOR).getParamValue()); + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_COLOR).getParamValue(), + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_ENABLED).getParamValue(), + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_CONTENT).getParamValue(), + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_FONTSIZE).getParamValue(), + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_COLOR).getParamValue(), + userParamService.getValue(visit.getUserId(), UserParamEnum.WEB_BLOG_WATERMARK_GAP).getParamValue() + ); } } diff --git a/blossom-backend/backend/src/main/java/com/blossom/backend/server/utils/ArticleUtil.java b/blossom-backend/backend/src/main/java/com/blossom/backend/server/utils/ArticleUtil.java index dd6f39b..878a70c 100644 --- a/blossom-backend/backend/src/main/java/com/blossom/backend/server/utils/ArticleUtil.java +++ b/blossom-backend/backend/src/main/java/com/blossom/backend/server/utils/ArticleUtil.java @@ -1,18 +1,16 @@ package com.blossom.backend.server.utils; import cn.hutool.core.util.ArrayUtil; -import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import com.blossom.backend.base.user.pojo.UserEntity; import com.blossom.backend.server.article.draft.pojo.ArticleEntity; -import com.blossom.common.base.util.DateUtils; +import com.blossom.common.base.enums.YesNo; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.IOException; import java.io.InputStream; -import java.util.Date; /** * 文章工具类 @@ -105,7 +103,7 @@ public class ArticleUtil { /** * */ - private static final String SCRIPT_TAG_BLOG_COLOR = " " + ""; + private static final String HEAD_SCRIPT_WATERMARK = ""; - private static final String prefix = "\n" + + + private static final String BODY_HEADER_AND_TOC = "\n" + "
\n" + "
本文作者:{BLOSSOM_EXPORT_HTML_AUTHOR}。著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Export by Blossom\n" + "
《{BLOSSOM_EXPORT_HTML_ARTICLE_NAME}》
\n" + "
目录
\n" + - "
"; + "
"; - private static final String suffix = "
"; + private static final String suffix = ""; - private static String htmlTag; + private static String htmlTemplate; static { Resource resource = new ClassPathResource("exportTemplate.html"); try (InputStream is = resource.getInputStream()) { byte[] bytes = new byte[is.available()]; is.read(bytes); - htmlTag = new String(bytes); + htmlTemplate = new String(bytes); } catch (IOException e) { e.printStackTrace(); } @@ -162,48 +362,74 @@ public class ArticleUtil { /** * 将文章转换为 html 格式 * - * @param article 文章 - * @param user 用户, 用户获取作者 - * @param blogColor 主颜色 + * @param article 文章 + * @param user 用户, 用户获取作者 + * @param blogColor 主颜色 + * @param WEB_BLOG_WATERMARK_ENABLED 开启水印 + * @param WEB_BLOG_WATERMARK_CONTENT 水印内容 + * @param WEB_BLOG_WATERMARK_FONTSIZE 水印字体大小 + * @param WEB_BLOG_WATERMARK_COLOR 水印颜色 + * @param WEB_BLOG_WATERMARK_GAP 水印密集度 * @return html 内容 */ - public static String toHtml(ArticleEntity article, UserEntity user, String blogColor) { - return htmlTag + - SCRIPT_TAG_BLOG_COLOR - .replaceAll("\\{BLOSSOM_WEB_BLOG_COLOR}", blogColor) + - // 替换作者, 文章名称 - prefix - .replaceAll("\\{BLOSSOM_EXPORT_HTML_AUTHOR}", user.getNickName()) - .replaceAll("\\{BLOSSOM_EXPORT_HTML_ARTICLE_NAME}", article.getName()) + - article.getHtml() + suffix; + public static String toHtml(ArticleEntity article, + UserEntity user, + String blogColor, + String WEB_BLOG_WATERMARK_ENABLED, + String WEB_BLOG_WATERMARK_CONTENT, + String WEB_BLOG_WATERMARK_FONTSIZE, + String WEB_BLOG_WATERMARK_COLOR, + String WEB_BLOG_WATERMARK_GAP) { + return htmlTemplate + + appendHeadScript(blogColor, + WEB_BLOG_WATERMARK_ENABLED, + WEB_BLOG_WATERMARK_CONTENT, + WEB_BLOG_WATERMARK_FONTSIZE, + WEB_BLOG_WATERMARK_COLOR, + WEB_BLOG_WATERMARK_GAP) + + appendBodyHeader(article, user) + + article.getHtml() + + suffix; } - - private static void genHeatmap() { - Date begin = DateUtils.parse("2023-04-01", DateUtils.PATTERN_YYYYMMDD); - Date end = DateUtils.parse("2023-06-30", DateUtils.PATTERN_YYYYMMDD); - for (; DateUtils.compare(begin, end) <= 0; begin = DateUtils.offsetDay(begin, 1)) { - String dt = DateUtils.format(begin, DateUtils.PATTERN_YYYYMMDD); - int value = RandomUtil.randomInt(0, 10); - System.out.println(String.format("insert into blossom_stat values(null, 1, '%s' ,%s);", dt, value)); + /** + * 添加 head 下的动态 script + * + * @param blogColor 主题色 + * @param WEB_BLOG_WATERMARK_ENABLED 开启水印 + * @param WEB_BLOG_WATERMARK_CONTENT 水印内容 + * @param WEB_BLOG_WATERMARK_FONTSIZE 水印字体大小 + * @param WEB_BLOG_WATERMARK_COLOR 水印颜色 + * @param WEB_BLOG_WATERMARK_GAP 水印密集度 + * @return head 下的所有动态 script + */ + private static String appendHeadScript(String blogColor, + String WEB_BLOG_WATERMARK_ENABLED, + String WEB_BLOG_WATERMARK_CONTENT, + String WEB_BLOG_WATERMARK_FONTSIZE, + String WEB_BLOG_WATERMARK_COLOR, + String WEB_BLOG_WATERMARK_GAP) { + String script = HEAD_SCRIPT_BLOG_COLOR.replaceAll("\\{BLOSSOM_WEB_BLOG_COLOR}", blogColor); + if (YesNo.YES.getValue().toString().equals(WEB_BLOG_WATERMARK_ENABLED)) { + script += HEAD_SCRIPT_WATERMARK + .replaceAll("\\{WEB_BLOG_WATERMARK_CONTENT}", WEB_BLOG_WATERMARK_CONTENT) + .replaceAll("\\{WEB_BLOG_WATERMARK_FONTSIZE}", WEB_BLOG_WATERMARK_FONTSIZE) + .replaceAll("\\{WEB_BLOG_WATERMARK_COLOR}", WEB_BLOG_WATERMARK_COLOR) + .replaceAll("\\{WEB_BLOG_WATERMARK_GAP}", WEB_BLOG_WATERMARK_GAP); } + return script + ""; } - private static void genWords() { - int value = 203012; - Date begin = DateUtils.parse("2019-01-01", DateUtils.PATTERN_YYYYMMDD); - Date end = DateUtils.parse("2023-06-30", DateUtils.PATTERN_YYYYMMDD); - for (; DateUtils.compare(begin, end) <= 0; begin = DateUtils.offsetMonth(begin, 1)) { - String dt = DateUtils.format(begin, DateUtils.PATTERN_YYYYMMDD); - value = value + RandomUtil.randomInt(0, 10000); - System.out.println(String.format("insert into blossom_stat values(null, 2, '%s' ,%s);", dt, value)); - } - } - - public static void main(String[] args) { -// ArticleEntity a = new ArticleEntity(); -// a.setName("123123213"); -// a.setHtml("asdasd"); -// System.out.println(exportHtml(a)); + /** + * 增加页面顶部的文章作者和目录顶部的文章名称 + * + * @param article 文章 + * @param user 用户, 用户获取作者 + * @return 页面顶部的文章作者和文章名称 + */ + private static String appendBodyHeader(ArticleEntity article, UserEntity user) { + return BODY_HEADER_AND_TOC + .replaceAll("\\{BLOSSOM_EXPORT_HTML_AUTHOR}", user.getNickName()) + .replaceAll("\\{BLOSSOM_EXPORT_HTML_ARTICLE_NAME}", article.getName()); } } diff --git a/blossom-backend/backend/src/main/resources/exportTemplate.html b/blossom-backend/backend/src/main/resources/exportTemplate.html index 075bd67..0bee0fe 100644 --- a/blossom-backend/backend/src/main/resources/exportTemplate.html +++ b/blossom-backend/backend/src/main/resources/exportTemplate.html @@ -1358,6 +1358,9 @@ color: #c5c5c5; border-bottom: 1px solid #eaeaea; text-align: right; + position: fixed; + z-index: 999; + background-color: #fff; } .header .copyright { @@ -1380,7 +1383,7 @@ display: flex; flex-direction: row; justify-content: flex-start; - align-items: flex-start + align-items: flex-start; } .content .main { @@ -1388,7 +1391,8 @@ width: calc(100% - 300px); padding: 20px; overflow-x: hidden; - overflow-y: scroll + overflow-y: scroll; + margin-top: 30px; } .toc { @@ -1398,6 +1402,7 @@ border-right: 1px solid #eeeeee; overflow-x: hidden; overflow-y: scroll; + margin-top: 30px; } .toc > h1, @@ -1434,8 +1439,6 @@ transition: opacity 0.1s; } - - .toc > h1:hover, .toc > h2:hover, .toc > h3:hover, @@ -1615,6 +1618,7 @@ background-color: var(--bl-preview-bg-color); line-height: 23px; font-family: 'Microsoft YaHei', '微软雅黑', Arial, sans-serif; + position: relative; } .bl-preview * { @@ -2034,7 +2038,6 @@ for (let i = 0; i < heads.length; i++) { let head = heads[i]; let tocHead = document.createElement(head.localName); - console.log(head.id); tocHead.setAttribute('class', `toc-${head.localName}`); tocHead.innerText = head.innerText; tocHead.onclick = function () { @@ -2042,7 +2045,8 @@ if (ele == null || ele == undefined) { return } - ele.parentNode.scrollTop = ele.offsetTop - 30 + // ele.parentNode.scrollTop = ele.offsetTop - 30 + ele.scrollIntoView() }; tocContainer.appendChild(tocHead) }