Skip to content

Open new tabs instead of new splits #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "calendar",
"name": "Calendar",
"description": "Calendar view of your daily notes",
"version": "1.5.10",
"version": "1.5.11",
"author": "Liam Cain",
"authorUrl": "https://github.com/liamcain/",
"fundingUrl": "https://www.buymeacoffee.com/liamcain",
Expand Down
11 changes: 4 additions & 7 deletions src/io/dailyNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createDailyNote,
getDailyNoteSettings,
} from "obsidian-daily-notes-interface";
import openLeaf from "../openLeaf";

import type { ISettings } from "src/settings";
import { createConfirmationDialog } from "src/ui/modal";
Expand All @@ -13,21 +14,17 @@ import { createConfirmationDialog } from "src/ui/modal";
*/
export async function tryToCreateDailyNote(
date: Moment,
inNewSplit: boolean,
inNewTab: boolean,
settings: ISettings,
cb?: (newFile: TFile) => void
): Promise<void> {
const { workspace } = window.app;
const { format } = getDailyNoteSettings();
const filename = date.format(format);

const createFile = async () => {
const dailyNote = await createDailyNote(date);
const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();

await leaf.openFile(dailyNote, { active : true });
const leaf = openLeaf(inNewTab);
await leaf.openFile(dailyNote, { active: true });
cb?.(dailyNote);
};

Expand Down
10 changes: 4 additions & 6 deletions src/io/weeklyNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createWeeklyNote,
getWeeklyNoteSettings,
} from "obsidian-daily-notes-interface";
import openLeaf from "../openLeaf";

import type { ISettings } from "src/settings";
import { createConfirmationDialog } from "src/ui/modal";
Expand All @@ -13,21 +14,18 @@ import { createConfirmationDialog } from "src/ui/modal";
*/
export async function tryToCreateWeeklyNote(
date: Moment,
inNewSplit: boolean,
inNewTab: boolean,
settings: ISettings,
cb?: (file: TFile) => void
): Promise<void> {
const { workspace } = window.app;
const { format } = getWeeklyNoteSettings();
const filename = date.format(format);

const createFile = async () => {
const dailyNote = await createWeeklyNote(date);
const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
const leaf = openLeaf(inNewTab);

await leaf.openFile(dailyNote, { active : true });
await leaf.openFile(dailyNote, { active: true });
cb?.(dailyNote);
};

Expand Down
8 changes: 8 additions & 0 deletions src/openLeaf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { WorkspaceLeaf } from "obsidian";

export default function openLeaf(inNewTab: boolean): WorkspaceLeaf {
return inNewTab
? // @ts-expect-error: we're on an old version of obsidian types, for now this will cause error
window.app.workspace.createLeafInTabGroup()
: window.app.workspace.getUnpinnedLeaf();
}
39 changes: 14 additions & 25 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
tasksSource,
wordCountSource,
} from "./ui/sources";
import openLeaf from "./openLeaf";

export default class CalendarView extends ItemView {
private calendar: Calendar;
Expand Down Expand Up @@ -255,10 +256,7 @@ export default class CalendarView extends ItemView {
}
}

async openOrCreateWeeklyNote(
date: Moment,
inNewSplit: boolean
): Promise<void> {
async openOrCreateWeeklyNote(date: Moment, inNewTab: boolean): Promise<void> {
const { workspace } = this.app;

const startOfWeek = date.clone().startOf("week");
Expand All @@ -267,47 +265,38 @@ export default class CalendarView extends ItemView {

if (!existingFile) {
// File doesn't exist
tryToCreateWeeklyNote(startOfWeek, inNewSplit, this.settings, (file) => {
tryToCreateWeeklyNote(startOfWeek, inNewTab, this.settings, (file) => {
activeFile.setFile(file);
});
return;
}

const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
const leaf = openLeaf(inNewTab);
await leaf.openFile(existingFile);

activeFile.setFile(existingFile);
workspace.setActiveLeaf(leaf, true, true)
workspace.setActiveLeaf(leaf, true, true);
}

async openOrCreateDailyNote(
date: Moment,
inNewSplit: boolean
): Promise<void> {
const { workspace } = this.app;
async openOrCreateDailyNote(date: Moment, inNewTab: boolean): Promise<void> {
const existingFile = getDailyNote(date, get(dailyNotes));
if (!existingFile) {
// File doesn't exist
tryToCreateDailyNote(
date,
inNewSplit,
inNewTab,
this.settings,
(dailyNote: TFile) => {
activeFile.setFile(dailyNote);
}
);
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mode = (this.app.vault as any).getConfig("defaultViewMode");
const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
await leaf.openFile(existingFile, { active : true, mode });
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mode = (this.app.vault as any).getConfig("defaultViewMode");
const leaf = openLeaf(inNewTab);
await leaf.openFile(existingFile, { active: true, mode });

activeFile.setFile(existingFile);
activeFile.setFile(existingFile);
}
}
}
Loading