Commit dab4e6fb by Яков

fix

parent bbaa6d76
{
"name": "react-ag-qeditor",
"version": "1.0.98",
"version": "1.0.99",
"description": "WYSIWYG html editor",
"author": "atma",
"license": "MIT",
......
......@@ -105,10 +105,27 @@ const ResizableImageTemplate = ({ node, updateAttributes, editor, getPos, select
const naturalWidth = imgRef.current.naturalWidth;
const naturalHeight = imgRef.current.naturalHeight;
// Проверяем, что изображение загружено и имеет корректные размеры
if (naturalWidth <= 0 || naturalHeight <= 0) {
console.warn('Image has invalid natural dimensions, retrying...');
setTimeout(initImageSize, 100); // Повторная попытка через 100 мс
return;
}
// Рассчитываем начальные размеры с учетом максимальной ширины редактора
let initialWidth = naturalWidth;
let initialHeight = naturalHeight;
if (initialWidth > editorWidth) {
const ratio = editorWidth / initialWidth;
initialWidth = editorWidth;
initialHeight = Math.round(initialHeight * ratio);
}
safeUpdateAttributes({
width: naturalWidth,
height: naturalHeight,
'data-node-id': node.attrs['data-node-id'] || Math.random().toString(36).substr(2, 9)
width: initialWidth,
height: initialHeight,
'data-node-id': node.attrs['data-node-id'] || `img-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
});
isInitialized.current = true;
} catch (error) {
......@@ -116,14 +133,21 @@ const ResizableImageTemplate = ({ node, updateAttributes, editor, getPos, select
}
};
const handleLoad = () => {
// Добавляем небольшую задержку для гарантированного получения размеров
setTimeout(initImageSize, 50);
};
if (imgRef.current.complete) {
initImageSize();
handleLoad();
} else {
imgRef.current.onload = initImageSize;
imgRef.current.addEventListener('load', handleLoad);
}
return () => {
if (imgRef.current) imgRef.current.onload = null;
if (imgRef.current) {
imgRef.current.removeEventListener('load', handleLoad);
}
};
}, [node.attrs.width, node.attrs.height, node.attrs['data-node-id']]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment