From f50772c1dd9cd9a28b3c42813384a449a7990fec Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Wed, 15 May 2024 11:02:22 +0900 Subject: [PATCH] Fix #877 insert uploaded image even if CKEditor is in source mode --- .../js/plugins/jquery.fileupload/js/main.js | 27 +++++++++++++++++-- .../editor/skins/ckeditor/css/ckeditor.scss | 3 +++ modules/editor/skins/ckeditor/js/editor.js | 7 +++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/common/js/plugins/jquery.fileupload/js/main.js b/common/js/plugins/jquery.fileupload/js/main.js index 579f93b93..877f9a698 100644 --- a/common/js/plugins/jquery.fileupload/js/main.js +++ b/common/js/plugins/jquery.fileupload/js/main.js @@ -187,7 +187,18 @@ if(temp_code !== '') { try { - _getCkeInstance(settings.formData.editor_sequence).insertHtml(temp_code, "unfiltered_html"); + var textarea = _getCkeContainer(settings.formData.editor_sequence).find('.cke_source'); + var editor = _getCkeInstance(settings.formData.editor_sequence); + if (textarea.length && editor.mode == 'source') { + var caretPosition = textarea[0].selectionStart; + var currentText = textarea[0].value; + textarea.val(currentText.substring(0, caretPosition) + "\n" + + temp_code + "\n" + + currentText.substring(caretPosition) + ); + } else { + editor.insertHtml(temp_code, 'unfiltered_html'); + } } catch(err) { // pass @@ -392,7 +403,19 @@ if (result) { var html = self.generateHtml($container, result); try { - _getCkeInstance(data.editorSequence).insertHtml(html, 'unfiltered_html'); + var textarea = _getCkeContainer(data.editorSequence).find('.cke_source'); + var editor = _getCkeInstance(data.editorSequence); + console.log(textarea, editor.mode); + if (textarea.length && editor.mode == 'source') { + var caretPosition = textarea[0].selectionStart; + var currentText = textarea[0].value; + textarea.val(currentText.substring(0, caretPosition) + "\n" + + html + "\n" + + currentText.substring(caretPosition) + ); + } else { + editor.insertHtml(html, 'unfiltered_html'); + } } catch(err) { // pass diff --git a/modules/editor/skins/ckeditor/css/ckeditor.scss b/modules/editor/skins/ckeditor/css/ckeditor.scss index f77f91dba..a91b32590 100644 --- a/modules/editor/skins/ckeditor/css/ckeditor.scss +++ b/modules/editor/skins/ckeditor/css/ckeditor.scss @@ -103,6 +103,9 @@ body.cke_editable { padding: 10px; @include light-dark($colorset); } +textarea.cke_source.cke_editable { + padding: 10px; +} body.color_scheme_dark.cke_auto_dark_mode .cke_wysiwyg_div { background-color: #333; color: #fff; diff --git a/modules/editor/skins/ckeditor/js/editor.js b/modules/editor/skins/ckeditor/js/editor.js index 68d2719b2..5613a3cc8 100644 --- a/modules/editor/skins/ckeditor/js/editor.js +++ b/modules/editor/skins/ckeditor/js/editor.js @@ -199,6 +199,13 @@ function _getCkeInstance(editor_sequence) { return $('#ckeditor_instance_' + editor_sequence).data('cke_instance'); } +/** + * Legacy function to get the container element for CKEditor. + */ +function _getCkeContainer(editor_sequence) { + return $('#ckeditor_instance_' + editor_sequence); +} + /** * Legacy function to get HTML content from CKEditor. */