diff --git a/packages/forms/src/Components/RichEditor/RichContentFaker.php b/packages/forms/src/Components/RichEditor/RichContentFaker.php new file mode 100644 index 00000000000..1d2ccc91e51 --- /dev/null +++ b/packages/forms/src/Components/RichEditor/RichContentFaker.php @@ -0,0 +1,212 @@ +faker = Factory::create(); + + return $static; + } + + public function renderUsing(RichContentRenderer $renderer): static + { + $this->renderer = $renderer; + + return $this; + } + + public function getRenderer(): RichContentRenderer + { + return $this->renderer ?? RichContentRenderer::make(); + } + + public function asHtml(): string + { + return $this->output; + } + + /** + * @return array + */ + public function asJson(): array + { + return $this->getRenderer()->content($this->output)->toArray(); + } + + public function asText(): string + { + return $this->getRenderer()->content($this->output)->toText(); + } + + public function heading(int | string | null $level = 2): static + { + $this->output .= '' . Str::title($this->faker->words(mt_rand(3, 8), true)) . ''; + + return $this; + } + + public function emptyParagraph(): static + { + $this->output .= '

'; + + return $this; + } + + public function paragraphs(int $count = 1, bool $withRandomLinks = false): static + { + if ($withRandomLinks) { + $this->output .= '

' . collect($this->faker->paragraphs($count))->map(function ($paragraph): string { + $pos = mt_rand(3, strlen($paragraph)); + + $start = substr($paragraph, 0, $pos); + $end = substr($paragraph, $pos); + + $link = ' ' . $this->generateLink() . ' '; + + return $start . $link . $end; + })->implode('

') . '

'; + } else { + $this->output .= '

' . collect($this->faker->paragraphs($count))->implode('

') . '

'; + } + + return $this; + } + + public function link(): static + { + $this->output .= $this->generateLink(); + + return $this; + } + + public function lead(int $paragraphs = 1): static + { + $this->output .= '

' . collect($this->faker->paragraphs($paragraphs))->implode('

') . '

'; + + return $this; + } + + public function small(): static + { + $this->output .= '

' . $this->faker->words(mt_rand(3, 8), true) . '

'; + + return $this; + } + + public function unorderedList(int $count = 1): static + { + $this->output .= ''; + + return $this; + } + + public function orderedList(int $count = 1): static + { + $this->output .= '
  1. ' . collect($this->faker->paragraphs($count))->implode('
  2. ') . '
'; + + return $this; + } + + public function image(?string $source = null, ?int $width = 1280, ?int $height = 720): static + { + if ($source === null || $source === '' || $source === '0') { + $source = 'https://picsum.photos/' . $width . '/' . $height; + } + + $this->output .= '' . $this->faker->sentence . ''; + + return $this; + } + + public function details(bool $open = false): static + { + $this->output .= '' . $this->faker->sentence() . '

' . $this->faker->paragraph() . '

'; + + return $this; + } + + public function code(?string $className = null): static + { + $this->output .= "" . $this->faker->words(mt_rand(3, 5), true) . ''; + + return $this; + } + + public function codeBlock(?string $language = 'sh'): static + { + $this->output .= "
export default function testComponent({\n\tstate,\n}) {\n\treturn {\n\t\tstate,\n\t\tinit: function () {\n\t\t\t// Initialize the Alpine component here, if you need to.\n\t\t},\n\t}\n}
"; + + return $this; + } + + public function blockquote(): static + { + $this->output .= '

' . $this->faker->paragraph() . '

' . '
'; + + return $this; + } + + public function hr(): static + { + $this->output .= '
'; + + return $this; + } + + public function br(): static + { + $this->output .= '
'; + + return $this; + } + + public function table(?int $cols = null): static + { + $cols ??= mt_rand(3, 8); + + $this->output .= '
' . collect($this->faker->words($cols))->implode('') . '
' . collect($this->faker->words($cols))->implode('') . '
' . collect($this->faker->words($cols))->implode('') . '
'; + + return $this; + } + + /** + * @param array $cols + */ + public function grid(array $cols = [1, 1, 1], string $breakpoint = 'md'): static + { + $this->output .= '
'; + + foreach ($cols as $col) { + $this->output .= '

' . Str::title($this->faker->words(mt_rand(3, 8), true)) . '

' . $this->faker->paragraph() . '

'; + } + + $this->output .= '
'; + + return $this; + } + + private function generateLink(): string + { + return '' . $this->faker->words(mt_rand(3, 8), true) . ''; + } +}