-
Notifications
You must be signed in to change notification settings - Fork 17
/
markdown.php
328 lines (295 loc) · 7.78 KB
/
markdown.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* Visual Markdown Editor Field for Kirby 2.
*
* @version 1.5.0
* @author Jonas Döbertin <[email protected]>
* @copyright Jonas Döbertin <[email protected]>
* @link https://github.com/JonasDoebertin/kirby-visual-markdown
* @license GNU GPL v3.0 <http://opensource.org/licenses/GPL-3.0>
*/
/**
* Visual Markdown Editor Field.
*
* @since 1.0.0
*/
class MarkdownField extends InputField
{
/**
* Language files directory.
*
* @since 1.2.0
*/
const LANG_DIR = 'languages';
/**
* Define frontend assets.
*
* @var array
* @since 1.0.0
*/
public static $assets = [
'js' => [
'screenfull-2.0.0.min.js',
'codemirror-compressed-5.8.0.min.js',
'kirbytags-mode.js',
'visualmarkdownfield.js',
'visualmarkdowneditor.js',
],
'css' => [
'codemirror-5.8.0.css',
'visualmarkdown.css',
],
];
/**
* Option: Show/Hide toolbar.
*
* @since 1.1.0
* @var bool
*/
protected $toolbar = true;
/**
* Option: Header 1.
*
* @since 1.1.0
* @var string
*/
protected $header1 = 'h1';
/**
* Option: Header 2.
*
* @since 1.1.0
* @var string
*/
protected $header2 = 'h2';
/**
* Option: Available Tools.
*
* @since 1.3.0
* @var string
*/
protected $tools = [
'header1',
'header2',
'bold',
'italic',
'strikethrough',
'blockquote',
'unorderedList',
'orderedList',
'link',
'email',
'image',
'line',
];
/**
* Translated strings.
*
* @since 1.2.0
* @var array
*/
protected $translation;
/**
* Valid header1/header2 option values.
*
* @since 1.2.0
* @var array
*/
protected $validHeaderValues = [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
];
/**
* Default option values.
*
* @since 1.2.0
* @var array
*/
protected $defaultValues = [
'header1' => 'h1',
'header2' => 'h2',
'tools' => [
'header1',
'header2',
'bold',
'italic',
'blockquote',
'unorderedList',
'orderedList',
'link',
'image',
'line',
],
];
/**************************************************************************\
* GENERAL FIELD METHODS *
\**************************************************************************/
/**
* Field setup.
*
* @since 1.2.0
*/
public function __construct()
{
// Build translation file path
$baseDir = __DIR__ . DS . self::LANG_DIR . DS;
// Get panel language
if (version_compare(panel()->version(), '2.2', '>=')) {
$lang = panel()->translation()->code();
} else {
$lang = panel()->language();
}
// Load language files
if (file_exists($baseDir . $lang . '.php')) {
$this->translation = include $baseDir . $lang . '.php';
} else {
$this->translation = include $baseDir . 'en.php';
}
}
/**
* Magic setter.
*
* Set a fields property and apply default value if required.
*
* @since 1.1.0
* @param string $option
* @param mixed $value
*/
public function __set($option, $value)
{
/* Set given value */
$this->$option = $value;
/* Check if value is valid */
switch ($option) {
case 'toolbar':
$this->validateToolbarOption($value);
break;
case 'header1':
case 'header2':
$this->validateHeaderOption($option, $value);
break;
case 'tools':
$this->validateToolsOption($value);
break;
}
}
/**
* Validate "toolbar" option.
*
* @since 1.3.0
* @param mixed $value
*/
protected function validateToolbarOption($value)
{
$this->toolbar = !in_array($value, ['false', 'hide', 'no', false]);
}
/**
* Validate "headerX" option.
*
* @since 1.3.0
* @param string $header
* @param array $value
*/
protected function validateHeaderOption($header, $value)
{
if (!in_array($value, $this->validHeaderValues)) {
$this->$header = $this->defaultValues[$header];
}
}
/**
* Validate "tools" option.
*
* @since 1.3.0
* @param array $value
*/
protected function validateToolsOption($value)
{
if (!is_array($value) or empty($value)) {
$this->tools = $this->defaultValues['tools'];
}
}
/**
* Convert result to markdown.
*
* @since 1.0.0
* @return string
*/
public function result()
{
return str_replace(["\r\n", "\r"], "\n", parent::result());
}
/**************************************************************************\
* PANEL FIELD MARKUP *
\**************************************************************************/
/**
* Create input element.
*
* @since 1.0.0
* @return \Brick
*/
public function input()
{
// Set up modals
$modals = tpl::load(__DIR__ . DS . 'partials' . DS . 'modals.php', ['field' => $this]);
// Set up translation
$translation = tpl::load(__DIR__ . DS . 'partials' . DS . 'translation.php', ['translations' => $this->translation]);
// Set up textarea
$input = parent::input();
$input->tag('textarea');
$input->removeAttr('type');
$input->removeAttr('value');
$input->html($this->value() ?: false);
$input->data([
'field' => 'markdownfield',
'toolbar' => ($this->toolbar) ? 'true' : 'false',
'tools' => implode(',', $this->tools),
'header1' => $this->header1,
'header2' => $this->header2,
'kirbytext' => (c::get('panel.kirbytext', true)) ? 'true' : 'false',
]);
/*
FIX: Prevent Google Chrome from trying to validate the underlying
invisible textarea. the Panel will handle this instead.
See: https://github.com/JonasDoebertin/kirby-visual-markdown/issues/42
*/
$input->removeAttr('required');
// Set up wrapping element
$wrapper = new Brick('div', false);
$wrapper->addClass('markdownfield-wrapper');
$wrapper->addClass('markdownfield-field-' . $this->name);
return $wrapper
->append($modals)
->append($translation)
->append($input);
}
/**
* Create outer field element.
*
* @since 1.0.0
* @return \Brick
*/
public function element()
{
$element = parent::element();
$element->addClass('field-with-visualmarkdown');
return $element;
}
/**************************************************************************\
* HELPERS *
\**************************************************************************/
/**
* Return a translation from the internal translation storage.
*
* @since 1.3.2
* @param string $key
* @param string $default
* @return string
*/
public function lang($key, $default = '')
{
return (isset($this->translation[$key]) ? $this->translation[$key] : $default);
}
}