- md5
- d83ba65b865aa5463b705ec4aacd44af
- sha1
- 5d0c21c1b6d70a0cab2fef709530f3a174045127
- sha256
- dfb5568ddf3b7c1648e02e21fa4d8cd3d951e4e9c9dd3eacdcc1290c103689bc
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
<?php
/**
* @package Joomla.Plugin
* @subpackage Editors.codemirror
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die;
use Joomla\Event\Event;
/**
* CodeMirror Editor Plugin.
*
* @since 1.6
*/
class PlgEditorCodemirror extends JPlugin
{
/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
* @since 12.3
*/
protected $autoloadLanguage = true;
/**
* Mapping of syntax to CodeMirror modes.
*
* @var array
*/
protected $modeAlias = array();
/**
* Initialises the Editor.
*
* @return void
*/
public function onInit()
{
static $done = false;
// Do this only once.
if ($done)
{
return;
}
$done = true;
// Most likely need this later
$doc = JFactory::getDocument();
// Codemirror shall have its own group of plugins to modify and extend its behavior
JPluginHelper::importPlugin('editors_codemirror');
// At this point, params can be modified by a plugin before going to the layout renderer.
JFactory::getApplication()->triggerEvent('onCodeMirrorBeforeInit', array(&$this->params));
$displayData = (object) array('params' => $this->params);
// We need to do output buffering here because layouts may actually 'echo' things which we do not want.
ob_start();
JLayoutHelper::render('editors.codemirror.init', $displayData, __DIR__ . '/layouts');
ob_end_clean();
$font = $this->params->get('fontFamily', 0);
$fontInfo = $this->getFontInfo($font);
if (isset($fontInfo))
{
if (isset($fontInfo->url))
{
$doc->addStyleSheet($fontInfo->url);
}
if (isset($fontInfo->css))
{
$displayData->fontFamily = $fontInfo->css . '!important';
}
}
// We need to do output buffering here because layouts may actually 'echo' things which we do not want.
ob_start();
JLayoutHelper::render('editors.codemirror.styles', $displayData, __DIR__ . '/layouts');
ob_end_clean();
JFactory::getApplication()->triggerEvent('onCodeMirrorAfterInit', array(&$this->params));
}
/**
* Display the editor area.
*
* @param string $name The control name.
* @param string $content The contents of the text area.
* @param string $width The width of the text area (px or %).
* @param string $height The height of the text area (px or %).
* @param int $col The number of columns for the textarea.
* @param int $row The number of rows for the textarea.
* @param boolean $buttons True and the editor buttons will be displayed.
* @param string $id An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
* @param string $asset Not used.
* @param object $author Not used.
* @param array $params Associative array of editor parameters.
*
* @return string HTML
*/
public function onDisplay(
$name, $content, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
{
$id = empty($id) ? $name : $id;
// Must pass the field id to the buttons in this editor.
$buttons = $this->displayButtons($id, $buttons, $asset, $author);
// Only add "px" to width and height if they are not given as a percentage.
$width .= is_numeric($width) ? 'px' : '';
$height .= is_numeric($height) ? 'px' : '';
// Options for the CodeMirror constructor.
$options = new stdClass;
// Is field readonly?
if (!empty($params['readonly']))
{
$options->readOnly = 'nocursor';
}
// Should we focus on the editor on load?
$options->autofocus = (boolean) $this->params->get('autoFocus', true);
// Until there's a fix for the overflow problem, always wrap lines.
$options->lineWrapping = true;
// Add styling to the active line.
$options->styleActiveLine = (boolean) $this->params->get('activeLine', true);
// Add styling to the active line.
if ($this->params->get('selectionMatches', false))
{
$options->highlightSelectionMatches = array(
'showToken' => true,
'annotateScrollbar' => true,
);
}
// Do we use line numbering?
if ($options->lineNumbers = (boolean) $this->params->get('lineNumbers', 0))
{
$options->gutters[] = 'CodeMirror-linenumbers';
}
// Do we use code folding?
if ($options->foldGutter = (boolean) $this->params->get('codeFolding', 1))
{
$options->gutters[] = 'CodeMirror-foldgutter';
}
// Do we use a marker gutter?
if ($options->markerGutter = (boolean) $this->params->get('markerGutter', $this->params->get('marker-gutter', 0)))
{
$options->gutters[] = 'CodeMirror-markergutter';
}
// Load the syntax mode.
$syntax = $this->params->get('syntax', 'html');
$options->mode = $this->modeAlias[$syntax] ?? $syntax;
// Load the theme if specified.
if ($theme = $this->params->get('theme'))
{
$options->theme = $theme;
JHtml::_('stylesheet', $this->params->get('basePath', 'media/editors/codemirror/') . 'theme/' . $theme . '.css', array('version' => 'auto'));
}
// Special options for tagged modes (xml/html).
if (in_array($options->mode, array('xml', 'htmlmixed', 'htmlembedded', 'php')))
{
// Autogenerate closing tags (html/xml only).
$options->autoCloseTags = (boolean) $this->params->get('autoCloseTags', true);
// Highlight the matching tag when the cursor is in a tag (html/xml only).
$options->matchTags = (boolean) $this->params->get('matchTags', true);
}
// Special options for non-tagged modes.
if (!in_array($options->mode, array('xml', 'htmlmixed', 'htmlembedded')))
{
// Autogenerate closing brackets.
$options->autoCloseBrackets = (boolean) $this->params->get('autoCloseBrackets', true);
// Highlight the matching bracket.
$options->matchBrackets = (boolean) $this->params->get('matchBrackets', true);
}
$options->scrollbarStyle = $this->params->get('scrollbarStyle', 'native');
// Vim Keybindings.
$options->vimMode = (boolean) $this->params->get('vimKeyBinding', 0);
$displayData = (object) array(
'options' => $options,
'params' => $this->params,
'name' => $name,
'id' => $id,
'cols' => $col,
'rows' => $row,
'content' => $content,
'buttons' => $buttons
);
// At this point, displayData can be modified by a plugin before going to the layout renderer.
$results = JFactory::getApplication()->triggerEvent('onCodeMirrorBeforeDisplay', array(&$displayData));
$results[] = JLayoutHelper::render('editors.codemirror.element', $displayData, __DIR__ . '/layouts', array('debug' => JDEBUG));
foreach (JFactory::getApplication()->triggerEvent('onCodeMirrorAfterDisplay', array(&$displayData)) as $result)
{
$results[] = $result;
}
return implode("\n", $results);
}
/**
* Displays the editor buttons.
*
* @param string $name Button name.
* @param mixed $buttons [array with button objects | boolean true to display buttons]
* @param mixed $asset Unused.
* @param mixed $author Unused.
*
* @return string HTML
*/
protected function displayButtons($name, $buttons, $asset, $author)
{
$return = '';
$onGetInsertMethodEvent = new Event(
'onGetInsertMethod',
['name' => $name]
);
$rawResults = $this->getDispatcher()->dispatch('onGetInsertMethod', $onGetInsertMethodEvent);
$results = $rawResults['result'];
if (is_array($results) && !empty($results))
{
foreach ($results as $result)
{
if (is_string($result) && trim($result))
{
$return .= $result;
}
}
}
if (is_array($buttons) || (is_bool($buttons) && $buttons))
{
$buttonsEvent = new Event(
'getButtons',
[
'editor' => $name,
'buttons' => $buttons,
]
);
$buttonsResult = $this->getDispatcher()->dispatch('getButtons', $buttonsEvent);
$buttons = $buttonsResult['result'];
$return .= JLayoutHelper::render('joomla.editors.buttons', $buttons);
}
return $return;
}
/**
* Gets font info from the json data file
*
* @param string $font A key from the $fonts array.
*
* @return object
*/
protected function getFontInfo($font)
{
static $fonts;
if (!$fonts)
{
$fonts = json_decode(file_get_contents(__DIR__ . '/fonts.json'), true);
}
return isset($fonts[$font]) ? (object) $fonts[$font] : null;
}
}
No other release has this exact content. See every version of this file.