Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class WPCOM_REST_API_V2_Endpoint_Block_Editor_Assets extends WP_REST_Controller
'jetpack/google-calendar',
'jetpack/image-compare',
'jetpack/instagram-gallery',
'jetpack/math',
'jetpack/like',
'jetpack/mailchimp',
'jetpack/map',
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/move-latex-block-to-mathml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

It's a beta block.
24 changes: 0 additions & 24 deletions projects/plugins/jetpack/extensions/blocks/latex/block.json

This file was deleted.

116 changes: 0 additions & 116 deletions projects/plugins/jetpack/extensions/blocks/latex/edit.js

This file was deleted.

35 changes: 0 additions & 35 deletions projects/plugins/jetpack/extensions/blocks/latex/editor.scss

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions projects/plugins/jetpack/extensions/blocks/latex/view.js

This file was deleted.

49 changes: 49 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/math/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "jetpack/math",
"version": "0.1.0",
"title": "Math",
"keywords": [ "math", "formula", "equation", "mathml", "latex" ],
"category": "formatting",
"description": "Create and display math formulas with live preview and easy editing.",
"example": {
"attributes": {
"source": "\\frac{a}{b}"
}
},
"attributes": {
"source": {
"type": "string",
"default": ""
}
},
"supports": {
"html": true,
"color": {
"gradients": true
},
"spacing": {
"margin": true,
"padding": true
},
"typography": {
"fontSize": true
},
"align": [ "wide", "full" ],
"__experimentalBorder": {
"color": true,
"radius": true,
"style": true,
"width": true,
"__experimentalDefaultControls": {
"color": true,
"radius": true,
"style": true,
"width": true
}
}
},
Comment on lines +21 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alshakero it might be easier to split this PR into two: MathML and styles separately, as both have separate concerns to work through, so it would be faster keep momentum going in smaller PRs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really slammed right now. Thank you so much for pushing. Why don't we just merge?

"textdomain": "jetpack",
"style": "file:./view.css"
}
103 changes: 103 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/math/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { useBlockProps } from '@wordpress/block-editor';
import {
Button,
TextareaControl,
/* eslint-disable @wordpress/no-unsafe-wp-apis */
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalText as Text,
/* eslint-enable @wordpress/no-unsafe-wp-apis */
Placeholder,
} from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { icon } from './icon';
import 'katex/dist/katex.min.css';
import './editor.scss';
import { renderMath } from './utils';

const LatexPlaceholder = () => {
return (
<span className="jetpack-math-rendered-placeholder">{ __( 'Write math…', 'jetpack' ) }</span>
);
};

export default function Edit( { attributes, setAttributes, isSelected } ) {
const { source = '' } = attributes;
const textareaRef = useRef();
const [ renderAreaRef, setRenderAreaRef ] = useState( null );
const dispatch = useDispatch();
const blockProps = useBlockProps();
const { style, ...props } = blockProps;

useEffect( () => {
if ( renderAreaRef ) {
renderAreaRef.innerHTML = renderMath( source );
}
}, [ source, renderAreaRef ] );

if ( isSelected ) {
return (
<div { ...props }>
<Placeholder label={ __( 'Math', 'jetpack' ) } icon={ icon }>
<VStack
className="jetpack-math-textarea-container"
gap={ 4 }
justify="flex-start"
align="stretch"
>
<TextareaControl
label={ __( 'Math code (Tex or MathML)', 'jetpack' ) }
className="jetpack-math-textarea"
ref={ textareaRef }
value={ source }
onChange={ value => setAttributes( { source: value } ) }
rows={ 4 }
help={ __( 'To render MathML your code must be wrapped in <math> tags.', 'jetpack' ) }
/>
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text>
{ source.trim() && (
<div
className="jetpack-math-rendered"
style={ style }
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) }
/>
) }
Comment on lines +59 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text>
{ source.trim() && (
<div
className="jetpack-math-rendered"
style={ style }
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) }
/>
) }
{ source.trim() && (
<>
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text>
<div
className="jetpack-math-rendered"
style={ style }
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) }
/>
</>
) }

I think it looks better hiding the "Preview" until there's some content:

Screenshot 2025-08-29 at 10 23 07 Screenshot 2025-08-29 at 10 23 26

</VStack>
<HStack justify="flex-end">
<Button
__next40pxDefaultSize
variant="primary"
onClick={ () => {
setAttributes( { source } );
dispatch( 'core/block-editor' ).clearSelectedBlock();
} }
>
{ __( 'Done', 'jetpack' ) }
</Button>
</HStack>
</Placeholder>
</div>
);
}

return (
<div
{ ...blockProps }
tabIndex={ 0 }
role="button"
aria-label={ __( 'Edit math', 'jetpack' ) }
data-source={ source }
>
{ source.trim() ? (
<div
className="jetpack-math-rendered"
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) }
/>
) : (
<LatexPlaceholder />
) }
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ registerJetpackBlockFromMetadata(
{
edit,
save,
supports: metadata.supports,
}
);
Loading
Loading