PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.3
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Editors / BlockEditor / Blocks / CodeSnippet.php

CodeSnippet.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.3, at includes/Editors/BlockEditor/Blocks/CodeSnippet.php

119 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks;
4
5 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
6
7 class CodeSnippet extends Block {
8
9 protected $frontend_scripts = [ 'betterdocs-code-snippet' ];
10 protected $frontend_styles = [ 'betterdocs-code-snippet' ];
11
12 /**
13 * Unique name of the block
14 * @return string
15 */
16 public function get_name() {
17 return 'code-snippet';
18 }
19
20 /**
21 * Block default attributes
22 * @return array
23 */
24 public function get_default_attributes() {
25 return [
26 'blockId' => '',
27 'blockMeta' => (object) [],
28 'resOption' => 'Desktop',
29 'codeContent' => '',
30 'language' => 'javascript',
31 'showLanguageLabel' => true,
32 'showCopyButton' => true,
33 'showLineNumbers' => false,
34 'theme' => 'light'
35 ];
36 }
37
38 /**
39 * Register scripts and styles
40 */
41 public function register_scripts() {
42 $this->assets_manager->register( 'betterdocs-code-snippet', 'public/css/code-snippet.css' );
43 $this->assets_manager->register( 'betterdocs-code-snippet', 'public/js/code-snippet.js', [ 'wp-element' ] );
44
45 // Enqueue CodeMirror for Gutenberg editor
46 if ( is_admin() ) {
47 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ], 5 );
48 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_editor_assets' ], 5 );
49 }
50 }
51
52 /**
53 * Enqueue CodeMirror assets for the block editor
54 */
55 public function enqueue_editor_assets() {
56 // Only enqueue on block editor pages
57 $screen = get_current_screen();
58 if ( ! $screen || ( $screen->base !== 'post' && $screen->base !== 'site-editor' ) ) {
59 return;
60 }
61
62 // Enqueue WordPress CodeMirror with basic settings
63 if ( function_exists( 'wp_enqueue_code_editor' ) ) {
64 wp_enqueue_code_editor( [
65 'type' => 'text/html'
66 ] );
67 }
68
69 // Enqueue frontend assets for syntax highlighting in editor preview
70 wp_enqueue_style( 'betterdocs-code-snippet' );
71 wp_enqueue_script( 'betterdocs-code-snippet' );
72 }
73
74 /**
75 * Check if we're in editor mode
76 * @return bool
77 */
78 public function is_editor_mode() {
79 return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && $_REQUEST['context'] === 'edit';
80 }
81
82 /**
83 * Render the block
84 * @param array $attributes
85 * @param string $content
86 */
87 public function render( $attributes, $content ) {
88 // Use the template with view_params
89 $this->views( 'templates/parts/code-snippet' );
90 }
91
92 /**
93 * View parameters for the template
94 * @return array
95 */
96 public function view_params() {
97 $attributes = &$this->attributes;
98
99 return [
100 'code_content' => $attributes['codeContent'],
101 'language' => $attributes['language'],
102 'show_language_label' => $attributes['showLanguageLabel'],
103 'show_copy_button' => $attributes['showCopyButton'],
104 'show_copy_tooltip' => isset( $attributes['showCopyTooltip'] ) ? $attributes['showCopyTooltip'] : false,
105 'show_header' => isset( $attributes['showHeader'] ) ? $attributes['showHeader'] : true,
106 'show_line_numbers' => $attributes['showLineNumbers'],
107 'theme' => $attributes['theme'],
108 'block_id' => $attributes['blockId'],
109 'widget_type' => 'blocks',
110 'is_editor_mode' => $this->is_editor_mode(),
111 // File Preview Header
112 'file_name' => isset( $attributes['fileName'] ) ? $attributes['fileName'] : 'filename.js',
113 'show_traffic_lights' => isset( $attributes['showTrafficLights'] ) ? $attributes['showTrafficLights'] : true,
114 'show_file_icon' => isset( $attributes['showFileIcon'] ) ? $attributes['showFileIcon'] : true,
115 'file_icon' => isset( $attributes['fileIcon'] ) ? $attributes['fileIcon'] : '',
116 ];
117 }
118 }
119