Skip to content

Commit

Permalink
Fix desync of TinyMCE to text area when switching markup modes. (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Geer committed Jun 6, 2021
1 parent fafaefb commit a6f1510
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 65 deletions.
9 changes: 9 additions & 0 deletions plugins/tinymce.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public function show_editor($selector = '') {
}
};

// This will be called by the editor page to force a sync of editor
// content to the textarea. This prevents desyncing issues when
// switching between markup modes.
var editor_commit_current = function() {
if (tinymce.activeEditor) {
tinymce.triggerSave();
}
};

jQuery(document).ready(function() {
try {
initialize_tinymce();
Expand Down
135 changes: 70 additions & 65 deletions themes/default/scripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ $(document).ready(

$('.checktoggle[data-for]').on(
'change', function() {
var $this = $(this),
checked = $this.is(':checked'),
for_id = $this.attr('data-for');
$('#' + for_id).toggle(checked);
if (checked) {
$(for_id).focus();
}
var $this = $(this),
checked = $this.is(':checked'),
for_id = $this.attr('data-for');
$('#' + for_id).toggle(checked);
if (checked) {
$(for_id).focus();
}
}
);

Expand All @@ -401,9 +401,9 @@ $(document).ready(

$('.entry_preview .preview-close').on(
'click.preview', function (e) {
e.preventDefault();
$('.entry_preview').hide();
$('#postform').show();
e.preventDefault();
$('.entry_preview').hide();
$('#postform').show();
}
);

Expand All @@ -416,62 +416,67 @@ $(document).ready(

$('#postform').submit(
function () {
if ($('#preview').attr('rel') == 'clicked') {
var form_url = $('#postform').attr('action');
form_url += (form_url.indexOf('?') >= 0) ? '&' : '?';
form_url += 'preview=yes&ajax=1';

var has_files = false;
$("#postform input[type='file']").each(
function () {
if ($(this).val()) {
has_files = true;
}
}
);

if (has_files) {
var ret = window.confirm(strings.editor_submitWithFiles);
if (ret) {
form_url += '&save=draft';
} else {
return false;
}
}
// Make sure the WYSIWYG editor syncs to the text area.
if (typeof(editor_commit_current) == 'function') {
editor_commit_current();
}

var options = {
target: '.entry_preview',
url: form_url,
dataType: 'json',
success: function (response, statusText, xhr, $form) {
$('.entry_preview')
.find('.preview-text').html(unescape(response.content)).end()
.find('.preview-overlay').remove();
if (response.id.match(/draft/)) {
var form_url = $('#postform').attr('action');
form_url += form_url.match(/draft=/) ? '' : '&draft='+response.id;
form_url += form_url.match(/preview=1/) ? '' : '&preview=yes';
form_url += form_url.match(/ajax=1/) ? '' : 'ajax=1';
$('#postform').attr('action', form_url);
}
}
};

var markup = '<div class="preview-overlay"><div class="label"><img src="'+
window.INSTALL_ROOT+'/themes/default/images/ajax-loader.gif"> Loading...</div></div>';
$('.entry_preview').show().prepend(markup);
if ($('#preview').attr('rel') == 'clicked') {
var form_url = $('#postform').attr('action');
form_url += (form_url.indexOf('?') >= 0) ? '&' : '?';
form_url += 'preview=yes&ajax=1';

var has_files = false;
$("#postform input[type='file']").each(
function () {
if ($(this).val()) {
has_files = true;
}
}
);

if (has_files) {
var ret = window.confirm(strings.editor_submitWithFiles);
if (ret) {
form_url += '&save=draft';
} else {
return false;
}
}

var options = {
target: '.entry_preview',
url: form_url,
dataType: 'json',
success: function (response, statusText, xhr, $form) {
$('.entry_preview')
.find('.preview-text').html(unescape(response.content)).end()
.find('.preview-overlay').remove();
if (response.id.match(/draft/)) {
var form_url = $('#postform').attr('action');
form_url += form_url.match(/draft=/) ? '' : '&draft='+response.id;
form_url += form_url.match(/preview=1/) ? '' : '&preview=yes';
form_url += form_url.match(/ajax=1/) ? '' : 'ajax=1';
$('#postform').attr('action', form_url);
}
}
};

$('#postform').hide().ajaxSubmit(options);
return false;
} else {
// Clear out the stored post data because it's being saved now.
if (typeof localStorage != 'undefined') {
localStorage.removeItem(auto_save_key);
}
}
// Make the submission not prompt to leave the page.
current_text_content = original_text_content;
return true;
var markup = '<div class="preview-overlay"><div class="label"><img src="'+
window.INSTALL_ROOT+'/themes/default/images/ajax-loader.gif"> Loading...</div></div>';
$('.entry_preview').show().prepend(markup);

$('#postform').hide().ajaxSubmit(options);
return false;
} else {
// Clear out the stored post data because it's being saved now.
if (typeof localStorage != 'undefined') {
localStorage.removeItem(auto_save_key);
}
}
// Make the submission not prompt to leave the page.
current_text_content = original_text_content;
return true;
}
);

Expand All @@ -492,5 +497,5 @@ $(document).ready(
}
);
$('#tag_list').hide();
}
}
);

0 comments on commit a6f1510

Please sign in to comment.