Skip to content

Commit 39d75f0

Browse files
authored
Merge pull request #246 from codesnippetspro/feat--Adjustable-font-size-to-code-editor
Feat/ Adjustable font size to code editor
2 parents 62109a8 + 53da03a commit 39d75f0

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/js/services/settings/editor-preview.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ export const handleEditorPreviewUpdates = () => {
4141
})()
4242

4343
if (null !== value) {
44-
editor?.setOption(opt, value)
44+
if ('font_size' === setting.name) {
45+
const codeElement = document.querySelector('.CodeMirror-code')
46+
if (codeElement && codeElement instanceof HTMLElement) {
47+
codeElement.style.fontSize = `${value}px`
48+
}
49+
} else {
50+
editor?.setOption(opt, value)
51+
}
4552
}
4653
})
4754
}

src/php/editor.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ function enqueue_code_editor( string $type, array $extra_atts = [] ) {
6666
$atts[ $number_att ] = intval( $atts[ $number_att ] );
6767
}
6868

69+
// Remove fontSize from the options and add it as an inline style.
70+
if ( isset( $atts['fontSize'] ) ) {
71+
$font_size = intval( $atts['fontSize'] );
72+
unset( $atts['fontSize'] );
73+
wp_add_inline_style( 'code-editor', ".CodeMirror { font-size: {$font_size}px !important; }" );
74+
}
75+
6976
wp_enqueue_code_editor(
7077
[
7178
'type' => $modes[ $type ],

src/php/settings/settings-fields.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function get_default_settings(): array {
3737
'indent_with_tabs' => true,
3838
'tab_size' => 4,
3939
'indent_unit' => 4,
40+
'font_size' => 14,
4041
'wrap_lines' => true,
4142
'code_folding' => true,
4243
'line_numbers' => true,
@@ -161,12 +162,21 @@ function get_settings_fields(): array {
161162
'codemirror' => 'indentUnit',
162163
'min' => 0,
163164
],
165+
'font_size' => [
166+
'name' => __( 'Font Size', 'code-snippets' ),
167+
'type' => 'number',
168+
'label' => _x( 'px', 'unit', 'code-snippets' ),
169+
'codemirror' => 'fontSize',
170+
'min' => 8,
171+
'max' => 28,
172+
],
164173
'wrap_lines' => [
165174
'name' => __( 'Wrap Lines', 'code-snippets' ),
166175
'type' => 'checkbox',
167176
'label' => __( 'Soft-wrap long lines of code instead of horizontally scrolling.', 'code-snippets' ),
168177
'codemirror' => 'lineWrapping',
169178
],
179+
170180
'code_folding' => [
171181
'name' => __( 'Code Folding', 'code-snippets' ),
172182
'type' => 'checkbox',

0 commit comments

Comments
 (0)