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 c68634718..eeefc0ce0 100644 --- a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java @@ -58,9 +58,11 @@ public class FreemarkerConfigAwareListener { @Order(Ordered.HIGHEST_PRECEDENCE + 1) public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) { try { + configuration.setSharedVariable("theme", themeService.getActivatedTheme()); configuration.setSharedVariable("options", optionsService.listOptions()); configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); + log.info("Initialized freemarker configuration"); } catch (TemplateModelException e) { log.warn("Failed to configure freemarker", e); // Ignore this error @@ -70,8 +72,10 @@ public class FreemarkerConfigAwareListener { @Async @EventListener public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) { + log.debug("Received theme activated event"); + try { - ThemeProperty activatedTheme = themeActivatedEvent.getThemeProperty(); + ThemeProperty activatedTheme = themeService.getActivatedTheme(); log.debug("Set shared variable theme: [{}]", activatedTheme); configuration.setSharedVariable("theme", activatedTheme); Map options = optionService.listOptions(); @@ -81,6 +85,7 @@ public class FreemarkerConfigAwareListener { configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } catch (TemplateModelException e) { log.warn("Failed to configure freemarker", e); + // Ignore this error } } } diff --git a/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java b/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java index 6be0fb34f..173815552 100644 --- a/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java +++ b/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java @@ -13,21 +13,13 @@ import run.halo.app.handler.theme.config.support.ThemeProperty; */ public class ThemeActivatedEvent extends ApplicationEvent { - private final ThemeProperty themeProperty; - /** * Creates a new ApplicationEvent. * - * @param source the object on which the event initially occurred (never {@code null}) - * @param themeProperty theme property must not be null + * @param source the object on which the event initially occurred (never {@code null}) */ - public ThemeActivatedEvent(Object source, @NonNull ThemeProperty themeProperty) { + public ThemeActivatedEvent(Object source) { super(source); - Assert.notNull(themeProperty, "Activated theme property must not be null"); - this.themeProperty = themeProperty; } - public ThemeProperty getThemeProperty() { - return themeProperty; - } } diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index 14123081a..e8ae703e1 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -332,7 +332,7 @@ public class ThemeServiceImpl implements ThemeService { clearThemeCache(); // Publish a theme activated event - eventPublisher.publishEvent(new ThemeActivatedEvent(this, themeProperty)); + eventPublisher.publishEvent(new ThemeActivatedEvent(this)); return themeProperty; }