Skip to content

Commit 59bf9f2

Browse files
committed
Make the lenght calculations better
1 parent e747c40 commit 59bf9f2

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/commands/codeCommand.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ export const codeCommand: Command = {
1414

1515
// when there's no breaking line
1616
if (state1.selectedText.indexOf("\n") === -1) {
17-
const state2 = api.replaceSelection(`\`${state1.selectedText}\``);
17+
api.replaceSelection(`\`${state1.selectedText}\``);
1818
// Adjust the selection to not contain the **
19+
20+
const selectionStart = state1.selection.start + 1;
21+
const selectionEnd = selectionStart + state1.selectedText.length;
22+
1923
api.setSelectionRange({
20-
start: state2.selection.end - 1 - state1.selectedText.length,
21-
end: state2.selection.end - 1
24+
start: selectionStart,
25+
end: selectionEnd
2226
});
2327
return;
2428
}
@@ -29,11 +33,14 @@ export const codeCommand: Command = {
2933
const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
3034
const breaksAfter = Array(breaksAfterCount + 1).join("\n");
3135

32-
const state2 = api.replaceSelection(`${breaksBefore}\`\`\`\n${state1.selectedText}\n\`\`\`${breaksAfter}`);
33-
// Adjust the selection to not contain the **
36+
api.replaceSelection(`${breaksBefore}\`\`\`\n${state1.selectedText}\n\`\`\`${breaksAfter}`);
37+
38+
const selectionStart = state1.selection.start + breaksBeforeCount + 4;
39+
const selectionEnd = selectionStart + state1.selectedText.length;
40+
3441
api.setSelectionRange({
35-
start: state2.selection.end - 1 - state1.selectedText.length - breaksAfterCount - 3,
36-
end: state2.selection.end - 1 - breaksAfterCount - 3
42+
start: selectionStart,
43+
end: selectionEnd
3744
});
3845
}
3946
,

src/commands/listCommands.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,18 @@ export const unorderedListCommand: Command = {
4343
const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
4444
const breaksAfter = Array(breaksAfterCount + 1).join("\n");
4545

46-
const state2 = api.replaceSelection(`${breaksBefore}${state1.selectedText}${breaksAfter}`);
46+
const modifiedText = insertBeforeEachLine(state1.selectedText, "- ");
4747

48-
// const modifiedText = insertBeforeEachLine(state2.selectedText, "- ");
49-
// const state3 = api.replaceSelection(modifiedText.modifiedText);
48+
api.replaceSelection(`${breaksBefore}${modifiedText.modifiedText}${breaksAfter}`);
5049

51-
const modifiedText = { insertionLength: 0 };
52-
const state3 = state2;
53-
54-
const selectionRange = {
55-
start: state3.selection.end - state1.selectedText.length - breaksAfterCount,
56-
end: state3.selection.end - breaksAfterCount
57-
};
50+
const selectionStart = state1.selection.start + breaksBeforeCount;
51+
const selectionEnd = selectionStart + modifiedText.modifiedText.length;
5852

5953
// Adjust the selection to not contain the **
60-
api.setSelectionRange(selectionRange);
61-
}
62-
,
54+
api.setSelectionRange({
55+
start: selectionStart,
56+
end: selectionEnd
57+
});
58+
},
6359
keyCommand: "code",
6460
}

src/commands/quoteCommand.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ export const quoteCommand: Command = {
1919
const breaksAfter = Array(breaksAfterCount + 1).join("\n");
2020

2121
// Replaces the current selection with the bold mark up
22-
const state2 = api.replaceSelection( `${breaksBefore}> ${state1.selectedText}${breaksAfter}`);
22+
api.replaceSelection( `${breaksBefore}> ${state1.selectedText}${breaksAfter}`);
2323
// Adjust the selection to not contain the **
24+
25+
const selectionStart = state1.selection.start + breaksBeforeCount + 2;
26+
const selectionEnd = selectionStart + state1.selectedText.length;
27+
2428
api.setSelectionRange({
25-
start: state2.selection.end - state1.selectedText.length - breaksAfterCount,
26-
end: state2.selection.end - breaksAfterCount
29+
start: selectionStart,
30+
end: selectionEnd
2731
});
2832
},
2933
keyCommand: "quote",

0 commit comments

Comments
 (0)