-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcms.ts
More file actions
233 lines (205 loc) · 6.46 KB
/
cms.ts
File metadata and controls
233 lines (205 loc) · 6.46 KB
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
import type {
IDeveloperPages,
IDependencies,
IDependenciesCSS,
IDependenciesJS,
IConfigurablePages,
} from "../../types/cms";
export function newDeveloperPage(): IDeveloperPages {
return {
mg_tableclass: "",
name: "",
description: "",
html: "",
css: "",
javascript: "",
dependencies: [],
enableBaseStyles: true,
enableButtonStyles: true,
enableFullScreen: false,
};
}
const pageQuery = `query getContainers($filter:ContainersFilter) {
Containers(filter:$filter) {
# Containers
name
description
mg_tableclass
# Developer pages
html
css
javascript
dependencies {
mg_tableclass
name
url
defer
async
fetchPriority {
name
}
}
enableBaseStyles
enableButtonStyles
enableFullScreen
# Configurable pages
blockOrder(orderby: { order: ASC } ) {
id
order
block {
id
enableFullScreenWidth
mg_tableclass
# page headings
title
subtitle
backgroundImage {
image {
id
url
}
}
titleIsCentered
# components
componentOrder(orderby: {order:ASC}) {
order
component {
id
mg_tableclass
# TextElements
text
# Headings
level
headingIsCentered
# Paragraphs
paragraphIsCentered
# images
displayName
image {
id
size
filename
extension
url
}
alt
width
height
imageIsCentered
# navigation groups and cards
links {
id
title
description
url
urlLabel
urlIsExternal
}
}
}
}
}
}
}`;
export async function getPage(
schema: string,
page: string
): Promise<IDeveloperPages | IConfigurablePages> {
const { data } = await $fetch(`/${schema}/graphql`, {
method: "POST",
body: {
query: pageQuery,
variables: { filter: { name: { equals: page } } },
},
});
const currentPage = data.Containers[0];
return currentPage;
}
export function generateHtmlPreview(
content: IDeveloperPages,
ref: HTMLDivElement
) {
if (content && typeof content === "object" && Object.keys(content).length) {
ref.replaceChildren();
const parser = new DOMParser();
const documentHead = document.getElementsByTagName(
"head"
)[0] as HTMLHeadElement;
(content.dependencies as IDependencies[])?.forEach(
(dependency: IDependenciesCSS | IDependenciesJS) => {
if (dependency.mg_tableclass?.endsWith("CSS") && dependency.url) {
const elem = document.createElement("link");
elem.href = dependency.url;
elem.rel = "stylesheet";
documentHead.appendChild(elem);
}
if (dependency.mg_tableclass?.endsWith("JS") && dependency.url) {
const jsDependency = dependency as IDependenciesJS;
const elem = document.createElement("script") as HTMLScriptElement;
elem.src = jsDependency.url as string;
if (elem.src && jsDependency.async) {
elem.async = jsDependency.async as boolean;
}
if (elem.src && !jsDependency.async && jsDependency.defer) {
elem.defer = jsDependency.defer as boolean;
}
if (elem.src && jsDependency.fetchPriority) {
elem.fetchPriority = jsDependency.fetchPriority.name as
| "high"
| "low"
| "auto";
}
}
}
);
if (content.html) {
const doc = parser.parseFromString(content.html, "text/html");
Array.from(doc.body.children).forEach((element) => {
ref.appendChild(element);
});
}
if (content.css) {
const styleElement = document.createElement("style");
styleElement.textContent = content.css;
documentHead.appendChild(styleElement);
}
if (content.javascript) {
const scriptElement = document.createElement("script");
scriptElement.setAttribute("type", "text/javascript");
scriptElement.text = `setTimeout(() => {
/** timeout is required for correctly loading external dependencies */
${content.javascript}
}, 200)`;
ref.appendChild(scriptElement);
}
} else {
const parser = new DOMParser();
const htmlString: string = content as unknown as string;
const doc = parser.parseFromString(htmlString, "text/html");
/** Loop over the just parsed html items, and add them */
Array.from(doc.body.children).forEach((el) => {
if (el.tagName !== "SCRIPT") {
ref.appendChild(el);
} else {
/** Script tags need a special treatment, else they will not execute. **/
const scriptEl = document.createElement("script");
if ((el as HTMLScriptElement).src) {
/** If we have an external script. */
scriptEl.src = (el as HTMLScriptElement).src;
} else {
/** Regular inline script */
scriptEl.textContent = el.textContent;
}
ref.appendChild(scriptEl);
}
});
}
}
export function parsePageText(value?: string): string {
const val = value || "";
return val.replace(/(^"{1,})|("{1,}$)/g, "");
}
export function pageCopyDate(): string {
const date = new Date().toISOString();
return date.replace("T", " ").split(".")[0] as string;
}