Skip to content

Commit e491dc1

Browse files
SirLouent-hamanoyouknowriadntsekourasshimotmk
authored
Math: Fixing Edit as HTML feature and code editor sync (#72910)
* Math: Fixing Edit as HTML feature and code editor sync * fix: More syntantical uniformity * fix: Add support indication for core/math block in documentation Co-authored-by: SirLouen <sirlouen@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: shimotmk <shimotomoki@git.wordpress.org>
1 parent 5a123f7 commit e491dc1

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ Display mathematical notation using LaTeX. ([Source](https://github.com/WordPres
495495

496496
- **Name:** core/math
497497
- **Category:** text
498+
- **Supports:** ~~html~~
498499
- **Attributes:** latex, mathML
499500

500501
## Media & Text

packages/block-library/src/math/block.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"description": "Display mathematical notation using LaTeX.",
88
"keywords": [ "equation", "formula", "latex", "mathematics" ],
99
"textdomain": "default",
10+
"supports": {
11+
"html": false
12+
},
1013
"attributes": {
1114
"latex": {
1215
"type": "string",

packages/block-library/src/math/edit.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
privateApis as componentsPrivateApis,
1414
} from '@wordpress/components';
1515
import { useState, useEffect, useRef } from '@wordpress/element';
16-
import { useDispatch, useSelect } from '@wordpress/data';
16+
import { useDispatch } from '@wordpress/data';
1717

1818
/**
1919
* Internal dependencies
@@ -22,31 +22,15 @@ import { unlock } from '../lock-unlock';
2222

2323
const { Badge } = unlock( componentsPrivateApis );
2424

25-
export default function MathEdit( {
26-
attributes,
27-
setAttributes,
28-
isSelected,
29-
clientId,
30-
} ) {
31-
const { latex } = attributes;
25+
export default function MathEdit( { attributes, setAttributes, isSelected } ) {
26+
const { latex, mathML } = attributes;
3227
const [ blockRef, setBlockRef ] = useState();
3328
const [ error, setError ] = useState( null );
3429
const [ latexToMathML, setLatexToMathML ] = useState();
35-
const initialLatex = useRef( attributes.latex );
30+
const initialLatex = useRef( latex );
3631
const { __unstableMarkNextChangeAsNotPersistent } =
3732
useDispatch( blockEditorStore );
3833

39-
// Check if block is in HTML editing mode
40-
const { isEditingAsHTML } = useSelect(
41-
( select ) => {
42-
const { getBlockMode } = select( blockEditorStore );
43-
return {
44-
isEditingAsHTML: getBlockMode( clientId ) === 'html',
45-
};
46-
},
47-
[ clientId ]
48-
);
49-
5034
useEffect( () => {
5135
import( '@wordpress/latex-to-mathml' ).then( ( module ) => {
5236
setLatexToMathML( () => module.default );
@@ -72,18 +56,18 @@ export default function MathEdit( {
7256

7357
return (
7458
<div { ...blockProps }>
75-
{ attributes.mathML ? (
59+
{ mathML ? (
7660
<math
7761
// We can't spread block props on the math element because
7862
// it only supports a limited amount of global attributes.
7963
// For example, draggable will have no effect.
8064
display="block"
81-
dangerouslySetInnerHTML={ { __html: attributes.mathML } }
65+
dangerouslySetInnerHTML={ { __html: mathML } }
8266
/>
8367
) : (
8468
'\u200B'
8569
) }
86-
{ isSelected && ! isEditingAsHTML && (
70+
{ isSelected && (
8771
<Popover
8872
placement="bottom-start"
8973
offset={ 8 }
@@ -104,17 +88,17 @@ export default function MathEdit( {
10488
setAttributes( { latex: newLatex } );
10589
return;
10690
}
107-
let mathML = '';
91+
let newMathML = '';
10892
try {
109-
mathML = latexToMathML( newLatex, {
93+
newMathML = latexToMathML( newLatex, {
11094
displayMode: true,
11195
} );
11296
setError( null );
11397
} catch ( err ) {
11498
setError( err.message );
11599
}
116100
setAttributes( {
117-
mathML,
101+
mathML: newMathML,
118102
latex: newLatex,
119103
} );
120104
} }

0 commit comments

Comments
 (0)