From 294b8283405e826b00bc41c86e50032b26642348 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Tue, 12 Jan 2021 14:57:03 +0200 Subject: [PATCH] Made hyperlinks editable (#995) --- .../richtext/demo/hyperlink/Hyperlink.java | 2 +- .../richtext/demo/hyperlink/HyperlinkOps.java | 23 +------------------ .../demo/hyperlink/TextHyperlinkArea.java | 21 +++++++++++++++++ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/Hyperlink.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/Hyperlink.java index 450ab351f..a0393e65d 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/Hyperlink.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/Hyperlink.java @@ -21,7 +21,7 @@ public boolean isReal() { } public boolean shareSameAncestor(Hyperlink other) { - return link.equals(other.link) && originalDisplayedText.equals(other.originalDisplayedText); + return link.equals(other.link); } public int length() { diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/HyperlinkOps.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/HyperlinkOps.java index e07f753f3..19d589d44 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/HyperlinkOps.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/HyperlinkOps.java @@ -57,31 +57,10 @@ private Optional concatHyperlinks(Hyperlink leftSeg, Hyperlink rightS return Optional.empty(); } - String original = leftSeg.getOriginalDisplayedText(); String leftText = leftSeg.getDisplayedText(); String rightText = rightSeg.getDisplayedText(); - int leftOffset = 0; - int rightOffset = 0; - for (int i = 0; i <= original.length() - leftText.length(); i++) { - if (original.regionMatches(i, leftText, 0, leftText.length())) { - leftOffset = i; - break; - } - } - for (int i = 0; i <= original.length() - rightText.length(); i++) { - if (original.regionMatches(i, rightText, 0, rightText.length())) { - rightOffset = i; - break; - } - } - if (rightOffset + rightText.length() == leftOffset) { - return Optional.of(leftSeg.mapDisplayedText(rightText + leftText)); - } else if (leftOffset + leftText.length() == rightOffset) { - return Optional.of(leftSeg.mapDisplayedText(leftText + rightText)); - } else { - return Optional.empty(); - } + return Optional.of(leftSeg.mapDisplayedText(leftText + rightText)); } } diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/TextHyperlinkArea.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/TextHyperlinkArea.java index f5881c6af..322893a60 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/TextHyperlinkArea.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/hyperlink/TextHyperlinkArea.java @@ -5,9 +5,11 @@ import org.fxmisc.richtext.TextExt; import org.fxmisc.richtext.model.ReadOnlyStyledDocument; import org.fxmisc.richtext.model.SegmentOps; +import org.fxmisc.richtext.model.StyledDocument; import org.fxmisc.richtext.model.TextOps; import org.reactfx.util.Either; +import java.util.List; import java.util.Optional; import java.util.function.Consumer; @@ -58,6 +60,25 @@ public void replaceWithLink(int start, int end, String displayedText, String lin )); } + @Override + public void replaceText(int start, int end, String text) { + + if (start > 0 && end > 0) { + int s = Math.max(0, start-1); + int e = Math.min(end+1, getLength()-1); + List> segList = getDocument().subSequence( s, e ).getParagraph(0).getSegments(); + if (!segList.isEmpty() && segList.get(0).isRight()) { + String link = segList.get(0).getRight().getLink(); + replaceWithLink( start, end, text, link ); + return; + } + } + StyledDocument, TextStyle> doc = ReadOnlyStyledDocument.fromString( + text, getParagraphStyleForInsertionAt(start), getTextStyleForInsertionAt(start), EITHER_OPS + ); + replace(start, end, doc); + } + public static TextExt createStyledTextNode(Consumer applySegment) { TextExt t = new TextExt(); t.setTextOrigin(VPos.TOP);