From 81ba6e3c5fb47da5f68c0967d48e52aa98da4caf Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 23 Apr 2019 14:46:22 +0800 Subject: [PATCH] Update anatole theme. --- .../config/FreeMarkerAutoConfiguration.java | 13 ++++++- .../theme/FreemarkerConfigAwareListener.java | 9 ++++- .../service/impl/ThemeSettingServiceImpl.java | 15 +++++++- .../templates/themes/anatole/module/macro.ftl | 8 ++-- .../themes/anatole/module/page-top.ftl | 2 +- .../themes/anatole/module/sidebar.ftl | 6 +-- .../themes/anatole/module/social-list.ftl | 38 +++++++++---------- 7 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java b/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java index ddc6dde84..4c1a7e3b6 100644 --- a/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java @@ -9,6 +9,8 @@ import run.halo.app.model.freemarker.method.RecentCommentsMethod; import run.halo.app.model.freemarker.method.RecentPostsMethod; import run.halo.app.model.freemarker.tag.*; import run.halo.app.service.OptionService; +import run.halo.app.service.ThemeService; +import run.halo.app.service.ThemeSettingService; import run.halo.app.service.UserService; import javax.annotation.PostConstruct; @@ -32,6 +34,12 @@ public class FreeMarkerAutoConfiguration { @Autowired private UserService userService; + @Autowired + private ThemeService themeService; + + @Autowired + private ThemeSettingService themeSettingService; + @Autowired private PostTagDirective postTagDirective; @@ -66,7 +74,8 @@ public class FreeMarkerAutoConfiguration { public void setSharedVariable() { try { configuration.setSharedVariable("options", optionsService.listOptions()); - configuration.setSharedVariable("user",userService.getCurrentUser().orElse(null)); + configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); + configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); //Freemarker custom tags configuration.setSharedVariable("categoryTag", categoryTagDirective); configuration.setSharedVariable("commentTag", commentTagDirective); @@ -74,7 +83,7 @@ public class FreeMarkerAutoConfiguration { configuration.setSharedVariable("menuTag", menuTagDirective); configuration.setSharedVariable("tagTag", tagTagDirective); configuration.setSharedVariable("postTag", postTagDirective); - configuration.setSharedVariable("galleryTag",galleryTagDirective); + configuration.setSharedVariable("galleryTag", galleryTagDirective); configuration.setSharedVariable("randomMethod", randomMethod); configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); diff --git a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java index ad8e6f4b4..855085b07 100644 --- a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; +import run.halo.app.service.ThemeSettingService; import java.util.Map; @@ -29,12 +30,16 @@ public class FreemarkerConfigAwareListener { private final ThemeService themeService; + private final ThemeSettingService themeSettingService; + public FreemarkerConfigAwareListener(OptionService optionService, Configuration configuration, - ThemeService themeService) { + ThemeService themeService, + ThemeSettingService themeSettingService) { this.optionService = optionService; this.configuration = configuration; this.themeService = themeService; + this.themeSettingService = themeSettingService; } @Async @@ -59,6 +64,8 @@ public class FreemarkerConfigAwareListener { Map options = optionService.listOptions(); log.debug("Set shared variable options: [{}]", options); configuration.setSharedVariable("options", options); + log.debug("Set shared variable theme settings: [{}]", options); + configuration.setSharedVariable("setting",themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } catch (TemplateModelException e) { log.warn("Failed to configure freemarker", e); } diff --git a/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java index 161a72d9c..7aa6cd8e2 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java @@ -1,11 +1,14 @@ package run.halo.app.service.impl; +import freemarker.template.Configuration; +import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import run.halo.app.exception.ServiceException; import run.halo.app.handler.theme.config.support.Group; import run.halo.app.handler.theme.config.support.Item; import run.halo.app.model.entity.ThemeSetting; @@ -31,11 +34,15 @@ public class ThemeSettingServiceImpl extends AbstractCrudService save(key, value.toString(), themeId)); + + try { + configuration.setSharedVariable("settings", listAsMapBy(themeService.getActivatedThemeId())); + } catch (TemplateModelException e) { + throw new ServiceException("Save theme settings error", e); + } } @Override diff --git a/src/main/resources/templates/themes/anatole/module/macro.ftl b/src/main/resources/templates/themes/anatole/module/macro.ftl index a2b8af5b9..fc36e63ac 100644 --- a/src/main/resources/templates/themes/anatole/module/macro.ftl +++ b/src/main/resources/templates/themes/anatole/module/macro.ftl @@ -21,12 +21,12 @@ diff --git a/src/main/resources/templates/themes/anatole/module/page-top.ftl b/src/main/resources/templates/themes/anatole/module/page-top.ftl index 85387d054..ac4858870 100644 --- a/src/main/resources/templates/themes/anatole/module/page-top.ftl +++ b/src/main/resources/templates/themes/anatole/module/page-top.ftl @@ -15,7 +15,7 @@
- +
diff --git a/src/main/resources/templates/themes/anatole/module/sidebar.ftl b/src/main/resources/templates/themes/anatole/module/sidebar.ftl index 5351a8841..d04b1cb55 100644 --- a/src/main/resources/templates/themes/anatole/module/sidebar.ftl +++ b/src/main/resources/templates/themes/anatole/module/sidebar.ftl @@ -1,12 +1,12 @@