Skip to content

Commit

Permalink
Add cwd detection for canvases
Browse files Browse the repository at this point in the history
Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Sep 12, 2023
1 parent 1dcf8bd commit fdbee2f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-gorillas-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"obsidian-modules": minor
---

Add cwd detection for canvases.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Contributions are welcome!
The todos here, ordered alphabetically, are things planned for the plugin. There are no guarantees that they will be completed. However, we are likely to accept contributions for them.

- Add startup modules.
- Add context detection for `dv.view` and canvases.
- Add context detection for `dv.view`.
- User-defined module aliases.
- Add bare module transformation support for more CDNs such as <https://cdn.jsdelivr.net>.
- Faster import analysis and transformation.
Expand Down
33 changes: 31 additions & 2 deletions sources/@types/obsidian.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
declare module "obsidian" {
interface Canvas extends Private<$Canvas, PrivateKey> { }
interface CanvasNode extends Private<$CanvasNode, PrivateKey> { }
interface CanvasNodeInfo extends Private<$CanvasNodeInfo, PrivateKey> { }
interface MarkdownEmbedInfo
extends Private<$MarkdownEmbedInfo, PrivateKey> { }
interface MarkdownFileInfo extends Private<unknown, PrivateKey> { }
interface MarkdownPreviewRenderer
extends Private<$MarkdownPreviewRenderer, PrivateKey> { }
namespace Plugins {
Expand All @@ -9,7 +15,14 @@ declare module "obsidian" {
}
}
}
import type { MarkdownFileInfo } from "obsidian"
import type {
Canvas,
CanvasNode,
CanvasNodeInfo,
FileView,
MarkdownEmbedInfo,
MarkdownFileInfo,
} from "obsidian"
import type { Private } from "@polyipseity/obsidian-plugin-library"
import type { TemplaterPlugin } from "templater-obsidian"

Expand All @@ -21,7 +34,23 @@ declare module "@polyipseity/obsidian-plugin-library" {
}
}

interface $Canvas {
readonly view: FileView
}

interface $CanvasNode {
readonly canvas: Canvas
}

interface $CanvasNodeInfo extends MarkdownFileInfo {
readonly node: CanvasNode
}

interface $MarkdownEmbedInfo extends MarkdownFileInfo {
readonly owner: CanvasNodeInfo | MarkdownFileInfo
}

interface $MarkdownPreviewRenderer {
readonly owner: MarkdownFileInfo
readonly owner: MarkdownEmbedInfo | MarkdownFileInfo
readonly onRender: () => void
}
38 changes: 29 additions & 9 deletions sources/require/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
type CanvasNodeInfo,
type MarkdownFileInfo,
MarkdownPreviewRenderer,
editorInfoField,
} from "obsidian"
import { constant, noop } from "lodash-es"
import {
patchPlugin,
revealPrivate,
Expand All @@ -11,7 +13,6 @@ import { EditorView } from "@codemirror/view"
import type { ModulesPlugin } from "../main.js"
import type { StateField } from "@codemirror/state"
import { around } from "monkey-around"
import { noop } from "lodash-es"

export function patchContextForEditor(context: ModulesPlugin): void {
context.register(around(EditorView.prototype, {
Expand All @@ -21,12 +22,23 @@ export function patchContextForEditor(context: ModulesPlugin): void {
...args: Parameters<typeof next>
): ReturnType<typeof next> {
const { api: { requires } } = context,
req = requires.get(self)
req?.context.cwds.push(this.state.field(
// Typing bug
editorInfoField as StateField<MarkdownFileInfo>,
false,
)?.file?.parent?.path ?? null)
req = requires.get(self),
info = this.state.field(
// Typing bug
editorInfoField as StateField<MarkdownFileInfo>,
false,
)
let path = info?.file?.parent?.path
if (path === void 0 && info) {
const info2 = info as CanvasNodeInfo | typeof info
path = revealPrivate(context, [info2], info3 => {
if ("node" in info3) {
return info3.node.canvas.view.file.parent?.path
}
return path
}, constant(path))
}
req?.context.cwds.push(path ?? null)
try {
next.apply(this, args)
} finally {
Expand All @@ -47,8 +59,16 @@ export function patchContextForPreview(context: ModulesPlugin): void {
...args: Parameters<typeof next>
): ReturnType<typeof next> {
const { api: { requires } } = context,
req = requires.get(self)
req?.context.cwds.push(this.owner.file?.parent?.path ?? null)
req = requires.get(self),
{ owner } = this
let path = owner.file?.parent?.path
if (path === void 0 && "owner" in owner) {
const { owner: owner2 } = owner
if ("node" in owner2) {
path = owner2.node.canvas.view.file.parent?.path
}
}
req?.context.cwds.push(path ?? null)
try {
next.apply(this, args)
} finally {
Expand Down

0 comments on commit fdbee2f

Please sign in to comment.