1- import { Plugin , TAbstractFile , TFile , loadMermaid , loadMathJax , Platform } from 'obsidian' ;
2- import { checkLine , checkLines } from './cm5/checkLine' ;
1+ import { Plugin , TFile , loadMermaid , loadMathJax } from 'obsidian' ;
32import { OzanImagePluginSettingsTab } from './settings' ;
4- import { WYSIWYG_Style } from './cm5/constants' ;
53import { OzanImagePluginSettings , DEFAULT_SETTINGS } from './settings' ;
64import * as ObsidianHelpers from 'src/util/obsidianHelper' ;
75import * as ImageHandler from 'src/util/imageHandler' ;
8- import * as WidgetHandler from 'src/cm5/widgetHandler' ;
9- import { isAnExcalidrawFile , excalidrawPluginIsLoaded } from 'src/util/excalidrawHandler' ;
106import { buildExtension } from 'src/cm6' ;
11- import { getFileCmBelongsTo } from 'src/cm5/cm5Helper' ;
127
138export default class OzanImagePlugin extends Plugin {
149 settings : OzanImagePluginSettings ;
15- loadedStyles : Array < HTMLStyleElement > ;
16- imagePromiseList : Array < string > = [ ] ;
1710
1811 async onload ( ) {
1912 console . log ( 'Image in Editor Plugin is loaded' ) ;
@@ -35,94 +28,19 @@ export default class OzanImagePlugin extends Plugin {
3528 this . registerEditorExtension ( extension ) ;
3629 }
3730
38- // --> Legacy Editor (CM5) - Events only to be loaded on Desktop
39- if ( ! Platform . isMobile ) {
40- this . addCommand ( {
41- id : 'toggle-render-all' ,
42- name : 'Legacy Editor: Toggle Render All' ,
43- callback : ( ) => {
44- this . handleToggleRenderAll ( ! this . settings . renderAll ) ;
45- this . settings . renderAll = ! this . settings . renderAll ;
46- this . saveSettings ( ) ;
47- } ,
48- } ) ;
49-
50- this . addCommand ( {
51- id : 'toggle-WYSIWYG' ,
52- name : 'Legacy Editor: Toggle WYSIWYG' ,
53- callback : ( ) => {
54- this . handleWYSIWYG ( ! this . settings . WYSIWYG ) ;
55- this . settings . WYSIWYG = ! this . settings . WYSIWYG ;
56- this . saveSettings ( ) ;
57- } ,
58- } ) ;
59-
60- this . addCommand ( {
61- id : 'toggle-render-pdf' ,
62- name : 'Legacy Editor: Toggle Render PDF' ,
63- callback : ( ) => {
64- this . settings . renderPDF = ! this . settings . renderPDF ;
65- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
66- this . handleInitialLoad ( cm ) ;
67- } ) ;
68- this . saveSettings ( ) ;
69- } ,
70- } ) ;
71-
72- this . addCommand ( {
73- id : 'toggle-render-iframe' ,
74- name : 'Legacy Editor: Toggle Render Iframe' ,
75- callback : ( ) => {
76- this . settings . renderIframe = ! this . settings . renderIframe ;
77- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
78- this . handleInitialLoad ( cm ) ;
79- } ) ;
80- this . saveSettings ( ) ;
81- } ,
82- } ) ;
83-
84- this . addCommand ( {
85- id : 'toggle-refresh-images-after-changes' ,
86- name : 'Legacy Editor: Toggle Refresh Images After Changes' ,
87- callback : ( ) => {
88- this . handleRefreshImages ( ! this . settings . refreshImagesAfterChange ) ;
89- this . settings . refreshImagesAfterChange = ! this . settings . refreshImagesAfterChange ;
90- this . saveSettings ( ) ;
91- } ,
92- } ) ;
93-
94- document . on ( 'contextmenu' , `div.CodeMirror-linewidget.oz-image-widget > img[data-path]` , this . onImageMenu , false ) ;
95-
96- document . on ( 'click' , `.oz-obsidian-inner-link` , this . onClickTransclusionLink ) ;
97-
98- if ( this . settings . previewOnHoverInternalLink ) {
99- document . on ( 'mouseover' , '.oz-obsidian-inner-link' , this . filePreviewOnHover ) ;
100- }
101-
102- if ( this . settings . WYSIWYG ) this . load_WYSIWYG_Styles ( ) ;
103- if ( ! this . settings . renderAll ) return ;
104- this . registerCodeMirror ( ( cm : CodeMirror . Editor ) => {
105- cm . on ( 'change' , this . codemirrorLineChanges ) ;
106- this . handleInitialLoad ( cm ) ;
107- } ) ;
108- if ( ! this . settings . refreshImagesAfterChange ) return ;
109- this . app . vault . on ( 'modify' , this . handleFileModify ) ;
31+ // --> Custom Event Listeners
32+ document . on ( 'click' , `.oz-obsidian-inner-link` , this . onClickTransclusionLink ) ;
33+ document . on ( 'contextmenu' , `div.oz-image-widget-cm6 img[data-path]` , this . onImageMenu , false ) ;
34+ if ( this . settings . previewOnHoverInternalLink ) {
35+ document . on ( 'mouseover' , '.oz-obsidian-inner-link' , this . filePreviewOnHover ) ;
11036 }
11137 }
11238
11339 onunload ( ) {
114- // Unload CM5 Handlers
115- if ( ! Platform . isMobile ) {
116- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
117- cm . off ( 'change' , this . codemirrorLineChanges ) ;
118- WidgetHandler . clearAllWidgets ( cm ) ;
119- } ) ;
120- this . app . vault . off ( 'modify' , this . handleFileModify ) ;
121- document . off ( 'contextmenu' , `div.CodeMirror-linewidget.oz-image-widget > img[data-path]` , this . onImageMenu , false ) ;
122- document . off ( 'click' , `.oz-obsidian-inner-link` , this . onClickTransclusionLink ) ;
123- document . off ( 'mouseover' , '.oz-obsidian-inner-link' , this . filePreviewOnHover ) ;
124- this . unload_WYSIWYG_Styles ( ) ;
125- }
40+ // --> Unload Event Listeners
41+ document . off ( 'contextmenu' , `div.oz-image-widget-cm6 img[data-path]` , this . onImageMenu , false ) ;
42+ document . off ( 'click' , `.oz-obsidian-inner-link` , this . onClickTransclusionLink ) ;
43+ document . off ( 'mouseover' , '.oz-obsidian-inner-link' , this . filePreviewOnHover ) ;
12644 console . log ( 'Image in Editor Plugin is unloaded' ) ;
12745 }
12846
@@ -153,106 +71,4 @@ export default class OzanImagePlugin extends Plugin {
15371 filePreviewOnHover = ( event : MouseEvent , target : HTMLElement ) => {
15472 this . app . workspace . trigger ( 'link-hover' , { } , event . target , target . getAttr ( 'href' ) , target . getAttr ( 'href' ) ) ;
15573 } ;
156-
157- // Line Edit Changes
158- codemirrorLineChanges = ( cm : any , change : any ) => {
159- checkLines ( cm , change . from . line , change . from . line + change . text . length - 1 , this ) ;
160- } ;
161-
162- // Only Triggered during initial Load
163- handleInitialLoad = ( cm : CodeMirror . Editor ) => {
164- var lastLine = cm . lastLine ( ) ;
165- var file = getFileCmBelongsTo ( cm , this . app . workspace ) ;
166- for ( let i = 0 ; i < lastLine + 1 ; i ++ ) {
167- checkLine ( cm , i , file , this ) ;
168- }
169- } ;
170-
171- // Handle Toggle for renderAll
172- handleToggleRenderAll = ( newRenderAll : boolean ) => {
173- if ( newRenderAll ) {
174- this . registerCodeMirror ( ( cm : CodeMirror . Editor ) => {
175- cm . on ( 'change' , this . codemirrorLineChanges ) ;
176- this . handleInitialLoad ( cm ) ;
177- } ) ;
178- if ( this . settings . refreshImagesAfterChange ) this . app . vault . on ( 'modify' , this . handleFileModify ) ;
179- } else {
180- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
181- cm . off ( 'change' , this . codemirrorLineChanges ) ;
182- WidgetHandler . clearAllWidgets ( cm ) ;
183- } ) ;
184- this . app . vault . off ( 'modify' , this . handleFileModify ) ;
185- }
186- } ;
187-
188- // Handle Transclusion Setting Off
189- handleTransclusionSetting = ( newSetting : boolean ) => {
190- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
191- if ( ! newSetting ) {
192- for ( let i = 0 ; i <= cm . lastLine ( ) ; i ++ ) {
193- let line = cm . lineInfo ( i ) ;
194- WidgetHandler . clearTransclusionWidgets ( line ) ;
195- }
196- } else {
197- checkLines ( cm , 0 , cm . lastLine ( ) , this ) ;
198- }
199- } ) ;
200- } ;
201-
202- // Handle Toggle for Refresh Images Settings
203- handleRefreshImages = ( newRefreshImages : boolean ) => {
204- if ( newRefreshImages ) {
205- this . app . vault . on ( 'modify' , this . handleFileModify ) ;
206- } else {
207- this . app . vault . off ( 'modify' , this . handleFileModify ) ;
208- }
209- } ;
210-
211- // Handle File Changes to Refhres Images
212- handleFileModify = ( file : TAbstractFile ) => {
213- if ( ! ( file instanceof TFile ) ) return ;
214- if ( ImageHandler . pathIsAnImage ( file . path ) || ( excalidrawPluginIsLoaded ( this . app ) && isAnExcalidrawFile ( file ) ) ) {
215- this . app . workspace . iterateCodeMirrors ( ( cm ) => {
216- var lastLine = cm . lastLine ( ) ;
217- checkLines ( cm , 0 , lastLine , this , file . path ) ;
218- } ) ;
219- }
220- } ;
221-
222- // Handle WYSIWYG Toggle
223- handleWYSIWYG = ( newWYSIWYG : boolean ) => {
224- if ( newWYSIWYG ) {
225- this . load_WYSIWYG_Styles ( ) ;
226- } else {
227- this . unload_WYSIWYG_Styles ( ) ;
228- }
229- } ;
230-
231- load_WYSIWYG_Styles = ( ) => {
232- this . loadedStyles = Array < HTMLStyleElement > ( 0 ) ;
233- var style = document . createElement ( 'style' ) ;
234- style . innerHTML = WYSIWYG_Style ;
235- document . head . appendChild ( style ) ;
236- this . loadedStyles . push ( style ) ;
237- } ;
238-
239- unload_WYSIWYG_Styles = ( ) => {
240- if ( ! this . loadedStyles || typeof this . loadedStyles [ Symbol . iterator ] !== 'function' ) return ;
241- for ( let style of this . loadedStyles ) {
242- document . head . removeChild ( style ) ;
243- }
244- this . loadedStyles = Array < HTMLStyleElement > ( 0 ) ;
245- } ;
246-
247- addToImagePromiseList = ( path : string ) => {
248- if ( ! this . imagePromiseList . contains ( path ) ) {
249- this . imagePromiseList . push ( path ) ;
250- }
251- } ;
252-
253- removeFromImagePromiseList = ( path : string ) => {
254- if ( this . imagePromiseList . contains ( path ) ) {
255- this . imagePromiseList = this . imagePromiseList . filter ( ( crPath ) => crPath !== path ) ;
256- }
257- } ;
25874}
0 commit comments