Skip to content

Commit b6468b0

Browse files
author
Morten Barklund
committed
Changed highlight to use existing style manipulation
Reverted changes to `getValidHTML` and reused the existing HTML manipulation functions.
1 parent b63842a commit b6468b0

4 files changed

Lines changed: 22 additions & 48 deletions

File tree

assets/src/edit-story/elements/text/display.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* External dependencies
1919
*/
2020
import styled from 'styled-components';
21-
import { useEffect, useRef } from 'react';
21+
import { useEffect, useRef, useMemo } from 'react';
2222

2323
/**
2424
* Internal dependencies
@@ -34,8 +34,8 @@ import {
3434
import StoryPropTypes from '../../types';
3535
import { BACKGROUND_TEXT_MODE } from '../../constants';
3636
import { useTransformHandler } from '../../components/transform';
37-
import removeInlineStyle from '../../utils/removeInlineStyle';
38-
import getValidHTML from '../../utils/getValidHTML';
37+
import { getHTMLFormatters } from '../../components/richText/htmlManipulation';
38+
import createSolid from '../../utils/createSolid';
3939
import { getHighlightLineheight, generateParagraphTextStyle } from './util';
4040

4141
const HighlightWrapperElement = styled.div`
@@ -128,9 +128,12 @@ function TextDisplay({
128128
});
129129

130130
if (backgroundTextMode === BACKGROUND_TEXT_MODE.HIGHLIGHT) {
131-
const foregroundContent = getValidHTML(content);
132-
const backgroundContent = getValidHTML(content, (node) =>
133-
removeInlineStyle(node, 'color')
131+
const foregroundContent = content;
132+
// Setting the text color of the entire block to black essentially removes all inline
133+
// color styling allowing us to apply transparent to all of them.
134+
const backgroundContent = useMemo(
135+
() => getHTMLFormatters().setColor(content, createSolid(0, 0, 0)),
136+
[content]
134137
);
135138
return (
136139
<HighlightWrapperElement ref={ref} {...props}>
@@ -162,7 +165,7 @@ function TextDisplay({
162165
<FillElement
163166
ref={ref}
164167
dangerouslySetInnerHTML={{
165-
__html: getValidHTML(content),
168+
__html: content,
166169
}}
167170
{...props}
168171
/>

assets/src/edit-story/elements/text/output.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
* External dependencies
1919
*/
2020
import PropTypes from 'prop-types';
21+
import { useMemo } from 'react';
2122

2223
/**
2324
* Internal dependencies
2425
*/
2526
import StoryPropTypes from '../../types';
26-
import removeInlineStyle from '../../utils/removeInlineStyle';
2727
import generatePatternStyles from '../../utils/generatePatternStyles';
28-
import getValidHTML from '../../utils/getValidHTML';
28+
import { getHTMLFormatters } from '../../components/richText/htmlManipulation';
29+
import createSolid from '../../utils/createSolid';
2930
import { dataToEditorX, dataToEditorY } from '../../units';
3031
import { BACKGROUND_TEXT_MODE } from '../../constants';
3132
import { generateParagraphTextStyle, getHighlightLineheight } from './util';
@@ -129,9 +130,12 @@ export function TextOutputWithUnits({
129130
};
130131

131132
if (backgroundTextMode === BACKGROUND_TEXT_MODE.HIGHLIGHT) {
132-
const foregroundContent = getValidHTML(content);
133-
const backgroundContent = getValidHTML(content, (node) =>
134-
removeInlineStyle(node, 'color')
133+
const foregroundContent = content;
134+
// Setting the text color of the entire block to black essentially removes all inline
135+
// color styling allowing us to apply transparent to all of them.
136+
const backgroundContent = useMemo(
137+
() => getHTMLFormatters().setColor(content, createSolid(0, 0, 0)),
138+
[content]
135139
);
136140
return (
137141
<>
@@ -163,7 +167,7 @@ export function TextOutputWithUnits({
163167
<p
164168
className={className}
165169
style={fillStyle}
166-
dangerouslySetInnerHTML={{ __html: getValidHTML(content) }}
170+
dangerouslySetInnerHTML={{ __html: content }}
167171
/>
168172
);
169173
}

assets/src/edit-story/utils/getValidHTML.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
const contentBuffer = document.createElement('div');
17+
const contentBuffer = document.createElement('template');
1818

19-
export default function getValidHTML(string, callback = null) {
19+
export default function getValidHTML(string) {
2020
contentBuffer.innerHTML = string;
21-
if (callback) {
22-
callback(contentBuffer);
23-
}
2421
return contentBuffer.innerHTML;
2522
}

assets/src/edit-story/utils/removeInlineStyle.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)