Skip to content

Commit

Permalink
merge in main
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBatdorf committed Nov 17, 2022
2 parents bfef041 + 8586b5b commit 10e2dcc
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 15 deletions.
1 change: 1 addition & 0 deletions php/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function code_block_pro_register_settings()
'previousFontSize' => [ 'type' => ['string', 'null']],
'previousLineHeight' => [ 'type' => ['string', 'null']],
'previousHeaderType' => [ 'type' => ['string', 'null']],
'previousFooterType' => [ 'type' => ['string', 'null']],
'previousClampFonts' => [ 'type' => ['boolean', 'null']],
'previousDisablePadding' => [ 'type' => ['boolean', 'null']],
'previousLineNumbers' => [ 'type' => ['boolean', 'null']],
Expand Down
4 changes: 4 additions & 0 deletions src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
"lineNumbers": { "type": "boolean" },
"clampFonts": { "type": "boolean" },
"headerType": { "type": "string" },
"footerType": { "type": "string" },
"headerString": { "type": "string" },
"disablePadding": { "type": "boolean" },
"footerString": { "type": "string" },
"footerLink": { "type": "string" },
"footerLinkTarget": { "type": "boolean" },
"startingLineNumber": { "type": "number", "default": 1 },
"lineNumbersWidth": { "type": "number" },
"frame": { "type": "boolean" },
Expand Down
5 changes: 4 additions & 1 deletion src/editor/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const Edit = ({
lineNumbersWidth,
lineNumbers,
startingLineNumber,
footerType,
} = attributes;
const textAreaRef = useRef<HTMLDivElement>(null);
const handleChange = (code: string) => setAttributes({ code });
Expand All @@ -28,6 +29,8 @@ export const Edit = ({
theme,
lang: language ?? previousLanguage,
});
const hasFooter =
attributes?.footerType && attributes?.footerType !== 'none';
useDefaults({ attributes, setAttributes });

useEffect(() => {
Expand Down Expand Up @@ -110,7 +113,7 @@ export const Edit = ({
onValueChange={handleChange}
padding={{
top: disablePadding ? 0 : 16,
bottom: disablePadding ? 0 : 16,
bottom: disablePadding || hasFooter ? 0 : 16,
left:
(disablePadding ? 0 : 16) +
// If line numbers are disabled, just offset the 12px padding
Expand Down
66 changes: 66 additions & 0 deletions src/editor/components/FooterSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { BaseControl } from '@wordpress/components';
import { sprintf, __ } from '@wordpress/i18n';
import { Attributes } from '../../types';
import { SimpleStringEnd } from './footers/SimpleStringEnd';

type FooterSelectProps = {
attributes: Attributes;
onClick: (slug: string) => void;
};
export const FooterSelect = ({ attributes, onClick }: FooterSelectProps) => {
const { footerType, ...attributesWithoutFooterType }: Partial<Attributes> =
attributes;
const { bgColor } = attributes;
const types = {
none: __('None', 'code-block-pro'),
simpleStringEnd: __('Simple string end', 'code-block-pro'),
};

return (
<div className="code-block-pro-editor">
{Object.entries(types).map(([slug, type]) => (
<BaseControl
id={`code-block-pro-footer-${slug}`}
label={
footerType === slug
? sprintf(
__('%s (current)', 'code-block-pro'),
type,
)
: type
}
help={
['simpleStringEnd'].includes(slug)
? __('Update text in Settings', 'code-block-pro')
: undefined
}
key={slug}>
<button
id={`code-block-pro-footer-${slug}`}
type="button"
onClick={() => onClick(slug)}
className="p-0 border flex items-start w-full text-left outline-none cursor-pointer no-underline ring-offset-2 ring-offset-white focus:shadow-none focus:ring-wp overflow-x-auto">
<span className="pointer-events-none w-full">
<span
className="block w-full h-8"
style={{ backgroundColor: bgColor }}
/>
<FooterType
footerType={slug}
{...attributesWithoutFooterType}
/>
</span>
</button>
</BaseControl>
))}
</div>
);
};

export const FooterType = (attributes: Partial<Attributes>) => {
const { footerType } = attributes;
if (footerType === 'simpleStringEnd') {
return <SimpleStringEnd {...attributes} />;
}
return null;
};
14 changes: 8 additions & 6 deletions src/editor/components/HeaderSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type HeaderSelectProps = {
onClick: (slug: string) => void;
};
export const HeaderSelect = ({ attributes, onClick }: HeaderSelectProps) => {
const { headerType, bgColor } = attributes;
const { headerType, ...attributesWithoutHeaderType }: Partial<Attributes> =
attributes;
const { bgColor } = attributes;
const types = {
none: __('None', 'code-block-pro'),
headlights: __('Headlights', 'code-block-pro'),
Expand All @@ -20,11 +22,6 @@ export const HeaderSelect = ({ attributes, onClick }: HeaderSelectProps) => {
simpleString: __('Simple string', 'code-block-pro'),
};

const attributesWithoutHeaderType = {
...attributes,
} as Partial<Attributes>;
delete attributesWithoutHeaderType.headerType;

return (
<div className="code-block-pro-editor">
{Object.entries(types).map(([slug, type]) => (
Expand All @@ -38,6 +35,11 @@ export const HeaderSelect = ({ attributes, onClick }: HeaderSelectProps) => {
)
: type
}
help={
['simpleString'].includes(slug)
? __('Update text in Settings', 'code-block-pro')
: undefined
}
key={slug}>
<button
id={`code-block-pro-header-${slug}`}
Expand Down
6 changes: 5 additions & 1 deletion src/editor/components/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export const Notice = () => {
const [email, setEmail] = useState('');
const [emailSent, setEmailSent] = useState(false);
const maybeEmail = useSelect((select) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line
const user = select('core').getCurrentUser() as { id: number };
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line
const details = select('core').getEntityRecord(
'root',
'user',
user.id,
) as { email: string };
return details?.email;
});
}, []);
const sendEmail = () => {
fetch('https://www.kevinbatdorf.com/api/interest', {
method: 'POST',
Expand Down
83 changes: 83 additions & 0 deletions src/editor/components/footers/SimpleStringEnd.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { colord, AnyColor } from 'colord';
import { Lang } from 'shiki';
import { Attributes } from '../../../types';
import { languages } from '../../../util/languages';

export const SimpleStringEnd = ({
language,
bgColor,
textColor,
footerString,
footerLink,
footerLinkTarget,
disablePadding,
}: Partial<Attributes>) => {
const bgC = colord(bgColor as AnyColor);
const textC = colord(textColor as AnyColor);
const text = textC.isDark() ? textC.lighten(0.05) : textC.darken(0.05);
return (
<span
style={{
display: 'flex',
alignItems: 'flex-end',
padding: disablePadding ? '10px 0 0 0' : '10px',
width: '100%',
justifyContent: 'flex-end',
backgroundColor: bgC.toHex(),
color: text.toHex(),
fontSize: '12px',
lineHeight: '1',
position: 'relative',
}}>
<Inner
text={footerString || languages[language as Lang]}
color={text.toHex()}
link={footerLink}
linkTarget={footerLinkTarget}
/>
</span>
);
};

const Inner = ({
text,
color,
link,
linkTarget,
}: {
text?: string;
color: string;
link?: string;
linkTarget?: boolean;
}) => {
if (!link) return <>{text}</>;
const style = {
color,
// attempt to reset the link to not inheret from theme
textDecoration: 'none',
fontWeight: 'normal',
border: 'none',
background: 'none',
borderRadius: '0',
padding: '0',
margin: '0',
};
if (linkTarget) {
return (
<a
className="cbp-footer-link"
href={link}
style={style}
target={'_blank'}
// Gutenberg required
rel={'noopener noreferrer'}>
{text}
</a>
);
}
return (
<a className="cbp-footer-link" href={link} style={style}>
{text}
</a>
);
};
75 changes: 71 additions & 4 deletions src/editor/controls/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
FontLineHeightSelect,
FontFamilySelect,
} from '../components/FontSelect';
import { FooterSelect } from '../components/FooterSelect';
import { HeaderSelect } from '../components/HeaderSelect';
import { Notice } from '../components/Notice';
// import { Notice } from '../components/Notice';
import { ThemeSelect } from '../components/ThemeSelect';

export const SidebarControls = ({
Expand All @@ -32,8 +33,12 @@ export const SidebarControls = ({
const { recentLanguages } = useLanguageStore();
const { updateThemeHistory } = useThemeStore();
const { setPreviousSettings } = useGlobalStore();
const { headerType, footerType } = attributes;

const showHeaderTextEdit = ['simpleString'].includes(attributes.headerType);
const showHeaderTextEdit = ['simpleString'].includes(headerType);
const showFooterTextEdit = ['simpleStringEnd'].includes(footerType);
const showFooterLinkEdit = ['simpleStringEnd'].includes(footerType);
const showFooterLinkTargetEdit = ['simpleStringEnd'].includes(footerType);

return (
<InspectorControls>
Expand Down Expand Up @@ -89,7 +94,58 @@ export const SidebarControls = ({
/>
</BaseControl>
)}
<Notice />
{showFooterTextEdit && (
<BaseControl id="code-block-pro-footer-text">
<TextControl
id="code-block-pro-footer-text"
spellCheck={false}
autoComplete="off"
label={__('Footer Text', 'code-block-pro')}
placeholder={languages[language]}
onChange={(footerString) => {
setAttributes({ footerString });
}}
value={attributes.footerString ?? ''}
/>
</BaseControl>
)}
{showFooterTextEdit && (
<BaseControl id="code-block-pro-footer-link">
<TextControl
id="code-block-pro-footer-link"
spellCheck={false}
type="url"
autoComplete="off"
label={__('Footer Link', 'code-block-pro')}
placeholder="https://example.com"
help={__(
'Leave blank to disable',
'code-block-pro',
)}
onChange={(footerLink) => {
setAttributes({ footerLink });
}}
value={attributes.footerLink ?? ''}
/>
</BaseControl>
)}
{showFooterLinkTargetEdit && (
<BaseControl id="code-block-pro-footer-link-target">
<CheckboxControl
id="code-block-pro-footer-link-target"
label={__('Open in new tab?', 'code-block-pro')}
checked={attributes.footerLinkTarget}
onChange={(footerLinkTarget) => {
setAttributes({ footerLinkTarget });
updateThemeHistory({
...attributes,
footerLinkTarget,
});
}}
/>
</BaseControl>
)}
{/* <Notice /> */}
</div>
</PanelBody>
<PanelBody
Expand Down Expand Up @@ -186,12 +242,23 @@ export const SidebarControls = ({
initialOpen={false}>
<HeaderSelect
attributes={attributes}
onClick={(headerType: string) => {
onClick={(headerType) => {
setAttributes({ headerType });
updateThemeHistory({ ...attributes, headerType });
}}
/>
</PanelBody>
<PanelBody
title={__('Footer Type', 'code-block-pro')}
initialOpen={false}>
<FooterSelect
attributes={attributes}
onClick={(footerType) => {
setAttributes({ footerType });
updateThemeHistory({ ...attributes, footerType });
}}
/>
</PanelBody>
<PanelBody
title={__('Themes', 'code-block-pro')}
initialOpen={false}>
Expand Down
3 changes: 3 additions & 0 deletions src/editor/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
width: auto !important;
position: initial;
}
.code-block-pro-editor .cbp-footer-link {
@apply pointer-events-none no-underline;
}

.code-block-pro-editor code {
font-family: inherit !important;
Expand Down
5 changes: 4 additions & 1 deletion src/front/BlockOutput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RichText, useBlockProps as blockProps } from '@wordpress/block-editor';
import classNames from 'classnames';
import { colord, AnyColor } from 'colord';
import { FooterType } from '../editor/components/FooterSelect';
import { HeaderType } from '../editor/components/HeaderSelect';
import { Attributes } from '../types';
import { fontFamilyLong, maybeClamp } from '../util/fonts';
Expand All @@ -12,6 +12,8 @@ export const BlockOutput = ({ attributes }: { attributes: Attributes }) => (
{...blockProps.save({
className: classNames({
'padding-disabled': attributes.disablePadding,
'padding-bottom-disabled':
attributes?.footerType && attributes?.footerType !== 'none',
'cbp-has-line-numbers': attributes.lineNumbers,
}),
})}
Expand Down Expand Up @@ -47,6 +49,7 @@ export const BlockOutput = ({ attributes }: { attributes: Attributes }) => (
<CopyButton attributes={attributes} />
)}
<RichText.Content value={attributes.codeHTML} />
<FooterType {...attributes} />
</>
) : null}
</div>
Expand Down
Loading

0 comments on commit 10e2dcc

Please sign in to comment.