-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert.idjs
108 lines (85 loc) · 2.41 KB
/
alert.idjs
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
await alert("Alert message.", "warn");
await alert("Info message.", "info");
await alert("Unglaublich: Mit munterer Müdigkeit mehrfach gestrandet, eilen kabellose Kapuziner kantigen Kaufmannschaften hinterher. Greift da etwa die öde Haubitze nach Keimzellen kläglicher Katastralgemeinden.");
async function alert(msg, type) {
if(!msg || msg.constructor !== String) {
msg = "";
}
if(!type || type.constructor !== String) {
type = "";
}
const app = global.app ?? require("indesign").app;
let dialogElem = document.getElementById("rd-alert");
if(!dialogElem) {
dialogElem = createAlertDialog();
}
let backgroundColor;
switch(type.toLowerCase()) {
case "warn":
backgroundColor = 'lightcoral';
break;
case "info":
backgroundColor = 'lightgreen ';
break;
default:
backgroundColor = 'initial';
}
dialogElem.style.backgroundColor = backgroundColor;
if(backgroundColor === 'initial'){
if(app.generalPreferences.uiBrightnessPreference <= 0.5) {
dialogElem.style.color = "white";
} else {
dialogElem.style.color = "black";
}
}
let msgElem = dialogElem.querySelector('#rd-alert-message');
if(!!msgElem) {
msgElem.textContent = msg;
}
await dialogElem.showModal();
return dialogElem;
}
/**
* Create dialog
* @returns {Element}
*/
function createAlertDialog() {
const DIALOG_ID = "rd-alert";
/* Namespace für styles hinzufügen rd- */
const styles = `
dialog#${DIALOG_ID} {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 80px;
max-height: 800px;
min-width: 200px;
max-width: 400px;
padding: 30px 40px 20px 40px;
color: inherit;
}
p#${DIALOG_ID}-message {
margin-bottom: 2rem;
font-size: 1rem;
}
`;
const template = `
<p id="${DIALOG_ID}-message"></p>
<sp-button id="${DIALOG_ID}-close-button">OK</sp-button>
`;
const styleElem = document.createElement('style');
const stylesTextNode = document.createTextNode(styles);
styleElem.appendChild(stylesTextNode);
const headElem = document.querySelector('head');
headElem.appendChild(styleElem);
const dialogElem = document.createElement('dialog');
dialogElem.setAttribute('id', DIALOG_ID);
dialogElem.innerHTML = template;
document.body.appendChild(dialogElem);
const buttonElem = dialogElem.querySelector(`#${DIALOG_ID}-close-button`);
buttonElem.addEventListener('click', evt => {
dialogElem.close();
});
return dialogElem;
}