A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(Img): Correctly handle `onerror` issues for images where the `src` address changes.
haitaoob75c4115afa6
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsxindex f267de5c..54ed6e9f 100644--- a/ui/src/components/SchemaForm/index.tsx+++ b/ui/src/components/SchemaForm/index.tsx@@ -239,6 +239,7 @@ const SchemaForm: ForwardRefRenderFunction<FormRef, FormProps> = (uiSchema?.[key] || {};formData ||= {};const fieldState = formData[key];+const uiSimplify = widget === 'legend' || uiOpt?.simplify;let groupClassName: BaseUIOptions['fieldClassName'] = uiOpt?.simplify? 'mb-2'@@ -249,7 +250,9 @@ const SchemaForm: ForwardRefRenderFunction<FormRef, FormProps> = (if (uiOpt?.fieldClassName) {groupClassName = uiOpt.fieldClassName;}+const readOnly = uiOpt?.readOnly || false;+return (<Form.Groupkey={title}diff --git a/ui/src/index.scss b/ui/src/index.scssindex b3148310..7e7df5ee 100644--- a/ui/src/index.scss+++ b/ui/src/index.scss@@ -26,6 +26,11 @@ body {a {text-decoration: none;}+// If the image does not have a `src` attribute, it must break and may not trigger the `onerror` event.+// With or without the `alt` attribute, it is visually hidden directly.+img[src=""] {+ visibility: hidden !important;+}.btn-link {text-decoration: none;diff --git a/ui/src/index.tsx b/ui/src/index.tsxindex 2d79405b..cc6b8c93 100644--- a/ui/src/index.tsx+++ b/ui/src/index.tsx@@ -12,24 +12,37 @@ const root = ReactDOM.createRoot(/*** Uniformly hide broken images+ * - The `onload` event for elements such as `img` can only be `capture` on `document` (window cannot).+ * - For images with an empty `src` attribute, sometimes the browser will simply display the broken image without reporting an 'error' event.*/-document.addEventListener(- 'error',- (err) => {- const { target } = err;- if (target === null || !(target instanceof Element)) {- return;- }+const handleImgLoad = (evt: Event | UIEvent) => {+ const { target } = evt;++ if (target === null || !(target instanceof Element)) {+ return;+ }+ if (!/IMG/i.test(target.nodeName)) {+ return;+ }- if (/IMG/.test(target.nodeName)) {- const altText = target.getAttribute('alt')?.trim();- if (!altText) {- target.classList.add('invisible');- }+ if (/error/i.test(evt.type)) {+ target.classList.add('broken');+ const attrSrc = target.getAttribute('src');+ const attrAlt = target.getAttribute('alt')?.trim();+ // Images without the `src` attribute are hidden directly by `css`.+ // Images with `alt` content are not hidden - the display of the `alt` content is also hidden.+ if (attrSrc && !attrAlt) {+ target.classList.add('invisible');}- },- true,-);+ }++ if (/load/i.test(evt.type)) {+ target.classList.remove('broken', 'invisible');+ }+};++document.addEventListener('error', handleImgLoad, true);+document.addEventListener('load', handleImgLoad, true);root.render(<React.StrictMode>