Skip to content

Commit fcee060

Browse files
authored
refactor(ideal-image-plugin): internalize legacy component code (#11010)
* almost working * refactor: apply lint autofix * cspell ignore * refactor: apply lint autofix * type fixes --------- Co-authored-by: slorber <[email protected]>
1 parent 502b900 commit fcee060

33 files changed

+1717
-133
lines changed

.cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"CHANGELOG.md",
2424
"patches",
2525
"packages/docusaurus-theme-translations/locales",
26+
"packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy",
2627
"package.json",
2728
"yarn.lock",
2829
"project-words.txt",

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ packages/create-docusaurus/templates/facebook
2222

2323
website/_dogfooding/_swizzle_theme_tests
2424
website/_dogfooding/_asset-tests/badSyntax.js
25+
26+
27+
packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy

packages/docusaurus-plugin-ideal-image/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@docusaurus/theme-translations": "3.7.0",
2727
"@docusaurus/types": "3.7.0",
2828
"@docusaurus/utils-validation": "3.7.0",
29-
"@slorber/react-ideal-image": "^0.0.14",
3029
"react-waypoint": "^10.3.0",
3130
"sharp": "^0.32.3",
3231
"tslib": "^2.6.0",

packages/docusaurus-plugin-ideal-image/src/deps.d.ts

-124
This file was deleted.

packages/docusaurus-plugin-ideal-image/src/plugin-ideal-image.d.ts

+121
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
/// <reference types="@docusaurus/module-type-aliases" />
9+
810
declare module '@docusaurus/plugin-ideal-image' {
911
export type PluginOptions = {
1012
/**
@@ -74,3 +76,122 @@ declare module '@theme/IdealImage' {
7476
}
7577
export default function IdealImage(props: Props): ReactNode;
7678
}
79+
80+
declare module '@theme/IdealImageLegacy' {
81+
/**
82+
* @see https://github.com/endiliey/react-ideal-image/blob/master/index.d.ts
83+
* Note: the original type definition is WRONG. getIcon & getMessage receive
84+
* full state object.
85+
*/
86+
87+
import type {
88+
ComponentProps,
89+
ComponentType,
90+
CSSProperties,
91+
ReactNode,
92+
} from 'react';
93+
94+
export type LoadingState = 'initial' | 'loading' | 'loaded' | 'error';
95+
96+
export type State = {
97+
pickedSrc: {
98+
size: number;
99+
};
100+
loadInfo: 404 | null;
101+
loadState: LoadingState;
102+
};
103+
104+
export type IconKey =
105+
| 'load'
106+
| 'loading'
107+
| 'loaded'
108+
| 'error'
109+
| 'noicon'
110+
| 'offline';
111+
112+
export type SrcType = {
113+
width: number;
114+
src?: string;
115+
size?: number;
116+
format?: 'webp' | 'jpeg' | 'png' | 'gif';
117+
};
118+
119+
type ThemeKey = 'placeholder' | 'img' | 'icon' | 'noscript';
120+
121+
export interface ImageProps
122+
extends Omit<ComponentProps<'img'>, 'srcSet' | 'placeholder'> {
123+
/**
124+
* This function decides what icon to show based on the current state of the
125+
* component.
126+
*/
127+
getIcon?: (state: State) => IconKey;
128+
/**
129+
* This function decides what message to show based on the icon (returned
130+
* from `getIcon` prop) and the current state of the component.
131+
*/
132+
getMessage?: (icon: IconKey, state: State) => string | null;
133+
/**
134+
* This function is called as soon as the component enters the viewport and
135+
* is used to generate urls based on width and format if `props.srcSet`
136+
* doesn't provide `src` field.
137+
*/
138+
getUrl?: (srcType: SrcType) => string;
139+
/**
140+
* The Height of the image in px.
141+
*/
142+
height: number;
143+
/**
144+
* This provides a map of the icons. By default, the component uses icons
145+
* from material design, Implemented as React components with the SVG
146+
* element. You can customize icons
147+
*/
148+
icons?: Partial<{[icon in IconKey]: ComponentType}>;
149+
/**
150+
* This prop takes one of the 2 options, xhr and image.
151+
* Read more about it:
152+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#cancel-download
153+
*/
154+
loader?: 'xhr' | 'image';
155+
/**
156+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#lqip
157+
*/
158+
placeholder: {color: string} | {lqip: string};
159+
/**
160+
* This function decides if image should be downloaded automatically. The
161+
* default function returns false for a 2g network, for a 3g network it
162+
* decides based on `props.threshold` and for a 4g network it returns true
163+
* by default.
164+
*/
165+
shouldAutoDownload?: (options: {
166+
connection?: 'slow-2g' | '2g' | '3g' | '4g';
167+
size?: number;
168+
threshold?: number;
169+
possiblySlowNetwork?: boolean;
170+
}) => boolean;
171+
/**
172+
* This provides an array of sources of different format and size of the
173+
* image. Read more about it:
174+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#srcset
175+
*/
176+
srcSet: SrcType[];
177+
/**
178+
* This provides a theme to the component. By default, the component uses
179+
* inline styles, but it is also possible to use CSS modules and override
180+
* all styles.
181+
*/
182+
theme?: Partial<{[key in ThemeKey]: string | CSSProperties}>;
183+
/**
184+
* Tells how much to wait in milliseconds until consider the download to be
185+
* slow.
186+
*/
187+
threshold?: number;
188+
/**
189+
* Width of the image in px.
190+
*/
191+
width: number;
192+
}
193+
194+
export interface Props extends ImageProps {}
195+
196+
export default function IdealImageLegacy(props: ImageProps): ReactNode;
197+
}

packages/docusaurus-plugin-ideal-image/src/theme/IdealImage/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77

88
import React, {type ReactNode} from 'react';
9+
import {translate} from '@docusaurus/Translate';
910
import ReactIdealImage, {
1011
type IconKey,
1112
type State,
12-
} from '@slorber/react-ideal-image';
13-
import {translate} from '@docusaurus/Translate';
13+
} from '@theme/IdealImageLegacy';
1414

1515
import type {Props} from '@theme/IdealImage';
1616

@@ -93,7 +93,6 @@ export default function IdealImage(props: Props): ReactNode {
9393

9494
return (
9595
<ReactIdealImage
96-
{...propsRest}
9796
height={img.src.height ?? 100}
9897
width={img.src.width ?? 100}
9998
placeholder={{lqip: img.preSrc}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2017 stereobooster
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Legacy React IdealImage lib
2+
3+
This is legacy code from an npm package we forked, then internalized
4+
5+
See also:
6+
7+
- https://github.com/slorber/docusaurus-react-ideal-image
8+
- https://github.com/endiliey/react-ideal-image
9+
- https://github.com/stereobooster/react-ideal-image
10+
11+
---
12+
13+
TODO: we need to clean it up, remove what we don't need, and maintain it up to date

0 commit comments

Comments
 (0)