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

Font Library: move font uploads to a new tab #54655

Merged
merged 5 commits into from
Sep 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,6 @@ function FontLibraryProvider( { children } ) {
setIsInstalling( false );
return true;
} catch ( e ) {
// eslint-disable-next-line no-console
console.error( e );
createErrorNotice( __( 'Error installing fonts.' ), {
type: 'snackbar',
} );
setIsInstalling( false );
return false;
}
Expand Down Expand Up @@ -313,7 +308,7 @@ function FontLibraryProvider( { children } ) {
// Get the src of the font.
const src = getDisplaySrcFromFontFace( fontFace.src, themeUrl );
// If the font is already loaded, don't load it again.
if ( loadedFontUrls.has( src ) ) return;
if ( ! src || loadedFontUrls.has( src ) ) return;
// Load the font in the browser.
loadFontFaceInBrowser( fontFace, src, 'document' );
// Add the font to the loaded fonts list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import { useContext } from '@wordpress/element';
*/
import InstalledFonts from './installed-fonts';
import FontCollection from './font-collection';
import UploadFonts from './upload-fonts';
import { FontLibraryContext } from './context';

const INSTALLED_FONTS_TAB = {
name: 'installed-fonts',
title: __( 'Library' ),
className: 'installed-fonts',
};
const DEFAULT_TABS = [
{
name: 'installed-fonts',
title: __( 'Library' ),
className: 'installed-fonts',
},
{
name: 'upload-fonts',
title: __( 'Upload' ),
className: 'upload-fonts',
},
];

const tabsFromCollections = ( collections ) =>
collections.map( ( { id, name } ) => ( {
Expand All @@ -35,7 +43,7 @@ function FontLibraryModal( {
const { collections } = useContext( FontLibraryContext );

const tabs = [
INSTALLED_FONTS_TAB,
...DEFAULT_TABS,
...tabsFromCollections( collections || [] ),
];

Expand All @@ -53,6 +61,8 @@ function FontLibraryModal( {
>
{ ( tab ) => {
switch ( tab.name ) {
case 'upload-fonts':
return <UploadFonts />;
case 'installed-fonts':
return <InstalledFonts />;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { FontLibraryContext } from './context';
import FontsGrid from './fonts-grid';
import LibraryFontDetails from './library-font-details';
import LibraryFontCard from './library-font-card';
import LocalFonts from './local-fonts';
import ConfirmDeleteDialog from './confirm-delete-dialog';
import { unlock } from '../../../lock-unlock';
const { ProgressBar } = unlock( componentsPrivateApis );
Expand Down Expand Up @@ -129,9 +128,6 @@ function InstalledFonts() {
</FontsGrid>
</>
) }

<Spacer margin={ 8 } />
<LocalFonts />
</>
) }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import {
Button,
DropZone,
__experimentalSpacer as Spacer,
__experimentalText as Text,
__experimentalVStack as VStack,
FormFileUpload,
Notice,
FlexItem,
} from '@wordpress/components';
import { useContext } from '@wordpress/element';
import { useContext, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -22,6 +25,12 @@ import { loadFontFaceInBrowser } from './utils';

function LocalFonts() {
const { installFonts } = useContext( FontLibraryContext );
const [ notice, setNotice ] = useState( null );
const supportedFormats =
ALLOWED_FILE_EXTENSIONS.slice( 0, -1 )
.map( ( extension ) => `.${ extension }` )
.join( ', ' ) +
` ${ __( 'and' ) } .${ ALLOWED_FILE_EXTENSIONS.slice( -1 ) }`;

const handleDropZone = ( files ) => {
handleFilesUpload( files );
Expand All @@ -37,6 +46,7 @@ function LocalFonts() {
* @return {void}
*/
const handleFilesUpload = ( files ) => {
setNotice( null );
const uniqueFilenames = new Set();
const selectedFiles = [ ...files ];
const allowedFiles = selectedFiles.filter( ( file ) => {
Expand Down Expand Up @@ -126,33 +136,58 @@ function LocalFonts() {
*/
const handleInstall = async ( fontFaces ) => {
const fontFamilies = makeFamiliesFromFaces( fontFaces );
await installFonts( fontFamilies );
const status = await installFonts( fontFamilies );
if ( status ) {
setNotice( {
type: 'success',
message: __( 'Upload successful.' ),
} );
}
};

return (
<>
<Text className="font-library-modal__subtitle">
{ __( 'Upload Fonts' ) }
</Text>
<Spacer margin={ 2 } />
<Spacer margin={ 16 } />
<DropZone onFilesDrop={ handleDropZone } />
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
<span>
{ __( 'Drag and drop your font files here.' ) }
</span>
</Button>
<VStack className="font-library-modal__local-fonts">
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
<span>{ __( 'Upload font' ) }</span>
</Button>
) }
/>
{ notice && (
<FlexItem>
<Spacer margin={ 2 } />
<Notice
isDismissible={ false }
status={ notice.type }
className="font-library-modal__upload-area__notice"
>
{ notice.message }
</Notice>
</FlexItem>
) }
/>
<Spacer margin={ 2 } />
<Text className="font-library-modal__upload-area__text">
{ sprintf(
/* translators: %s: supported font formats: ex: .ttf, .woff and .woff2 */
__(
'Uploaded fonts appear in your library and can be used in your theme. Supported formats: %s.'
),
supportedFormats
) }
</Text>
</VStack>
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,24 @@
align-items: center;
display: flex;
justify-content: center;
height: 100px;
height: 250px;
width: 100%;
background-color: #f0f0f0;
}

.font-library-modal__local-fonts {
margin: 0 auto;
width: 80%;

.font-library-modal__upload-area__text {
color: #6e6e6e;
}

.font-library-modal__upload-area__notice {
margin: 0;
}
}

.font-library-modal__font-name {
font-weight: bold;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { __experimentalSpacer as Spacer } from '@wordpress/components';

/**
* Internal dependencies
*/
import LocalFonts from './local-fonts';

function UploadFonts() {
return (
<>
<Spacer margin={ 8 } />
<LocalFonts />
</>
);
}

export default UploadFonts;
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export async function loadFontFaceInBrowser( fontFace, source, addTo = 'all' ) {
}

export function getDisplaySrcFromFontFace( input, urlPrefix ) {
if ( ! input ) {
return;
}

let src;
if ( Array.isArray( input ) ) {
src = input[ 0 ];
Expand Down
Loading