Skip to content
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

Add text highlight mode for text background #684

Merged
merged 30 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
24a7360
Add text highlight mode for display and output
obetomuniz Mar 20, 2020
1405dbe
Merge branch 'master' into add/text-highlighting
obetomuniz Mar 24, 2020
ca24447
Remove clone node and unnecessary vendor prefix explict declarations
obetomuniz Mar 24, 2020
9b76100
Fix typo declaring vendor prefix
obetomuniz Mar 24, 2020
bc18d56
Fix mini preview while using Highlight backgroud on text
obetomuniz Mar 24, 2020
56f0934
Simplify Text Highlight structure (it should also fix CI).
obetomuniz Mar 24, 2020
b647074
Merge branch 'master' into add/text-highlighting
obetomuniz Mar 24, 2020
6b9139b
Add multiple selection support to background mode
obetomuniz Mar 24, 2020
3b26daf
Add support to NONE state.
obetomuniz Mar 24, 2020
948e260
Address some PR review comments
obetomuniz Mar 25, 2020
c32451d
Add data migration
obetomuniz Mar 25, 2020
e71df1f
Merge branch 'master' into add/text-highlighting
Mar 25, 2020
e4b90b1
Added better highlight fill styles
Mar 26, 2020
f7a854a
Added keyboard arrow nav for bg text mode toggles
Mar 26, 2020
f64e945
Better line height management for highlight mode
Mar 26, 2020
3019863
Revert changes to text frame
Mar 26, 2020
0a053c7
Reuse better line height calc for text frame
Mar 26, 2020
4a7abb3
Merge branch 'master' into add/text-highlighting
Mar 26, 2020
ef57283
Merge branch 'master' into add/text-highlighting
Mar 28, 2020
24a0d99
Merge branch 'master' into add/text-highlighting
Mar 28, 2020
b2afd2b
Address some PR review comments.
obetomuniz Mar 31, 2020
25ff1b2
Allow missing padding
Mar 31, 2020
62e5540
Remove doubled ref
Mar 31, 2020
59cf798
Merge branch 'master' into add/text-highlighting
Mar 31, 2020
4ff1587
Merge branch 'add/text-highlighting' of github.com:google/web-stories…
Mar 31, 2020
3dc9ab4
Fix highlight text field sizing
Mar 31, 2020
f1e5c55
Fix default background fill mode
Mar 31, 2020
fc194bb
Better edit component (but still not great)
Mar 31, 2020
898ccec
Bump migration version
Mar 31, 2020
39c2f1b
Merge branch 'master' into add/text-highlighting
Mar 31, 2020
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
4 changes: 3 additions & 1 deletion assets/src/edit-story/components/form/toggleButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ function ToggleButton({
iconWidth,
iconHeight,
label,
className,
...rest
}) {
value = value === MULTIPLE_VALUE ? '' : value;
return (
<Container>
<Container className={className}>
<ContainerLabel
disabled={disabled}
value={value}
Expand Down Expand Up @@ -127,6 +128,7 @@ ToggleButton.propTypes = {
disabled: PropTypes.bool,
iconHeight: PropTypes.number,
iconWidth: PropTypes.number,
className: PropTypes.string,
};

ToggleButton.defaultProps = {
Expand Down
131 changes: 118 additions & 13 deletions assets/src/edit-story/components/panels/textStyle/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* External dependencies
*/
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { useRef } from 'react';

/**
* WordPress dependencies
Expand All @@ -27,15 +29,81 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { Color, Label, Row } from '../../form';
import { useCommonColorValue } from '../utils';
import { BACKGROUND_TEXT_MODE } from '../../../constants';
import { ReactComponent as NoneIcon } from '../../../icons/fill_none_icon.svg';
import { ReactComponent as FilledIcon } from '../../../icons/fill_filled_icon.svg';
import { ReactComponent as HighlightedIcon } from '../../../icons/fill_highlighted_icon.svg';
import { Color, Label, Row, ToggleButton } from '../../form';
import { useKeyDownEffect } from '../../keyboard';
import { useCommonColorValue, getCommonValue } from '../utils';

const FillRow = styled(Row)`
align-items: flex-start;
justify-content: flex-start;
`;

const FillLabel = styled(Label)`
flex-basis: 45px;
line-height: 32px;
`;

const FillToggleButton = styled(ToggleButton)`
flex: 1 1 32px;
svg {
width: 16px;
height: 16px;
}
`;

const Space = styled.div`
flex: ${({ flex }) => flex};
`;

const BUTTONS = [
{
mode: BACKGROUND_TEXT_MODE.NONE,
label: __('None', 'web-stories'),
Icon: NoneIcon,
},
{
mode: BACKGROUND_TEXT_MODE.FILL,
label: __('Fill', 'web-stories'),
Icon: FilledIcon,
},
{
mode: BACKGROUND_TEXT_MODE.HIGHLIGHT,
label: __('Highlight', 'web-stories'),
Icon: HighlightedIcon,
},
];

function ColorControls({ selectedElements, pushUpdate }) {
const color = useCommonColorValue(selectedElements, 'color');
const backgroundColor = useCommonColorValue(
selectedElements,
'backgroundColor'
);
const backgroundTextMode = getCommonValue(
selectedElements,
'backgroundTextMode'
);
const fillRow = useRef();

useKeyDownEffect(
fillRow,
['left', 'right'],
({ key }) => {
const current = BUTTONS.findIndex(
({ mode }) => mode === backgroundTextMode
);
const next = current + (key === 'ArrowRight' ? 1 : -1);
if (next < 0 || next > BUTTONS.length - 1) {
return;
}
pushUpdate({ backgroundTextMode: BUTTONS[next].mode }, true);
},
[backgroundTextMode]
);

return (
<>
Expand All @@ -44,19 +112,56 @@ function ColorControls({ selectedElements, pushUpdate }) {
<Color
data-testid="text.color"
value={color}
onChange={(value) => pushUpdate({ color: value }, true)}
/>
</Row>
<Row>
<Label>{__('Textbox', 'web-stories')}</Label>
<Color
data-testid="text.backgroundColor"
hasGradient
value={backgroundColor}
onChange={(value) => pushUpdate({ backgroundColor: value }, true)}
label={__('Background color', 'web-stories')}
onChange={(value) =>
pushUpdate(
{
color: value,
},
true
)
}
/>
</Row>
<FillRow ref={fillRow}>
<FillLabel>{__('Fill', 'web-stories')}</FillLabel>
{BUTTONS.map(({ mode, label, Icon }) => (
<FillToggleButton
key={mode}
icon={<Icon />}
value={backgroundTextMode === mode}
label={label}
onChange={(value) =>
value &&
pushUpdate(
{
backgroundTextMode: mode,
},
true
)
}
/>
))}
<Space flex="2" />
</FillRow>
{backgroundTextMode !== BACKGROUND_TEXT_MODE.NONE && (
<Row>
<Label>{__('Textbox', 'web-stories')}</Label>
<Color
data-testid="text.backgroundColor"
hasGradient
value={backgroundColor}
onChange={(value) =>
pushUpdate(
{
backgroundColor: value,
},
true
)
}
label={__('Background color', 'web-stories')}
/>
</Row>
)}
</>
);
}
Expand Down
6 changes: 6 additions & 0 deletions assets/src/edit-story/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ export const Z_INDEX_CANVAS = {
MOVABLE: 10,
FLOAT_PANEL: 11,
};

export const BACKGROUND_TEXT_MODE = {
NONE: 'NONE',
FILL: 'FILL',
HIGHLIGHT: 'HIGHLIGHT',
};
108 changes: 97 additions & 11 deletions assets/src/edit-story/elements/text/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,73 @@ import {
elementWithTextParagraphStyle,
} from '../shared';
import StoryPropTypes from '../../types';
import { BACKGROUND_TEXT_MODE } from '../../constants';
import { useTransformHandler } from '../../components/transform';
import { draftMarkupToContent, generateParagraphTextStyle } from './util';

const Element = styled.p`
${elementFillContent}
${elementWithFont}
${elementWithBackgroundColor}
${elementWithFontColor}
${elementWithTextParagraphStyle}
import {
draftMarkupToContent,
getHighlightLineheight,
generateParagraphTextStyle,
} from './util';

const HighlightElement = styled.p`
${elementFillContent}
${elementWithFont}
${elementWithFontColor}
${elementWithTextParagraphStyle}
line-height: ${({ lineHeight, verticalPadding }) =>
getHighlightLineheight(lineHeight, verticalPadding)};
margin: 0;
padding: 0;
`;

const HIGHLIGHT_MARGIN_BUFFER = 10;

const MarginedElement = styled.span`
display: inline-block;
barklund marked this conversation as resolved.
Show resolved Hide resolved
position: relative;
top: 0;
margin: ${({ horizontalPadding, horizontalBuffer }) =>
`0 ${horizontalPadding + horizontalBuffer}px`};
left: ${({ horizontalPadding, horizontalBuffer }) =>
`-${horizontalPadding + horizontalBuffer}px`};
`;

const Span = styled.span`
${elementWithBackgroundColor}
${elementWithTextParagraphStyle}

border-radius: 3px;
box-decoration-break: clone;
position: relative;
`;

const BackgroundSpan = styled(Span)`
color: transparent;
`;

const ForegroundSpan = styled(Span)`
background: none;
`;

const FillElement = styled.p`
margin: 0;
${elementFillContent}
${elementWithFont}
${elementWithBackgroundColor}
${elementWithFontColor}
${elementWithTextParagraphStyle}
`;

function TextDisplay({
element: { id, bold, content, color, backgroundColor, ...rest },
element: {
id,
bold,
content,
color,
backgroundColor,
backgroundTextMode,
...rest
},
}) {
const ref = useRef(null);

Expand All @@ -57,6 +111,9 @@ function TextDisplay({
color,
backgroundColor,
...generateParagraphTextStyle(rest, dataToEditorX, dataToEditorY),
horizontalPadding: dataToEditorX(rest.padding.horizontal),
barklund marked this conversation as resolved.
Show resolved Hide resolved
horizontalBuffer: dataToEditorX(HIGHLIGHT_MARGIN_BUFFER),
verticalPadding: dataToEditorX(rest.padding.vertical),
};
const {
actions: { maybeEnqueueFontStyle },
Expand All @@ -75,10 +132,39 @@ function TextDisplay({
: '';
});

if (backgroundTextMode === BACKGROUND_TEXT_MODE.HIGHLIGHT) {
return (
<>
<HighlightElement ref={ref} {...props}>
<MarginedElement {...props}>
<BackgroundSpan
{...props}
dangerouslySetInnerHTML={{
__html: draftMarkupToContent(content, bold),
}}
/>
</MarginedElement>
</HighlightElement>
<HighlightElement ref={ref} {...props}>
barklund marked this conversation as resolved.
Show resolved Hide resolved
<MarginedElement {...props}>
<ForegroundSpan
{...props}
dangerouslySetInnerHTML={{
__html: draftMarkupToContent(content, bold),
}}
/>
</MarginedElement>
</HighlightElement>
</>
);
}

return (
<Element
<FillElement
ref={ref}
dangerouslySetInnerHTML={{ __html: draftMarkupToContent(content, bold) }}
dangerouslySetInnerHTML={{
__html: draftMarkupToContent(content, bold),
}}
{...props}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion assets/src/edit-story/elements/text/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Element = styled.p`
${elementWithFont}
${elementWithTextParagraphStyle}

opacity: 0;
opacity: 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any special highlight work in frame? We use an invisible text to figure out which part of an element is selected for editor.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not completely sure what you mean here. Currently there's no special handling in the frame for a highlight text element. What should happen in the frame, if things were done correctly?

Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be ok to move it to a separate pull request, as long as we don't forget about this entirely. But this is actually originally your code. We generally try to make sure the text in the frame is displayed (but not visible) the same way as display component to ensure that selection logic is correct. See this: https://github.com/google/web-stories-wp/blob/c6af16cfc77e8fc02efe215358a20e8dc8fd3f64/assets/src/edit-story/elements/text/frame.js#L111-L114

user-select: none;
`;

Expand Down
2 changes: 2 additions & 0 deletions assets/src/edit-story/elements/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/**
* Internal dependencies
*/
import { BACKGROUND_TEXT_MODE } from '../../constants';
import { PanelTypes } from '../../components/panels';
import { SHARED_DEFAULT_ATTRIBUTES } from '../shared';
export { default as Display } from './display';
Expand All @@ -30,6 +31,7 @@ export { default as updateForResizeEvent } from './updateForResizeEvent';

export const defaultAttributes = {
...SHARED_DEFAULT_ATTRIBUTES,
backgroundTextMode: BACKGROUND_TEXT_MODE.FILL,
barklund marked this conversation as resolved.
Show resolved Hide resolved
bold: false,
fontFamily: 'Arial',
fontFallback: ['Helvetica Neue', 'Helvetica', 'sans-serif'],
Expand Down
Loading