-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarkdownEditor.php
78 lines (70 loc) · 2.11 KB
/
MarkdownEditor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @package yii2-markdown
* @author Razzwan <razvanlomov@gmail.com>
* @version 1.0
*/
namespace altiore\yii2\markdown;
use Yii;
use yii\helpers\Html;
use yii\web\View;
use yii\widgets\InputWidget;
/**
* Markdown provides concrete implementation for A simple, beautiful, and embeddable JavaScript Markdown editor.
* - Delightful editing for beginners and experts alike.
* - Features built-in autosaving and spell checking. https://simplemde.com
* @author Razzwan <razvanlomov@gmail.com>
* @since 1.0
*/
class MarkdownEditor extends InputWidget
{
/**
* Initialize the widget
*/
public function init()
{
parent::init();
$this->options['id'] = array_key_exists('id', $this->options) ? $this->options['id'] : $this->id;
$this->registerAssets();
echo $this->renderInput();
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
MarkdownEditorAsset::register($view);
$js = "var simplemde = new SimpleMDE({ {$this->prepareOptionsForSimpleMDE()} });";
$view->registerJs($js, View::POS_END);
}
/**
* Render the text area input
*/
protected function renderInput()
{
if ($this->hasModel()) {
$input = Html::activeTextArea($this->model, $this->attribute, $this->options);
} else {
$input = Html::textArea($this->name, $this->value, $this->options);
}
return $input;
}
/**
* Prepare options to widget
* @return string
*/
private function prepareOptionsForSimpleMDE()
{
$this->options['element'] = "document.getElementById('{$this->options['id']}')";
unset($this->options['id']);
foreach ($this->options as $key => $value) {
if (strpos($value, 'document') !== false || strpos($value, '$') !== false) {
$this->options[$key] = $key . ': ' . $value;
} else {
$this->options[$key] = $key . ': "' . $value . '"';
}
}
return implode(', ', $this->options);
}
}