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:
parent
6f500bcc16
commit
dc7de9d8f9
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user