Skip to content
Merged
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
7 changes: 2 additions & 5 deletions src/components/resizable-column/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@
transform: translate(-6px, -6px) rotate(45deg);
}
// Drop down icon.
&::after {
content: "\f347";
font-family: Dashicons;
svg {
top: 2px;
position: relative;
margin-left: 4px;
font-size: 11px;
display: none;
}

Expand Down Expand Up @@ -116,7 +113,7 @@
background: transparent;
display: inline;
}
&::after {
svg {
display: inline;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/resizable-column/images/arrow-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 20 additions & 15 deletions src/components/resizable-column/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { fixFractionWidths, getSnapWidths } from './get-snap-widths'
import { AdvancedTextControl, Popover } from '..'
import { ColumnShowTooltipContext } from '../column-inner-blocks'
import ArrowDownSvg from './images/arrow-down.svg'

/**
* External dependencies
Expand Down Expand Up @@ -37,6 +38,11 @@ const MIN_COLUMN_WIDTH_PERCENTAGE = {
Mobile: 10,
}

// Prevent rounding off decimals.
const formatLabel = value => {
return ( Math.trunc( value * 10 ) / 10 ).toFixed( 1 )
}

const ResizableColumn = props => {
const { clientId } = useBlockEditContext()
const blockContext = useBlockContext()
Expand Down Expand Up @@ -207,15 +213,15 @@ const ResizableColumn = props => {

// Fix the widths, ensure that our total width is 100%
columnPercentages = ( columnWidths || [] ).map( width => {
return parseFloat( ( width / totalWidth * 100 ).toFixed( 1 ) )
return parseFloat( formatLabel( width / totalWidth * 100 ) )
} )
// Fix the widths, ensure that we don't end up with off numbers 49.9% and 50.1%.
columnPercentages = fixFractionWidths( columnPercentages, isShiftKey )

// Ensure that the total width is exactly 100%.
let totalCurrentWidth = columnPercentages.reduce( ( a, b ) => a + b, 0 )
if ( totalCurrentWidth !== 100 ) {
columnPercentages[ adjacentBlockIndex ] = parseFloat( ( columnPercentages[ adjacentBlockIndex ] + 100 - totalCurrentWidth ).toFixed( 1 ) )
columnPercentages[ adjacentBlockIndex ] = parseFloat( formatLabel( columnPercentages[ adjacentBlockIndex ] + 100 - totalCurrentWidth ) )
}

// There are cases where the total width may slightly go past 100%
Expand All @@ -235,7 +241,7 @@ const ResizableColumn = props => {
max-width: ${ width }% !important;
}
[data-block="${ adjacentBlocks[ i ].clientId }"] .stk-resizable-column__size-tooltip {
--width: '${ width.toFixed( 1 ) }%' !important;
--width: '${ formatLabel( width ) }%' !important;
}`
} ).join( '' )
setTempStyles( columnStyles )
Expand All @@ -251,8 +257,10 @@ const ResizableColumn = props => {
// control.
} else {
const newWidth = currentWidth + delta.width
columnPercentages = clamp( parseFloat( ( newWidth / maxWidth * 100 ).toFixed( 1 ) ), 0, 100 )
columnPercentages = clamp( parseFloat( formatLabel( newWidth / maxWidth * 100 ) ), 0, 100 )

// Fix the widths, ensure that we don't end up with off numbers 49.9% and 50.1%.
columnPercentages = fixFractionWidths( [ columnPercentages ], isShiftKey )[ 0 ]
setNewWidthsPercent( columnPercentages )

// Take into account the number of adjacent columns per row
Expand All @@ -267,7 +275,7 @@ const ResizableColumn = props => {
max-width: calc(${ columnPercentages }% - var(--stk-column-gap, 0px) * ${ adjacentColumnCount - 1 } / ${ adjacentColumnCount } ) !important;
}
[data-block="${ clientId }"] .stk-resizable-column__size-tooltip {
--width: '${ columnPercentages.toFixed( 1 ) }%' !important;
--width: '${ formatLabel( columnPercentages ) }%' !important;
}`
setTempStyles( columnStyles )

Expand Down Expand Up @@ -465,25 +473,21 @@ const ResizableTooltip = memo( props => {
let columnLabel = ''
if ( typeof adjacentBlocks !== 'undefined' && ! props.value && ! originalInputValue ) {
// The columns are evenly distributed by default.
if ( deviceType === 'Desktop' ) {
const value = ( 100 / adjacentBlocks.length ).toFixed( 1 )
if ( deviceType === 'Desktop' || deviceType === 'Tablet' ) {
const value = formatLabel( 100 / adjacentBlocks.length )
if ( value.toString() === '33.3' ) {
columnLabel = 33.33
} else {
columnLabel = value
}
} else {
// In mobile, the columns collapse to 100%.
columnLabel = 100.0
}
// In mobile, the columns are "auto" so that we don't display
// inaccurate percentage widths.
} else if ( deviceType === 'Tablet' ) {
columnLabel = __( 'Auto', i18n )
} else {
// In mobile, the columns collapse to 100%.
columnLabel = 100.0
}

// Create the label of the tooltip.
const _label = ( props.value ? parseFloat( props.value ).toFixed( 1 ) : '' ) || originalInputValue || columnLabel
const _label = ( props.value ? formatLabel( parseFloat( props.value ) ) : '' ) || originalInputValue || columnLabel
const tooltipLabel = _label !== __( 'Auto', i18n ) ? `'${ _label }%'` : `'${ _label }'`

// Setup the input field when the popup opens.
Expand Down Expand Up @@ -593,6 +597,7 @@ const ResizableTooltip = memo( props => {
role="button"
tabIndex="0"
>
<ArrowDownSvg fill="currentColor" width="10" />
</div>
)
}
Expand Down
15 changes: 14 additions & 1 deletion src/hooks/use-with-shift.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ const useWithShift = () => {

useEffect( () => {
const handleShiftKeyToggle = event => {
setIsShiftKey( event.shiftKey )
setIsShiftKey( !! event.shiftKey )
}

window.addEventListener( 'keydown', handleShiftKeyToggle ) // eslint-disable-line @wordpress/no-global-event-listener
window.addEventListener( 'keyup', handleShiftKeyToggle ) // eslint-disable-line @wordpress/no-global-event-listener

// This ensures to toggle the shift key when the iframe is in focus.
const iframe = document?.querySelector( 'iframe[name="editor-canvas"]' )
if ( iframe ) {
const doc = iframe.contentDocument || iframe.contentWindow.document
doc.addEventListener( 'keyup', handleShiftKeyToggle )
}

return () => {
window.removeEventListener( 'keydown', handleShiftKeyToggle ) // eslint-disable-line @wordpress/no-global-event-listener
window.removeEventListener( 'keyup', handleShiftKeyToggle ) // eslint-disable-line @wordpress/no-global-event-listener

const iframe = document?.querySelector( 'iframe[name="editor-canvas"]' )
if ( iframe ) {
const doc = iframe.contentDocument || iframe.contentWindow.document
doc.removeEventListener( 'keyup', handleShiftKeyToggle )
}
}
}, [] )

Expand Down