Skip to content

Commit 85d90bb

Browse files
committed
Added ellipsis to search matches if they are windowed
1 parent 91f3e70 commit 85d90bb

File tree

3 files changed

+45
-58
lines changed

3 files changed

+45
-58
lines changed

app/components/SearchPalette.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,14 @@ function SearchResultValue({
312312
function createOutputForMatch(
313313
stringValue: string,
314314
isHighlighted: boolean,
315-
match?: Fuse.FuseResultMatch
315+
match?: Fuse.FuseResultMatch,
316+
maxLength: number = 68
316317
): JSX.Element {
317318
if (!match) {
318-
return <>{truncate(stringValue, { length: 56 })}</>;
319+
return <>{truncate(stringValue, { length: maxLength })}</>;
319320
}
320321

321-
const stringSlices = getStringSlices(stringValue, match.indices, 56);
322+
const stringSlices = getStringSlices(stringValue, match.indices, maxLength);
322323

323324
return (
324325
<>

app/utilities/search.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ export function getRawValue(type: JSONValueType): string | undefined {
8585
}
8686

8787
type StringSlice = {
88-
start: number;
89-
end: number;
9088
isMatch: boolean;
9189
slice: string;
9290
};
@@ -96,11 +94,13 @@ type StringSlice = {
9694
// and the matches are [[10, 15], [80, 90], [100, 105]]
9795
// then we should return slices:
9896
// [
99-
// { start: 70, end: 80, isMatch: false, slice: "the string," },
100-
// { start: 80, end: 90, isMatch: true, slice: ", we should" },
101-
// { start: 90, end: 100, isMatch: false, slice: "d only retu" },
102-
// { start: 100, end: 105, isMatch: true, slice: "urn sl" },
103-
// { start: 105, end: 125, isMatch: false, slice: "lices that are within" },
97+
// { isMatch: false, slice: "…" }
98+
// { isMatch: false, slice: "the string," },
99+
// { isMatch: true, slice: ", we should" },
100+
// { isMatch: false, slice: "d only retu" },
101+
// { isMatch: true, slice: "urn sl" },
102+
// { isMatch: false, slice: "lices that are within" },
103+
// { isMatch: false, slice: "…" },
104104
//
105105
// If stringValue length is less than the windowSize, then we should return all the string slices of the string
106106
export function getStringSlices(
@@ -110,13 +110,12 @@ export function getStringSlices(
110110
): Array<StringSlice> {
111111
const slices: StringSlice[] = [];
112112

113-
const addSlice = (
114-
start: number,
115-
end: number,
116-
isMatch: boolean,
117-
slice: string
118-
) => {
119-
slices.push({ start, end, isMatch, slice });
113+
const addSlice = (isMatch: boolean, slice: string) => {
114+
slices.push({ isMatch, slice });
115+
};
116+
117+
const addEllipsis = () => {
118+
addSlice(false, "…");
120119
};
121120

122121
const calculateWindow = (): { start: number; end: number } => {
@@ -152,38 +151,31 @@ export function getStringSlices(
152151

153152
let currentIndex = window.start;
154153

154+
if (window.start > 0) {
155+
addEllipsis();
156+
}
157+
155158
for (const [start, end] of matchingIndices) {
156159
if (start < window.start && end <= window.start) {
157160
continue;
158161
} else if (start >= window.end) {
159162
continue;
160163
} else if (start < window.start && end > window.start) {
161-
addSlice(
162-
window.start,
163-
end + 1,
164-
true,
165-
stringValue.slice(window.start, end + 1)
166-
);
164+
addSlice(true, stringValue.slice(window.start, end + 1));
167165

168166
currentIndex = end + 1;
169167
} else if (start >= window.start && end <= window.end) {
170-
addSlice(
171-
currentIndex,
172-
start,
173-
false,
174-
stringValue.slice(currentIndex, start)
175-
);
176-
addSlice(start, end + 1, true, stringValue.slice(start, end + 1));
168+
addSlice(false, stringValue.slice(currentIndex, start));
169+
addSlice(true, stringValue.slice(start, end + 1));
177170
currentIndex = end + 1;
178171
}
179172
}
180173

181-
addSlice(
182-
currentIndex,
183-
window.end,
184-
false,
185-
stringValue.slice(currentIndex, window.end + 1)
186-
);
174+
addSlice(false, stringValue.slice(currentIndex, window.end + 1).trimEnd());
175+
176+
if (window.end < stringValue.length - 1) {
177+
addEllipsis();
178+
}
187179

188180
return slices;
189181
}

tests/search.test.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,16 @@ describe("getStringSlices", () => {
6969
expect(slices).toMatchInlineSnapshot(`
7070
Array [
7171
Object {
72-
"end": 10,
7372
"isMatch": false,
7473
"slice": "This is a ",
75-
"start": 0,
7674
},
7775
Object {
78-
"end": 16,
7976
"isMatch": true,
8077
"slice": "really",
81-
"start": 10,
8278
},
8379
Object {
84-
"end": 37,
8580
"isMatch": false,
8681
"slice": " great (short) string",
87-
"start": 16,
8882
},
8983
]
9084
`);
@@ -104,34 +98,32 @@ Array [
10498
expect(slices).toMatchInlineSnapshot(`
10599
Array [
106100
Object {
107-
"end": 80,
101+
"isMatch": false,
102+
"slice": "…",
103+
},
104+
Object {
108105
"isMatch": false,
109106
"slice": "e is outside of the windo",
110-
"start": 55,
111107
},
112108
Object {
113-
"end": 91,
114109
"isMatch": true,
115110
"slice": "w, so we sh",
116-
"start": 80,
117111
},
118112
Object {
119-
"end": 100,
120113
"isMatch": false,
121114
"slice": "ould try ",
122-
"start": 91,
123115
},
124116
Object {
125-
"end": 106,
126117
"isMatch": true,
127118
"slice": "and ge",
128-
"start": 100,
129119
},
130120
Object {
131-
"end": 115,
132121
"isMatch": false,
133122
"slice": "t only sli",
134-
"start": 106,
123+
},
124+
Object {
125+
"isMatch": false,
126+
"slice": "…",
135127
},
136128
]
137129
`);
@@ -150,22 +142,24 @@ Array [
150142
expect(slices2).toMatchInlineSnapshot(`
151143
Array [
152144
Object {
153-
"end": 103,
145+
"isMatch": false,
146+
"slice": "…",
147+
},
148+
Object {
154149
"isMatch": false,
155150
"slice": " incididunt ut labore et ",
156-
"start": 78,
157151
},
158152
Object {
159-
"end": 109,
160153
"isMatch": true,
161154
"slice": "dolore",
162-
"start": 103,
163155
},
164156
Object {
165-
"end": 133,
166157
"isMatch": false,
167158
"slice": " magna aliqua. Ut enim ad",
168-
"start": 109,
159+
},
160+
Object {
161+
"isMatch": false,
162+
"slice": "…",
169163
},
170164
]
171165
`);

0 commit comments

Comments
 (0)