diff --git a/.eslintignore b/.eslintignore index 1618630fab..8b2f6e2c0f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -252,6 +252,9 @@ packages/app-desktop/gui/EditFolderDialog/Dialog.js.map packages/app-desktop/gui/EditFolderDialog/IconSelector.d.ts packages/app-desktop/gui/EditFolderDialog/IconSelector.js packages/app-desktop/gui/EditFolderDialog/IconSelector.js.map +packages/app-desktop/gui/EmojiBox.d.ts +packages/app-desktop/gui/EmojiBox.js +packages/app-desktop/gui/EmojiBox.js.map packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.d.ts packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.js packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.js.map diff --git a/.gitignore b/.gitignore index 100f7e7740..f2b576562b 100644 --- a/.gitignore +++ b/.gitignore @@ -240,6 +240,9 @@ packages/app-desktop/gui/EditFolderDialog/Dialog.js.map packages/app-desktop/gui/EditFolderDialog/IconSelector.d.ts packages/app-desktop/gui/EditFolderDialog/IconSelector.js packages/app-desktop/gui/EditFolderDialog/IconSelector.js.map +packages/app-desktop/gui/EmojiBox.d.ts +packages/app-desktop/gui/EmojiBox.js +packages/app-desktop/gui/EmojiBox.js.map packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.d.ts packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.js packages/app-desktop/gui/EncryptionConfigScreen/EncryptionConfigScreen.js.map diff --git a/packages/app-desktop/gui/EmojiBox.tsx b/packages/app-desktop/gui/EmojiBox.tsx new file mode 100644 index 0000000000..dd3951b5ba --- /dev/null +++ b/packages/app-desktop/gui/EmojiBox.tsx @@ -0,0 +1,46 @@ +import { useMemo, useRef } from 'react'; + +interface Props { + width: number; + height: number; + emoji: string; +} + +const fontSizeCache_: Record = {}; + +export default (props: Props) => { + const containerRef = useRef(null); + + const fontSize = useMemo(() => { + if (!containerRef.current) return props.height; + + const cacheKey = [props.width, props.height, props.emoji].join('-'); + if (fontSizeCache_[cacheKey]) { + return fontSizeCache_[cacheKey]; + } + + // Set the emoji font size so that it fits within the specified width + // and height. In fact, currently it only looks at the height. + + let spanFontSize = props.height; + + const span = document.createElement('span'); + span.innerText = props.emoji; + span.style.fontSize = `${spanFontSize}px`; + containerRef.current.appendChild(span); + + let rect = span.getBoundingClientRect(); + while (rect.height > props.height) { + spanFontSize -= .5; + span.style.fontSize = `${spanFontSize}px`; + rect = span.getBoundingClientRect(); + } + + span.remove(); + + fontSizeCache_[cacheKey] = spanFontSize; + return spanFontSize; + }, [props.width, props.height, props.emoji, containerRef]); + + return
{props.emoji}
; +}; diff --git a/packages/app-desktop/gui/FolderIconBox.tsx b/packages/app-desktop/gui/FolderIconBox.tsx index f2c51f7e97..ca67fbfeeb 100644 --- a/packages/app-desktop/gui/FolderIconBox.tsx +++ b/packages/app-desktop/gui/FolderIconBox.tsx @@ -1,4 +1,5 @@ import { FolderIcon, FolderIconType } from '@joplin/lib/services/database/types'; +import EmojiBox from './EmojiBox'; interface Props { folderIcon: FolderIcon; @@ -8,13 +9,15 @@ interface Props { export default function(props: Props) { const folderIcon = props.folderIcon; const opacity = 'opacity' in props ? props.opacity : 1; + const width = 20; + const height = 20; if (folderIcon.type === FolderIconType.Emoji) { - return {folderIcon.emoji}; + return ; } else if (folderIcon.type === FolderIconType.DataUrl) { - return ; + return ; } else if (folderIcon.type === FolderIconType.FontAwesome) { - return ; + return ; } else { throw new Error(`Unsupported folder icon type: ${folderIcon.type}`); }