-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Dialog.ts
233 lines (203 loc) · 6.68 KB
/
Dialog.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
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
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { Types } from '@ephox/bridge';
import { Arr, Option } from '@ephox/katamari';
import Editor from 'tinymce/core/api/Editor';
import Promise from 'tinymce/core/api/util/Promise';
import Tools from 'tinymce/core/api/util/Tools';
import XHR from 'tinymce/core/api/util/XHR';
import Settings from '../api/Settings';
import Templates from '../core/Templates';
import * as Utils from '../core/Utils';
interface UrlTemplate {
title: string;
description: string;
url: string;
}
interface ContentTemplate {
title: string;
description: string;
content: string;
}
type ExternalTemplate = UrlTemplate | ContentTemplate;
interface InternalTemplate {
selected: boolean;
text: string;
value: {
url: Option<string>;
content: Option<string>;
description: string;
};
}
type DialogData = {
template: string;
preview: string;
};
type UpdateDialogCallback = (dialogApi: Types.Dialog.DialogInstanceApi<DialogData>, template: InternalTemplate, previewHtml: string) => void;
const getPreviewContent = (editor: Editor, html: string) => {
if (html.indexOf('<html>') === -1) {
let contentCssLinks = '';
Tools.each(editor.contentCSS, (url) => {
contentCssLinks += '<link type="text/css" rel="stylesheet" href="' +
editor.documentBaseURI.toAbsolute(url) +
'">';
});
let bodyClass = editor.settings.body_class || '';
if (bodyClass.indexOf('=') !== -1) {
bodyClass = editor.getParam('body_class', '', 'hash');
bodyClass = bodyClass[editor.id] || '';
}
const encode = editor.dom.encode;
const directionality = editor.getBody().dir;
const dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
html = (
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
contentCssLinks +
'</head>' +
'<body class="' + encode(bodyClass) + '"' + dirAttr + '>' +
html +
'</body>' +
'</html>'
);
}
return Templates.replaceTemplateValues(html, Settings.getPreviewReplaceValues(editor));
};
const open = (editor: Editor, templateList: ExternalTemplate[]) => {
const createTemplates = (): Option<Array<InternalTemplate>> => {
if (!templateList || templateList.length === 0) {
const message = editor.translate('No templates defined.');
editor.notificationManager.open({ text: message, type: 'info' });
return Option.none();
}
return Option.from(Tools.map(templateList, (template: ExternalTemplate, index) => {
const isUrlTemplate = (t: ExternalTemplate): t is UrlTemplate => (t as UrlTemplate).url !== undefined;
return {
selected: index === 0,
text: template.title,
value: {
url: isUrlTemplate(template) ? Option.from(template.url) : Option.none(),
content: !isUrlTemplate(template) ? Option.from(template.content) : Option.none(),
description: template.description
}
};
}));
};
const createSelectBoxItems = (templates: InternalTemplate[]) => {
return Arr.map(templates, (t) => {
return {
text: t.text,
value: t.text
};
});
};
const findTemplate = (templates: InternalTemplate[], templateTitle: string) => {
return Arr.find(templates, (t) => {
return t.text === templateTitle;
});
};
const getTemplateContent = (t: InternalTemplate) => {
return new Promise<string>((resolve, reject) => {
t.value.url.fold(() => resolve(t.value.content.getOr('')), (url) => XHR.send({
url,
success (html: string) {
resolve(html);
},
error: (e) => {
reject(e);
}
}));
});
};
const onChange = (templates: InternalTemplate[], updateDialog: UpdateDialogCallback) => (api: Types.Dialog.DialogInstanceApi<DialogData>, change: { name: string }) => {
if (change.name === 'template') {
const newTemplateTitle = api.getData().template;
findTemplate(templates, newTemplateTitle).each((t) => {
api.block('Loading...');
getTemplateContent(t).then((previewHtml) => {
updateDialog(api, t, previewHtml);
api.unblock();
});
});
}
};
const onSubmit = (templates: InternalTemplate[]) => (api: Types.Dialog.DialogInstanceApi<DialogData>) => {
const data = api.getData();
findTemplate(templates, data.template).each((t) => {
getTemplateContent(t).then((previewHtml) => {
Templates.insertTemplate(editor, false, previewHtml);
api.close();
});
});
};
const openDialog = (templates: InternalTemplate[]) => {
const selectBoxItems = createSelectBoxItems(templates);
const buildDialogSpec = (bodyItems: Types.Dialog.BodyComponentApi[], initialData: DialogData): Types.Dialog.DialogApi<DialogData> => ({
title: 'Insert Template',
size: 'large',
body: {
type: 'panel',
items: bodyItems
},
initialData,
buttons: [
{
type: 'cancel',
name: 'cancel',
text: 'Cancel',
},
{
type: 'submit',
name: 'save',
text: 'Save',
primary: true
}
],
onSubmit: onSubmit(templates),
onChange: onChange(templates, updateDialog)
});
const updateDialog = (dialogApi: Types.Dialog.DialogInstanceApi<DialogData>, template: InternalTemplate, previewHtml: string) => {
const content = getPreviewContent(editor, previewHtml);
const bodyItems: Types.Dialog.BodyComponentApi[] = [
{
type: 'selectbox',
name: 'template',
label: 'Templates',
items: selectBoxItems
},
{
type: 'htmlpanel',
html: `<p aria-live="polite">${Utils.htmlEscape(template.value.description)}</p>`
},
{
label: 'Preview',
type: 'iframe',
name: 'preview',
sandboxed: false
}
];
const initialData = {
template: template.text,
preview: content
};
dialogApi.unblock();
dialogApi.redial(buildDialogSpec(bodyItems, initialData));
dialogApi.focus('template');
};
const dialogApi = editor.windowManager.open(buildDialogSpec([], { template: '', preview: '' }));
dialogApi.block('Loading...');
getTemplateContent(templates[0]).then((previewHtml) => {
updateDialog(dialogApi, templates[0], previewHtml);
});
};
const optTemplates: Option<InternalTemplate[]> = createTemplates();
optTemplates.each(openDialog);
};
export default {
open
};