| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 6 |
|
| 7 |
class ToC extends Block { |
| 8 |
public function get_name() { |
| 9 |
return 'table-of-contents'; |
| 10 |
} |
| 11 |
|
| 12 |
public function get_default_attributes() { |
| 13 |
return [ |
| 14 |
'blockId' => '', |
| 15 |
'toc_supported_tags' => [ |
| 16 |
[ |
| 17 |
'value' => 1, |
| 18 |
'label' => 'H1' |
| 19 |
], |
| 20 |
[ |
| 21 |
'value' => 2, |
| 22 |
'label' => 'H2' |
| 23 |
], |
| 24 |
[ |
| 25 |
'value' => 3, |
| 26 |
'label' => 'H3' |
| 27 |
], |
| 28 |
[ |
| 29 |
'value' => 4, |
| 30 |
'label' => 'H4' |
| 31 |
], |
| 32 |
[ |
| 33 |
'value' => 5, |
| 34 |
'label' => 'H5' |
| 35 |
] |
| 36 |
], |
| 37 |
'toc_list_heirarchy' => true, |
| 38 |
'toc_list_number' => true, |
| 39 |
'toc_collapsible_small_devices' => true, |
| 40 |
'toc_title_text' => 'Table Of Contents' |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function render( $attributes, $content ) { |
| 45 |
$this->views( 'widgets/toc' ); |
| 46 |
} |
| 47 |
|
| 48 |
public function view_params() { |
| 49 |
$htags = []; |
| 50 |
|
| 51 |
if ( ! empty( $this->attributes['toc_supported_tags'] ) ) { |
| 52 |
$htags = array_map( function ( $item ) { |
| 53 |
return $item['value']; |
| 54 |
}, $this->attributes['toc_supported_tags'] ); |
| 55 |
} |
| 56 |
|
| 57 |
$toc_setting = [ |
| 58 |
'htags' => $htags, |
| 59 |
'hierarchy' => $this->attributes['toc_list_heirarchy'], |
| 60 |
'list_number' => $this->attributes['toc_list_number'] |
| 61 |
]; |
| 62 |
|
| 63 |
//set TOC data in Transient, whenever TOC(block) is called. |
| 64 |
set_transient( 'betterdocs_toc_setting', $toc_setting ); |
| 65 |
|
| 66 |
$htags = implode( ',', $htags ); |
| 67 |
|
| 68 |
$attributes = betterdocs()->template_helper->get_html_attributes( [ |
| 69 |
'htags' => $htags, |
| 70 |
'hierarchy' => $this->attributes['toc_list_heirarchy'], |
| 71 |
'list_number' => $this->attributes['toc_list_number'], |
| 72 |
'collapsible_on_mobile' => $this->attributes['toc_collapsible_small_devices'], |
| 73 |
'toc_title' => $this->attributes['toc_title_text'] |
| 74 |
] ); |
| 75 |
|
| 76 |
return [ |
| 77 |
'attributes' => $attributes |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
|