PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Utils / editor.php

editor.php in Code Snippets 4.0.0-beta.1, at php/Utils/editor.php

135 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 * Functions for using the built-in code editor library
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets\Utils;
9
10 use Code_Snippets\Settings\Settings_Fields;
11 use function Code_Snippets\Settings\get_setting;
12 use function Code_Snippets\Settings\get_settings_values;
13 use const Code_Snippets\PLUGIN_FILE;
14 use const Code_Snippets\PLUGIN_VERSION;
15
16 /**
17 * Register and load the CodeMirror library.
18 *
19 * @param string $type Type of code editor – either 'php', 'css', 'js', or 'html'.
20 * @param array<string, mixed> $extra_atts Pass a list of attributes to override the saved ones.
21 */
22 function enqueue_code_editor( string $type, array $extra_atts = [] ) {
23 $modes = [
24 'css' => 'text/css',
25 'php' => 'php-snippet',
26 'js' => 'javascript',
27 'html' => 'application/x-httpd-php',
28 ];
29
30 if ( ! isset( $modes[ $type ] ) ) {
31 $type = 'php';
32 }
33
34 $default_atts = [
35 'mode' => $modes[ $type ],
36 'inputStyle' => 'textarea',
37 'matchBrackets' => true,
38 'extraKeys' => [
39 'Alt-F' => 'findPersistent',
40 'Ctrl-Space' => 'autocomplete',
41 'Ctrl-/' => 'toggleComment',
42 'Cmd-/' => 'toggleComment',
43 'Alt-Up' => 'swapLineUp',
44 'Alt-Down' => 'swapLineDown',
45 ],
46 'gutters' => [ 'CodeMirror-lint-markers', 'CodeMirror-foldgutter' ],
47 'lint' => 'css' === $type || 'php' === $type,
48 'direction' => 'ltr',
49 'colorpicker' => [ 'mode' => 'edit' ],
50 'foldOptions' => [ 'widget' => '...' ],
51 ];
52
53 // Add relevant saved setting values to the default attributes.
54 $plugin_settings = get_settings_values();
55 $field_definitions = Settings_Fields::get_field_definitions();
56
57 foreach ( $field_definitions['editor'] as $field_id => $field ) {
58 // The 'codemirror' setting field specifies the name of the attribute.
59 $default_atts[ $field['codemirror'] ] = $plugin_settings['editor'][ $field_id ];
60 }
61
62 // Merge the default attributes with the ones passed into the function.
63 $atts = wp_parse_args( $default_atts, $extra_atts );
64 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
65
66 // Ensure number values are not formatted as strings.
67 foreach ( [ 'indentUnit', 'tabSize' ] as $number_att ) {
68 $atts[ $number_att ] = intval( $atts[ $number_att ] );
69 }
70
71 // Remove fontSize from the options and add it as an inline style.
72 if ( isset( $atts['fontSize'] ) ) {
73 $font_size = intval( $atts['fontSize'] );
74 unset( $atts['fontSize'] );
75 wp_add_inline_style( 'code-editor', ".CodeMirror { font-size: {$font_size}px !important; }" );
76 }
77
78 wp_enqueue_code_editor(
79 [
80 'type' => $modes[ $type ],
81 'codemirror' => $atts,
82 ]
83 );
84
85 wp_enqueue_script( 'htmlhint' );
86 wp_enqueue_script( 'csslint' );
87 wp_enqueue_script( 'jshint' );
88
89 wp_enqueue_script(
90 'code-snippets-code-editor',
91 plugins_url( 'dist/editor.js', PLUGIN_FILE ),
92 [ 'code-editor' ],
93 PLUGIN_VERSION,
94 [ 'in_footer' => true ]
95 );
96
97 // CodeMirror Theme.
98 $theme = get_setting( 'editor', 'theme' );
99
100 if ( 'default' !== $theme ) {
101 wp_enqueue_style(
102 'code-snippets-editor-theme-' . $theme,
103 plugins_url( "dist/editor-themes/$theme.css", PLUGIN_FILE ),
104 [ 'code-editor' ],
105 PLUGIN_VERSION
106 );
107 }
108 }
109
110 /**
111 * Retrieve a list of the available CodeMirror themes.
112 *
113 * @return array<string> The available themes.
114 */
115 function get_editor_themes(): array {
116 static $themes = null;
117
118 if ( ! is_null( $themes ) ) {
119 return $themes;
120 }
121
122 $themes = array();
123 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'dist/editor-themes/';
124
125 $theme_files = glob( $themes_dir . '*.css' );
126
127 foreach ( $theme_files as $theme ) {
128 $theme = str_replace( $themes_dir, '', $theme );
129 $theme = str_replace( '.css', '', $theme );
130 $themes[] = $theme;
131 }
132
133 return $themes;
134 }
135