Skip to content
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
18 changes: 12 additions & 6 deletions src/io/dailyNotes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Moment } from "moment";
import type { TFile } from "obsidian";
import type { TFile, WorkspaceLeaf } from "obsidian";
import {
createDailyNote,
getDailyNoteSettings,
Expand All @@ -13,7 +13,7 @@ import { createConfirmationDialog } from "src/ui/modal";
*/
export async function tryToCreateDailyNote(
date: Moment,
inNewSplit: boolean,
ctrlPressed: boolean,
settings: ISettings,
cb?: (newFile: TFile) => void
): Promise<void> {
Expand All @@ -23,10 +23,16 @@ export async function tryToCreateDailyNote(

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

let leaf: WorkspaceLeaf;
if (ctrlPressed) {
if (settings.ctrlClickOpensInNewTab) {
leaf = workspace.getLeaf('tab');
} else {
leaf = workspace.getLeaf('split', 'vertical');
}
} else {
leaf = workspace.getLeaf(false);
}
await leaf.openFile(dailyNote, { active : true });
cb?.(dailyNote);
};
Expand Down
18 changes: 12 additions & 6 deletions src/io/weeklyNotes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Moment } from "moment";
import type { TFile } from "obsidian";
import type { TFile, WorkspaceLeaf } from "obsidian";
import {
createWeeklyNote,
getWeeklyNoteSettings,
Expand All @@ -13,7 +13,7 @@ import { createConfirmationDialog } from "src/ui/modal";
*/
export async function tryToCreateWeeklyNote(
date: Moment,
inNewSplit: boolean,
ctrlPressed: boolean,
settings: ISettings,
cb?: (file: TFile) => void
): Promise<void> {
Expand All @@ -23,10 +23,16 @@ export async function tryToCreateWeeklyNote(

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

let leaf: WorkspaceLeaf;
if (ctrlPressed) {
if (settings.ctrlClickOpensInNewTab) {
leaf = workspace.getLeaf('tab');
} else {
leaf = workspace.getLeaf('split', 'vertical');
}
} else {
leaf = workspace.getLeaf(false);
}
await leaf.openFile(dailyNote, { active : true });
cb?.(dailyNote);
};
Expand Down
19 changes: 19 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ISettings {
wordsPerDot: number;
weekStart: IWeekStartOption;
shouldConfirmBeforeCreate: boolean;
ctrlClickOpensInNewTab: boolean;

// Weekly Note settings
showWeeklyNote: boolean;
Expand All @@ -33,6 +34,7 @@ const weekdays = [
export const defaultSettings = Object.freeze({
shouldConfirmBeforeCreate: true,
weekStart: "locale" as IWeekStartOption,
ctrlClickOpensInNewTab: false,

wordsPerDot: DEFAULT_WORDS_PER_DOT,

Expand Down Expand Up @@ -79,6 +81,7 @@ export class CalendarSettingsTab extends PluginSettingTab {
});
this.addDotThresholdSetting();
this.addWeekStartSetting();
this.addCtrlClickSetting();
this.addConfirmCreateSetting();
this.addShowWeeklyNoteSetting();

Expand Down Expand Up @@ -147,6 +150,22 @@ export class CalendarSettingsTab extends PluginSettingTab {
});
}

addCtrlClickSetting(): void {
new Setting(this.containerEl)
.setName("Ctrl + Click Behaviour")
.setDesc("Set the behaviour of Ctrl + Clicking on a date")
.addDropdown((dropdown) => {
dropdown.addOption("new-tab", "Open in new tab");
dropdown.addOption("new-split", "Open in new split");
dropdown.setValue(this.plugin.options.ctrlClickOpensInNewTab ? "new-tab" : "new-split");
dropdown.onChange(async (value) => {
this.plugin.writeOptions(() => ({
ctrlClickOpensInNewTab: value === "new-tab",
}));
});
});
}

addConfirmCreateSetting(): void {
new Setting(this.containerEl)
.setName("Confirm before creating new note")
Expand Down
9 changes: 9 additions & 0 deletions src/ui/fileMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export function showFileMenu(app: App, file: TFile, position: Point): void {
(<any>app).fileManager.promptForFileDeletion(file);
})
);
fileMenu.addItem((item) =>
item
.setTitle("Open in new tab")
.setIcon("file-plus")
.setSection("open")
.onClick(() => {
app.workspace.openLinkText(file.path, "", true);
})
);

app.workspace.trigger(
"file-menu",
Expand Down
43 changes: 25 additions & 18 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export default class CalendarView extends ItemView {

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

Expand All @@ -267,32 +267,36 @@ export default class CalendarView extends ItemView {

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

const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
let leaf: WorkspaceLeaf;
if (ctrlPressed) {
if (this.settings.ctrlClickOpensInNewTab) {
leaf = workspace.getLeaf('tab');
} else {
leaf = workspace.getLeaf('split', 'vertical');
}
} else {
leaf = workspace.getLeaf(false);
}
await leaf.openFile(existingFile);

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

async openOrCreateDailyNote(
date: Moment,
inNewSplit: boolean
ctrlPressed: boolean
): Promise<void> {
const { workspace } = this.app;
const existingFile = getDailyNote(date, get(dailyNotes));
if (!existingFile) {
// File doesn't exist
tryToCreateDailyNote(
date,
inNewSplit,
ctrlPressed,
this.settings,
(dailyNote: TFile) => {
activeFile.setFile(dailyNote);
Expand All @@ -301,13 +305,16 @@ export default class CalendarView extends ItemView {
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 });

activeFile.setFile(existingFile);
let leaf: WorkspaceLeaf;
if (ctrlPressed) {
if (this.settings.ctrlClickOpensInNewTab) {
leaf = workspace.getLeaf('tab');
} else {
leaf = workspace.getLeaf('split', 'vertical');
}
} else {
leaf = workspace.getLeaf(false);
}
await leaf.openFile(existingFile);
}
}