Skip to content

Commit

Permalink
fix: Fix bug
Browse files Browse the repository at this point in the history
Signed-off-by: unknowIfGuestInDream <[email protected]>
  • Loading branch information
unknowIfGuestInDream committed Jun 9, 2024
1 parent 61fabd8 commit 3c933b6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.List;
Expand Down Expand Up @@ -109,7 +108,7 @@ public void start(Stage stage) {
TextDecoration textDecoration = TextDecoration.builder().presets()
.fontFamily("Arial")
.fontSize(20)
.foreground(Color.RED)
.foreground("red")
.build();
ParagraphDecoration paragraphDecoration = ParagraphDecoration.builder().presets().build();
DecorationModel decorationModel = new DecorationModel(0, text.length(), textDecoration, paragraphDecoration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
package com.tlcsdm.demo.internal.richtextarea;

import com.gluonhq.emoji.Emoji;
import com.gluonhq.emoji.EmojiData;
import com.gluonhq.richtextarea.RichTextArea;
import com.gluonhq.richtextarea.Selection;
import com.gluonhq.richtextarea.action.Action;
import com.gluonhq.richtextarea.action.DecorateAction;
import com.gluonhq.richtextarea.action.ParagraphDecorateAction;
Expand Down Expand Up @@ -116,11 +118,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -152,10 +158,18 @@ public class FullFeaturedDemo extends Application {
}
}

private static final Pattern codenamePattern = Pattern.compile(":([a-zA-Z0-9_-]*):");
private static final Pattern markdownDetector = Pattern.compile(
"(\\`)([^s]?.*?[^s]?|[^s]*)(\\`)|(\\_)([^s]?.*?[^s]?|[^s]*)(\\_)|(\\*)([^s]?.*?[^s]?|[^s]*)(\\*)",
Pattern.DOTALL);

private static final String MARKER_BOLD = "*", MARKER_ITALIC = "_", MARKER_MONO = "`";

private final List<DecorationModel> decorations;

{
TextDecoration bold14 = TextDecoration.builder().presets().fontWeight(BOLD).fontSize(14).build();
TextDecoration bold14 = TextDecoration.builder().presets().fontWeight(BOLD).fontSize(14).foreground("darkblue")
.build();
TextDecoration preset = TextDecoration.builder().presets().build();
ParagraphDecoration center63 = ParagraphDecoration.builder().presets().alignment(TextAlignment.CENTER).topInset(
6).bottomInset(3).build();
Expand Down Expand Up @@ -199,6 +213,19 @@ public void start(Stage stage) {
editor.textLengthProperty().addListener((o, ov, nv) ->
textLengthLabel.setText("Text length: " + nv)
);
editor.documentProperty().subscribe(d -> {
String nv = d.getText();
if (nv != null) {
String substring = nv.substring(0, Math.min(editor.getCaretPosition(), nv.length())).toLowerCase(
Locale.ROOT);
findEmoji(substring,
(emoji, start) -> editor.getActionFactory().selectAndInsertEmoji(
new Selection(start, editor.getCaretPosition()), emoji, true).execute(new ActionEvent()));
findMarkdown(substring, (start, marker) -> editor.getActionFactory().removeExtremesAndDecorate(
new Selection(start, editor.getCaretPosition()), getStyleFromMarker(marker))
.execute(new ActionEvent()));
}
});

ComboBox<Presets> presets = new ComboBox<>();
presets.getItems().setAll(Presets.values());
Expand Down Expand Up @@ -229,7 +256,7 @@ public Presets fromString(String s) {

ComboBox<String> fontFamilies = new ComboBox<>();
fontFamilies.getItems().setAll(Font.getFamilies());
fontFamilies.setValue("Arial");
fontFamilies.setValue("System");
fontFamilies.setPrefWidth(100);
new TextDecorateAction<>(editor, fontFamilies.valueProperty(), TextDecoration::getFontFamily,
(builder, a) -> builder.fontFamily(a).build());
Expand Down Expand Up @@ -257,14 +284,16 @@ public Double fromString(String s) {

final ColorPicker textForeground = new ColorPicker();
textForeground.getStyleClass().add("foreground");
new TextDecorateAction<>(editor, textForeground.valueProperty(), TextDecoration::getForeground,
(builder, a) -> builder.foreground(a).build());
new TextDecorateAction<>(editor, textForeground.valueProperty(),
(TextDecoration textDecoration1) -> Color.web(textDecoration1.getForeground()),
(builder, a) -> builder.foreground(toHexString(a)).build());
textForeground.setValue(Color.BLACK);

final ColorPicker textBackground = new ColorPicker();
textBackground.getStyleClass().add("background");
new TextDecorateAction<>(editor, textBackground.valueProperty(), TextDecoration::getBackground,
(builder, a) -> builder.background(a).build());
new TextDecorateAction<>(editor, textBackground.valueProperty(),
(TextDecoration textDecoration) -> Color.web(textDecoration.getBackground()),
(builder, a) -> builder.background(toHexString(a)).build());
textBackground.setValue(Color.TRANSPARENT);

CheckBox editableProp = new CheckBox("Editable");
Expand Down Expand Up @@ -365,6 +394,8 @@ public Double fromString(String s) {

Menu fileMenu = new Menu("File");
CheckMenuItem autoSaveMenuItem = new CheckMenuItem("Auto Save");
// needed for the document listener
autoSaveMenuItem.setSelected(true);
editor.autoSaveProperty().bind(autoSaveMenuItem.selectedProperty());
fileMenu.getItems().addAll(
actionMenuItem("New Text", LineAwesomeSolid.FILE, editor.getActionFactory().newDocument()),
Expand Down Expand Up @@ -601,6 +632,57 @@ private MenuItem actionMenuItem(String text, Ikon ikon, Action action) {
return menuItem;
}

private static void findEmoji(String text, BiConsumer<Emoji, Integer> onCodeNameFound) {
if (text.endsWith(" ")) {
return;
}
int wordBeginIndex = Math.max(text.lastIndexOf(" ") + 1, text.lastIndexOf("\n") + 1);
String word = text.substring(wordBeginIndex);
if (word.startsWith(":") && word.length() > 2) {
// check if word contains an emoji codename :name:
if (word.substring(1).contains(":")) {
Matcher matcher = codenamePattern.matcher(word);
if (matcher.find()) {
EmojiData.emojiFromShortName(matcher.group(1))
.ifPresent(emoji -> onCodeNameFound.accept(emoji, wordBeginIndex));
}
}
}
}

private static void findMarkdown(String text, BiConsumer<Integer, String> onFound) {
Matcher matcher = markdownDetector.matcher(text);
while (matcher.find()) {
for (int i = 1; i < matcher.groupCount(); i++) {
String marker = matcher.group(i);
if (marker != null) {
onFound.accept(matcher.start(), marker);
break;
}
}
}
}

private TextDecoration getStyleFromMarker(String marker) {
TextDecoration.Builder builder = TextDecoration.builder();
switch (marker) {
case MARKER_BOLD:
return builder.fontWeight(BOLD).build();
case MARKER_ITALIC:
return builder.fontPosture(ITALIC).build();
case MARKER_MONO:
return builder.fontFamily("monospaced").build();
}
return builder.build();
}

private String toHexString(Color value) {
return String.format("#%02X%02X%02X%02X", (int) Math.round(value.getRed() * 255),
(int) Math.round(value.getGreen() * 255),
(int) Math.round(value.getBlue() * 255),
(int) Math.round(value.getOpacity() * 255));
}

public static void main(String[] args) {
launch(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.ArrayList;
Expand Down Expand Up @@ -98,7 +97,7 @@ public class HighlightDemo extends Application {
TextDecoration.builder().presets().fontFamily("Arial").fontWeight(BOLD).fontSize(16).build();
private static final TextDecoration mono =
TextDecoration.builder().presets().fontFamily("Monospaced").fontWeight(BOLD)
.fontPosture(ITALIC).background(Color.CORNFLOWERBLUE).build();
.fontPosture(ITALIC).background("#6495ED").build();
private static final ParagraphDecoration parPreset =
ParagraphDecoration.builder().presets().build();

Expand Down

0 comments on commit 3c933b6

Please sign in to comment.