From 1684320d2ad50c4aedb2af7ba38b5d4494c10538 Mon Sep 17 00:00:00 2001 From: chs <“caihaosen@mail.bnu.edu.cn> Date: Tue, 2 Jul 2024 09:26:11 +0800 Subject: [PATCH 1/2] extension design and registration --- .../run/halo/injection/HtmlInjection.java | 47 +++++++++++++++++++ .../run/halo/injection/InjectionPlugin.java | 23 +++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/main/java/run/halo/injection/HtmlInjection.java diff --git a/src/main/java/run/halo/injection/HtmlInjection.java b/src/main/java/run/halo/injection/HtmlInjection.java new file mode 100644 index 00000000..162a36ff --- /dev/null +++ b/src/main/java/run/halo/injection/HtmlInjection.java @@ -0,0 +1,47 @@ +package run.halo.injection; + +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import run.halo.app.extension.AbstractExtension; +import run.halo.app.extension.GVK; + +@Data +@EqualsAndHashCode(callSuper = true) +@GVK(group = "theme.halo.run", + version = "v1alpha1", + kind = "HtmlInjection", + plural = "htmlInjections", + singular = "htmlInjection") +public class HtmlInjection extends AbstractExtension { + + @Schema(requiredMode = REQUIRED) + private Spec spec; + + @Data + public static class Spec { + @Schema(description = "The name of the code snippet", maxLength = 100) + private String name; + + @Schema(description = "The description of the code snippet", maxLength = 500) + private String description; + + @Schema(description = "The content of the HTML", maxLength = 5000) + private String fragment; + + @Schema(description = "where to inject", allowableValues = {"HEADER", "FOOTER"}) + private InjectionPoint injectionPoint; + + @Schema(description = "The pages where the code snippet should be injected") + private String[] targetPages; + + @Schema(description = "Whether the code snippet is enabled") + private boolean isEnabled; + } + + public enum InjectionPoint { + HEADER, FOOTER + } +} \ No newline at end of file diff --git a/src/main/java/run/halo/injection/InjectionPlugin.java b/src/main/java/run/halo/injection/InjectionPlugin.java index fe70a839..580e736f 100644 --- a/src/main/java/run/halo/injection/InjectionPlugin.java +++ b/src/main/java/run/halo/injection/InjectionPlugin.java @@ -1,11 +1,28 @@ package run.halo.injection; -import org.pf4j.PluginWrapper; + import org.springframework.stereotype.Component; +import run.halo.app.extension.SchemeManager; import run.halo.app.plugin.BasePlugin; +import run.halo.app.plugin.PluginContext; + + @Component public class InjectionPlugin extends BasePlugin { - public InjectionPlugin(PluginWrapper wrapper) { - super(wrapper); + private final SchemeManager schemeManager; + + public InjectionPlugin(PluginContext pluginContext, SchemeManager schemeManager) { + super(pluginContext); + this.schemeManager = schemeManager; + } + + @Override + public void start() { + schemeManager.register(HtmlInjection.class); + } + + @Override + public void stop() { + schemeManager.unregister(schemeManager.get(HtmlInjection.class)); } } From 7d85ddbdab06ce099f6a35b2428f75742b0b854b Mon Sep 17 00:00:00 2001 From: CaiHaosen <“caihaosen@mail.bnu.edu.cn> Date: Thu, 4 Jul 2024 09:05:44 +0800 Subject: [PATCH 2/2] chore: rename variables and update schemeManager.unregister method --- src/main/java/run/halo/injection/HtmlInjection.java | 13 +++++++------ .../java/run/halo/injection/InjectionPlugin.java | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/run/halo/injection/HtmlInjection.java b/src/main/java/run/halo/injection/HtmlInjection.java index 162a36ff..08e00ed1 100644 --- a/src/main/java/run/halo/injection/HtmlInjection.java +++ b/src/main/java/run/halo/injection/HtmlInjection.java @@ -3,6 +3,7 @@ import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; import io.swagger.v3.oas.annotations.media.Schema; +import java.util.Set; import lombok.Data; import lombok.EqualsAndHashCode; import run.halo.app.extension.AbstractExtension; @@ -13,8 +14,8 @@ @GVK(group = "theme.halo.run", version = "v1alpha1", kind = "HtmlInjection", - plural = "htmlInjections", - singular = "htmlInjection") + plural = "htmlinjections", + singular = "htmlinjection") public class HtmlInjection extends AbstractExtension { @Schema(requiredMode = REQUIRED) @@ -28,17 +29,17 @@ public static class Spec { @Schema(description = "The description of the code snippet", maxLength = 500) private String description; - @Schema(description = "The content of the HTML", maxLength = 5000) + @Schema(description = "The content of the HTML") private String fragment; @Schema(description = "where to inject", allowableValues = {"HEADER", "FOOTER"}) private InjectionPoint injectionPoint; @Schema(description = "The pages where the code snippet should be injected") - private String[] targetPages; + private Set pageRules; - @Schema(description = "Whether the code snippet is enabled") - private boolean isEnabled; + @Schema(description = "Whether the code snippet is enabled", defaultValue = "false") + private boolean enabled; } public enum InjectionPoint { diff --git a/src/main/java/run/halo/injection/InjectionPlugin.java b/src/main/java/run/halo/injection/InjectionPlugin.java index 580e736f..029b35e6 100644 --- a/src/main/java/run/halo/injection/InjectionPlugin.java +++ b/src/main/java/run/halo/injection/InjectionPlugin.java @@ -1,6 +1,7 @@ package run.halo.injection; import org.springframework.stereotype.Component; +import run.halo.app.extension.Scheme; import run.halo.app.extension.SchemeManager; import run.halo.app.plugin.BasePlugin; import run.halo.app.plugin.PluginContext; @@ -22,7 +23,7 @@ public void start() { @Override public void stop() { - schemeManager.unregister(schemeManager.get(HtmlInjection.class)); + schemeManager.unregister(Scheme.buildFromType(HtmlInjection.class)); } }