winter/modules/backend/formwidgets/MarkdownEditor.php

86 lines
1.7 KiB
PHP
Raw Normal View History

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\Models\EditorPreferences;
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 bool Display mode: split, tab.
*/
public $mode = 'tab';
2015-07-29 19:18:42 +10:00
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'markdown';
/**
* {@inheritDoc}
*/
public function init()
{
$this->fillFromConfig([
'mode',
]);
2015-07-29 19:18:42 +10:00
}
/**
* {@inheritDoc}
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('markdowneditor');
}
/**
* Prepares the widget data
*/
public function prepareVars()
{
$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;
$this->vars['name'] = $this->formField->getName();
$this->vars['value'] = $this->getLoadValue();
}
/**
* {@inheritDoc}
*/
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()
{
$value = post($this->formField->getName());
$previewHtml = Markdown::parse($value);
return [
'preview' => $previewHtml
];
}
2015-07-29 19:18:42 +10:00
}