-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdialogs.js
320 lines (300 loc) · 10.9 KB
/
dialogs.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2018 Adobe Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This file was forked from https://github.com/AdobeXD/plugin-samples
/**
* Converts a string (or an array of strings or other objects) to a nicer HTML
* representation. Essentially this is a _very_ basic markdown parser.
*
* The following tokens are understood, when encountered at the beginning of
* a string:
*
* Token | Result
* -------------|-----------------------
* `##` | `<h2>`
* `###` | `<h3>`
* `* ` | Bulleted list
* `----` | `<hr class="small">`
* `---` | `<hr />`
* `[...](href)`| `<p><a href="href">...</a></p>`
*
* @param {string | string[] | * | Array<*>} str
* @returns {string} the HTML representation
*/
function strToHtml(str) {
// allow some common overloads, including arrays and non-strings
if (Array.isArray(str)) {
return str.map(str => strToHtml(str)).join('');
}
if (typeof str !== 'string') {
return strToHtml(`${str}`);
}
let html = str;
// handle some markdown stuff
if (html.substr(0, 2) === '##') {
html = `<h3>${html.substr(2).trim().toUpperCase()}</h3>`;
} else if (html.substr(0, 1) === '#') {
html = `<h2>${html.substr(1).trim()}</h2>`;
} else if (html.substr(0, 2) === '* ') {
html = `<p class="list"><span class="bullet margin">•</span><span class="margin">${html.substr(2).trim()}</span></p>`;
} else if (html.substr(0, 4) === '----') {
html = `<hr class="small"/>${html.substr(5).trim()}`;
} else if (html.substr(0, 3) === '---') {
html = `<hr/>${html.substr(4).trim()}`;
} else {
html = `<p>${html.trim()}</p>`;
}
// handle links -- the catch here is that the link will transform the entire paragraph!
const regex = /\[([^\]]*)\]\(([^\)]*)\)/;
const matches = str.match(regex);
if (matches) {
const title = matches[1];
const url = matches[2];
html = `<p><a href="${url}">${html.replace(regex, title).replace(/\<\|?p\>/g, '')}</a></p>`;
}
return html;
}
/*
* Generates a "notice" dialog with the title, default icon, and a series of messages.
*
* @param {*} param
* @property {string} param.title The dialog title
* @property {string} [param.icon] The dialog icon to use. If not provided, no icon will be rendered
* @property {string[]} param.msgs The messages to render. If a message starts with `http`, it will be rendered as a link.
* @property {string} [param.prompt] If specified, will render as a prompt with a single input field and the prompt as a placeholder
* @property {boolean} [param.multiline=false] If `true`, the prompt will render as a multi-line text field.
* @property {boolean} [param.isError=false] If specified, will render the header in a red color
* @property {string} param.styles A <style> block that will be prepended to the dialog's .container
* @property {Function} [param.render] If set, the results of this function (a DOM tree) will be appended into the content area of the dialog.
* @property {Function<String>} [param.template] If set, the results of this function (a string) will be appended into the content area of the dialog.
* @property {Object[]} [buttons] Indicates the buttons to render. If none are specified, a `Close` button is rendered.
* @returns {Promise} Resolves to an object of the form {which, value}. `value` only makes sense if `prompt` is set. `which` indicates which button was pressed.
*/
async function createDialog({
title,
icon = 'plugin-icon',
msgs,
prompt,
multiline = false,
styles,
render,
template,
isError = false,
buttons = [
{ label: 'Close', variant: 'cta', type: 'submit' }
] } = {},
width = 360,
height = 'auto',
iconSize = 18
) {
let messages = Array.isArray(msgs) ? msgs : [msgs];
const dialog = document.createElement('dialog');
dialog.innerHTML = `
<style>
form {
width: ${width}px;
}
.h1 {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.h1 img {
width: ${iconSize}px;
height: ${iconSize}px;
flex: 0 0 ${iconSize}px;
padding: 0;
margin: 0;
}
img.plugin-icon {
border-radius: 4px;
overflow: hidden;
}
.list {
display: flex;
flex-direction: row;
}
.list .margin {
margin-bottom: 0;
margin-left: 0;
}
.list span {
flex: 0 0 auto;
border: 1px solid transparent;
}
.list .bullet {
text-align: center;
}
.list + .list {
margin-top: 0;
}
textarea {
height: 200px;
}
.container {
zoverflow-x: hidden;
overflow-y: auto;
height: ${height === 'auto' ? height : `${height}px`};
}
</style>
<form method="dialog">
<h1 class="h1">
<span ${isError ? `class="color-red"` : ''}>${title}</span>
<a class='learn-more' href="https://www.derekknox.com/articles/bookmarks-adobe-xd-plugin" target="_blank" title="Learn More">Learn More</a>
</h1>
<hr />
<div class="container">
${styles}
${(!render && (template ? template() : messages
.map(msg => strToHtml(msg))
.join(
''
) + (prompt ? `<label>${multiline ? `<textarea id="prompt" placeholder="${prompt}"></textarea>` : `<input type="text" id="prompt" placeholder="${prompt}" />`}</label>` : ''))) || ''}
</div>
<footer>
${buttons
.map(
({ label, type, variant } = {}, idx) =>
`<button id="btn${idx}" type="${type}" uxp-variant="${variant}">${label}</button>`
)
.join('')}
</footer>
</form>
`;
// if render fn is passed, we'll call it and attach the DOM tree
if (render) {
dialog.querySelector(".container").appendChild(render());
}
// The "ok" and "cancel" button indices. OK buttons are "submit" or "cta" buttons. Cancel buttons are "reset" buttons.
let okButtonIdx = -1;
let cancelButtonIdx = -1;
let clickedButtonIdx = -1;
// Ensure that the form can submit when the user presses ENTER (we trigger the OK button here)
const form = dialog.querySelector('form');
form.onsubmit = () => dialog.close('ok');
// Attach button event handlers and set ok and cancel indices
buttons.forEach(({ type, variant } = {}, idx) => {
const button = dialog.querySelector(`#btn${idx}`);
if (type === 'submit' || variant === 'cta') {
okButtonIdx = idx;
}
if (type === 'reset') {
cancelButtonIdx = idx;
}
button.onclick = e => {
e.preventDefault();
clickedButtonIdx = idx;
dialog.close(idx === cancelButtonIdx ? 'reasonCanceled' : 'ok');
}
});
try {
document.appendChild(dialog);
const response = await dialog.showModal();
if (response === 'reasonCanceled') {
// user hit ESC
return { which: cancelButtonIdx, value: '' };
} else {
if (clickedButtonIdx === -1) {
// user pressed ENTER, so no button was clicked!
clickedButtonIdx = okButtonIdx; // may still be -1, but we tried
}
return { which: clickedButtonIdx, value: prompt ? dialog.querySelector('#prompt').value : '' };
}
} catch (err) {
// system refused the dialog
return { which: cancelButtonIdx, value: '' };
} finally {
dialog.remove();
}
}
/**
* Generates an alert message
*
* @param {string} title
* @param {string[]} msgs
* @returns {Promise<{which: number}>} `which` indicates which button was clicked.
*/
async function alert(title, ...msgs) {
return createDialog({ title, msgs });
}
/**
* Generates a warning message
*
* @param {string} title
* @param {string[]} msgs
* @returns {Promise<{which: number}>} `which` indicates which button was clicked.
*/
async function error(title, ...msgs) {
return createDialog({ title, isError: true, msgs });
}
/**
* Displays a confirmation dialog.
*
* @param {string} title
* @param {string} msg
* @param {string[]} [buttons = ['Cancel', 'OK']] the buttons to display (in macOS order); TWO MAX.
* @returns {Promise<{which: number}>} `which` indicates which button was clicked.
*/
async function confirm(title, msg, buttons = ['Cancel', 'OK']) {
return createDialog({
title, msgs: [msg], buttons: [
{ label: buttons[0], type: 'reset', variant: 'primary' },
{ label: buttons[1], type: 'submit', variant: 'cta' }
]
});
}
/**
* Displays a warning dialog.
*
* @param {string} title
* @param {string} msg
* @param {string[]} [buttons = ['Cancel', 'OK']] the buttons to display (in macOS order); TWO MAX.
* @returns {Promise<{which: number}>} `which` indicates which button was clicked.
*/
async function warning(title, msg, buttons = ['Cancel', 'OK']) {
return createDialog({
title, msgs: [msg], buttons: [
{ label: buttons[0], type: 'submit', variant: 'primary' },
{ label: buttons[1], type: 'button', variant: 'warning' }
]
});
}
/**
* Displays a warning dialog.
*
* @param {string} title
* @param {string} msg
* @param {string} prompt
* @param {string[]} [buttons = ['Cancel', 'OK']] the buttons to display (in macOS order); TWO MAX.
* @param {boolean} [multiline = false] If `true`, a multiline textarea will be used instead of a single line editor.
* @returns {Promise<{which: number, value: string}>} `which` indicates which button was clicked, and `value` indicates the entered value in the text field.
*/
async function prompt(title, msg, prompt, buttons = ['Cancel', 'OK'], multiline = false) {
return createDialog({
title, msgs: [msg], prompt, multiline, buttons: [
{ label: buttons[0], type: 'reset', variant: 'primary' },
{ label: buttons[1], type: 'submit', variant: 'cta' }
]
});
}
module.exports = {
createDialog,
alert,
error,
confirm,
warning,
prompt
};