Skip to content

Commit

Permalink
Merge pull request #41 from KevinBatdorf/add-padding-disable-option
Browse files Browse the repository at this point in the history
Add option to disable padding
  • Loading branch information
KevinBatdorf committed Sep 5, 2022
2 parents dc7ef00 + 50fd6c1 commit 0282d26
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 20 deletions.
Binary file added .wordpress-org/screenshot-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ View this block plugin [on WordPress.org](https://wordpress.org/plugins/code-blo

## Features
- Includes 28 themes built in to choose from.
- Supports over 140 languages
- Supports over 140 programming languages
- Optionally load programming fonts
- Various header styles (more coming soon)
- Optionally add a copy button to let users copy the code
- Native Gutenberg block output - no special requirements
- No frontend JavaScript required - works in headless mode
- Supports converting from the default code block


## Example Screenshots
![alt text](.wordpress-org/screenshot-1.gif "Example")
![alt text](.wordpress-org/screenshot-2.png "Example 2")
![alt text](.wordpress-org/screenshot-1.png "Example")
![alt text](.wordpress-org/screenshot-2.gif "Example 2")
![alt text](.wordpress-org/screenshot-3.png "Example 3")
2 changes: 2 additions & 0 deletions php/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function code_block_pro_register_settings() {
'previousFontSize' => [ 'type' => ['string', 'null']],
'previousLineHeight' => [ 'type' => ['string', 'null']],
'previousHeaderType' => [ 'type' => ['string', 'null']],
'previousClampFonts' => [ 'type' => ['boolean', 'null']],
'previousDisablePadding' => [ 'type' => ['boolean', 'null']],
],
],
],
Expand Down
7 changes: 5 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,16 @@ Look under the "Styling" tab and turn on "Clamp Values", which will compute the

== Screenshots ==

1. Choose from more than 25 themes
2. Customize fonts, themes, and behavior
1. Choose from more than 25 themes.
2. Customize fonts, themes, and behavior.
3. Example showing light theme with padding disabled.

== Changelog ==

= 1.5.1 - 2022-09-05 =
- Tweak: Allow users to disable padding
- Fix: Fixes a bug where the header type set to none doesn't persist
- Fix: Clamp font settings were not being persisted

= 1.5.0 - 2022-09-05 =
- Feature: Add toggle so users can clamp font sizes to reasonable values.
Expand Down
1 change: 1 addition & 0 deletions src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lineNumbers": { "type": "boolean" },
"clampFonts": { "type": "boolean", "default": false },
"headerType": { "type": "string" },
"disablePadding": { "type": "boolean", "default": false },
"startingLineNumber": { "type": "number", "default": 1 },
"frame": { "type": "boolean" },
"renderType": { "type": "string", "default": "code" },
Expand Down
3 changes: 2 additions & 1 deletion src/editor/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const Edit = ({
code = '',
bgColor: backgroundColor,
textColor: color,
disablePadding,
} = attributes;
const textAreaRef = useRef<HTMLDivElement>(null);
const handleChange = (code: string) => setAttributes({ code });
Expand Down Expand Up @@ -72,7 +73,7 @@ export const Edit = ({
<Editor
value={code}
onValueChange={handleChange}
padding={16}
padding={disablePadding ? 0 : 16}
style={{ backgroundColor, color }}
// eslint-disable-next-line
onKeyDown={(e: any) =>
Expand Down
43 changes: 31 additions & 12 deletions src/editor/controls/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,37 @@ export const SidebarControls = ({
<PanelBody
title={__('Extra Settings', 'code-block-pro')}
initialOpen={false}>
<CheckboxControl
label={__('Copy Button', 'code-block-pro')}
help={__(
'If checked, users will be able to copy your code snippet to their clipboard.',
'code-block-pro',
)}
checked={attributes.copyButton}
onChange={(value) => {
setPreviousSettings({ copyButton: value });
setAttributes({ copyButton: value });
}}
/>
<BaseControl id="code-block-pro-show-copy-button">
<CheckboxControl
label={__('Copy Button', 'code-block-pro')}
help={__(
'If checked, users will be able to copy your code snippet to their clipboard.',
'code-block-pro',
)}
checked={attributes.copyButton}
onChange={(value) => {
setPreviousSettings({ copyButton: value });
setAttributes({ copyButton: value });
}}
/>
</BaseControl>
<BaseControl id="code-block-pro-disable-padding">
<CheckboxControl
label={__('Disable Padding', 'code-block-pro')}
help={__(
'This is useful if you pick a theme that matches your background color, and want the code to line up to the edge of your content.',
'code-block-pro',
)}
checked={attributes.disablePadding}
onChange={(disablePadding) => {
setAttributes({ disablePadding });
updateThemeHistory({
...attributes,
disablePadding,
});
}}
/>
</BaseControl>
</PanelBody>
</InspectorControls>
);
Expand Down
6 changes: 5 additions & 1 deletion src/front/BlockOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import './style.css';

export const BlockOutput = ({ attributes }: { attributes: Attributes }) => (
<div
{...blockProps.save()}
{...blockProps.save({
className: attributes.disablePadding
? 'padding-disabled'
: undefined,
})}
data-code-block-pro-font-family={attributes.fontFamily}
style={{
fontSize: maybeClamp(attributes.fontSize, attributes.clampFonts),
Expand Down
3 changes: 3 additions & 0 deletions src/front/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
font-family: inherit;
@apply overflow-auto text-inherit text-left m-0 whitespace-pre bg-none border-none border-0 rounded-none shadow-none;
}
.wp-block-kevinbatdorf-code-block-pro.padding-disabled pre {
padding: 0;
}
.wp-block-kevinbatdorf-code-block-pro:not(.code-block-pro-editor) pre code {
border: 0;
background: none;
Expand Down
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useBlockProps as blockProps } from '@wordpress/block-editor';
import { createBlock, registerBlockType } from '@wordpress/blocks';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { Lang } from 'shiki';
import blockConfig from './block.json';
import { Edit } from './editor/Edit';
Expand Down Expand Up @@ -34,6 +35,7 @@ registerBlockType<Attributes>(blockConfig.name, {
clampFonts: { type: 'boolean', default: false },
lineNumbers: { type: 'boolean' },
headerType: { type: 'string' },
disablePadding: { type: 'boolean', default: false },
startingLineNumber: { type: 'number', default: 1 },
frame: { type: 'boolean' },
renderType: { type: 'string', default: 'code' },
Expand All @@ -58,7 +60,9 @@ registerBlockType<Attributes>(blockConfig.name, {
/>
<div
{...blockProps({
className: 'code-block-pro-editor',
className: classnames('code-block-pro-editor', {
'padding-disabled': attributes.disablePadding,
}),
style: {
fontSize: maybeClamp(
attributes.fontSize,
Expand Down
3 changes: 3 additions & 0 deletions src/state/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ThemeType = {
previousFontSize: string;
previousHeaderType: string;
previousClampFonts: boolean;
previousDisablePadding: boolean;
updateThemeHistory: (settings: Partial<Attributes>) => void;
};
const path = '/wp/v2/settings';
Expand All @@ -29,6 +30,7 @@ export const useThemeStore = create<ThemeType>()(
previousFontSize: '.875rem',
previousHeaderType: 'headlights',
previousClampFonts: false,
previousDisablePadding: false,
updateThemeHistory(attributes) {
set((state) => ({
...state,
Expand All @@ -38,6 +40,7 @@ export const useThemeStore = create<ThemeType>()(
previousFontSize: attributes.fontSize,
previousHeaderType: attributes.headerType,
previousClampFonts: attributes.clampFonts,
previousDisablePadding: attributes.disablePadding,
}));
},
}),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Attributes = {
lineNumbers: boolean;
clampFonts: boolean;
headerType: string;
disablePadding: boolean;
startingLineNumber: number;
frame: boolean;
renderType: string;
Expand Down

0 comments on commit 0282d26

Please sign in to comment.