You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-10 22:11:50 +02:00
Desktop: Fixes #12358: Selected emoji for new notebooks display too large until Joplin is restarted (#12888)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useMemo, useRef, useState } from 'react';
|
import { useMemo, useRef, useState, useCallback } from 'react';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
width: number;
|
width: number;
|
||||||
@@ -9,40 +9,62 @@ interface Props {
|
|||||||
const fontSizeCache_: Record<string, number> = {};
|
const fontSizeCache_: Record<string, number> = {};
|
||||||
|
|
||||||
export default (props: Props) => {
|
export default (props: Props) => {
|
||||||
const containerRef = useRef(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const [containerReady, setContainerReady] = useState(false);
|
const [containerReady, setContainerReady] = useState(false);
|
||||||
|
|
||||||
|
const refCallback = useCallback((el: HTMLDivElement | null) => {
|
||||||
|
if (el && !containerRef.current) {
|
||||||
|
containerRef.current = el;
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
setContainerReady(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
const fontSize = useMemo(() => {
|
const fontSize = useMemo(() => {
|
||||||
if (!containerReady) return props.height;
|
if (!containerReady || !containerRef.current) {
|
||||||
|
return Math.min(props.height * 0.7, 14);
|
||||||
|
}
|
||||||
|
|
||||||
const cacheKey = [props.width, props.height, props.emoji].join('-');
|
const cacheKey = [props.width, props.height, props.emoji].join('-');
|
||||||
if (fontSizeCache_[cacheKey]) {
|
if (fontSizeCache_[cacheKey]) {
|
||||||
return 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;
|
let spanFontSize = props.height;
|
||||||
|
|
||||||
const span = document.createElement('span');
|
const span = document.createElement('span');
|
||||||
span.innerText = props.emoji;
|
span.innerText = props.emoji;
|
||||||
span.style.fontSize = `${spanFontSize}px`;
|
span.style.fontSize = `${spanFontSize}px`;
|
||||||
|
span.style.visibility = 'hidden';
|
||||||
|
span.style.position = 'absolute';
|
||||||
|
span.style.whiteSpace = 'nowrap';
|
||||||
containerRef.current.appendChild(span);
|
containerRef.current.appendChild(span);
|
||||||
|
|
||||||
let rect = span.getBoundingClientRect();
|
let rect = span.getBoundingClientRect();
|
||||||
|
while ((rect.height > props.height || rect.width > props.width) && spanFontSize > 1) {
|
||||||
while (rect.height > props.height) {
|
spanFontSize -= 0.5;
|
||||||
spanFontSize -= .5;
|
|
||||||
span.style.fontSize = `${spanFontSize}px`;
|
span.style.fontSize = `${spanFontSize}px`;
|
||||||
rect = span.getBoundingClientRect();
|
rect = span.getBoundingClientRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
span.remove();
|
span.remove();
|
||||||
|
|
||||||
fontSizeCache_[cacheKey] = spanFontSize;
|
fontSizeCache_[cacheKey] = spanFontSize;
|
||||||
return spanFontSize;
|
return spanFontSize;
|
||||||
}, [props.width, props.height, props.emoji, containerReady, containerRef]);
|
}, [props.width, props.height, props.emoji, containerReady]);
|
||||||
|
|
||||||
return <div className="emoji-box" ref={el => { containerRef.current = el; setContainerReady(true); }} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', width: props.width, height: props.height, fontSize }}>{props.emoji}</div>;
|
return <div
|
||||||
|
ref={refCallback}
|
||||||
|
style={{
|
||||||
|
width: props.width,
|
||||||
|
height: props.height,
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center',
|
||||||
|
overflow: 'hidden',
|
||||||
|
fontSize,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.emoji}
|
||||||
|
</div>;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user