Skip to content

Commit

Permalink
Made hyperlinks editable (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen authored Jan 12, 2021
1 parent fa17d28 commit 294b828
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,10 @@ private Optional<Hyperlink> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Either<String, Hyperlink>> 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<Void, Either<String, Hyperlink>, TextStyle> doc = ReadOnlyStyledDocument.fromString(
text, getParagraphStyleForInsertionAt(start), getTextStyleForInsertionAt(start), EITHER_OPS
);
replace(start, end, doc);
}

public static TextExt createStyledTextNode(Consumer<TextExt> applySegment) {
TextExt t = new TextExt();
t.setTextOrigin(VPos.TOP);
Expand Down

0 comments on commit 294b828

Please sign in to comment.