fix: 按图片真实尺寸登记文档,避免播放端尺寸校验失败
This commit is contained in:
1 parent
5c24941d50
commit
c6a5938e33
7 files changed
+203
-14
No files matched your search
@@ -629,6 +629,7 @@
|
||||
history.replaceState(null, '', `?id=${encodeURIComponent(id)}`);
|
||||
renderAll();
|
||||
toast('已加载「' + (state.doc.title || id) + '」');
|
||||
normalizeImageSizes();
|
||||
} catch (e) {
|
||||
toast(e.message, true);
|
||||
}
|
||||
@@ -849,6 +850,72 @@
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------- 图片尺寸校正 ---------------- */
|
||||
|
||||
/** 浏览器加载图片拿真实像素尺寸;失败返回 null(保留原值,避免写成 0)。 */
|
||||
function probeImageSize(url) {
|
||||
return new Promise((resolve) => {
|
||||
const image = new Image();
|
||||
image.onload = () => resolve({ w: image.naturalWidth, h: image.naturalHeight });
|
||||
image.onerror = () => resolve(null);
|
||||
image.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
/** 把组件像素宽高写成真实尺寸,返回是否有变化。 */
|
||||
function applyImageSize(item, size) {
|
||||
if (!size || !size.w || !size.h) return false;
|
||||
const width = String(size.w);
|
||||
const height = String(size.h);
|
||||
if (item.props.pixelWidth === width && item.props.pixelHeight === height) return false;
|
||||
item.props.pixelWidth = width;
|
||||
item.props.pixelHeight = height;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 修改 URL 后自动探测尺寸,避免登记的宽高与图片实际尺寸不一致导致播放端报错。 */
|
||||
async function syncImageSize(item) {
|
||||
const doc = state.doc;
|
||||
const url = ((item.props && item.props.url) || '').trim();
|
||||
if (!url) return;
|
||||
const size = await probeImageSize(url);
|
||||
if (state.doc !== doc || !applyImageSize(item, size)) return;
|
||||
renderAll();
|
||||
refreshDirty();
|
||||
toast(`已校正图片尺寸为 ${size.w}×${size.h}`);
|
||||
}
|
||||
|
||||
/** 载入页面后批量校正图片尺寸:站内资源取资源库数据,外部 URL 用浏览器探测。 */
|
||||
async function normalizeImageSizes() {
|
||||
const doc = state.doc;
|
||||
if (!doc) return;
|
||||
const images = doc.items.filter((item) => item.type === 'IMAGE' && item.props && item.props.url);
|
||||
if (!images.length) return;
|
||||
|
||||
let assets = [];
|
||||
try {
|
||||
const res = await fetch('/api/remote-ui/assets');
|
||||
if (res.ok) assets = await res.json();
|
||||
} catch (ignored) {
|
||||
// 资源库不可用时仍然尝试逐个探测外部图片。
|
||||
}
|
||||
|
||||
let changed = 0;
|
||||
for (const item of images) {
|
||||
if (state.doc !== doc) return;
|
||||
const url = item.props.url.split('?')[0];
|
||||
const asset = assets.find((candidate) => url.endsWith(candidate.path));
|
||||
const size = asset
|
||||
? { w: asset.pixelWidth, h: asset.pixelHeight }
|
||||
: await probeImageSize(item.props.url);
|
||||
if (applyImageSize(item, size)) changed += 1;
|
||||
}
|
||||
if (state.doc !== doc || !changed) return;
|
||||
renderAll();
|
||||
refreshDirty();
|
||||
toast(`已自动校正 ${changed} 张图片尺寸,请保存并发布`);
|
||||
}
|
||||
|
||||
async function readError(res) {
|
||||
try {
|
||||
const body = await res.json();
|
||||
@@ -1233,6 +1300,11 @@
|
||||
};
|
||||
});
|
||||
|
||||
const imageUrlInput = inspector.querySelector('[data-prop="url"]');
|
||||
if (imageUrlInput && item.type === 'IMAGE') {
|
||||
imageUrlInput.addEventListener('change', () => syncImageSize(item));
|
||||
}
|
||||
|
||||
const actionSelect = inspector.querySelector('[data-action="type"]');
|
||||
const actionTarget = inspector.querySelector('[data-action="target"]');
|
||||
actionSelect.value = item.action ? item.action.type : '';
|
||||
|
||||
Reference in new issue
Block a user