update listify command to work with selection and add keyboard shortcut (#290)

Co-authored-by: Tristan Sokol <tristan@rightfoot.com>
This commit is contained in:
Tristan Sokol
2023-01-06 18:47:08 +01:00
committed by GitHub
co-authored by Tristan Sokol
parent 6f500bcc16
commit dc7de9d8f9
2 changed files with 18 additions and 6 deletions
+2
View File
@@ -293,6 +293,8 @@ functions:
path: ./text.ts:listifySelection
command:
name: "Text: Listify Selection"
key: "Ctrl-Shift-8"
mac: "Cmd-Shift-8"
numberListifySelection:
path: ./text.ts:numberListifySelection
command:
+16 -6
View File
@@ -22,7 +22,18 @@ export async function quoteSelection() {
export async function listifySelection() {
let text = await editor.getText();
const selection = await editor.getSelection();
//if very first of doc, just add a bullet and end
if (selection.to == 0 && selection.from == 0) {
await editor.insertAtCursor("* ")
return
}
let from = selection.from;
if (text[from] == "\n") {
//end of line, need to find previous line break
from--
}
while (from >= 0 && text[from] !== "\n") {
from--;
}
@@ -42,12 +53,11 @@ export async function numberListifySelection() {
from++;
text = text.slice(from, selection.to);
let counter = 1;
text = `1. ${
text.replaceAll(/\n(?!\n)/g, () => {
counter++;
return `\n${counter}. `;
})
}`;
text = `1. ${text.replaceAll(/\n(?!\n)/g, () => {
counter++;
return `\n${counter}. `;
})
}`;
await editor.replaceRange(from, selection.to, text);
}