-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
163 lines (143 loc) · 6.88 KB
/
plugin.js
File metadata and controls
163 lines (143 loc) · 6.88 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
/**
* @license MIT
* Snippet Manager for TinyMCE 6 & 7
* Developed by Gerits Aurélien (https://www.gerits-aurelien.be)
* Copyright (c) 2026, Magix CMS.
* Version 1.0
*/
tinymce.PluginManager.requireLangPack("snippets");
tinymce.PluginManager.add('snippets', function (editor, url) {
const _ = (text) => editor.translate(text);
const openSnippetModal = () => {
const endpoint = editor.getParam('snippets_url');
if (!endpoint) {
editor.notificationManager.open({
text: _('Error: snippets_url not configured.'),
type: 'error'
});
return;
}
editor.setProgressState(true);
fetch(endpoint)
.then(response => response.json())
.then(allSnippets => {
editor.setProgressState(false);
// UID unique pour gérer les doublons de noms
const snippetsWithId = allSnippets.map((s, i) => ({
...s,
uid: s.id ? s.id.toString() : `snip-${i}`
}));
let currentFiltered = snippetsWithId;
const loadPreview = (snippet) => {
const iframe = document.getElementById('snippet-preview-iframe');
if (!iframe || !snippet) return;
fetch(snippet.url).then(res => res.text()).then(html => {
const doc = iframe.contentDocument || iframe.contentWindow.document;
doc.documentElement.innerHTML = "";
const contentCss = editor.getParam('content_css');
let cssLinks = '';
if (contentCss) {
const cssFiles = Array.isArray(contentCss) ? contentCss : contentCss.split(',');
cssFiles.forEach(file => {
if(file.trim() !== "") cssLinks += `<link rel="stylesheet" href="${file.trim()}">`;
});
}
doc.head.innerHTML = `${cssLinks}<style>body{padding:15px;background:#fff;margin:0;font-family:sans-serif;}img{max-width:100%;height:auto;}</style>`;
doc.body.innerHTML = html;
});
};
const getDialogConfig = (items) => ({
title: _('Snippet Library'),
size: 'large',
body: {
type: 'panel',
items: [
{
type: 'input',
name: 'searchQuery',
label: _('Search (Title or Description)'),
placeholder: _('Filter snippets...')
},
{
type: 'selectbox',
name: 'snippetUid',
label: _('Found snippets ({0})').replace('{0}', items.length),
items: items.map(item => ({
text: item.description ? `${item.title} (${item.description})` : item.title,
value: item.uid
}))
},
{
type: 'htmlpanel',
html: '<div style="border:1px solid #ddd; background:#fff; border-radius:3px; overflow:hidden;"><iframe id="snippet-preview-iframe" style="width:100%; height:380px; border:none; display:block;"></iframe></div>'
}
]
},
buttons: [
{ type: 'cancel', text: _('Cancel') },
{ type: 'submit', text: _('Insert'), primary: true }
],
onChange: (api, details) => {
const data = api.getData();
if (details.name === 'searchQuery') {
const query = data.searchQuery.toLowerCase();
currentFiltered = snippetsWithId.filter(s =>
s.title.toLowerCase().includes(query) ||
(s.description && s.description.toLowerCase().includes(query))
);
api.redial(getDialogConfig(currentFiltered));
api.setData({ searchQuery: data.searchQuery });
if (currentFiltered.length > 0) {
loadPreview(currentFiltered[0]);
} else {
const iframe = document.getElementById('snippet-preview-iframe');
if (iframe) {
const doc = iframe.contentDocument || iframe.contentWindow.document;
doc.body.innerHTML = `<div style="padding:20px;text-align:center;color:#666;"><em>${_('No matching snippets found.')}</em></div>`;
}
}
}
if (details.name === 'snippetUid') {
const selected = currentFiltered.find(s => s.uid === data.snippetUid);
if (selected) loadPreview(selected);
}
},
onSubmit: (api) => {
const iframe = document.getElementById('snippet-preview-iframe');
if (iframe) {
const doc = iframe.contentDocument || iframe.contentWindow.document;
editor.insertContent(doc.body.innerHTML);
}
api.close();
}
});
editor.windowManager.open(getDialogConfig(snippetsWithId));
if (snippetsWithId.length > 0) loadPreview(snippetsWithId[0]);
})
.catch(err => {
editor.setProgressState(false);
console.error("Snippets error:", err);
});
};
// Raccourcis et Enregistrement
editor.addShortcut('meta+shift+s', _('Open Snippets'), openSnippetModal);
editor.ui.registry.addButton('snippets', {
icon: 'template',
tooltip: _('Insert snippet'),
onAction: openSnippetModal
});
editor.ui.registry.addMenuItem('snippets', {
text: _('Snippet...'),
icon: 'template',
shortcut: 'Meta+Shift+S',
onAction: openSnippetModal
});
return {
getMetadata: () => ({
name: "Snippet Manager Pro",
url: "https://www.gerits-aurelien.be",
author: "Gerits Aurélien",
version: "1.1.2"
})
};
});