ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilUIMarkdownPreviewGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 use ILIAS\Data\URI;
26 
32 {
33  protected const CMD_RENDER_ASYNC = 'renderAsync';
34 
35  protected Refinery $refinery;
37  protected HTTPServices $http;
38 
39  public function __construct()
40  {
41  global $DIC;
42 
43  $this->refinery = $DIC->refinery();
44  $this->ctrl = $DIC->ctrl();
45  $this->http = $DIC->http();
46  }
47 
48  public function executeCommand(): void
49  {
50  if (!$this->ctrl->isAsynch() && self::CMD_RENDER_ASYNC !== $this->ctrl->getCmd()) {
51  $this->http->saveResponse(
52  $this->http
53  ->response()
54  ->withBody(Streams::ofString('Whoops, something went wrong!'))
55  ->withStatus(404)
56  );
57 
58  $this->http->sendResponse();
59  $this->http->close();
60  }
61 
62  $this->renderAsync();
63  }
64 
65  public function getAsyncUrl(): string
66  {
67  return $this->ctrl->getLinkTargetByClass(
68  self::class,
69  self::CMD_RENDER_ASYNC,
70  null,
71  true
72  );
73  }
74 
75  public function getParameterName(): string
76  {
77  return 'raw_markdown';
78  }
79 
80  public function renderAsync(): void
81  {
82  $parameter_name = $this->getParameterName();
83 
84  if (!$this->http->wrapper()->post()->has($parameter_name)) {
85  $this->sendResponse('');
86  }
87 
88  $raw_markdown = $this->http->wrapper()->post()->retrieve(
89  $parameter_name,
90  $this->refinery->to()->string()
91  );
92 
93  $this->sendResponse($this->render($raw_markdown));
94  }
95 
96  public function render(string $markdown_text): string
97  {
98  return $this->refinery->string()->markdown()->toHTML()->transform($markdown_text);
99  }
100 
101  protected function sendResponse(string $html): void
102  {
103  $this->http->saveResponse(
104  $this->http->response()->withBody(Streams::ofString($html))
105  );
106 
107  $this->http->sendResponse();
108  $this->http->close();
109  }
110 }
getAsyncUrl()
Returns an endpoint where the clientside input can submit it&#39;s value to and receive the current previ...
render(string $markdown_text)
Returns the rendered preview of the given string.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
getParameterName()
Returns the name of the $_POST variable the asynchronous input submits to.
global $DIC
Definition: shib_login.php:22
renderAsync()
Sends a JSON-response with the rendered preview of the submitted input.