WithMathRender.php 914 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Livewire\Traits;
  3. trait WithMathRender
  4. {
  5. public function bootWithMathRender(): void
  6. {
  7. // 标记该组件需要数学公式渲染
  8. $this->dispatch = array_merge($this->dispatch ?? [], [
  9. 'math:render' => 'triggerMathRender'
  10. ]);
  11. }
  12. public function triggerMathRender(): void
  13. {
  14. // 这个方法可以被前端调用来手动触发数学公式渲染
  15. $this->dispatch('math-rendered');
  16. }
  17. protected function renderMathContent(string $content): string
  18. {
  19. // 在服务器端预解析数学公式(可选)
  20. // 注意:通常我们只在客户端渲染,这里只是预处理
  21. return $content;
  22. }
  23. /**
  24. * 在视图中调用以获取需要渲染的内容
  25. */
  26. public function getMathContent($content): string
  27. {
  28. return $this->renderMathContent($content ?? '');
  29. }
  30. }