Skip to content

Commit

Permalink
Make CodeMirror work in repeatable subforms (#12542)
Browse files Browse the repository at this point in the history
* One function to initialize any and all CodeMirror instances rather than individual functions to initialize one-by-one. Call on page load and also on subform-row-add

* Minor js changes

* Codemirror fullscreen modifier message (do we still need this?)
  • Loading branch information
okonomiyaki3000 authored and Michael Babker committed Apr 22, 2018
1 parent 0b349eb commit 49e681f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 11 additions & 9 deletions plugins/editors/codemirror/layouts/editors/codemirror/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
$buttons = $displayData->buttons;
$modifier = $params->get('fullScreenMod', '') !== '' ? implode($params->get('fullScreenMod', ''), ' + ') . ' + ' : '';

JFactory::getDocument()->addScriptDeclaration('
jQuery(function () {
var id = ' . json_encode($id) . ', options = ' . json_encode($options) . ';
/** Register Editor */
Joomla.editors.instances[id] = CodeMirror.fromTextArea(document.getElementById(id), options);
});
');
?>

<p class="label">
<?php echo JText::sprintf('PLG_CODEMIRROR_TOGGLE_FULL_SCREEN', $modifier, $params->get('fullScreen', 'F10')); ?>
</p>
<?php echo '<textarea name="', $name, '" id="', $id, '" cols="', $cols, '" rows="', $rows, '">', $content, '</textarea>'; ?>

<?php echo $displayData->buttons; ?>
<?php
echo '<textarea class="codemirror-source" name="', $name,
'" id="', $id,
'" cols="', $cols,
'" rows="', $rows,
'" data-options="', htmlspecialchars(json_encode($options)),
'">', $content, '</textarea>';
?>

<?php echo $buttons; ?>
20 changes: 19 additions & 1 deletion plugins/editors/codemirror/layouts/editors/codemirror/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
// jQuery's ready function.
$(function () {
// Some browsers do something weird with the fieldset which doesn't work well with CodeMirror. Fix it.
$(editor.getTextArea()).parent('fieldset').css('min-width', 0);
$(editor.getWrapperElement()).parent('fieldset').css('min-width', 0);
// Listen for Bootstrap's 'shown' event. If this editor was in a hidden element when created, it may need to be refreshed.
$(document.body).on("shown shown.bs.tab shown.bs.modal", function () { editor.refresh(); });
});
Expand All @@ -80,6 +80,24 @@ function makeMarker()
marker.className = "CodeMirror-markergutter-mark";
return marker;
}
// Initialize any CodeMirrors on page load and when a subform is added
$(function ($) {
initCodeMirror();
$('body').on('subform-row-add', initCodeMirror);
});
function initCodeMirror(event, container)
{
container = container || document;
$(container).find('textarea.codemirror-source').each(function () {
var input = $(this).removeClass('codemirror-source');
var id = input.prop('id');
Joomla.editors.instances[id] = cm.fromTextArea(this, input.data('options'));
});
}
}(CodeMirror, jQuery));
JS
);

0 comments on commit 49e681f

Please sign in to comment.