-
Notifications
You must be signed in to change notification settings - Fork 826
LaTeX Block: Switch to MathML and add styling support #44928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
d600efd
815f712
ecab99d
c1f1bff
0b03f90
b334296
c8a1602
b23d547
12383c2
07d4c2c
c28cd17
14a4bf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
It's a beta block. |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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 | ||
} | ||
} | ||
}, | ||
"textdomain": "jetpack", | ||
"style": "file:./view.css" | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think it looks better hiding the "Preview" until there's some content: ![]() ![]() |
||||||||||||||||||||||||||||||||||||||
</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 |
---|---|---|
|
@@ -12,5 +12,6 @@ registerJetpackBlockFromMetadata( | |
{ | ||
edit, | ||
save, | ||
supports: metadata.supports, | ||
} | ||
); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?