-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPartsKit.php
354 lines (317 loc) · 10.4 KB
/
PartsKit.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
namespace viget\base\services;
use Craft;
use craft\helpers\StringHelper;
use craft\helpers\FileHelper;
use craft\helpers\Template as TemplateHelper;
use craft\elements\Asset;
use Twig\Markup;
use viget\base\models\NavNode;
use viget\base\Module;
/**
* Functionality for easily dropping in "Storybook" style parts kit
*/
class PartsKit
{
/**
* Determine if this is a request to the parts kit
*
* @return boolean
*/
public static function isRequest(): bool
{
return (Craft::$app->request->segments[0] ?? null) === self::getConfig('directory');
}
/**
* Determine if this is a request to the root of the parts kit
* @return bool
*/
public static function isRootRequest(): bool
{
$isRoot = count(Craft::$app->request->getSegments()) === 1;
return $isRoot && self::isRequest();
}
/**
* Get a config item either the default or from the config file
*
* @param string $key
* @return string|array|null
*/
public static function getConfig(string $key)
{
return Module::$config['partsKit'][$key] ?? null;
}
/**
* Get navigation of files/folders in parts kit
*
* @return array
*/
public static function getNav(): array
{
$templatesPath = Craft::$app->getPath()->getSiteTemplatesPath();
$partsKitFolderName = self::getConfig('directory');
$partsPath = $templatesPath . '/' . $partsKitFolderName . '/';
// Combine and sort all files & directories in the parts kit
$directories = FileHelper::findDirectories($partsPath);
$files = FileHelper::findFiles($partsPath);
$templates = [
...$directories,
...$files
];
$skipPaths = [
$partsPath . 'index.twig',
$partsPath . 'index.html',
];
sort($templates);
/** @var NavNode[] $result */
$result = [];
// Loop through all the paths in the folder.
// Creating a NavNode object and putting the path to each node in the map.
foreach ($templates as $templatePath) {
// Skip ignored paths
if (in_array($templatePath, $skipPaths)) {
continue;
}
$path = str_replace($partsPath, '', $templatePath);
$pathParts = explode('/', $path);
$title = self::_formatTitle(end($pathParts));
$url = is_file($templatePath)
? '/' . $partsKitFolderName . '/' . self::_removeExtension($path)
: null;
$result[$path] = new NavNode(
title: $title,
path: $path,
url: $url,
);
}
// Reverse the array, so we can build up child nav nodes into their parents.
// We remove child nodes as they're added.
$result = array_reverse($result);
foreach ($result as $node) {
$pathParts = explode('/', $node->path);
$parentPath = implode('/', array_slice($pathParts, 0, -1));
$parentNode = $result[$parentPath] ?? null;
if (!$parentNode) {
continue;
}
array_unshift($parentNode->children, $node);
unset($result[$node->path]);
}
// Sort by keys so they're in alpha order
ksort($result);
return array_values($result);
}
/**
* The JS file for the parts kit UI library. This needs to be added on both the
* parts kit root page and on each "part" page.
*
* unpkg.com respects server. This URL will need to be adjusted when major versions
* are released.
*
* @return string
*/
public static function getUiScriptUrl(): string
{
return 'https://unpkg.com/@viget/parts-kit@^0/lib/parts-kit.js';
}
/**
* Remove extension from file name
*
* @param string $file
* @return string
*/
private static function _removeExtension(string $file): string
{
$extensions = array_map(function($extension) {
return '.' . $extension;
}, Craft::$app->config->general->defaultTemplateExtensions);
return str_replace($extensions, '', $file);
}
/**
* Humanize the string and replace - with space
*
* @param string $str
* @return string
*/
private static function _formatTitle(string $str): string
{
$str = self::_removeExtension($str);
$str = StringHelper::toKebabCase($str);
$str = StringHelper::humanize($str);
return str_replace('-', ' ', $str);
}
/**
* Get parts kit asset by file name
*
* @param string $name
* @return Asset|null
*/
public static function getImage(string $name): ?Asset
{
$volume = Craft::$app->getVolumes()->getVolumeByHandle(self::getConfig('volume'));
if (!$volume) return null;
return Asset::findOne([
'filename' => $name,
'volumeId' => $volume->id,
]);
}
/**
* Get random Lorem ipsum text
*
* @param integer $words
* @return string
*/
public static function getText(int $words = 10): string
{
$dictionary = [
'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'sed', 'do', 'eiusmod' ,'tempor', 'incididunt', 'labore', 'et', 'magna', 'aliqua', 'enim', 'ad', 'minim' ,'veniam', 'quis', 'nostrud', 'exercitation', 'ullamco', 'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea' ,'commodo', 'consequat', 'duis', 'aute', 'irure', 'reprehenderit', 'voluptate' ,'velit', 'esse', 'cillum', 'dolore', 'eu', 'fugiat', 'nulla', 'pariatur', 'excepteur', 'sint' ,'occaecat', 'cupidatat', 'non', 'proident', 'sunt', 'in', 'culpa', 'qui', 'officia', 'deserunt' ,'mollit', 'anim', 'id', 'est', 'laborum',
];
$selection = [];
while(count($selection) < $words) {
$selection[] = $dictionary[array_rand($dictionary)];
}
return StringHelper::humanize(implode(' ', $selection));
}
/**
* Get random Lorem ipsum title
*
* @param integer $words
* @return string
*/
public static function getTitle(int $words = 5): string
{
return self::getText($words);
}
/**
* Get random Lorem ipsum sentence
*
* @param integer $words
* @return string
*/
public static function getSentence(int $words = 10): string
{
return self::getText($words) . '.';
}
/**
* Get random Lorem ipsum paragraph
*
* @param integer $sentences
* @return string
*/
public static function getParagraph(int $sentences = 5): string
{
$selection = [];
while (count($selection) < $sentences) {
$selection[] = self::getSentence(rand(8, 12));
}
return implode(' ', $selection);
}
/**
* Get random Lorem ipsum short rich text sample
*
* @return Markup
*/
public static function getRichTextShort(): Markup
{
return TemplateHelper::raw(
'<p>' .
self::getSentence(8) .
' <strong>' . self::getSentence(5). '</strong> ' .
self::getSentence() .
' <b>' . self::getSentence(3). '</b> ' .
'</p>' .
'<p>' .
self::getSentence(14) .
'</p>' .
'<p>' .
'<a href="#">' . self::getSentence(4) . '</a> ' .
self::getSentence(8) .
' <em>' . self::getSentence(5). '</em> ' .
self::getSentence() .
' <i>' . self::getSentence(3). '</i> ' .
'</p>'
);
}
/**
* Get random Lorem ipsum kitchen sink rich text example
*
* @return Markup
*/
public static function getRichTextFull(): Markup
{
return TemplateHelper::raw(
'<h2>Level 2 Heading</h2>' .
self::getRichTextShort() .
'<hr>
<h3>Level 3 Heading</h3>
<p>' . self::getSentence() . '</p>
<blockquote>
<p>Blockquote: ' . self::getSentence() . '</p>
<footer>—Quote Attribution</footer>
</blockquote>
<ul>
<li>
<strong>Bold text</strong>
Unordered list item
</li>
<li>
Unordered list item
<ul>
<li>Nested unordered list item</li>
<li>Nested unordered list item</li>
</ul>
</li>
<li>Unordered list item</li>
</ul>
<hr>
<h4>Level 4 Heading</h4>
<ol>
<li>
Ordered list item
</li>
<li>
Ordered list item
<ol>
<li>Nested ordered list item</li>
<li>Nested ordered list item</li>
</ol>
</li>
<li>Ordered list item</li>
</ol>
<p>' . self::getSentence() . '</p>
<table>
<thead>
<tr>
<th scope="col">Table Header Cell</th>
<th scope="col">Table Header Cell</th>
<th scope="col">Table Header Cell</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Table Header Cell</th>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
</tr>
<tr>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
</tr>
<tr>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Table Header Cell</th>
<td>Table Data Cell</td>
<td>Table Data Cell</td>
</tr>
</tfoot>
</table>'
);
}
}