Skip to content

Commit 4fb9e58

Browse files
committed
Refactor isLivePreview detection to use built-in editorLivePreviewField
1 parent 7e98635 commit 4fb9e58

1 file changed

Lines changed: 3 additions & 7 deletions

File tree

src/frontmatterImageEditorExtension.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
WidgetType,
1212
} from "@codemirror/view";
1313
import FrontmatterImagePlugin from "./main";
14-
import { Pos, TFile } from "obsidian";
14+
import { Pos, TFile, editorLivePreviewField } from "obsidian";
1515
import { getImageSrc, appendFrontmatterImage } from "./utils";
1616

1717
export interface FrontmatterImageStateFieldValue {
@@ -22,11 +22,6 @@ export interface FrontmatterImageStateFieldValue {
2222
export const frontmatterImageEditorExtension = (
2323
plugin: FrontmatterImagePlugin,
2424
): StateField<FrontmatterImageStateFieldValue | undefined> => {
25-
const isLivePreview = () => {
26-
const view = EditorView.findFromDOM(document.body);
27-
return view?.contentDOM.closest(".is-live-preview") !== null;
28-
};
29-
3025
const buildDecorationSet = (
3126
frontmatterPosition: Pos,
3227
resolvedImageSrc?: string,
@@ -65,7 +60,8 @@ export const frontmatterImageEditorExtension = (
6560
): FrontmatterImageStateFieldValue | undefined => {
6661
if (!oldValue) return;
6762

68-
if (!isLivePreview()) {
63+
const isLivePreview = transaction.state.field(editorLivePreviewField);
64+
if (!isLivePreview) {
6965
return { ...oldValue, decorationSet: undefined };
7066
}
7167

0 commit comments

Comments
 (0)