Skip to content

Commit e8834d5

Browse files
committed
Fixed modal dimensions
1 parent b6c3334 commit e8834d5

File tree

2 files changed

+58
-43
lines changed

2 files changed

+58
-43
lines changed

main.ts

+14-26
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ export default class Yesterday extends Plugin {
128128
if (isDreamStart && !this.inDream) {
129129
this.inDream = true;
130130
this.dreamContent = "> [!yesterday-dream] Dream\n";
131-
}
131+
}
132132

133-
if (this.inDream) {
133+
if (this.inDream) {
134134
let textWithoutMarkers = text.replace(/^§§§|§§§$/g, '').trim();
135135
this.dreamContent += `> ${textWithoutMarkers}\n`;
136-
if(!isDreamEnd) {
136+
if (!isDreamEnd) {
137137
this.dreamContent += `> \n`;
138138
this.dreamParagraphsToRemove.push(paragraph);
139139
}
@@ -286,7 +286,7 @@ export default class Yesterday extends Plugin {
286286
document.body.classList.remove('hide-media-files');
287287
}
288288
}
289-
289+
290290
setColorClasses() {
291291
if (this.settings.colorMarkdownFiles) {
292292
document.body.classList.add('color-markdown-files');
@@ -298,7 +298,7 @@ export default class Yesterday extends Plugin {
298298
async saveSettings() {
299299
await this.saveData(this.settings);
300300
}
301-
301+
302302
}
303303

304304
function updateStatusBarTodoCount(barElement: HTMLElement) {
@@ -382,15 +382,15 @@ class YesterdaySettingTab extends PluginSettingTab {
382382
p.createEl('a', { href: 'https://www.yesterday.md', text: 'yesterday.md' });
383383

384384
new Setting(containerEl)
385-
.setName('Color Entries')
386-
.setDesc('Highlights open and resolved entries in different colors')
387-
.addToggle(toggle => toggle
388-
.setValue(this.plugin.settings.colorMarkdownFiles)
389-
.onChange(async (value) => {
390-
this.plugin.settings.colorMarkdownFiles = value;
391-
await this.plugin.saveSettings();
392-
this.plugin.setColorClasses();
393-
}));
385+
.setName('Color Entries')
386+
.setDesc('Highlights open and resolved entries in different colors')
387+
.addToggle(toggle => toggle
388+
.setValue(this.plugin.settings.colorMarkdownFiles)
389+
.onChange(async (value) => {
390+
this.plugin.settings.colorMarkdownFiles = value;
391+
await this.plugin.saveSettings();
392+
this.plugin.setColorClasses();
393+
}));
394394

395395
new Setting(containerEl)
396396
.setName('Hide Media Files')
@@ -402,17 +402,5 @@ class YesterdaySettingTab extends PluginSettingTab {
402402
await this.plugin.saveSettings();
403403
this.plugin.setMediaClasses();
404404
}));
405-
406-
// new Setting(containerEl)
407-
// .setName('DarkSky API Key')
408-
// .setDesc('You can get it from DarkSky')
409-
// .addText(text => text
410-
// .setPlaceholder('Enter your API key')
411-
// .setValue(this.plugin.settings.darkSkyApiKey)
412-
// .onChange(async (value) => {
413-
// console.log('Secret: ' + value);
414-
// this.plugin.settings.darkSkyApiKey = value;
415-
// await this.plugin.saveSettings();
416-
// }));
417405
}
418406
}

media.ts

+44-17
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,53 @@ export class ImageModal extends Modal {
4747
*/
4848
onOpen() {
4949
const { contentEl } = this;
50-
contentEl.createEl('img', {
51-
attr: {
52-
src: this.imgSrc,
53-
style: 'width: 100%; height: 100%; object-fit: contain; object-position: center;'
54-
}
55-
});
5650

57-
Object.assign(this.modalEl.style, {
58-
width: '100%',
59-
height: '100%',
60-
boxShadow: 'none'
51+
// Create an image element
52+
const img = contentEl.createEl('img', {
53+
attr: {
54+
src: this.imgSrc,
55+
// Remove the explicit width and height setting to allow natural size
56+
style: 'max-width: 100%; max-height: 100%; object-fit: contain; object-position: center;'
57+
}
6158
});
6259

60+
// Wait for the image to load to get its natural dimensions
61+
img.onload = () => {
62+
// Calculate the aspect ratio of the image
63+
const aspectRatio = img.naturalWidth / img.naturalHeight;
64+
65+
// Determine the maximum size of the modal based on the window size
66+
const maxWidth = window.innerWidth * 0.9; // 90% of the viewport width
67+
const maxHeight = window.innerHeight * 0.8; // 80% of the viewport height
68+
69+
// Calculate the optimal size of the modal based on the image aspect ratio
70+
let modalWidth, modalHeight;
71+
if (aspectRatio > 1) {
72+
// Image is wider than it is tall
73+
modalWidth = Math.min(maxWidth, img.naturalWidth);
74+
modalHeight = modalWidth / aspectRatio;
75+
} else {
76+
// Image is taller than it is wide or square
77+
modalHeight = Math.min(maxHeight, img.naturalHeight);
78+
modalWidth = modalHeight * aspectRatio;
79+
}
80+
81+
// Apply the calculated dimensions to the modal
82+
Object.assign(this.modalEl.style, {
83+
width: `${modalWidth}px`,
84+
height: `${modalHeight}px`,
85+
boxShadow: 'none'
86+
});
87+
};
88+
89+
// Close the modal on click
6390
this.modalEl.addEventListener('click', () => this.close());
64-
}
91+
}
6592

66-
/**
67-
* Called when the modal is closed. Cleans up by emptying the content element.
68-
*/
69-
onClose() {
70-
this.contentEl.empty();
93+
/**
94+
* Called when the modal is closed. Cleans up by emptying the content element.
95+
*/
96+
onClose() {
97+
this.contentEl.empty();
98+
}
7199
}
72-
}

0 commit comments

Comments
 (0)