winter/modules/backend/formwidgets/MarkdownEditor.php
Klaas Poortinga 2ad046f7ae
Markdown formwidget improvements (#5004)
* add readonly and disabled support to markdown editor

* add attributes support to markdown editor
2020-03-29 09:37:03 -06:00

108 lines
2.3 KiB
PHP

<?php namespace Backend\FormWidgets;
use BackendAuth;
use Markdown;
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
//
/**
* @var string Display mode: split, tab.
*/
public $mode = 'tab';
/**
* @var bool Render preview with safe markdown.
*/
public $safe = false;
/**
* @var bool If true, the editor is set to read-only mode
*/
public $readOnly = false;
/**
* @var bool If true, the editor is set to read-only mode
*/
public $disabled = false;
//
// Object properties
//
/**
* @inheritDoc
*/
protected $defaultAlias = 'markdown';
/**
* @inheritDoc
*/
public function init()
{
$this->fillFromConfig([
'mode',
'safe',
'readOnly',
'disabled',
]);
}
/**
* @inheritDoc
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('markdowneditor');
}
/**
* Prepares the widget data
*/
public function prepareVars()
{
$this->vars['mode'] = $this->mode;
$this->vars['stretch'] = $this->formField->stretch;
$this->vars['size'] = $this->formField->size;
$this->vars['name'] = $this->getFieldName();
$this->vars['value'] = $this->getLoadValue();
$this->vars['readOnly'] = $this->readOnly;
$this->vars['disabled'] = $this->disabled;
$this->vars['useMediaManager'] = BackendAuth::getUser()->hasAccess('media.manage_media');
}
/**
* @inheritDoc
*/
protected function loadAssets()
{
$this->addCss('css/markdowneditor.css', 'core');
$this->addJs('js/markdowneditor.js', 'core');
$this->addJs('/modules/backend/formwidgets/codeeditor/assets/js/build-min.js', 'core');
}
public function onRefresh()
{
$value = post($this->getFieldName());
$previewHtml = $this->safe
? Markdown::parseSafe($value)
: Markdown::parse($value);
return [
'preview' => $previewHtml
];
}
}