From 08b89bdd7ac8087b7637167deb76dcea260176d3 Mon Sep 17 00:00:00 2001 From: ivan-lednev Date: Mon, 19 Feb 2024 21:02:43 +0100 Subject: [PATCH] refactor: clean up hover editor code --- src/main.ts | 18 ++---------------- src/types.ts | 4 ++-- src/ui/components/task.svelte | 4 ++-- src/use-newly-started-tasks.ts | 1 + src/util/create-show-preview.ts | 16 ++++++++++++++++ 5 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 src/util/create-show-preview.ts diff --git a/src/main.ts b/src/main.ts index c8115a79b..5c9476056 100644 --- a/src/main.ts +++ b/src/main.ts @@ -37,6 +37,7 @@ import { } from "./util/clock"; import { createHooks } from "./util/create-hooks"; import { createRenderMarkdown } from "./util/create-render-markdown"; +import { createShowPreview } from "./util/create-show-preview"; import { createDailyNoteIfNeeded } from "./util/daily-notes"; import { replaceSTaskInFile, toMarkdown } from "./util/dataview"; import { locToEditorPosition } from "./util/editor"; @@ -109,21 +110,6 @@ export default class DayPlanner extends Plugin { this.app.workspace.rightSplit.expand(); }; - // todo: move out - // todo: use a more descriptive name - handleMouseEnter = (el: HTMLElement, path: string, line = 0) => { - // @ts-ignore - if (!this.app.internalPlugins.plugins["page-preview"].enabled) { - return; - } - - const previewLocation = { - scroll: line, - }; - - this.app.workspace.trigger("link-hover", {}, el, path, "", previewLocation); - }; - private registerCommands() { this.addCommand({ id: "show-day-planner-timeline", @@ -339,7 +325,7 @@ export default class DayPlanner extends Plugin { clockOutUnderCursor: this.clockOutUnderCursor, clockInUnderCursor: this.clockInUnderCursor, cancelClockUnderCursor: this.cancelClockUnderCursor, - handleMouseEnter: this.handleMouseEnter, + showPreview: createShowPreview(this.app), isModPressed, }; diff --git a/src/types.ts b/src/types.ts index ef06b90c5..cee98967c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,10 +3,10 @@ import { Pos } from "obsidian"; import { STask } from "obsidian-dataview"; import { Readable, Writable } from "svelte/store"; -import DayPlanner from "./main"; import type { getHorizontalPlacing } from "./overlap/horizontal-placing"; import type { ObsidianFacade } from "./service/obsidian-facade"; import { useEditContext } from "./ui/hooks/use-edit/use-edit-context"; +import { createShowPreview } from "./util/create-show-preview"; import { getDiff, updateText } from "./util/tasks-utils"; export interface TaskLocation { @@ -84,7 +84,7 @@ export interface ObsidianContext { cancelClockUnderCursor: () => void; sTasksWithActiveClockProps: Readable; showReleaseNotes: () => void; - handleMouseEnter: DayPlanner["handleMouseEnter"]; + showPreview: ReturnType; isModPressed: Readable; } diff --git a/src/ui/components/task.svelte b/src/ui/components/task.svelte index efceca99a..d0ba0c5c8 100644 --- a/src/ui/components/task.svelte +++ b/src/ui/components/task.svelte @@ -10,7 +10,7 @@ // TODO: this should live in useTaskVisuals export let relationToNow = ""; - const { handleMouseEnter: baseHandleMouseEnter, isModPressed } = + const { showPreview, isModPressed } = getContext(obsidianContext); let el: HTMLDivElement; let hovering = false; @@ -24,7 +24,7 @@ } $: if ($isModPressed && hovering && task.location.path) { - baseHandleMouseEnter(el, task.location.path, task.location.line); + showPreview(el, task.location.path, task.location.line); } diff --git a/src/use-newly-started-tasks.ts b/src/use-newly-started-tasks.ts index eed33035c..02b6df1bc 100644 --- a/src/use-newly-started-tasks.ts +++ b/src/use-newly-started-tasks.ts @@ -12,6 +12,7 @@ interface UseNewlyStartedTasksProps { tasksForToday: Readable; } +// todo: move export function useNewlyStartedTasks(props: UseNewlyStartedTasksProps) { const { settings, currentTime, tasksForToday } = props; let previousTasksInProgress: PlacedTask[] = []; diff --git a/src/util/create-show-preview.ts b/src/util/create-show-preview.ts new file mode 100644 index 000000000..76418bbcd --- /dev/null +++ b/src/util/create-show-preview.ts @@ -0,0 +1,16 @@ +import { App } from "obsidian"; + +export const createShowPreview = + (app: App) => + (el: HTMLElement, path: string, line = 0) => { + // @ts-ignore + if (!app.internalPlugins.plugins["page-preview"].enabled) { + return; + } + + const previewLocation = { + scroll: line, + }; + + app.workspace.trigger("link-hover", {}, el, path, "", previewLocation); + };