mirror of
https://github.com/maradotwebp/obsidian-avatar.git
synced 2026-07-22 07:30:24 +00:00
fix issue with view error in certain modes
This commit is contained in:
parent
fb75185a89
commit
cb53cace40
2 changed files with 24 additions and 7 deletions
|
|
@ -2,8 +2,7 @@
|
|||
import {
|
||||
App,
|
||||
MarkdownPostProcessorContext,
|
||||
MarkdownRenderer,
|
||||
MarkdownView,
|
||||
MarkdownRenderer, MarkdownView,
|
||||
TFile
|
||||
} from "obsidian";
|
||||
import Fab from "./Fab.svelte";
|
||||
|
|
@ -11,6 +10,7 @@
|
|||
import type {SetState, State} from "../core/stateProviders";
|
||||
import {SelectImageModal} from "./SelectImageModal";
|
||||
import {AvatarPlugin} from "../plugin";
|
||||
import {onMount} from "svelte";
|
||||
|
||||
interface AvatarViewState {
|
||||
image: string;
|
||||
|
|
@ -29,12 +29,20 @@
|
|||
let descriptionEditEl: HTMLElement;
|
||||
let descriptionPreviewEl: HTMLElement;
|
||||
|
||||
$: inSourceMode = app ? isSourceMode(app) : false;
|
||||
let inSourceMode = false;
|
||||
$: fallbackImage = `https://ui-avatars.com/api/?name=${ctx?.sourcePath.split("/").at(-1) ?? "::"}&size=240`;
|
||||
|
||||
function isSourceMode(app: App) {
|
||||
const view = app.workspace.getMostRecentLeaf()?.view as MarkdownView;
|
||||
return view?.getMode() === "source";
|
||||
onMount(() => {
|
||||
inSourceMode = isSourceMode();
|
||||
let x = app.workspace.on("layout-change", () => {
|
||||
inSourceMode = isSourceMode();
|
||||
});
|
||||
return () => app.workspace.offref(x);
|
||||
});
|
||||
|
||||
function isSourceMode() {
|
||||
const view = app.workspace.getActiveViewOfType(MarkdownView);
|
||||
return view?.getMode?.() === "source";
|
||||
}
|
||||
|
||||
function enterEditMode() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import type {App, MarkdownPostProcessorContext, Plugin} from "obsidian";
|
||||
import type {SvelteComponent} from "svelte";
|
||||
import type {StateProvider} from "./stateProviders";
|
||||
import {MarkdownRenderChild} from "obsidian";
|
||||
|
||||
export interface CodeBlockProcessorProps extends Record<string, any> {
|
||||
app: App;
|
||||
|
|
@ -20,7 +21,7 @@ export function renderCodeBlockProcessor<S>(
|
|||
) {
|
||||
return (source: string, containerEl: HTMLElement, ctx: MarkdownPostProcessorContext) => {
|
||||
const node = containerEl.createEl("div");
|
||||
new component({
|
||||
const svelteComponent = new component({
|
||||
target: containerEl,
|
||||
props: {
|
||||
...props,
|
||||
|
|
@ -28,5 +29,13 @@ export function renderCodeBlockProcessor<S>(
|
|||
ctx
|
||||
}
|
||||
});
|
||||
|
||||
class UnloadSvelteComponent extends MarkdownRenderChild {
|
||||
onunload() {
|
||||
svelteComponent.$destroy();
|
||||
}
|
||||
}
|
||||
|
||||
ctx.addChild(new UnloadSvelteComponent(node));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue