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 path: ./text.ts:listifySelection
command: command:
name: "Text: Listify Selection" name: "Text: Listify Selection"
key: "Ctrl-Shift-8"
mac: "Cmd-Shift-8"
numberListifySelection: numberListifySelection:
path: ./text.ts:numberListifySelection path: ./text.ts:numberListifySelection
command: command:
+16 -6
View File
@@ -22,7 +22,18 @@ export async function quoteSelection() {
export async function listifySelection() { export async function listifySelection() {
let text = await editor.getText(); let text = await editor.getText();
const selection = await editor.getSelection(); 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; let from = selection.from;
if (text[from] == "\n") {
//end of line, need to find previous line break
from--
}
while (from >= 0 && text[from] !== "\n") { while (from >= 0 && text[from] !== "\n") {
from--; from--;
} }
@@ -42,12 +53,11 @@ export async function numberListifySelection() {
from++; from++;
text = text.slice(from, selection.to); text = text.slice(from, selection.to);
let counter = 1; let counter = 1;
text = `1. ${ text = `1. ${text.replaceAll(/\n(?!\n)/g, () => {
text.replaceAll(/\n(?!\n)/g, () => { counter++;
counter++; return `\n${counter}. `;
return `\n${counter}. `; })
}) }`;
}`;
await editor.replaceRange(from, selection.to, text); await editor.replaceRange(from, selection.to, text);
} }