Skip to content

Commit

Permalink
fix #962 Add configuration for RichTextEditor buttons and toolbars
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Sep 19, 2024
1 parent 1c0e08a commit 95fb887
Show file tree
Hide file tree
Showing 40 changed files with 498 additions and 321 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.domino.ui.config;

import java.util.Arrays;
import java.util.Collection;
import org.dominokit.domino.ui.richtext.RichTextActions;

public interface RichTextConfig extends ComponentConfig {

default Collection<RichTextActions> getDefaultRichTextActions() {
return Arrays.asList(RichTextActions.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public interface UIConfig
TimePickerConfig,
DelayedActionConfig,
DatatableConfig,
CarouselConfig {}
CarouselConfig,
RichTextConfig {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.domino.ui.richtext;

import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.utils.Counter;

public interface IsRichTextEditor {
DivElement getEditableElement();

Counter getDefaultFontSizeCounter();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.domino.ui.richtext;

import java.util.function.Function;
import org.dominokit.domino.ui.IsElement;
import org.dominokit.domino.ui.button.group.ButtonsGroup;
import org.dominokit.domino.ui.richtext.commands.BackColorCommand;
import org.dominokit.domino.ui.richtext.commands.BoldCommand;
import org.dominokit.domino.ui.richtext.commands.CopyCommand;
import org.dominokit.domino.ui.richtext.commands.CutCommand;
import org.dominokit.domino.ui.richtext.commands.DecreaseFontCommand;
import org.dominokit.domino.ui.richtext.commands.FontNameCommand;
import org.dominokit.domino.ui.richtext.commands.ForeColorCommand;
import org.dominokit.domino.ui.richtext.commands.HeadingCommand;
import org.dominokit.domino.ui.richtext.commands.HiliteColorCommand;
import org.dominokit.domino.ui.richtext.commands.HorizontalRuleCommand;
import org.dominokit.domino.ui.richtext.commands.IncreaseFontCommand;
import org.dominokit.domino.ui.richtext.commands.IndentCommand;
import org.dominokit.domino.ui.richtext.commands.InsertHtmlCommand;
import org.dominokit.domino.ui.richtext.commands.InsertImageCommand;
import org.dominokit.domino.ui.richtext.commands.InsertImageLinkCommand;
import org.dominokit.domino.ui.richtext.commands.InsertLinkCommand;
import org.dominokit.domino.ui.richtext.commands.InsertOrderedListCommand;
import org.dominokit.domino.ui.richtext.commands.InsertParagraphCommand;
import org.dominokit.domino.ui.richtext.commands.InsertUnorderedListCommand;
import org.dominokit.domino.ui.richtext.commands.ItalicCommand;
import org.dominokit.domino.ui.richtext.commands.JustifyCenterCommand;
import org.dominokit.domino.ui.richtext.commands.JustifyFullCommand;
import org.dominokit.domino.ui.richtext.commands.JustifyLeftCommand;
import org.dominokit.domino.ui.richtext.commands.JustifyRightCommand;
import org.dominokit.domino.ui.richtext.commands.OutdentCommand;
import org.dominokit.domino.ui.richtext.commands.PasteCommand;
import org.dominokit.domino.ui.richtext.commands.RedoCommand;
import org.dominokit.domino.ui.richtext.commands.RemoveFormatCommand;
import org.dominokit.domino.ui.richtext.commands.RemoveLinkCommand;
import org.dominokit.domino.ui.richtext.commands.StrikeThroughCommand;
import org.dominokit.domino.ui.richtext.commands.SubscriptCommand;
import org.dominokit.domino.ui.richtext.commands.SuperscriptCommand;
import org.dominokit.domino.ui.richtext.commands.UnderLineCommand;
import org.dominokit.domino.ui.richtext.commands.UndoCommand;

public enum RichTextActions {
COPY_PASTE(
editor ->
ButtonsGroup.create()
.appendChild(CopyCommand.create(editor))
.appendChild(CutCommand.create(editor))
.appendChild(PasteCommand.create(editor))),

TEXT_STYLE(
editor ->
ButtonsGroup.create()
.appendChild(BoldCommand.create(editor))
.appendChild(ItalicCommand.create(editor))
.appendChild(StrikeThroughCommand.create(editor))
.appendChild(UnderLineCommand.create(editor))),

TEXT_JUSTIFY(
editor ->
ButtonsGroup.create()
.appendChild(JustifyFullCommand.create(editor))
.appendChild(JustifyCenterCommand.create(editor))
.appendChild(JustifyLeftCommand.create(editor))
.appendChild(JustifyRightCommand.create(editor))),

TEXT_INDENT(
editor ->
ButtonsGroup.create()
.appendChild(IndentCommand.create(editor))
.appendChild(OutdentCommand.create(editor))),

TEXT_SIZE(
editor ->
ButtonsGroup.create()
.appendChild(IncreaseFontCommand.create(editor))
.appendChild(DecreaseFontCommand.create(editor))
.appendChild(SubscriptCommand.create(editor))
.appendChild(SuperscriptCommand.create(editor))),

TEXT_FONT(editor -> ButtonsGroup.create().appendChild(FontNameCommand.create(editor))),

HEADING(editor -> ButtonsGroup.create().appendChild(HeadingCommand.create(editor))),

INSERT_ELEMENTS(
editor ->
ButtonsGroup.create()
.appendChild(HorizontalRuleCommand.create(editor))
.appendChild(InsertLinkCommand.create(editor))
.appendChild(RemoveLinkCommand.create(editor))
.appendChild(InsertHtmlCommand.create(editor))
.appendChild(InsertOrderedListCommand.create(editor))
.appendChild(InsertUnorderedListCommand.create(editor))
.appendChild(InsertParagraphCommand.create(editor))
.appendChild(InsertImageCommand.create(editor))
.appendChild(InsertImageLinkCommand.create(editor))),

INSERT_IMAGE(
editor ->
ButtonsGroup.create()
.appendChild(InsertImageCommand.create(editor))
.appendChild(InsertImageLinkCommand.create(editor))),

CLEAR_FORMAT(editor -> ButtonsGroup.create().appendChild(RemoveFormatCommand.create(editor))),

BACK_COLOR(BackColorCommand::create),
FORE_COLOR(ForeColorCommand::create),
HILITE_COLOR(HiliteColorCommand::create),

UNDO_REDO(
editor ->
ButtonsGroup.create()
.appendChild(UndoCommand.create(editor))
.appendChild(RedoCommand.create(editor))),
;

private final Function<RichTextEditor, IsElement<?>> handler;

RichTextActions(Function<RichTextEditor, IsElement<?>> handler) {
this.handler = handler;
}

public IsElement<?> apply(RichTextEditor editor) {
return this.handler.apply(editor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
package org.dominokit.domino.ui.richtext;

import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.utils.Domino.*;

import elemental2.dom.DomGlobal;
import elemental2.dom.HTMLElement;
import elemental2.dom.Range;
import elemental2.dom.Selection;
import java.util.Optional;
import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.i18n.HasLabels;
import org.dominokit.domino.ui.i18n.RichTextLabels;
import org.dominokit.domino.ui.utils.BaseDominoElement;
Expand All @@ -50,15 +48,15 @@
*/
public abstract class RichTextCommand<T extends RichTextCommand<T>>
extends BaseDominoElement<HTMLElement, T> implements HasLabels<RichTextLabels> {
protected final DivElement editableElement;
protected final IsRichTextEditor isRichTextEditor;

/**
* Creates a new command for a given editable element.
*
* @param editableElement The editable content area on which the command operates.
* @param isRichTextEditor The editable content area on which the command operates.
*/
public RichTextCommand(DivElement editableElement) {
this.editableElement = editableElement;
public RichTextCommand(IsRichTextEditor isRichTextEditor) {
this.isRichTextEditor = isRichTextEditor;
}

/** Executes the specific implementation of the rich text command. */
Expand Down Expand Up @@ -88,7 +86,9 @@ protected Optional<Range> getSelectedRange() {
private boolean withinElement(Selection sel) {
if (sel.rangeCount > 0) {
for (int i = 0; i < sel.rangeCount; ++i) {
if (!editableElement.contains(sel.getRangeAt(i).commonAncestorContainer)) {
if (!isRichTextEditor
.getEditableElement()
.contains(sel.getRangeAt(i).commonAncestorContainer)) {
return false;
}
}
Expand Down
Loading

0 comments on commit 95fb887

Please sign in to comment.