2015-07-29 19:18:42 +10:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
2015-08-01 20:20:43 +10:00
|
|
|
use Markdown;
|
2015-07-29 19:18:42 +10:00
|
|
|
use Backend\Classes\FormWidgetBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Code Editor
|
|
|
|
* Renders a code editor field.
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class MarkdownEditor extends FormWidgetBase
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Configurable properties
|
|
|
|
//
|
|
|
|
|
2015-08-02 10:17:26 +10:00
|
|
|
/**
|
2016-05-08 17:43:21 +10:00
|
|
|
* @var string Display mode: split, tab.
|
2015-08-02 10:17:26 +10:00
|
|
|
*/
|
|
|
|
public $mode = 'tab';
|
|
|
|
|
2017-07-22 19:01:20 +10:00
|
|
|
/**
|
|
|
|
* @var bool Render preview with safe markdown.
|
|
|
|
*/
|
|
|
|
public $safe = false;
|
|
|
|
|
2015-07-29 19:18:42 +10:00
|
|
|
//
|
|
|
|
// Object properties
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-07-29 19:18:42 +10:00
|
|
|
*/
|
|
|
|
protected $defaultAlias = 'markdown';
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-07-29 19:18:42 +10:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
2015-08-02 10:17:26 +10:00
|
|
|
$this->fillFromConfig([
|
|
|
|
'mode',
|
2017-07-22 19:01:20 +10:00
|
|
|
'safe',
|
2015-08-02 10:17:26 +10:00
|
|
|
]);
|
2015-07-29 19:18:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-07-29 19:18:42 +10:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$this->prepareVars();
|
|
|
|
return $this->makePartial('markdowneditor');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the widget data
|
|
|
|
*/
|
|
|
|
public function prepareVars()
|
|
|
|
{
|
2015-08-02 10:17:26 +10:00
|
|
|
$this->vars['mode'] = $this->mode;
|
2015-07-29 19:18:42 +10:00
|
|
|
$this->vars['stretch'] = $this->formField->stretch;
|
|
|
|
$this->vars['size'] = $this->formField->size;
|
2016-11-28 07:50:06 +11:00
|
|
|
$this->vars['name'] = $this->getFieldName();
|
2015-07-29 19:18:42 +10:00
|
|
|
$this->vars['value'] = $this->getLoadValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-07-29 19:18:42 +10:00
|
|
|
*/
|
2015-08-04 19:32:51 +10:00
|
|
|
protected function loadAssets()
|
2015-07-29 19:18:42 +10:00
|
|
|
{
|
|
|
|
$this->addCss('css/markdowneditor.css', 'core');
|
|
|
|
$this->addJs('js/markdowneditor.js', 'core');
|
|
|
|
$this->addJs('/modules/backend/formwidgets/codeeditor/assets/js/build-min.js', 'core');
|
|
|
|
}
|
|
|
|
|
2015-08-01 20:20:43 +10:00
|
|
|
public function onRefresh()
|
|
|
|
{
|
2016-11-28 07:50:06 +11:00
|
|
|
$value = post($this->getFieldName());
|
2017-07-22 19:01:20 +10:00
|
|
|
$previewHtml = $this->safe
|
|
|
|
? Markdown::parseSafe($value)
|
|
|
|
: Markdown::parse($value);
|
2015-08-01 20:20:43 +10:00
|
|
|
|
|
|
|
return [
|
|
|
|
'preview' => $previewHtml
|
|
|
|
];
|
|
|
|
}
|
2015-07-29 19:18:42 +10:00
|
|
|
}
|