-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
markdown.php
191 lines (172 loc) · 6.69 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
<?php
use Kirby\Toolkit\A;
$options = require __DIR__ . '/../../../../../kirby/config/fields/textarea.php';
/* Merge new properties
--------------------------------*/
$options = A::merge($options, [
'props' => [
/**
* Sets the toolbar buttons.
*/
'buttons' => function($buttons = null) {
$buttons = $buttons ?? option('community.markdown-field.buttons');
$uniqueButtons = array();
foreach ($buttons as $button) {
if ($button == 'divider' || !in_array($button, $uniqueButtons)) {
$uniqueButtons[] = $button;
}
}
return $uniqueButtons;
},
/*
* Sets the editor font. Allowed values: monospace, sans-serif
*/
'font' => function($font = null) {
$fontOptions = option('community.markdown-field.font');
if($font) {
// if there is a family set but no scaling option
if(array_key_exists('family', $font) && !array_key_exists('scaling', $font)) {
// default scaling: true for sans-serif, false for monospace
$scaling = $font['family'] == 'sans-serif';
$fontOptions = A::merge($fontOptions, array('scaling' => $scaling));
}
// merge field options with global defaults
$fontOptions = A::merge($fontOptions, $font);
}
return $fontOptions;
},
/**
* Whether link / email buttons should open a modal. Boolean.
*/
'modals' => function($modals = null) {
return $modals ?? option('community.markdown-field.modals');
},
/**
* Whether link dialogs enable editors to easily set a target="_blank". Boolean.
*/
'blank' => function($blank = null) {
return $blank ?? option('community.markdown-field.blank');
},
/**
* Whether the 'invisibles' button should be displayed. Boolean.
*/
'invisibles' => function($invisibles = null) {
return $invisibles ?? option('community.markdown-field.invisibles');
},
'query' => function($query = null) {
$queryOptions = option('community.markdown-field.query');
$queryOptions = $query ? A::merge($queryOptions, $query) : $queryOptions;
return $queryOptions;
}
],
'computed' => [
/**
* Returns an array of known KirbyTags, used by the syntax highlighter.
* Highlighting only known KirbyTags decreases the chance of false
* positives.
*/
'kirbytags' => function() {
$tags = array_keys(kirby()->extensions('tags'));
return $tags;
}
],
'methods' => [
'pageResponse' => function($page) {
$thumb = ['width' => 100, 'height' => 100];
$image = $page->panelImage($this->image, $thumb);
$model = $this->model();
return [
'text' => $page->toString('{{ page.title }}'),
'link' => $page->panelUrl(true),
'id' => $page->id(),
'info' => false,
'image' => $image,
'icon' => $page->panelIcon($image),
'hasChildren' => $page->hasChildren(),
];
},
'fileResponse' => function($file) {
$thumb = ['width' => 100, 'height' => 100];
$image = $file->panelImage($this->image, $thumb);
$model = $this->model();
$uuid = $file->parent() === $model ? $file->filename() : $file->id();
return [
'filename' => $file->filename(),
'text' => $file->toString('{{ file.filename }}'),
'link' => $file->panelUrl(true),
'id' => $file->id(),
'uuid' => $uuid,
'url' => $file->url(),
'info' => $file->toString(false),
'image' => $image,
'icon' => $file->panelIcon($image),
'type' => $file->type(),
];
},
],
'api' => function() {
return [
[
'pattern' => 'get-pages',
'action' => function () {
$field = $this->field();
$query = $field->query()['pagelink'];
if ($query) {
$pages = $field->model()->query($query, 'Kirby\Cms\Pages');
$model = null;
}
else {
if (!$parent = $this->site()->find($this->requestQuery('parent'))) {
$parent = $this->site();
}
$pages = $parent->children();
$model = [
'id' => $parent->id() == '' ? null : $parent->id(),
'title' => $parent->title()->value()
];
}
$children = [];
foreach ($pages as $index => $page) {
if ($page->isReadable() === true) {
$children[] = $field->pageResponse($page);
}
}
return [
'model' => $model,
'pages' => $children
];
}
],
[
'pattern' => 'get-images',
'method' => 'GET',
'action' => function () {
$field = $this->field();
$files = $field->model()->query('page.images', 'Kirby\Cms\Files');
$files = $files ?? $field->model()->query('site.images', 'Kirby\Cms\Files');
$data = [];
foreach ($files as $index => $file) {
$data[] = $field->fileResponse($file);
}
return $data;
}
],
[
'pattern' => 'get-files',
'method' => 'GET',
'action' => function () {
$field = $this->field();
$files = $field->model()->query('page.files.filterBy("type", "!=", "image")', 'Kirby\Cms\Files');
$files = $files ?? $field->model()->query('site.files.filterBy("type", "!=", "image")', 'Kirby\Cms\Files');
$data = [];
foreach ($files as $index => $file) {
$data[] = $field->fileResponse($file);
}
return $data;
}
]
];
},
]);
// return the updated options
return $options;