Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitMurf/cffd1446cbd693f6b145e77388163754 to your computer and use it in GitHub Desktop.
Save GitMurf/cffd1446cbd693f6b145e77388163754 to your computer and use it in GitHub Desktop.
Add a couple Outliner capability features to Obsidian's native list handling: Hotkey to select all text in current bullet, select all children bullets, select entire line of non list item text.
<%*
const editor = app.workspace.activeLeaf.view.editor;
const getCurSelection = editor.getSelection();
const getCur = editor.getCursor("from");
const curLineNo = getCur.line;
const curLineText = editor.getLine(curLineNo);
const matchBullet = curLineText.match(/^[ \t]*[-\*] (.*)/);
let foundContent = "";
if(matchBullet) {
foundContent = matchBullet[1];
}
if(!foundContent) {
// Not a list item, so just select the entire line
const multiSelect = editor.listSelections();
const startLine = editor.getCursor("from").line;
const endLine = editor.getCursor("to").line;
let compareSelection = curLineText;
if(startLine !== endLine) {
// already multiple lines selected
compareSelection = editor.getRange({ line: startLine, ch: 0 }, { line: endLine, ch: editor.getLine(endLine).length })
}
if(getCurSelection === compareSelection) {
// if line is already selected then extend selection to the line below
editor.setSelection({ line: startLine, ch: 0 }, { line: endLine + 1, ch: editor.getLine(endLine + 1).length });
} else {
const matchBullet = curLineText.match(/^[ \t]*(.*)/);
if(matchBullet) {
// this is to account for if in code files like templater select the line content without the prepended indented spaces
const foundPosStart = curLineText.indexOf(matchBullet[1]);
const selectedWithoutSpace = editor.getRange({ line: curLineNo, ch: foundPosStart }, { line: curLineNo, ch: curLineText.length });
if(getCurSelection === selectedWithoutSpace) {
editor.setSelection({ line: curLineNo, ch: 0 }, { line: curLineNo, ch: curLineText.length });
} else {
editor.setSelection({ line: curLineNo, ch: foundPosStart }, { line: curLineNo, ch: curLineText.length });
}
}
}
return;
}
const secondSelection = getCurSelection === foundContent ? true : false;
switch(secondSelection) {
case false:
const foundPosition = curLineText.indexOf(foundContent);
editor.setSelection({ line: curLineNo, ch: foundPosition }, { line: curLineNo, ch: curLineText.length });
break;
case true:
// list item already selected so now select all children
const activeFile = app.workspace.getActiveFile();
if(!activeFile) { return; }
const mdCache = app.metadataCache.getFileCache(activeFile);
if(!mdCache) { return; }
const mdCacheListItems = mdCache.listItems;
if(!mdCacheListItems) { return; }
let foundParentLine = false;
let foundLastChild = false;
let foundLastChildLine = curLineNo;
mdCacheListItems.forEach(eachLI => {
if(foundParentLine === true && foundLastChild === false) {
if(eachLI.parent >= curLineNo) {
foundLastChildLine = eachLI.position.start.line;
} else {
foundLastChild = true;
}
}
if(eachLI.position.start.line === curLineNo) { foundParentLine = true; }
});
editor.setSelection({ line: curLineNo, ch: 0 }, { line: foundLastChildLine, ch: editor.getLine(foundLastChildLine).length });
break;
}
return;
%>
@GitMurf
Copy link
Author

GitMurf commented Aug 12, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment