forked from FHachez/obsidian-convert-url-to-iframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure_iframe_modal.ts
130 lines (104 loc) · 3.81 KB
/
configure_iframe_modal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { App, Modal } from 'obsidian';
const defaultHeightValue = 'calc(var(--width) * (9/16))';
export class ConfigureIframeModal extends Modal {
url: string;
sucess: boolean;
generatedIframe: string;
editor: any;
constructor(app: App, url: string, editor: any) {
super(app);
this.url = url;
this.editor = editor;
}
onOpen() {
let { contentEl } = this;
const container = contentEl.createEl('div');
container.className = 'iframe__container';
const title = contentEl.createEl('h2');
title.innerText = "This is how the iframe is going to look (your can choose the size)";
const iframeContainer = createIframeContainerEl(contentEl, this.url);
const widthCheckbox = createShouldUseDefaultWidthCheckbox(iframeContainer);
const heightInput = createHeightInput(iframeContainer);
const okButton = contentEl.createEl('button');
okButton.setText('OK');
okButton.className = 'mod-warning';
okButton.onclick = (e) => {
e.preventDefault();
const generatedIframe = iframeContainer.outerHTML;
this.editor.replaceSelection(generatedIframe);
this.close();
};
const cancelButton = contentEl.createEl('button');
cancelButton.setText('Cancel');
cancelButton.onclick = (e) => {
e.preventDefault();
this.close();
};
const buttonContainer = contentEl.createEl('div');
buttonContainer.className = 'button__container';
buttonContainer.appendChild(okButton);
buttonContainer.appendChild(cancelButton);
container.appendChild(title);
container.appendChild(widthCheckbox);
container.appendChild(heightInput);
container.appendChild(iframeContainer);
container.appendChild(buttonContainer);
contentEl.appendChild(container);
}
onClose() {
let { contentEl } = this;
contentEl.empty();
}
}
export function createIframeContainerEl(contentEl: HTMLElement, url: string): HTMLElement {
const iframeContainer = contentEl.createEl('div');
iframeContainer.style.setProperty('--width', '100%');
iframeContainer.style.position = 'relative';
iframeContainer.style.width = '100%';
iframeContainer.style.paddingBottom = defaultHeightValue;
// Overflow cannot be set to "visible" (default) when using resize
iframeContainer.style.overflow = 'auto';
iframeContainer.style.resize = 'vertical';
const iframe = iframeContainer.createEl('iframe');
iframe.src = url;
iframe.style.position = 'absolute';
iframe.style.height = '100%';
iframe.style.width = '100%';
return iframeContainer;
}
export function createShouldUseDefaultWidthCheckbox(iframeContainer: HTMLElement): HTMLDivElement {
const name = "shouldUseDefaultWidth";
const checkboxContainer = iframeContainer.createEl('div');
const checkbox = checkboxContainer.createEl('input');
checkbox.type = 'checkbox';
checkbox.name = name;
checkbox.checked = true;
const label = checkboxContainer.createEl('label');
label.setAttribute('for', name);
label.innerText = 'Do you want to fix the iframe width to the note width?';
checkbox.onclick = (e) => {
if (checkbox.checked) {
iframeContainer.style.resize = 'vertical';
iframeContainer.style.width = '100%';
} else {
iframeContainer.style.resize = 'both';
iframeContainer.style.width = '';
}
}
return checkboxContainer;
}
export function createHeightInput(iframeContainer: HTMLElement): HTMLDivElement {
const heightInputName = "heightValue";
const heightInputContainer = iframeContainer.createEl('div');
const heightInputLabel = heightInputContainer.createEl('label');
heightInputLabel.setAttribute('for', heightInputName);
heightInputLabel.innerText = 'Minimum height (can be resized larger):';
const heightInput = heightInputContainer.createEl('input');
heightInput.type = 'text';
heightInput.name = heightInputName;
heightInput.value = defaultHeightValue;
heightInput.oninput = (e) => {
iframeContainer.style.paddingBottom = heightInput.value;
}
return heightInputContainer;
}