ArticleTemplate.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Render\RenderContext;
  6. /**
  7. * 嵌入文章 / 章节:`{{article|type|id|title|channel|style|book|paragraphs|anthology}}`。
  8. * `type=chapter` 且没给 id 时由 book + paragraphs 拼出 id。
  9. */
  10. final class ArticleTemplate extends Template
  11. {
  12. protected string $component = 'article';
  13. public function params(): array
  14. {
  15. return [
  16. 'type' => ParamSpec::at(1),
  17. 'id' => ParamSpec::at(2),
  18. 'title' => ParamSpec::at(3),
  19. 'channel' => ParamSpec::at(4),
  20. 'style' => ParamSpec::at(5),
  21. 'book' => ParamSpec::at(6),
  22. 'paragraphs' => ParamSpec::at(7),
  23. 'anthology' => ParamSpec::at(8),
  24. ];
  25. }
  26. /**
  27. * @return array<string, mixed>
  28. */
  29. public function props(Call $call, RenderContext $ctx): array
  30. {
  31. $type = (string) $call->param('type');
  32. $id = (string) $call->param('id');
  33. $book = (string) $call->param('book');
  34. $paragraphs = (string) $call->param('paragraphs');
  35. if ($type === 'chapter' && $id === '') {
  36. $id = ((int) $book).'-'.((int) $paragraphs);
  37. }
  38. $props = ['type' => $type, 'id' => $id];
  39. // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
  40. foreach (['style', 'channel', 'title', 'anthology'] as $name) {
  41. $value = (string) $call->param($name);
  42. if ($value !== '') {
  43. $props[$name] = $value;
  44. }
  45. }
  46. if ($book !== '') {
  47. $props['book'] = $book;
  48. }
  49. if ($paragraphs !== '') {
  50. $props['paragraphs'] = $paragraphs;
  51. }
  52. $props['parentChannels'] = $call->scope->channels;
  53. return $props;
  54. }
  55. /**
  56. * 章节 / 段落(type=chapter|para)引用了经文段落;文章、文集不算。
  57. */
  58. public function references(Call $call): array
  59. {
  60. $type = (string) $call->param('type');
  61. if (! in_array($type, ['chapter', 'para'], true)) {
  62. return [];
  63. }
  64. $id = (string) $call->param('id');
  65. if ($id === '') {
  66. $id = $call->param('book').'-'.$call->param('paragraphs');
  67. }
  68. return self::paragraphRef($id);
  69. }
  70. public function plain(Call $call, RenderContext $ctx, Format $format): string
  71. {
  72. return $format === Format::Prompt ? '' : (string) $call->param('title');
  73. }
  74. public function html(Call $call, RenderContext $ctx): string
  75. {
  76. return '';
  77. }
  78. }