Skip to content

Commit

Permalink
Add enhancedRichText
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Feb 20, 2024
1 parent 90b4c72 commit 6bf6de3
Show file tree
Hide file tree
Showing 15 changed files with 750 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.merkle.oss.magnolia.definition.custom.richtext;

import com.merkle.oss.magnolia.definition.custom.richtext.toolbarbuilder.RichTextToolbarConfig;
import info.magnolia.ui.field.RichTextFieldDefinition;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

public class ExtendedRichTextDefinition extends RichTextFieldDefinition {
@Nullable
private String formatTags;
@Nullable
private String customStyleSet;
@Nullable
private String customTemplates;
@Nullable
private String template;
@Nullable
private String extraAllowedContent;
@Nullable
private String contentCss;
@Nullable
private String enterMode;
private boolean forcePasteAsPlainText;
private boolean pasteFromWordRemoveFontStyles;
private boolean pasteFromWordPromptCleanup;
@Nullable
private String bodyClass;
@Nullable
private RichTextToolbarConfig toolbarConfig;
private Map<String, String> extraConfig = Collections.emptyMap();
private Map<String, String> externalPlugins = Collections.emptyMap();

public ExtendedRichTextDefinition() {
setFactoryClass(ExtendedRichTextFactory.class);
}

public Optional<RichTextToolbarConfig> getToolbarConfig() {
return Optional.ofNullable(toolbarConfig);
}

public void setToolbarConfig(RichTextToolbarConfig toolbarConfig) {
this.toolbarConfig = toolbarConfig;
}

public Optional<String> getFormatTags() {
return Optional.ofNullable(formatTags);
}

public void setFormatTags(final String formatTags) {
this.formatTags = formatTags;
}

public Optional<String> getCustomStyleSet() {
return Optional.ofNullable(customStyleSet);
}

public void setCustomStyleSet(final String customStyleSet) {
this.customStyleSet = customStyleSet;
}

public Optional<String> getCustomTemplates() {
return Optional.ofNullable(customTemplates);
}

public void setCustomTemplates(final String customTemplates) {
this.customTemplates = customTemplates;
}

public Optional<String> getTemplate() {
return Optional.ofNullable(template);
}

public void setTemplate(final String template) {
this.template = template;
}

public Optional<String> getExtraAllowedContent() {
return Optional.ofNullable(extraAllowedContent);
}

public void setExtraAllowedContent(final String extraAllowedContent) {
this.extraAllowedContent = extraAllowedContent;
}

public Optional<String> getContentCss() {
return Optional.ofNullable(contentCss);
}

public void setContentCss(final String contentCss) {
this.contentCss = contentCss;
}

public Optional<String> getEnterMode() {
return Optional.ofNullable(enterMode);
}

public void setEnterMode(final String enterMode) {
this.enterMode = enterMode;
}

public boolean isForcePasteAsPlainText() {
return forcePasteAsPlainText;
}

public void setForcePasteAsPlainText(final boolean forcePasteAsPlainText) {
this.forcePasteAsPlainText = forcePasteAsPlainText;
}

public boolean isPasteFromWordRemoveFontStyles() {
return pasteFromWordRemoveFontStyles;
}

public void setPasteFromWordRemoveFontStyles(boolean pasteFromWordRemoveFontStyles) {
this.pasteFromWordRemoveFontStyles = pasteFromWordRemoveFontStyles;
}

public boolean isPasteFromWordPromptCleanup() {
return pasteFromWordPromptCleanup;
}

public void setPasteFromWordPromptCleanup(boolean pasteFromWordPromptCleanup) {
this.pasteFromWordPromptCleanup = pasteFromWordPromptCleanup;
}

public Optional<String> getBodyClass() {
return Optional.ofNullable(bodyClass);
}

public void setBodyClass(final String bodyClass) {
this.bodyClass = bodyClass;
}

public void setExtraConfig(final Map<String, String> extraConfig) {
this.extraConfig = extraConfig;
}

public Map<String, String> getExtraConfig() {
return extraConfig;
}

public void setExternalPlugins(final Map<String, String> externalPlugins) {
this.externalPlugins = externalPlugins;
}

public Map<String, String> getExternalPlugins() {
return externalPlugins;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.merkle.oss.magnolia.definition.custom.richtext;

import com.merkle.oss.magnolia.definition.builder.simple.AbstractRichTextFieldDefinitionBuilder;
import com.merkle.oss.magnolia.definition.custom.richtext.toolbarbuilder.RichTextToolbarConfig;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ExtendedRichTextDefinitionBuilder extends AbstractRichTextFieldDefinitionBuilder<ExtendedRichTextDefinition, ExtendedRichTextDefinitionBuilder> {
@Nullable
private String formatTags;
@Nullable
private String customStyleSet;
@Nullable
private String customTemplates;
@Nullable
private String template;
@Nullable
private String enterMode;
@Nullable
private String contentCss;
@Nullable
private String extraAllowedContent;
@Nullable
private Boolean forcePasteAsPlainText;
@Nullable
private Boolean pasteFromWordRemoveFontStyles;
@Nullable
private Boolean pasteFromWordPromptCleanup;
@Nullable
private String bodyClass;
@Nullable
private RichTextToolbarConfig toolbarConfig;
private final Map<String, String> extraConfig = new HashMap<>();
private final Map<String, String> externalPlugins = new HashMap<>();

public ExtendedRichTextDefinitionBuilder() {
super(ExtendedRichTextDefinition::new);
}

public ExtendedRichTextDefinitionBuilder formatTags(final String formatTags) {
this.formatTags = formatTags;
return self();
}

public ExtendedRichTextDefinitionBuilder customStyleSet(final String customStyleSet) {
this.customStyleSet = customStyleSet;
return self();
}

public ExtendedRichTextDefinitionBuilder customTemplates(final String customTemplates) {
this.customTemplates = customTemplates;
return self();
}

public ExtendedRichTextDefinitionBuilder template(final String template) {
this.template = template;
return self();
}

public ExtendedRichTextDefinitionBuilder enterMode(final String enterMode) {
this.enterMode = enterMode;
return self();
}

public ExtendedRichTextDefinitionBuilder extraAllowedContent(final String extraAllowedContent) {
this.extraAllowedContent = extraAllowedContent;
return self();
}

public ExtendedRichTextDefinitionBuilder contentCss(final String contentCss) {
this.contentCss = contentCss;
return self();
}

public ExtendedRichTextDefinitionBuilder forcePasteAsPlainText(final boolean forcePasteAsPlainText) {
this.forcePasteAsPlainText = forcePasteAsPlainText;
return self();
}

public ExtendedRichTextDefinitionBuilder pasteFromWordRemoveFontStyles(final boolean pasteFromWordRemoveFontStyles) {
this.pasteFromWordRemoveFontStyles = pasteFromWordRemoveFontStyles;
return self();
}

public ExtendedRichTextDefinitionBuilder pasteFromWordPromptCleanup(final boolean pasteFromWordPromptCleanup) {
this.pasteFromWordPromptCleanup = pasteFromWordPromptCleanup;
return self();
}

public ExtendedRichTextDefinitionBuilder bodyClass(final String bodyClass) {
this.bodyClass = bodyClass;
return self();
}

public ExtendedRichTextDefinitionBuilder toolbarConfig(final RichTextToolbarConfig toolbarConfig) {
this.toolbarConfig = toolbarConfig;
return self();
}

public ExtendedRichTextDefinitionBuilder externalPlugin(final String name, final String source) {
this.externalPlugins.put(name, source);
return self();
}

public ExtendedRichTextDefinitionBuilder extraConfig(final String name, final String value) {
this.extraConfig.put(name, value);
return self();
}

public ExtendedRichTextDefinition build(String name) {
final ExtendedRichTextDefinition definition = super.build(name);
Optional.ofNullable(formatTags).ifPresent(definition::setFormatTags);
Optional.ofNullable(customStyleSet).ifPresent(definition::setCustomStyleSet);
Optional.ofNullable(customTemplates).ifPresent(definition::setCustomTemplates);
Optional.ofNullable(template).ifPresent(definition::setTemplate);
Optional.ofNullable(enterMode).ifPresent(definition::setEnterMode);
Optional.ofNullable(contentCss).ifPresent(definition::setContentCss);
Optional.ofNullable(extraAllowedContent).ifPresent(definition::setExtraAllowedContent);
Optional.ofNullable(forcePasteAsPlainText).ifPresent(definition::setForcePasteAsPlainText);
Optional.ofNullable(pasteFromWordPromptCleanup).ifPresent(definition::setPasteFromWordPromptCleanup);
Optional.ofNullable(pasteFromWordRemoveFontStyles).ifPresent(definition::setPasteFromWordRemoveFontStyles);
Optional.ofNullable(bodyClass).ifPresent(definition::setBodyClass);
Optional.ofNullable(toolbarConfig).ifPresent(definition::setToolbarConfig);
definition.setExternalPlugins(externalPlugins);
definition.setExtraConfig(extraConfig);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.merkle.oss.magnolia.definition.custom.richtext;

import com.merkle.oss.magnolia.definition.custom.richtext.toolbarbuilder.RichTextToolbarConfig;
import com.vaadin.server.VaadinService;
import info.magnolia.i18nsystem.I18nizer;
import info.magnolia.i18nsystem.SimpleTranslator;
import info.magnolia.ui.dialog.DialogDefinitionRegistry;
import info.magnolia.ui.field.factory.RichTextFieldFactory;
import info.magnolia.ui.framework.ioc.UiComponentProvider;
import info.magnolia.ui.vaadin.ckeditor.MagnoliaCKEditorConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class ExtendedRichTextFactory extends RichTextFieldFactory {
private static final Logger LOG = LoggerFactory.getLogger(ExtendedRichTextFactory.class);

@Inject
public ExtendedRichTextFactory(
final ExtendedRichTextDefinition definition,
final UiComponentProvider componentProvider,
final SimpleTranslator i18n,
final DialogDefinitionRegistry dialogDefinitionRegistry,
final I18nizer i18nizer
) {
super(definition, componentProvider, i18n, dialogDefinitionRegistry, i18nizer);
}

@Override
protected List<MagnoliaCKEditorConfig.ToolbarGroup> initializeToolbarConfig() {
return Optional.ofNullable(getDefinition())
.flatMap(ExtendedRichTextDefinition::getToolbarConfig)
.map(RichTextToolbarConfig::getConfig)
.orElseGet(() -> {
LOG.info("No toolbar configuration set.");
return Collections.emptyList();
});
}

@Override
protected MagnoliaCKEditorConfig initializeCKEditorConfig() {
final String path = VaadinService.getCurrentRequest().getContextPath();
@Nullable
final String configJsFile = getDefinition().getConfigJsFile();
getDefinition().setConfigJsFile(null); //super.initializeCKEditorConfig is not applying other configs if configJs file is present
final MagnoliaCKEditorConfig config = super.initializeCKEditorConfig();
if(configJsFile != null) {
config.addExtraConfig("customConfig", "'" + path + configJsFile + "'");
}

// by default is allowedContent = true
config.setAllowedContentAll();

getDefinition().getExtraConfig().forEach(config::addExtraConfig);
getDefinition().getExternalPlugins().forEach(config::addExternalPlugin);
getDefinition().getExternalPlugins().keySet().forEach(config::addToExtraPlugins);
config.setForcePasteAsPlainText(getDefinition().isForcePasteAsPlainText());
config.setPasteFromWordRemoveFontStyles(getDefinition().isPasteFromWordRemoveFontStyles());
config.setPasteFromWordPromptCleanup(getDefinition().isPasteFromWordPromptCleanup());
// set enter mode (default is <p>):
getDefinition().getEnterMode().ifPresent(config::setEnterMode);
getDefinition().getExtraAllowedContent().ifPresent(config::setExtraAllowedContent);
getDefinition().getContentCss().ifPresent(config::setContentsCss);
getDefinition().getBodyClass().ifPresent(config::setBodyClass);
getDefinition().getFormatTags().ifPresent(formatTags ->
config.addExtraConfig("format_tags", "'" + formatTags + "'")
);
getDefinition().getCustomStyleSet().ifPresent(customStyleSet ->
config.setStylesSet(customStyleSet + "?uncache= " + System.currentTimeMillis())
);
getDefinition().getCustomTemplates().ifPresent(customTemplates -> {
config.addTemplatesFiles(customTemplates + "?uncache= " + System.currentTimeMillis());
config.setTemplatesReplaceContent(false);
getDefinition().getTemplate().ifPresent(templates ->
config.addExtraConfig("templates", "'" + templates + "'")
);
});
return config;
}

@Override
protected ExtendedRichTextDefinition getDefinition() {
return (ExtendedRichTextDefinition)super.getDefinition();
}
}
Loading

0 comments on commit 6bf6de3

Please sign in to comment.