Skip to content

Commit

Permalink
Fix copy/paste issues (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperedadnr authored Sep 20, 2024
1 parent e5a2532 commit ce689a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
12 changes: 12 additions & 0 deletions rta/src/main/java/com/gluonhq/richtextarea/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;

import java.util.regex.Pattern;

public class Tools {

public static final Pattern URL_PATTERN = Pattern.compile(
"\\b((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

private Tools() {}

public static IndexRange NO_SELECTION = new IndexRange(-1,-1);
Expand Down Expand Up @@ -110,4 +116,10 @@ public static String formatTextWithAnchors(String text) {
.replaceAll(TextBuffer.EMOJI_ANCHOR_TEXT, "<e>")
.replaceAll("" + TextBuffer.ZERO_WIDTH_TABLE_SEPARATOR, "<t>");
}

public static boolean isURL(String text) {
if (text == null) return false;
return URL_PATTERN.matcher(text).matches();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.gluonhq.richtextarea.Selection;
import com.gluonhq.richtextarea.Tools;
import com.gluonhq.richtextarea.model.Decoration;
import com.gluonhq.richtextarea.model.DecorationModel;
import com.gluonhq.richtextarea.model.Document;
import com.gluonhq.richtextarea.model.ImageDecoration;
import com.gluonhq.richtextarea.model.Paragraph;
Expand Down Expand Up @@ -454,8 +453,13 @@ void clipboardCopy(final boolean cutText) {
if (selection.isDefined()) {
Document currentDocument = getCurrentDocument(selection);
final ClipboardContent content = new ClipboardContent();
content.put(RTA_DATA_FORMAT, currentDocument);
content.putString(currentDocument.getText().replaceAll(TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT, ""));
String text = currentDocument.getText();
if (Tools.isURL(text)) {
content.putUrl(text);
} else {
content.put(RTA_DATA_FORMAT, currentDocument);
}
content.putString(text.replaceAll(TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT, ""));
if (cutText) {
commandManager.execute(new RemoveTextCmd(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gluonhq.emoji.EmojiData;
import com.gluonhq.richtextarea.RichTextArea;
import com.gluonhq.richtextarea.Selection;
import com.gluonhq.richtextarea.Tools;
import com.gluonhq.richtextarea.action.Action;
import com.gluonhq.richtextarea.action.DecorateAction;
import com.gluonhq.richtextarea.action.ParagraphDecorateAction;
Expand Down Expand Up @@ -573,6 +574,21 @@ private static void findEmoji(String text, BiConsumer<Emoji, Integer> onCodeName
}

private static void findMarkdown(String text, BiConsumer<Integer, String> onFound) {
int start = 0;
Matcher urlMatcher = Tools.URL_PATTERN.matcher(text);
while (urlMatcher.find()) {
int begin = urlMatcher.start();
if (start != begin) {
findMarkdownWithoutURL(text.substring(start, begin), onFound);
}
start = urlMatcher.end();
}
if (start == 0) {
findMarkdownWithoutURL(text, onFound);
}
}

private static void findMarkdownWithoutURL(String text, BiConsumer<Integer, String> onFound) {
Matcher matcher = markdownDetector.matcher(text);
while (matcher.find()) {
for (int i = 1; i < matcher.groupCount(); i++) {
Expand All @@ -585,7 +601,7 @@ private static void findMarkdown(String text, BiConsumer<Integer, String> onFoun
}
}

private TextDecoration getStyleFromMarker(String marker) {
private TextDecoration getStyleFromMarker(String marker) {
TextDecoration.Builder builder = TextDecoration.builder();
switch (marker) {
case MARKER_BOLD:
Expand Down

0 comments on commit ce689a2

Please sign in to comment.