Skip to content

Commit

Permalink
Add Ctrl + Click behavior for opening notes in new tab or split view.…
Browse files Browse the repository at this point in the history
… Merged liamcain#331
  • Loading branch information
FBarrca committed Oct 17, 2024
1 parent 9d54e5d commit d303869
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
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
20 changes: 13 additions & 7 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,11 +23,17 @@ export async function tryToCreateWeeklyNote(

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

await leaf.openFile(dailyNote, { active : true });
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
23 changes: 21 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface ISettings {
wordsPerDot: number;
weekStart: IWeekStartOption;
shouldConfirmBeforeCreate: boolean;
showQuarter: boolean; // Existing property
ctrlClickOpensInNewTab: boolean;
showQuarter: boolean;
// Weekly Note settings
showWeeklyNote: boolean;
showWeeklyNoteRight: boolean;
Expand All @@ -34,6 +35,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 @@ -82,6 +84,7 @@ export class CalendarSettingsTab extends PluginSettingTab {
});
this.addDotThresholdSetting();
this.addWeekStartSetting();
this.addCtrlClickSetting();
this.addConfirmCreateSetting();
this.addShowWeeklyNoteSetting();
this.addShowWeeklyNoteRightSetting();
Expand Down Expand Up @@ -150,7 +153,23 @@ 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
1 change: 1 addition & 0 deletions src/testUtils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getDefaultSettings(
{
weekStart: "sunday",
shouldConfirmBeforeCreate: false,
ctrlClickOpensInNewTab: false,
wordsPerDot: 50,
showWeeklyNote: false,
showQuarter: true,
Expand Down
10 changes: 9 additions & 1 deletion src/ui/fileMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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",
fileMenu,
Expand Down
42 changes: 25 additions & 17 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ export default class CalendarView extends ItemView {

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

Expand All @@ -442,31 +442,35 @@ export default class CalendarView extends ItemView {
const existingFile = getWeeklyNote(date, get(weeklyNotes));

if (!existingFile) {
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) {
tryToCreateDailyNote(
date,
inNewSplit,
ctrlPressed,
this.settings,
(dailyNote: TFile) => {
activeFile.setFile(dailyNote);
Expand All @@ -475,13 +479,17 @@ export default class CalendarView extends ItemView {
return;
}

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);
}

async openOrCreateMonthlyNote(
Expand Down

0 comments on commit d303869

Please sign in to comment.