This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
field.php
executable file
·169 lines (154 loc) · 5.29 KB
/
field.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
<?php
return [
'mixins' => ['filepicker', 'upload'],
'props' => [
'allowed' => function (array $allowed = null) {
return $allowed;
},
'autofocus' => function (bool $autofocus = false) {
return $autofocus;
},
'default' => function ($default = null) {
return $default;
},
'disallowed' => function (array $disallowed = null) {
return $disallowed;
},
/**
* Sets the options for the files picker
*/
'files' => function ($files = []) {
if (is_string($files) === true) {
return ['query' => $files];
}
if (is_array($files) === false) {
$files = [];
}
return $files;
},
'pretty' => function (bool $pretty = true) {
return $pretty;
},
'spellcheck' => function (bool $spellcheck = true) {
return $spellcheck;
},
'value' => function ($value = null) {
return $this->toValue($value);
},
],
'computed' => [
'isSupported' => function () {
if (version_compare($this->kirby()->version(), '3.3.0', '<')) {
throw new Exception('The editor requires Kirby version 3.3.0 or higher');
}
},
'default' => function () {
return $this->toValue($this->default);
}
],
'methods' => [
'toBlocks' => function ($value) {
return Kirby\Editor\Blocks::factory($value, $this->model());
},
'toValue' => function ($value) {
if ($this->inline) {
if (is_array($value) === false) {
try {
$value = Json::decode((string)$value);
} catch (Throwable $e) {
return $value;
}
}
return $this->toBlocks($value)->toHtml();
} else {
return $this->toBlocks($value)->toArray();
}
}
],
'save' => function ($value) {
if (empty($value)) {
return '';
}
if ($this->inline) {
return $value;
}
$value = $this
->toBlocks($value)
->toStorage();
if ($this->pretty === true) {
return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return json_encode($value);
},
'api' => function () {
return [
[
'pattern' => 'files',
'action' => function () {
$field = $this->field();
$files = $field->files();
// inject stuff from the query
$files['page'] = $this->requestQuery('page');
$files['search'] = $this->requestQuery('search');
return $field->filepicker($files);
}
],
[
'pattern' => 'upload',
'method' => 'POST',
'action' => function () {
return $this->field()->upload($this, $this->field()->uploads(), function ($file) {
return [
'filename' => $file->filename(),
'link' => $file->panelUrl(true),
'url' => $file->url(),
];
});
}
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () {
return Kirby\Editor\Blocks::factory(get('html'), $this->field()->model())->toArray();
}
],
[
'pattern' => 'import',
'method' => 'POST',
'action' => function () {
$api = $this;
$field = $this->field();
return $this->upload(function ($source, $filename) use ($api, $field) {
$html = F::read($source);
$blocks = Kirby\Editor\Blocks::factory($html, $field->model())->toArray();
return $blocks;
});
}
],
[
'pattern' => 'export',
'method' => 'POST',
'action' => function () {
$data = get('data');
$type = get('type');
$blocks = Kirby\Editor\Blocks::factory($data, $this->field()->model());
switch ($type) {
case 'md':
$result = $blocks->toMarkdown();
break;
case 'html':
$result = $blocks->toHtml();
break;
default:
$result = json_encode($blocks->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
break;
}
return [
'data' => $result
];
}
],
];
},
];