Skip to content

Commit 2264b06

Browse files
committed
fix(minimap): element at the end of a highlight makes it open ended
1 parent bbc15f1 commit 2264b06

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

src/stores/minimap/subscriptions/effects/minimap-canvas/worker/shapes/helpers/calculate-chunk-positions.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,46 @@ describe('calculate-chunk-position', () => {
14201420
const actual = calculateChunkPositions(input, N_CHARS_PER_LINE, '', '');
14211421
expect(actual).toEqual(output);
14221422
});
1423+
1424+
test('case: bold at the end of highlight', () => {
1425+
const input = '==word1 **bold**== word2';
1426+
const output = {
1427+
chunks: [
1428+
{
1429+
chunk: '==word1 ',
1430+
line: 0,
1431+
x_chars: 0,
1432+
length_chars: 8,
1433+
type: 'highlight',
1434+
},
1435+
{
1436+
chunk: '**bold**',
1437+
line: 0,
1438+
x_chars: 8,
1439+
length_chars: 8,
1440+
type: 'bold_italic',
1441+
},
1442+
{
1443+
chunk: '==',
1444+
line: 0,
1445+
x_chars: 16,
1446+
length_chars: 2,
1447+
type: 'highlight',
1448+
},
1449+
{
1450+
chunk: 'word2',
1451+
line: 0,
1452+
x_chars: 19,
1453+
length_chars: 5,
1454+
type: null,
1455+
},
1456+
],
1457+
totalLines: 1,
1458+
empty: false,
1459+
};
1460+
const actual = calculateChunkPositions(input, N_CHARS_PER_LINE, '', '');
1461+
expect(actual).toEqual(output);
1462+
});
14231463
});
14241464

14251465
describe('performance-test: calculate-chunk-position', () => {

src/stores/minimap/subscriptions/effects/minimap-canvas/worker/shapes/helpers/calculate-chunk-positions.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ type ElementMeta = {
5757
type State = {
5858
element: MinimapElement;
5959
elementMeta: ElementMeta | null;
60-
currentCharacterName: ElementName | null;
61-
60+
closingTagLength: number;
6261
x: number;
6362
lineNumber: number;
6463
// highlight/task/bullet point
@@ -126,7 +125,7 @@ export const calculateChunkPositions = (
126125
},
127126
elementMeta: null,
128127
parentElementMeta: null,
129-
currentCharacterName: null,
128+
closingTagLength: 0,
130129
x: 0,
131130
lineNumber: 0,
132131
};
@@ -148,13 +147,11 @@ export const calculateChunkPositions = (
148147
if (state.elementMeta?.noSpaces) {
149148
state.elementMeta = null;
150149
}
151-
state.currentCharacterName = null;
152150
} else if (
153151
!state.elementMeta ||
154152
state.elementMeta?.scope !== ElementScope.full_line
155153
) {
156154
const elementName = charToElementName[character];
157-
state.currentCharacterName = elementName;
158155
if (elementName) {
159156
const nextCharacter = content[i + 1];
160157
// heading
@@ -243,7 +240,9 @@ export const calculateChunkPositions = (
243240
scope: ElementScope.character,
244241
};
245242
}
246-
} else {
243+
}
244+
// double tag blocks
245+
else {
247246
const isHighlight = character === '=';
248247
const isDoubleCharacterTag =
249248
(isHighlight && nextCharacter === '=') ||
@@ -262,7 +261,9 @@ export const calculateChunkPositions = (
262261
const isClosingTag =
263262
state.elementMeta?.elementName === elementName;
264263
if (isClosingTag) {
265-
state.elementMeta = null;
264+
state.closingTagLength = isDoubleCharacterTag
265+
? 2
266+
: 1;
266267
} else {
267268
const scope = isHighlight
268269
? ElementScope.multi_line
@@ -289,7 +290,7 @@ export const calculateChunkPositions = (
289290
}
290291
// fallback to highlight/task/bullet-point types
291292
else if (
292-
!state.currentCharacterName &&
293+
state.closingTagLength === 0 &&
293294
!state.elementMeta &&
294295
state.parentElementMeta
295296
) {
@@ -372,7 +373,18 @@ export const calculateChunkPositions = (
372373
} else {
373374
state.element.chunk += character;
374375
}
375-
376+
if (state.closingTagLength > 0) {
377+
state.closingTagLength--;
378+
// end of the element
379+
if (state.closingTagLength === 0) {
380+
if (state.parentElementMeta) {
381+
state.elementMeta = state.parentElementMeta;
382+
state.parentElementMeta = null;
383+
} else {
384+
state.elementMeta = null;
385+
}
386+
}
387+
}
376388
state.x++;
377389
}
378390
state.element.length_chars = state.element.chunk.length;

0 commit comments

Comments
 (0)