Skip to content

Commit bdbf6c7

Browse files
committed
feat(editor): added syntas highlighting in editor as well (#3301)
1 parent b1d96aa commit bdbf6c7

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

phpmyfaq/admin/assets/scss/style.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
// Bootstrap Icons
2525
@import '../../../../node_modules/bootstrap-icons/font/bootstrap-icons';
2626

27+
// Highlight.js
28+
@import '../../../../node_modules/highlight.js/styles/default.css';
29+
2730
// Import Mixins
2831
@import 'mixins.scss';
2932

phpmyfaq/admin/assets/src/content/editor.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import 'jodit/esm/modules/uploader/uploader.js';
4242
import 'jodit/esm/plugins/video/video.js';
4343
import '../plugins/phpmyfaq/phpmyfaq.js';
4444
import '../plugins/code-snippet/code-snippet.js';
45+
import hljs from 'highlight.js';
4546

4647
export const renderEditor = () => {
4748
const editor = document.getElementById('editor');
@@ -52,6 +53,8 @@ export const renderEditor = () => {
5253
const joditEditor = Jodit.make(editor, {
5354
zIndex: 0,
5455
readonly: false,
56+
beautifyHTML: false,
57+
sourceEditor: 'area',
5558
activeButtonsInReadOnly: ['source', 'fullsize', 'print', 'about', 'dots'],
5659
toolbarButtonSize: 'middle',
5760
theme: 'default',
@@ -61,10 +64,10 @@ export const renderEditor = () => {
6164
triggerChangeEvent: true,
6265
width: 'auto',
6366
height: 'auto',
64-
minHeight: 100,
67+
minHeight: 400,
6568
direction: '',
6669
language: 'auto',
67-
debugLanguage: true,
70+
debugLanguage: false,
6871
i18n: 'en',
6972
tabIndex: -1,
7073
toolbar: true,
@@ -246,4 +249,16 @@ export const renderEditor = () => {
246249
showFileName: true,
247250
},
248251
});
252+
253+
joditEditor.events.on('afterSetValue', () => {
254+
joditEditor.container.querySelectorAll('pre code').forEach((block) => {
255+
hljs.highlightElement(block);
256+
});
257+
});
258+
259+
joditEditor.events.on('change', () => {
260+
joditEditor.container.querySelectorAll('pre code').forEach((block) => {
261+
hljs.highlightElement(block);
262+
});
263+
});
249264
};

phpmyfaq/admin/assets/src/plugins/code-snippet/code-snippet.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Jodit Editor plugin to insert source code snippets with syntax highlighting
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public License,
5+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
* obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* @package phpMyFAQ
9+
* @author Thorsten Rinne <[email protected]>
10+
* @copyright 2025 phpMyFAQ Team
11+
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
* @link https://www.phpmyfaq.de
13+
* @since 2025-01-05
14+
*/
15+
116
import { Jodit } from 'jodit';
217
import codeSnippet from '../code-snippet/code-snippet.svg.js';
318

@@ -9,7 +24,9 @@ Jodit.plugins.add('codeSnippet', (editor) => {
924
name: 'codeSnippet',
1025
group: 'insert',
1126
icon: 'codeSnippet',
12-
text: 'Insert Source Code Snippet',
27+
options: {
28+
tooltip: 'Insert Source Code Snippet',
29+
},
1330
});
1431

1532
// Register the command
@@ -74,6 +91,7 @@ Jodit.plugins.add('codeSnippet', (editor) => {
7491
const selectedCode = code.value;
7592
const codeSnippet = `<pre><code class="language-${selectedLanguage}">${encodeHTML(selectedCode)}</code></pre>`;
7693
editor.selection.insertHTML(codeSnippet);
94+
editor.events.fire('change');
7795
dialog.close();
7896
});
7997
});

0 commit comments

Comments
 (0)