Skip to content

Commit

Permalink
fix: set cursor position when opening already opened file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Nov 10, 2023
1 parent 59092c6 commit d3f3b02
Showing 1 changed file with 88 additions and 12 deletions.
100 changes: 88 additions & 12 deletions src/utils/create_note.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
import i18next from "i18next";
import { App, normalizePath,Notice, TFile, TFolder, WorkspaceLeaf } from "obsidian";
import { App, getLinkpath,MarkdownView, normalizePath,Notice, TFile, TFolder, WorkspaceLeaf } from "obsidian";
import { DefaultOpening, FolderSettings, SplitDirection } from "src/interface";
import NoteInFolder from "src/main";

import { generateFileName, isTemplaterNeeded, replaceVariables } from "./utils";

function scrollToPosition(app: App, parts: {
path: string
heading?: string
block?: string
}) {
const cache = app.metadataCache.getCache(parts.path);
const view = app.workspace.getActiveViewOfType(MarkdownView);
if (!view || !cache) return;
// Get the corresponding position for the heading/block
if (parts.heading) {
const heading = cache.headings?.find(
heading => heading.heading === parts.heading
);
if (heading) {
view.editor.setCursor(heading.position.start.line);
}
} else if (parts.block) {
const block = cache.blocks?.[parts.block];
if (block) {
view.editor.setCursor(block.position.start.line);
}
}
}

function getLinkParts(path: string, app: App): {
path: string
heading?: string
block?: string
} {
// Extract the #^block from the path
const blockMatch = path.match(/\^(.*)$/);
const block = blockMatch ? blockMatch[1] : undefined;
// Remove the #^block
path = path.replace(/(\^.*)$/, "");

// Extract the #heading from the path
const headingMatch = path.match(/#(.*)$/);
const heading = headingMatch ? headingMatch[1] : undefined;
// Remove the #heading
path = path.replace(/(#.*)$/, "");

return {
path:
app.metadataCache.getFirstLinkpathDest(
getLinkpath(path),
app.workspace.getActiveFile()?.path ?? ""
)?.path ?? path,
heading,
block,
};
}


function getOpening(app: App, currentFolder: FolderSettings, param: DefaultOpening = currentFolder.opening, split: SplitDirection = currentFolder.splitDefault) {
switch (param) {
case DefaultOpening.split:
Expand Down Expand Up @@ -59,15 +112,23 @@ export async function createNoteInFolder(newFolder: FolderSettings, plugin: Note
if (currentFolder.opening !== DefaultOpening.nothing) {
//search if the file is already open to prevent opening it twice
let leaf = getLeafWithNote(app, file);
if (!leaf)
if (leaf) {
leaf.openFile(file, { active: currentFolder.focused });
scrollToPosition(app, getLinkParts(defaultName, app));
} else {
leaf = getOpening(app, currentFolder, currentFolder.opening, currentFolder.splitDefault) as WorkspaceLeaf;
await leaf.openFile(file, { active: currentFolder.focused });
await leaf.openFile(file, { active: currentFolder.focused });
}
} else if (currentFolder.alreadyExistOpening.opening !== DefaultOpening.nothing) {
//search if the file is already open to prevent opening it twice
let leaf = getLeafWithNote(app, file);
if (!leaf)
if (leaf) {
leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
scrollToPosition(app, getLinkParts(defaultName, app));
} else {
leaf = getOpening(app, currentFolder, currentFolder.alreadyExistOpening.opening, currentFolder.alreadyExistOpening.splitDefault);
if (leaf) await leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
if (leaf) await leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
}
}
} else if (!file) {
const leaf = getOpening(app, currentFolder);
Expand Down Expand Up @@ -113,13 +174,28 @@ export function createFolderInCurrent(newFolder: FolderSettings, currentFile: TF

const createdFilePath = normalizePath(`${currentFolder.path}/${defaultName}`);
const file = app.vault.getAbstractFileByPath(createdFilePath);
if (leaf && file instanceof TFile) {
leaf.openFile(file, { active: currentFolder.focused });
} else if (!leaf && file instanceof TFile && currentFolder.alreadyExistOpening.opening !== DefaultOpening.nothing) {
leaf = getLeafWithNote(app, file);
if (!leaf)
leaf = getOpening(app, currentFolder, currentFolder.alreadyExistOpening.opening);
if (leaf) leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
if (file instanceof TFile) {
if (currentFolder.opening !== DefaultOpening.nothing) {
//search if the file is already open to prevent opening it twice
let leaf = getLeafWithNote(app, file);
if (leaf) {
leaf.openFile(file, { active: currentFolder.focused });
scrollToPosition(app, getLinkParts(defaultName, app));
} else {
leaf = getOpening(app, currentFolder, currentFolder.opening, currentFolder.splitDefault) as WorkspaceLeaf;
leaf.openFile(file, { active: currentFolder.focused });
}
} else if (currentFolder.alreadyExistOpening.opening !== DefaultOpening.nothing) {
//search if the file is already open to prevent opening it twice
let leaf = getLeafWithNote(app, file);
if (leaf) {
leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
scrollToPosition(app, getLinkParts(defaultName, app));
} else {
leaf = getOpening(app, currentFolder, currentFolder.alreadyExistOpening.opening, currentFolder.alreadyExistOpening.splitDefault);
if (leaf) leaf.openFile(file, { active: currentFolder.alreadyExistOpening.focused });
}
}
} else if (!file) {
if (leaf) {
leaf = leaf as WorkspaceLeaf;
Expand Down

0 comments on commit d3f3b02

Please sign in to comment.