Skip to content

Commit

Permalink
🐛 修复插件配置修改之后不生效的问题
Browse files Browse the repository at this point in the history
之前由于错误的将 config 设置在构造函数中,并且将其进行了缓存,导致配置更新之后插件仍旧只能读取到旧数据。
  • Loading branch information
LIlGG committed Feb 24, 2023
1 parent 87e421c commit bddbfa2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "io.github.guqing.plugin-development" version "0.0.6-SNAPSHOT"
id "io.github.guqing.plugin-development" version "0.0.8-SNAPSHOT"
id 'java'
}

Expand Down
43 changes: 21 additions & 22 deletions src/main/java/run/halo/live2d/Live2dInitProcessor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package run.halo.live2d;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Objects;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
Expand All @@ -26,11 +28,10 @@
* 将在网站其他内容加载完成之后再进行初始化,尽量保证不阻塞页面运行。
* </p>
*
* @see <a href="https://github.com/stevenjoezhang/live2d-widget">live2d-widget</a>
*
* @author LIlGG
* @since 2022-11-30
* @version 1.0.1
* @see <a href="https://github.com/stevenjoezhang/live2d-widget">live2d-widget</a>
* @since 2022-11-30
*/
@Component
@Slf4j
Expand All @@ -52,10 +53,8 @@ public Live2dInitProcessor(Live2dSetting live2dSetting) {
@Override
public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
return live2dSetting.getConfig().map(config -> {
if (log.isDebugEnabled()) {
log.debug("live2d config {}", config.toPrettyString());
}
return this.live2dSetting.getConfig().map(config -> {
log.info("live2d config {}", config.toPrettyString());
final IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(live2dAutoloadScript(config)));
return Mono.empty();
Expand All @@ -69,30 +68,30 @@ private CharSequence live2dAutoloadScript(JsonNode config) {
loadTime = node.asText(LIVE2D_LOAD_TIME);
}
String template = """
live2d.init("%1$s", %2$s)
""".formatted(LIVE2D_SOURCE_PATH, config.toPrettyString());
live2d.init("%1$s", %2$s)
""".formatted(LIVE2D_SOURCE_PATH, config.toPrettyString());
return """
<script src="%1$sjs/live2d-autoload.min.js" %2$s></script>
<script type="text/javascript">
%3$s
</script>
""".formatted(LIVE2D_SOURCE_PATH, loadTime, loadLive2d(loadTime, template));
<script src="%1$sjs/live2d-autoload.min.js" %2$s></script>
<script type="text/javascript">
%3$s
</script>
""".formatted(LIVE2D_SOURCE_PATH, loadTime, loadLive2d(loadTime, template));
}

private CharSequence loadLive2d(String loadTime, String loadingScript) {
String template;
if (Objects.equals(loadTime, LIVE2D_LOAD_TIME)) {
template = """
document.addEventListener('DOMContentLoaded', () => {
%s
})
""";
document.addEventListener('DOMContentLoaded', () => {
%s
})
""";
} else {
template = """
windows.onload = function() {
%s
}
""";
window.onload = function() {
%s
}
""";
}
return template.formatted(loadingScript);
}
Expand Down
36 changes: 22 additions & 14 deletions src/main/java/run/halo/live2d/Live2dSettingProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Map;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import run.halo.app.plugin.SettingFetcher;

/**
* Live2d 配置处理器
*
Expand All @@ -25,36 +28,40 @@ public class Live2dSettingProcess extends JsonNodeFactory implements Live2dSetti

private final ThemeFetcher themeFetcher;

private final Map<String, JsonNode> settingMap;
private final SettingFetcher settingFetcher;

private ObjectNode configNode;

private Map<String, JsonNode> settingMap;

public Live2dSettingProcess(SettingFetcher settingFetcher,
ThemeFetcher themeFetcher) {
this.settingMap = settingFetcher.getValues();
this.settingFetcher = settingFetcher;
this.themeFetcher = themeFetcher;
initConfigNode();
}

public void initConfigNode() {
public ObjectNode initConfigNode() {
this.settingMap = settingFetcher.getValues();
this.configNode = new ObjectNode(this);
this.settingMap.forEach((group, jsonNode) -> {
JsonNode node = this.settingMap.get(group);
if(log.isDebugEnabled()) {
settingMap.forEach((group, jsonNode) -> {
JsonNode node = settingMap.get(group);
if (log.isDebugEnabled()) {
log.debug("live2d config -> {} group save settingMap json {}", group, node.toPrettyString());
}
if (jsonNode instanceof ObjectNode) {
this.configNode.setAll((ObjectNode) node);
configNode.setAll((ObjectNode) node);
}
});
// 移除不必要的参数
this.configNode.remove("slots");
setThemeLive2dTipsPath();
configNode.remove("slots");
setThemeLive2dTipsPath(configNode);
return configNode;
}

private void setThemeLive2dTipsPath() {
private void setThemeLive2dTipsPath(ObjectNode configNode) {
this.themeFetcher.getActiveThemeName().ifPresent(activeThemeName -> {
this.configNode.put("themeTipsPath", THEME_TIPS_PATH_TEMPLATE.formatted(activeThemeName));
configNode.put("themeTipsPath", THEME_TIPS_PATH_TEMPLATE.formatted(activeThemeName));
});
}

Expand All @@ -65,9 +72,10 @@ public JsonNode getValue(String groupName, String key) {

@Override
public Optional<JsonNode> getConfig() {
if(log.isDebugEnabled()) {
log.debug("live2d config -> {}", configNode.toPrettyString());
initConfigNode();
if (log.isDebugEnabled()) {
log.debug("live2d config -> {}", this.configNode.toPrettyString());
}
return Optional.of(configNode);
return Optional.of(this.configNode);
}
}
20 changes: 6 additions & 14 deletions src/main/java/run/halo/live2d/ThemeFetcher.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package run.halo.live2d;

import com.nimbusds.jose.util.JSONObjectUtils;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.stereotype.Component;
import run.halo.app.core.extension.Theme;
import run.halo.app.extension.ConfigMap;
Expand All @@ -20,16 +22,15 @@ public class ThemeFetcher {

private final ExtensionClient extensionClient;

private final Map<String, String> configMap;

public ThemeFetcher(ExtensionClient extensionClient) {
this.extensionClient = extensionClient;
this.configMap = extensionClient.fetch(ConfigMap.class, SystemSetting.SYSTEM_CONFIG)
.map(ConfigMap::getData)
.orElse(new HashMap<>());
}

public Optional<String> getActiveThemeName() {
final Map<String, String> configMap = this.extensionClient
.fetch(ConfigMap.class, SystemSetting.SYSTEM_CONFIG)
.map(ConfigMap::getData)
.orElse(new HashMap<>());
try {
Map<String, Object> activeJson = JSONObjectUtils.parse(configMap.get("theme"));
return Optional.ofNullable((String) activeJson.get("active"));
Expand All @@ -38,13 +39,4 @@ public Optional<String> getActiveThemeName() {
}
return Optional.empty();
}

public Optional<Theme> getActiveTheme() {
Optional<String> activeThemeNameOptional = getActiveThemeName();
if (activeThemeNameOptional.isPresent()) {
String activeThemeName = activeThemeNameOptional.get();
return this.extensionClient.fetch(Theme.class, activeThemeName);
}
return Optional.empty();
}
}

0 comments on commit bddbfa2

Please sign in to comment.