-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
204 lines (179 loc) · 6.97 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Modulix Editor</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css"
integrity="sha512-q3eWabyZPc1XTCmF+8/LuE1ozpg5xxn7iO89yfSOd5/oKvyqLngoNGsx8jq92Y8eXJ/IRxQbEC+FGSYxtk2oiw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/es2015/jodit.min.css"
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Modulix Editor</h1>
<main class="modulix-editor">
<div class="modulix-editor__editor">
<div class="mb-2">
<button class="btn btn-outline-info" id="preview-button">
<span class="fa fa-eye"></span>
Previsualiser
</button>
<button class="btn btn-outline-success" id="toggle-json-button">
<span class="fa fa-code"></span>
Afficher le JSON
</button>
<button class="btn btn-outline-warning" id="download-json-button">
<span class="fa fa-download"></span>
Télécharger le JSON
</button>
<button class="btn btn-outline-danger" id="reset-button">
<span class="fa fa-trash"></span>
Réinitialiser
</button>
</div>
<h2>Contenu</h2>
<div id="editor_holder"></div>
</div>
<div class="modulix-editor__render" id="json-output-pane">
<h2>JSON à copier/coller</h2>
<textarea
id="json_output"
class="modulix-editor-render__input"
></textarea>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es2018/jodit.fat.min.js"></script>
<script type="module">
import { schema } from './modulix.json-schema.js';
import LocalBackup from './LocalBackup.js';
const element = document.getElementById('editor_holder');
const jsonOutput = document.getElementById('json_output');
Jodit.defaultOptions.toolbarAdaptive = false;
Jodit.defaultOptions.buttons =
'paragraph,|,bold,italic,strikethrough,link,eraser,|,ul,ol,|,hr,|,source';
Jodit.defaultOptions.controls.paragraph.list = {
p: 'Paragraph',
h3: 'Heading 3',
h4: 'Heading 4',
blockquote: 'Quote',
code: 'Source code',
};
Jodit.defaultOptions.controls.ul.list = null;
Jodit.defaultOptions.controls.ol.list = null;
schema.format = 'categories';
schema.properties.transitionTexts.items.properties.grainId.format = '';
const editor = new JSONEditor(element, {
schema,
theme: 'bootstrap5',
iconlib: 'fontawesome5',
no_additional_properties: true,
disable_edit_json: true,
disable_properties: true,
disable_array_reorder: false,
form_name_root: 'Module',
});
const previewButton = document.querySelector('#preview-button');
let previewWindow;
previewButton.addEventListener('click', () => {
const moduleContent = editor.getValue();
const windowName = `modulix-preview-${moduleContent.id}`;
previewWindow = window.open('https://app.recette.pix.fr/modules/preview', windowName);
});
window.addEventListener('message', (event) => {
if (event.data?.from === 'pix-app' && event.data?.message === 'Ready to receive content !') {
const moduleContent = editor.getValue();
sendDataForPreview(previewWindow, moduleContent);
}
});
const jsonOutputPane = document.querySelector('#json-output-pane');
const toggleCodeButton = document.querySelector('#toggle-json-button');
let jsonOutputPaneDisplayed = false;
toggleCodeButton.addEventListener('click', () => {
jsonOutputPaneDisplayed = !jsonOutputPaneDisplayed;
jsonOutputPane.style.display = jsonOutputPaneDisplayed ? 'block' : 'none';
});
const downloadButton = document.querySelector('#download-json-button');
downloadButton.addEventListener('click', () => {
const downloadLink = document.createElement('a');
const json = editor.getValue();
const jsonContent = JSON.stringify(json, null, 2);
downloadLink.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(jsonContent));
downloadLink.setAttribute('download', `${json.slug}.json`);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
const resetButton = document.querySelector('#reset-button');
resetButton.addEventListener('click', () => {
if (window.confirm('Voulez-vous vraiment réinitialiser le module ? Toutes les modifications seront perdues.')) {
LocalBackup.delete();
window.location.reload();
}
});
editor.on('change', () => {
if (JSON.stringify(editor.getValue(), null, 2) !== jsonOutput.value) {
jsonOutput.value = JSON.stringify(editor.getValue(), null, 2);
}
displayJsonOutputError();
LocalBackup.save(editor.getValue());
const moduleContent = editor.getValue();
sendDataForPreview(previewWindow, moduleContent)
});
jsonOutput.addEventListener('focusout', () => {
try {
const value = JSON.parse(jsonOutput.value);
editor.setValue(value);
} catch (error) {
console.error(error);
}
displayJsonOutputError();
});
editor.on('ready', () => {
const schema = LocalBackup.load();
if (schema) {
editor.setValue(schema);
}
});
function displayJsonOutputError() {
try {
JSON.parse(jsonOutput.value);
jsonOutput.classList.remove(
'modulix-editor-render__input--has-error'
);
} catch {
jsonOutput.classList.add('modulix-editor-render__input--has-error');
}
}
/**
* Send module content to Pix App preview
* @param moduleContent
* @param previewWindow
*/
function sendDataForPreview(previewWindow, moduleContent) {
previewWindow?.postMessage({from: 'modulix-editor', moduleContent}, '*');
}
</script>
</body>
</html>