-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
197 lines (176 loc) · 7.49 KB
/
Copy pathplugin.js
File metadata and controls
197 lines (176 loc) · 7.49 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
/**
* Plugin CodeSource pour TinyMCE 7
* Éditeur de code source avec coloration syntaxique CodeMirror 6
*
* @copyright (C) 2008 - 2026 Gerits Aurelien (Magix CMS)
* @license GPL-3.0-or-later
*/
(function () {
'use strict';
tinymce.PluginManager.add('codesource', function (editor, url) {
let cmInstance = null;
// 1. Formateur HTML (Beautifier) ultra-léger
const formatHTML = function (html) {
let indentLevel = 0;
return html
.replace(/>\s*</g, '>\n<') // Casse les balises collées
.split('\n')
.map(function (line) {
line = line.trim();
if (!line) return '';
if (line.match(/^<\/(?!html)/)) {
indentLevel = Math.max(0, indentLevel - 1);
}
const formatted = ' '.repeat(indentLevel) + line;
if (line.match(/^<(?!!--|img|br|hr|input|meta|link|area|base|col|param)[^\/][^>]*>$/)) {
indentLevel++;
}
return formatted;
})
.join('\n');
};
// 2. Injection du CSS de base
const injectStyles = function () {
const styleId = 'cm6-tinymce-modal-style';
if (!document.getElementById(styleId)) {
const style = document.createElement('style');
style.id = styleId;
style.textContent = `
.tox-dialog__body-content:has(#cm-editor-modal) {
padding: 0 !important;
}
#cm-editor-modal {
height: 68vh;
min-height: 400px;
width: 100%;
display: flex;
flex-direction: column;
text-align: left;
/* Plus de background forcé ici, oneDark s'en charge ! */
}
`;
document.head.appendChild(style);
}
};
// 3. Chargement autonome du bundle CM6
const ensureDependencies = function (callback) {
if (window.CM6 && window.CM6.openSearchPanel) { // Sécurité supplémentaire
callback();
return;
}
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = url + '/codemirror.bundle.js?v=1.1';
script.onload = callback;
script.onerror = function () {
editor.notificationManager.open({ text: 'Échec du chargement de CodeMirror 6', type: 'error' });
};
document.head.appendChild(script);
};
// 4. Ouverture de la modale TinyMCE 7
const openEditorDialog = function () {
ensureDependencies(function () {
injectStyles();
const { EditorView, EditorState, basicSetup, html, oneDark, openSearchPanel } = window.CM6;
editor.windowManager.open({
title: 'Code source HTML',
size: 'large',
body: {
type: 'panel',
items: [
{
type: 'htmlpanel',
html: '<div id="cm-editor-modal"></div>'
}
]
},
buttons: [
{ type: 'cancel', name: 'cancel', text: 'Annuler' },
{ type: 'custom', name: 'search', text: 'Rechercher', icon: 'search' },
{ type: 'submit', name: 'save', text: 'Enregistrer', primary: true }
],
onAction: function (api, details) {
if (details.name === 'search' && cmInstance) {
openSearchPanel(cmInstance);
}
},
onSubmit: function (api) {
if (cmInstance) {
const updatedContent = cmInstance.state.doc.toString();
editor.setContent(updatedContent);
cmInstance.destroy();
cmInstance = null;
}
api.close();
},
onClose: function () {
if (cmInstance) {
cmInstance.destroy();
cmInstance = null;
}
}
});
// 5. Initialisation de CodeMirror
setTimeout(function () {
try {
const container = document.getElementById('cm-editor-modal');
if (!container) return;
// 👉 LA SOLUTION MAGIQUE : LE SHADOW DOM
// On crée un DOM isolé pour bloquer l'interférence du CSS de TinyMCE
const shadowRoot = container.attachShadow({ mode: 'open' });
const rawCode = editor.getContent({ source_view: true });
const beautifulCode = formatHTML(rawCode);
// Structure visuelle confinée dans le Shadow DOM
const customTheme = EditorView.theme({
"&": {
height: "100%",
fontSize: "14px",
fontFamily: "Menlo, Monaco, Consolas, 'Courier New', monospace"
},
".cm-scroller": { overflow: "auto" },
"&.cm-focused": { outline: "none" }
});
cmInstance = new EditorView({
state: EditorState.create({
doc: beautifulCode,
extensions: [
basicSetup,
html(),
oneDark,
customTheme,
EditorView.lineWrapping
]
}),
// 👇 On accroche l'éditeur au Shadow DOM au lieu du conteneur classique
parent: shadowRoot
});
setTimeout(() => cmInstance.focus(), 50);
} catch (e) {
console.error("CodeSource ERREUR FATALE :", e);
}
}, 50);
});
};
// 6. Enregistrement TinyMCE
editor.addCommand('mceCodeSource', openEditorDialog);
editor.ui.registry.addButton('codesource', {
icon: 'sourcecode',
tooltip: 'Code source (CodeMirror 6)',
onAction: () => editor.execCommand('mceCodeSource')
});
editor.ui.registry.addMenuItem('codesource', {
icon: 'sourcecode',
text: 'Code source...',
onAction: () => editor.execCommand('mceCodeSource')
});
return {
getMetadata: function () {
return {
name: 'CodeSource CodeMirror 6',
url: 'https://github.com/gtraxx/tinymce-codesource',
author: 'Aurélien Gérits (Magix CMS)'
};
}
};
});
})();