Skip to content

Commit 699d3eb

Browse files
author
Yubo-Cao
committed
Fix according to Trag
1 parent fda54f7 commit 699d3eb

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

jabgui/src/main/java/org/jabref/gui/util/MarkdownTextFlow.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,12 @@ private void addHyperlinkNode(String text, String url, Node astNode, String... s
115115

116116
@Override
117117
public void copySelectedText() {
118-
if (startHit == null || endHit == null) {
118+
if (!isSelectionActive()) {
119119
return;
120120
}
121121

122-
int hitStart = startHit.getCharIndex();
123-
int hitEnd = endHit.getCharIndex();
124-
int selStart = Math.min(hitStart, hitEnd);
125-
int selEnd = Math.max(hitStart + 1, hitEnd + 1);
122+
int selStart = getSelectionStartIndex();
123+
int selEnd = getSelectionEndIndex();
126124

127125
StringJoiner result = new StringJoiner("");
128126
int currentPos = 0;

jabgui/src/main/java/org/jabref/gui/util/SelectableTextFlow.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import org.jspecify.annotations.Nullable;
1919

2020
public class SelectableTextFlow extends TextFlow {
21-
@Nullable protected HitInfo startHit;
22-
@Nullable protected HitInfo endHit;
23-
@Nullable protected Path selectionPath;
21+
@Nullable private HitInfo startHit;
22+
@Nullable private HitInfo endHit;
23+
@Nullable private Path selectionPath;
2424

2525
private final Pane parentPane;
2626
private boolean isDragging = false;
@@ -46,12 +46,12 @@ public SelectableTextFlow(Pane parent) {
4646
}
4747

4848
public void copySelectedText() {
49-
if (startHit == null || endHit == null) {
49+
if (!isSelectionActive()) {
5050
return;
5151
}
5252

53-
int startIndex = Math.min(startHit.getCharIndex(), endHit.getCharIndex());
54-
int endIndex = Math.max(startHit.getCharIndex() + 1, endHit.getCharIndex() + 1);
53+
int startIndex = getSelectionStartIndex();
54+
int endIndex = getSelectionEndIndex();
5555

5656
String fullText = getTextFlowContent();
5757
if (startIndex < 0 || endIndex > fullText.length() || startIndex >= endIndex) {
@@ -77,6 +77,22 @@ public void clearSelection() {
7777
removeHighlight();
7878
}
7979

80+
public boolean isSelectionActive() {
81+
return startHit != null && endHit != null && startHit.getCharIndex() != endHit.getCharIndex();
82+
}
83+
84+
/// Returns the start index of the selection. Assumes that the selection is active.
85+
public int getSelectionStartIndex() {
86+
assert isSelectionActive();
87+
return Math.min(startHit.getCharIndex(), endHit.getCharIndex());
88+
}
89+
90+
/// Returns the end index of the selection. Assumes that the selection is active.
91+
public int getSelectionEndIndex() {
92+
assert isSelectionActive();
93+
return Math.max(startHit.getCharIndex() + 1, endHit.getCharIndex() + 1);
94+
}
95+
8096
private String getTextFlowContent() {
8197
StringBuilder sb = new StringBuilder();
8298
for (Node node : getChildren()) {
@@ -90,14 +106,11 @@ private String getTextFlowContent() {
90106
private void updateSelectionHighlight() {
91107
removeHighlight();
92108

93-
if (startHit == null || endHit == null || startHit.getCharIndex() == endHit.getCharIndex()) {
109+
if (!isSelectionActive()) {
94110
return;
95111
}
96112

97-
int startIndex = Math.min(startHit.getCharIndex(), endHit.getCharIndex());
98-
int endIndex = Math.max(startHit.getCharIndex() + 1, endHit.getCharIndex() + 1);
99-
100-
PathElement[] elements = rangeShape(startIndex, endIndex);
113+
PathElement[] elements = rangeShape(getSelectionStartIndex(), getSelectionEndIndex());
101114

102115
Path path = new Path();
103116
path.getElements().addAll(elements);

0 commit comments

Comments
 (0)