Skip to content

Instantly share code, notes, and snippets.

@GitMurf
Created January 8, 2022 04:07
Show Gist options
  • Save GitMurf/c52dfef13162d88375975e4eefedf3a7 to your computer and use it in GitHub Desktop.
Save GitMurf/c52dfef13162d88375975e4eefedf3a7 to your computer and use it in GitHub Desktop.

Revisions

  1. GitMurf created this gist Jan 8, 2022.
    58 changes: 58 additions & 0 deletions obsidian.templater.pane.send-to-next.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <%*
    //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;
    }
    }
    }
    %>