1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class MarkdownTextareaField extends TextareaField { |
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Define the actions allowed on this field |
7
|
|
|
* @var array |
8
|
|
|
*/ |
9
|
|
|
private static $allowed_actions = array( |
|
|
|
|
10
|
|
|
'preview', |
11
|
|
|
'parse' |
12
|
|
|
); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int Visible number of text lines. |
16
|
|
|
* Default on TextareaField is too small |
17
|
|
|
*/ |
18
|
|
|
protected $rows = 15; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Toggle rendering markdown with extra syntax enabled. |
22
|
|
|
* @link http://michelf.ca/projects/php-markdown/extra |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected $enable_extra = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the field holder used by templates |
29
|
|
|
* @return string HTML to be used |
30
|
|
|
*/ |
31
|
|
|
public function FieldHolder($properties = array()) { |
32
|
|
|
|
33
|
|
|
$this->extraClasses['stacked'] = 'stacked'; |
34
|
|
|
|
35
|
|
|
Requirements::css('markdowntextareafield/templates/css/styles.css'); |
36
|
|
|
|
37
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
38
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); |
39
|
|
|
Requirements::javascript('markdowntextareafield/thirdparty/textinputs_jquery.js'); |
40
|
|
|
Requirements::javascript('markdowntextareafield/templates/javascript/script.js'); |
41
|
|
|
|
42
|
|
|
return parent::FieldHolder($properties); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Turn on extra syntax support |
47
|
|
|
* @return MarkdownTextareaField |
48
|
|
|
*/ |
49
|
|
|
public function enableExtra() { |
50
|
|
|
$this->enable_extra = true; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Body for the preview iframe with just the typography styles included |
57
|
|
|
* @return string html |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function preview() { |
60
|
|
|
Requirements::clear(); |
61
|
|
|
// Should contain text styles of the page by Silverstripe theme conventions. |
62
|
|
|
Requirements::css('themes/' . Config::inst()->get('SSViewer', 'theme') . '/css/editor.css'); |
63
|
|
|
return $this->renderWith('PreviewFrame'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Parse markdown into html |
68
|
|
|
* @return string html |
69
|
|
|
*/ |
70
|
|
|
public function parse() { |
71
|
|
|
$parser = new MarkdownParser($this->request['markdown']); |
72
|
|
|
|
73
|
|
|
return ($this->enable_extra) ? $parser->parseExtra() : $parser->parse(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get buttons described in buttons.yml and wrap them in ViewableData |
78
|
|
|
* @return ArrayList list of buttons and theyr configurations |
79
|
|
|
*/ |
80
|
|
|
public function ToolbarButtons() { |
81
|
|
|
$buttons = new ArrayList(); |
82
|
|
|
|
83
|
|
|
foreach($this->config()->get('buttons') as $button) { |
|
|
|
|
84
|
|
|
$buttons->push(new ArrayData($button)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $buttons; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.