← All changes
|
includes/Editors/BlockEditor/Blocks/CodeSnippet.php
+73
-10
4.6.2
→
4.9.2
View file →
| @@ -26,17 +26,23 @@ | ||
| 26 | 26 | * @return array |
| 27 | 27 | */ |
| 28 | 28 | public function get_default_attributes() { |
| 29 | 29 | return [ |
| 30 | - 'blockId' => '', | |
| 31 | - 'blockMeta' => (object) [], | |
| 32 | - 'resOption' => 'Desktop', | |
| 33 | - 'codeContent' => '', | |
| 34 | - 'language' => 'javascript', | |
| 35 | - 'showLanguageLabel' => true, | |
| 36 | - 'showCopyButton' => true, | |
| 37 | - 'showLineNumbers' => false, | |
| 38 | - 'theme' => 'light' | |
| 30 | + 'blockId' => '', | |
| 31 | + 'blockMeta' => (object) [], | |
| 32 | + 'resOption' => 'Desktop', | |
| 33 | + 'codeContent' => '', | |
| 34 | + 'language' => 'javascript', | |
| 35 | + 'codeVariants' => [], | |
| 36 | + 'showLanguageLabel' => true, | |
| 37 | + 'showCopyButton' => true, | |
| 38 | + 'showHeader' => true, | |
| 39 | + 'showLineNumbers' => false, | |
| 40 | + 'theme' => 'light', | |
| 41 | + 'fileName' => 'filename.js', | |
| 42 | + 'showFileIcon' => true, | |
| 43 | + 'fileIcon' => '', | |
| 44 | + 'showTrafficLights' => true | |
| 39 | 45 | ]; |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | 48 | /** |
| @@ -45,8 +51,21 @@ | ||
| 45 | 51 | public function register_scripts() { |
| 46 | 52 | $this->assets_manager->register( 'betterdocs-code-snippet', 'public/css/code-snippet.css' ); |
| 47 | 53 | $this->assets_manager->register( 'betterdocs-code-snippet', 'public/js/code-snippet.js', [ 'wp-element' ] ); |
| 48 | 54 | |
| 55 | + // Self-hosted Highlight.js (vendored under assets/static/vendor/highlightjs/, | |
| 56 | + // same convention as assets/vendor/scalar/). The frontend + editor read | |
| 57 | + // these URLs off the localized config instead of hitting cdnjs. | |
| 58 | + $this->assets_manager->localize( | |
| 59 | + 'betterdocs-code-snippet', | |
| 60 | + 'BetterDocsCodeSnippetConfig', | |
| 61 | + [ | |
| 62 | + 'hljsUrl' => $this->assets_manager->asset_url( 'vendor/highlightjs/highlight.min.js' ), | |
| 63 | + 'cssLight' => $this->assets_manager->asset_url( 'vendor/highlightjs/github.min.css' ), | |
| 64 | + 'cssDark' => $this->assets_manager->asset_url( 'vendor/highlightjs/github-dark.min.css' ) | |
| 65 | + ] | |
| 66 | + ); | |
| 67 | + | |
| 49 | 68 | // Enqueue CodeMirror for Gutenberg editor |
| 50 | 69 | if ( is_admin() ) { |
| 51 | 70 | add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ], 5 ); |
| 52 | 71 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_editor_assets' ], 5 ); |
| @@ -104,11 +123,13 @@ | ||
| 104 | 123 | |
| 105 | 124 | return [ |
| 106 | 125 | 'code_content' => $attributes['codeContent'], |
| 107 | 126 | 'language' => $attributes['language'], |
| 127 | + // Combined [primary + codeVariants] list the template renders as a | |
| 128 | + // language dropdown when it holds more than one entry. | |
| 129 | + 'code_variants' => $this->build_variants( $attributes ), | |
| 108 | 130 | 'show_language_label' => $attributes['showLanguageLabel'], |
| 109 | 131 | 'show_copy_button' => $attributes['showCopyButton'], |
| 110 | - 'show_copy_tooltip' => isset( $attributes['showCopyTooltip'] ) ? $attributes['showCopyTooltip'] : false, | |
| 111 | 132 | 'show_header' => isset( $attributes['showHeader'] ) ? $attributes['showHeader'] : true, |
| 112 | 133 | 'show_line_numbers' => $attributes['showLineNumbers'], |
| 113 | 134 | 'theme' => $attributes['theme'], |
| 114 | 135 | 'block_id' => $attributes['blockId'], |
| @@ -119,6 +140,48 @@ | ||
| 119 | 140 | 'show_traffic_lights' => isset( $attributes['showTrafficLights'] ) ? $attributes['showTrafficLights'] : true, |
| 120 | 141 | 'show_file_icon' => isset( $attributes['showFileIcon'] ) ? $attributes['showFileIcon'] : true, |
| 121 | 142 | 'file_icon' => isset( $attributes['fileIcon'] ) ? $attributes['fileIcon'] : '', |
| 122 | 143 | ]; |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * Build the ordered language list: the primary language/code first, then | |
| 148 | + * each non-empty entry from `codeVariants`. One entry → single-language | |
| 149 | + * (renders exactly as before); more → language dropdown. | |
| 150 | + * | |
| 151 | + * @param array $attributes | |
| 152 | + * @return array<int, array{language: string, code: string, file_name: string}> | |
| 153 | + */ | |
| 154 | + protected function build_variants( $attributes ) { | |
| 155 | + $variants = []; | |
| 156 | + | |
| 157 | + $primary_code = isset( $attributes['codeContent'] ) ? (string) $attributes['codeContent'] : ''; | |
| 158 | + if ( '' !== $primary_code ) { | |
| 159 | + $variants[] = [ | |
| 160 | + 'language' => isset( $attributes['language'] ) ? (string) $attributes['language'] : 'javascript', | |
| 161 | + 'code' => $primary_code, | |
| 162 | + 'file_name' => isset( $attributes['fileName'] ) ? (string) $attributes['fileName'] : '' | |
| 163 | + ]; | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( ! empty( $attributes['codeVariants'] ) && is_array( $attributes['codeVariants'] ) ) { | |
| 167 | + foreach ( $attributes['codeVariants'] as $variant ) { | |
| 168 | + if ( ! is_array( $variant ) ) { | |
| 169 | + continue; | |
| 170 | + } | |
| 171 | + | |
| 172 | + $code = isset( $variant['codeContent'] ) ? (string) $variant['codeContent'] : ''; | |
| 173 | + if ( '' === $code ) { | |
| 174 | + continue; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $variants[] = [ | |
| 178 | + 'language' => isset( $variant['language'] ) ? (string) $variant['language'] : 'javascript', | |
| 179 | + 'code' => $code, | |
| 180 | + 'file_name' => isset( $variant['fileName'] ) ? (string) $variant['fileName'] : '' | |
| 181 | + ]; | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + return $variants; | |
| 123 | 186 | } |
| 124 | 187 | } |