Skip to content

Commit

Permalink
Left truncate the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 30, 2022
1 parent 7674a98 commit 58bec17
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
3 changes: 0 additions & 3 deletions packages/edit-post/src/components/sidebar/post-url/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
}

.components-button.edit-post-post-url__toggle {
display: inline-block;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}

.edit-post-post-url__dialog .editor-post-url {
Expand Down
43 changes: 42 additions & 1 deletion packages/editor/src/components/post-url/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,56 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { filterURLForDisplay } from '@wordpress/url';
import { useRef, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';

export default function PostURLLabel() {
return useSelect(
const postLink = useSelect(
( select ) => select( editorStore ).getCurrentPost().link,
[]
);
return (
<LeftTruncatedText>
{ filterURLForDisplay( postLink ) }
</LeftTruncatedText>
);
}

function LeftTruncatedText( { children: text } ) {
const ref = useRef();

useEffect( () => {
const container = ref.current;

container.innerText = text;

container.style.display = 'block';
container.style.width = '100%';
const maxWidth = container.clientWidth;

container.style.display = null;
container.style.width = null;

const canvas = container.ownerDocument.createElement( 'canvas' );
const context = canvas.getContext( '2d' );
context.font = window
.getComputedStyle( container )
.getPropertyValue( 'font' );

let truncatedText = text;
let truncateAmount = 1;
while ( context.measureText( truncatedText ).width > maxWidth ) {
truncatedText = '…' + text.substring( truncateAmount );
truncateAmount++;
}

container.innerText = truncatedText;
}, [ text ] );

return <span ref={ ref }></span>;
}

0 comments on commit 58bec17

Please sign in to comment.