Skip to content

Commit 05be1ef

Browse files
author
Ozan Tellioglu
committed
Codemirror 5 Removed from Plugin
1 parent d77fcd0 commit 05be1ef

File tree

5 files changed

+135
-275
lines changed

5 files changed

+135
-275
lines changed

src/cm5/checkLine.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'prismjs/components/prism-visual-basic.min';
2020
import 'prismjs/components/prism-json.min';
2121
import { PollUntil } from 'poll-until-promise';
2222
import { getFileCmBelongsTo } from 'src/cm5/cm5Helper';
23+
import { addToImagePromiseList, imagePromiseList, removeFromImagePromiseList } from 'src/cm5/removedFunctions';
2324

2425
// Check Single Line
2526
export const checkLine: any = async (cm: CodeMirror.Editor, lineNumber: number, targetFile: TFile, plugin: OzanImagePlugin, changedFilePath?: string) => {
@@ -105,8 +106,8 @@ export const checkLine: any = async (cm: CodeMirror.Editor, lineNumber: number,
105106

106107
if (lineFile && ExcalidrawHandler.excalidrawPluginIsLoaded(plugin.app) && ExcalidrawHandler.isAnExcalidrawFile(lineFile)) {
107108
// The file is an excalidraw drawing
108-
if (plugin.imagePromiseList.contains(lineFile.path)) return;
109-
plugin.addToImagePromiseList(lineFile.path);
109+
if (imagePromiseList.contains(lineFile.path)) return;
110+
addToImagePromiseList(plugin, lineFile.path);
110111
let excalidrawImage = await ExcalidrawHandler.createPNGFromExcalidrawFile(lineFile);
111112

112113
// Check if Object or Alt Changed
@@ -117,7 +118,7 @@ export const checkLine: any = async (cm: CodeMirror.Editor, lineNumber: number,
117118

118119
if (existingBlop.size === excalidrawImage.size && currentImageNode.alt === alt) {
119120
// Drawing hasn't changed
120-
plugin.removeFromImagePromiseList(lineFile.path);
121+
removeFromImagePromiseList(plugin, lineFile.path);
121122
return;
122123
}
123124
}
@@ -138,7 +139,7 @@ export const checkLine: any = async (cm: CodeMirror.Editor, lineNumber: number,
138139

139140
// Add Image widget under the Image Markdown
140141
cm.addLineWidget(lineNumber, img, { className: 'oz-image-widget', showIfHidden: false });
141-
plugin.removeFromImagePromiseList(lineFile.path);
142+
removeFromImagePromiseList(plugin, lineFile.path);
142143
return;
143144
}
144145
}

src/cm5/removedFunctions.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// ts-nocheck
2+
3+
import { WYSIWYG_Style } from 'src/cm5/constants';
4+
import OzanImagePlugin from 'src/main';
5+
import * as WidgetHandler from 'src/cm5/widgetHandler';
6+
import { checkLine, checkLines } from 'src/cm5/checkLine';
7+
import { getFileCmBelongsTo } from 'src/cm5/cm5Helper';
8+
import { isAnExcalidrawFile, excalidrawPluginIsLoaded } from 'src/util/excalidrawHandler';
9+
import { TAbstractFile, TFile } from 'obsidian';
10+
import * as ImageHandler from 'src/util/imageHandler';
11+
import CodeMirror from 'codemirror';
12+
13+
// --> Removed Plugin Constants
14+
let loadedStyles: Array<HTMLStyleElement>;
15+
let imagePromiseList: Array<string> = [];
16+
17+
const load_WYSIWYG_Styles = (plugin: any) => {
18+
plugin.loadedStyles = Array<HTMLStyleElement>(0);
19+
var style = document.createElement('style');
20+
style.innerHTML = WYSIWYG_Style;
21+
document.head.appendChild(style);
22+
plugin.loadedStyles.push(style);
23+
};
24+
25+
const unload_WYSIWYG_Styles = (plugin: any) => {
26+
if (!plugin.loadedStyles || typeof plugin.loadedStyles[Symbol.iterator] !== 'function') return;
27+
for (let style of plugin.loadedStyles) {
28+
document.head.removeChild(style);
29+
}
30+
plugin.loadedStyles = Array<HTMLStyleElement>(0);
31+
};
32+
33+
const handleWYSIWYG = (plugin: OzanImagePlugin, newWYSIWYG: boolean) => {
34+
if (newWYSIWYG) {
35+
load_WYSIWYG_Styles(plugin);
36+
} else {
37+
unload_WYSIWYG_Styles(plugin);
38+
}
39+
};
40+
41+
const handleToggleRenderAll = (plugin: any, newRenderAll: boolean) => {
42+
if (newRenderAll) {
43+
plugin.registerCodeMirror((cm: CodeMirror.Editor) => {
44+
cm.on('change', plugin.codemirrorLineChanges);
45+
plugin.handleInitialLoad(cm);
46+
});
47+
if (plugin.settings.refreshImagesAfterChange) plugin.app.vault.on('modify', plugin.handleFileModify);
48+
} else {
49+
plugin.app.workspace.iterateCodeMirrors((cm: CodeMirror.Editor) => {
50+
cm.off('change', plugin.codemirrorLineChanges);
51+
WidgetHandler.clearAllWidgets(cm);
52+
});
53+
plugin.app.vault.off('modify', plugin.handleFileModify);
54+
}
55+
};
56+
57+
// Handle Transclusion Setting Off
58+
const handleTransclusionSetting = (plugin: OzanImagePlugin, newSetting: boolean) => {
59+
plugin.app.workspace.iterateCodeMirrors((cm) => {
60+
if (!newSetting) {
61+
for (let i = 0; i <= cm.lastLine(); i++) {
62+
let line = cm.lineInfo(i);
63+
WidgetHandler.clearTransclusionWidgets(line);
64+
}
65+
} else {
66+
checkLines(cm, 0, cm.lastLine(), this);
67+
}
68+
});
69+
};
70+
71+
// Line Edit Changes
72+
const codemirrorLineChanges = (cm: any, change: any) => {
73+
checkLines(cm, change.from.line, change.from.line + change.text.length - 1, this);
74+
};
75+
76+
// Only Triggered during initial Load
77+
const handleInitialLoad = (plugin: OzanImagePlugin, cm: CodeMirror.Editor) => {
78+
var lastLine = cm.lastLine();
79+
var file = getFileCmBelongsTo(cm, plugin.app.workspace);
80+
for (let i = 0; i < lastLine + 1; i++) {
81+
checkLine(cm, i, file, this);
82+
}
83+
};
84+
85+
// Handle Toggle for Refresh Images Settings
86+
const handleRefreshImages = (plugin: any, newRefreshImages: boolean) => {
87+
if (newRefreshImages) {
88+
plugin.app.vault.on('modify', plugin.handleFileModify);
89+
} else {
90+
plugin.app.vault.off('modify', plugin.handleFileModify);
91+
}
92+
};
93+
94+
// Handle File Changes to Refhres Images
95+
const handleFileModify = (plugin: OzanImagePlugin, file: TAbstractFile) => {
96+
if (!(file instanceof TFile)) return;
97+
if (ImageHandler.pathIsAnImage(file.path) || (excalidrawPluginIsLoaded(plugin.app) && isAnExcalidrawFile(file))) {
98+
plugin.app.workspace.iterateCodeMirrors((cm) => {
99+
var lastLine = cm.lastLine();
100+
checkLines(cm, 0, lastLine, this, file.path);
101+
});
102+
}
103+
};
104+
105+
const addToImagePromiseList = (plugin: any, path: string) => {
106+
if (!plugin.imagePromiseList.contains(path)) {
107+
plugin.imagePromiseList.push(path);
108+
}
109+
};
110+
111+
const removeFromImagePromiseList = (plugin: any, path: string) => {
112+
if (plugin.imagePromiseList.contains(path)) {
113+
plugin.imagePromiseList = plugin.imagePromiseList.filter((crPath: any) => crPath !== path);
114+
}
115+
};
116+
117+
export { addToImagePromiseList, removeFromImagePromiseList, imagePromiseList };

src/cm6/widget.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ImageWidget extends WidgetType {
5757
container.addClass('oz-image-widget-cm6');
5858
const image = container.createEl('img');
5959
image.src = this.url;
60+
image.setAttr('data-path', this.filePath);
6061
let altSizer = ImageHandler.altWidthHeight(this.altText);
6162
if (altSizer) {
6263
image.width = altSizer.width;

src/main.ts

Lines changed: 10 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
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';
32
import { OzanImagePluginSettingsTab } from './settings';
4-
import { WYSIWYG_Style } from './cm5/constants';
53
import { OzanImagePluginSettings, DEFAULT_SETTINGS } from './settings';
64
import * as ObsidianHelpers from 'src/util/obsidianHelper';
75
import * as ImageHandler from 'src/util/imageHandler';
8-
import * as WidgetHandler from 'src/cm5/widgetHandler';
9-
import { isAnExcalidrawFile, excalidrawPluginIsLoaded } from 'src/util/excalidrawHandler';
106
import { buildExtension } from 'src/cm6';
11-
import { getFileCmBelongsTo } from 'src/cm5/cm5Helper';
127

138
export 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

Comments
 (0)