Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Made hyperlinks editable #995

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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