-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ExtendedRichText to ckEditor5
- Loading branch information
eschleb
committed
Dec 17, 2024
1 parent
8a60cda
commit 3b8b72d
Showing
28 changed files
with
684 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...n/java/com/merkle/oss/magnolia/definition/custom/richtext/ExtendedCKEditor5TextField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.merkle.oss.magnolia.definition.custom.richtext; | ||
|
||
import info.magnolia.ui.vaadin.ckeditor.CKEditor5TextField; | ||
import info.magnolia.ui.vaadin.ckeditor.CKEditor5TextFieldState; | ||
|
||
import com.vaadin.annotations.JavaScript; | ||
|
||
/** | ||
* Server side component for the ExtendedCKEditor 5 JavaScript rich-text editor. | ||
*/ | ||
@JavaScript({ | ||
//a virtual path, which is redirected to an actual build specified in the microprofile. | ||
//for developing set 'magnolia.ckeditor5.build' microprofile property. | ||
"vaadin://ckeditor5.js", | ||
"extended-ckeditor5-text-field-connector.js" | ||
}) | ||
public class ExtendedCKEditor5TextField extends CKEditor5TextField { | ||
|
||
@Override | ||
protected State getState() { | ||
return (State) super.getState(); | ||
} | ||
|
||
@Override | ||
protected State getState(final boolean markAsDirty) { | ||
return (State) super.getState(markAsDirty); | ||
} | ||
|
||
public void setConfig(final ExtendedCKEditor5TextFieldConfig config) { | ||
getState().extendedConfig = config; | ||
getState().config = config; | ||
} | ||
|
||
/** | ||
* Shared state for the {@link ExtendedCKEditor5TextField}. | ||
*/ | ||
public static class State extends CKEditor5TextFieldState { | ||
public ExtendedCKEditor5TextFieldConfig extendedConfig; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
.../com/merkle/oss/magnolia/definition/custom/richtext/ExtendedCKEditor5TextFieldConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.merkle.oss.magnolia.definition.custom.richtext; | ||
|
||
import info.magnolia.ui.vaadin.ckeditor.CKEditor5TextFieldConfig; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.merkle.oss.magnolia.definition.custom.richtext.toolbarbuilder.ToolbarGroup; | ||
import com.merkle.oss.magnolia.definition.custom.richtext.toolbarbuilder.groupbuilder.FontGroupBuilder; | ||
|
||
public class ExtendedCKEditor5TextFieldConfig extends CKEditor5TextFieldConfig { | ||
public final Heading heading; | ||
|
||
public ExtendedCKEditor5TextFieldConfig( | ||
final String licenseKey, | ||
final List<ToolbarGroup> toolbarGroups, | ||
final List<HeadingOption> options | ||
) { | ||
super(licenseKey); | ||
this.toolbar = new ExtendedToolbar(toolbarGroups, true); | ||
this.heading = new Heading(options); | ||
getToolbarGroup(toolbarGroups, FontGroupBuilder.FontToolbarGroup.class).ifPresent(fontToolbarGroup -> { | ||
this.fontFamily.options = fontToolbarGroup.getFonts(); | ||
this.fontSize.options = fontToolbarGroup.getFontSizes(); | ||
this.fontColor.colors = fontToolbarGroup.getFontColors(); | ||
}); | ||
} | ||
|
||
private <T extends ToolbarGroup> Optional<T> getToolbarGroup(final List<ToolbarGroup> toolbarGroups, final Class<T> clazz) { | ||
return toolbarGroups.stream() | ||
.filter(clazz::isInstance) | ||
.findFirst() | ||
.map(clazz::cast); | ||
} | ||
|
||
public static class ExtendedToolbar extends Toolbar { | ||
public ExtendedToolbar(final List<ToolbarGroup> toolbarGroups, final boolean shouldNotGroupWhenFull) { | ||
super.shouldNotGroupWhenFull = shouldNotGroupWhenFull; | ||
super.items = toolbarGroups.stream().flatMap(group -> | ||
Stream.concat( | ||
group.getItems().stream(), | ||
Stream.of("|") | ||
) | ||
).collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
/** | ||
* Represents a heading configuration. | ||
*/ | ||
// See implementation here: | ||
// https://github.com/ckeditor/ckeditor5/blob/v41.4.2/packages/ckeditor5-heading/src/utils.ts#L34 | ||
public static class Heading { | ||
public final List<Option> options; | ||
|
||
public Heading(final List<HeadingOption> options) { | ||
this.options = options.stream().map(Option::new).collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Represents a heading option. | ||
*/ | ||
// See implementation here: | ||
// https://github.com/ckeditor/ckeditor5/blob/v41.4.2/packages/ckeditor5-heading/src/headingconfig.ts#L76 | ||
public static class Option { | ||
public final String model; | ||
public final String view; | ||
public final String title; | ||
public final String clazz; | ||
|
||
public Option(final HeadingOption option) { | ||
this.model = option.getModel(); | ||
this.view = option.getView().orElse(null); | ||
this.title = option.getTitle(); | ||
this.clazz = option.getClazz(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.