@@ -12,19 +12,13 @@ import {
1212} from "@codemirror/view" ;
1313import FrontmatterImagePlugin from "./main" ;
1414import { Pos , TFile } from "obsidian" ;
15+ import { getImageSrc , renderFrontmatterImage } from "./utils" ;
1516
1617export interface FrontmatterImageStateFieldValue {
1718 readonly activeFile : TFile ;
1819 decorationSet ?: DecorationSet ;
1920}
2021
21- export const renderFrontmatterImage = ( src : string ) : HTMLElement => {
22- const img = document . createElement ( "img" ) ;
23- img . src = src ;
24- img . classList . add ( "frontmatter-image" ) ;
25- return img ;
26- }
27-
2822export const frontmatterImageEditorExtension = (
2923 plugin : FrontmatterImagePlugin ,
3024) : StateField < FrontmatterImageStateFieldValue | undefined > => {
@@ -35,9 +29,9 @@ export const frontmatterImageEditorExtension = (
3529
3630 const buildDecorationSet = (
3731 frontmatterPosition : Pos ,
38- currentImageValue ?: string ,
32+ resolvedImageSrc ?: string ,
3933 ) => {
40- if ( ! currentImageValue ) return Decoration . none ;
34+ if ( ! resolvedImageSrc ) return Decoration . none ;
4135
4236 const builder = new RangeSetBuilder < Decoration > ( ) ;
4337 builder . add (
@@ -46,7 +40,7 @@ export const frontmatterImageEditorExtension = (
4640 Decoration . widget ( {
4741 widget : new ( class extends WidgetType {
4842 toDOM ( view : EditorView ) : HTMLElement {
49- return renderFrontmatterImage ( currentImageValue ) ;
43+ return renderFrontmatterImage ( resolvedImageSrc ) ;
5044 }
5145 } ) ( ) ,
5246 } ) ,
@@ -69,29 +63,27 @@ export const frontmatterImageEditorExtension = (
6963 oldValue : FrontmatterImageStateFieldValue | undefined ,
7064 transaction : Transaction ,
7165 ) : FrontmatterImageStateFieldValue | undefined => {
72- if ( ! oldValue ) return ;
66+ if ( ! oldValue ) return ;
7367
7468 if ( ! isLivePreview ( ) ) {
75- return { ...oldValue , decorationSet : undefined } ;
69+ return { ...oldValue , decorationSet : undefined } ;
7670 }
7771
7872 const cachedMetadata = plugin . app . metadataCache . getFileCache ( oldValue . activeFile ) ;
79- const frontmatterCache = cachedMetadata ?. frontmatter ;
8073 const frontmatterPosition = cachedMetadata ?. frontmatterPosition ;
81- if ( ! frontmatterCache || ! frontmatterPosition ) {
82- return { ...oldValue , decorationSet : undefined } ;
74+ if ( ! frontmatterPosition ) {
75+ return { ...oldValue , decorationSet : undefined } ;
8376 }
8477
85- const currentImageKey = plugin . settings . imageKeys . find ( key => frontmatterCache [ key ] ) ;
86- const currentImageValue = currentImageKey && frontmatterCache [ currentImageKey ] ;
78+ const imageSrc = getImageSrc ( oldValue . activeFile . path , plugin ) ;
8779
88- return {
89- ...oldValue ,
90- decorationSet : buildDecorationSet (
91- frontmatterPosition ,
92- currentImageValue ,
93- )
94- } ;
80+ return {
81+ ...oldValue ,
82+ decorationSet : buildDecorationSet (
83+ frontmatterPosition ,
84+ imageSrc ,
85+ )
86+ } ;
9587 } ,
9688 provide : (
9789 field : StateField < FrontmatterImageStateFieldValue | undefined > ,
0 commit comments