Skip to content

Commit 56f1930

Browse files
author
Ozan Tellioglu
committed
Improve Load Unload Extension Mechanism
1 parent 50c3d26 commit 56f1930

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ FORMAT: ![[IMAGE_PATH_OR_NAME]]
3131
SAMPLE: ![[myimage.png]]
3232
```
3333

34-
**Scanned Image Formats** : `jpg`, `jpeg`, `png`, `gif`, `svg`, `bmp`
34+
**Scanned Image Formats** : `jpg`, `jpeg`, `png`, `gif`, `svg`, `bmp`, `webp`
3535

3636
## 📕 Image View Size
3737

src/cm6/viewPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { EditorView, ViewPlugin, ViewUpdate } from '@codemirror/view';
33
import OzanImagePlugin from 'src/main';
44
import { StatefulDecorationSet } from 'src/cm6/decorations';
55
import { livePreviewActive, pluginIsLoaded } from 'src/util/obsidianHelper';
6-
import { MSGHandlerPlugin } from 'src/types';
76

87
// --> View Plugin
98
export const getViewPlugin = (params: { plugin: OzanImagePlugin }): Extension => {

src/main.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { OzanImagePluginSettings, DEFAULT_SETTINGS } from './settings';
44
import * as ObsidianHelpers from 'src/util/obsidianHelper';
55
import * as ImageHandler from 'src/util/imageHandler';
66
import { buildExtension } from 'src/cm6';
7+
import { Extension } from '@codemirror/state';
78

89
export default class OzanImagePlugin extends Plugin {
910
settings: OzanImagePluginSettings;
11+
editorExtensions: Extension[] = [];
1012

1113
async onload() {
1214
console.log('Image in Editor Plugin is loaded');
@@ -77,10 +79,8 @@ export default class OzanImagePlugin extends Plugin {
7779
}
7880

7981
// --> New Editor (CM6)
80-
if (this.settings.cm6RenderAll) {
81-
const extension = buildExtension({ plugin: this });
82-
this.registerEditorExtension(extension);
83-
}
82+
this.registerEditorExtension(this.editorExtensions);
83+
if (this.settings.cm6RenderAll) this.loadCM6Extension();
8484

8585
// --> Custom Event Listeners
8686
document.on('click', `.oz-obsidian-inner-link`, this.onClickTransclusionLink);
@@ -106,6 +106,17 @@ export default class OzanImagePlugin extends Plugin {
106106
await this.saveData(this.settings);
107107
}
108108

109+
loadCM6Extension = () => {
110+
const extension = buildExtension({ plugin: this });
111+
this.editorExtensions.push(extension);
112+
this.app.workspace.updateOptions();
113+
};
114+
115+
unloadCM6Extension = () => {
116+
this.editorExtensions.length = 0;
117+
this.app.workspace.updateOptions();
118+
};
119+
109120
// Context Menu for Rendered Images
110121
onImageMenu = (event: MouseEvent, target: HTMLElement) => {
111122
const file = this.app.vault.getAbstractFileByPath(target.dataset.path);

src/settings.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ export class OzanImagePluginSettingsTab extends PluginSettingTab {
8080
.addToggle((toggle) => {
8181
toggle.setValue(this.plugin.settings.cm6RenderAll).onChange((value) => {
8282
this.plugin.settings.cm6RenderAll = value;
83+
if (value) {
84+
this.plugin.loadCM6Extension();
85+
} else {
86+
this.plugin.unloadCM6Extension();
87+
}
8388
this.plugin.saveSettings();
8489
});
8590
});
8691

8792
new Setting(containerEl)
8893
.setName('Render Images in Editor')
89-
.setDesc('Turn on this option if you want Image files (jpeg, jpg, png, gif, svg, bmp) to be rendered in Editor')
94+
.setDesc('Turn on this option if you want Image files (jpeg, jpg, png, gif, svg, bmp, webp) to be rendered in Editor')
9095
.addToggle((toggle) =>
9196
toggle.setValue(this.plugin.settings.renderImages).onChange((value) => {
9297
this.plugin.settings.renderImages = value;

0 commit comments

Comments
 (0)