Forked from GitMurf/obsidian.templater.pane.send-to-next.js
Created
December 20, 2024 22:59
-
-
Save jasondavis/ee3cab1b4e5bbf31393ec993a354391e to your computer and use it in GitHub Desktop.
Send the current active line of text or if text is selected, send the selection... to the pane next to the active one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%* | |
//OPTIONS: TOP / BOTTOM | |
const whereToWrite = "TOP"; | |
//OPTIONS: COPY / MOVE | |
const copyOrMove = "COPY"; | |
//Find leaf next door | |
const thisLeaf = app.workspace.activeLeaf; | |
const thisFile = thisLeaf.view.file; | |
let leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "right"); | |
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "left");} | |
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "bottom");} | |
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "top");} | |
if(leafToUse) { | |
//Find selected text or text on current line | |
const editor = app.workspace.activeLeaf.view.editor; | |
if(editor.somethingSelected() === false) { | |
const curLineNum = editor.getCursor().line; | |
editor.setSelection({ line: curLineNum, ch: 0 }, { line: curLineNum, ch: editor.getLine(curLineNum).length }); | |
} | |
let curLineStr = editor.getSelection(); | |
if(curLineStr) { | |
curLineStr = curLineStr.trim(); | |
const nextEditor = leafToUse.view.editor; | |
const foundFile = leafToUse.view.file; | |
let lineNo = 0; | |
switch(whereToWrite.toUpperCase()) { | |
case "TOP": | |
lineNo = 0; | |
const mdCache = app.metadataCache.getFileCache(foundFile); | |
if(mdCache) { | |
if(mdCache.frontmatter) { | |
lineNo = mdCache.frontmatter.position.end.line; | |
} | |
} | |
break; | |
case "BOTTOM": | |
lineNo = nextEditor.lastLine(); | |
break; | |
} | |
const curText = nextEditor.getLine(lineNo); | |
nextEditor.setLine(lineNo,`${curText}\n${curLineStr}`); | |
nextEditor.setCursor(lineNo + 1); | |
switch(copyOrMove.toUpperCase()) { | |
case "COPY": | |
tR = curLineStr; | |
break; | |
case "MOVE": | |
tR = ""; | |
break; | |
} | |
} | |
} | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment