@docusaurus/plugin-ideal-image 2.0.0-beta.15d451942 → 2.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React from 'react';
9
+ import ReactIdealImage, {
10
+ type IconKey,
11
+ type State,
12
+ } from '@endiliey/react-ideal-image';
13
+ import {translate} from '@docusaurus/Translate';
14
+
15
+ import type {Props} from '@theme/IdealImage';
16
+
17
+ // Adopted from https://github.com/endiliey/react-ideal-image/blob/master/src/components/helpers.js#L59-L65
18
+ const bytesToSize = (bytes: number) => {
19
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
20
+ if (bytes === 0) {
21
+ return 'n/a';
22
+ }
23
+ const scale = Math.floor(Math.log(bytes) / Math.log(1024));
24
+ if (scale === 0) {
25
+ return `${bytes} ${sizes[scale]}`;
26
+ }
27
+ return `${(bytes / 1024 ** scale).toFixed(1)} ${sizes[scale]}`;
28
+ };
29
+
30
+ // Adopted from https://github.com/endiliey/react-ideal-image/blob/master/src/components/IdealImage/index.js#L43-L75
31
+ const getMessage = (icon: IconKey, state: State) => {
32
+ switch (icon) {
33
+ case 'noicon':
34
+ case 'loaded':
35
+ return null;
36
+ case 'loading':
37
+ return translate({
38
+ id: 'theme.IdealImageMessage.loading',
39
+ message: 'Loading...',
40
+ description: 'When the full-scale image is loading',
41
+ });
42
+ case 'load': {
43
+ // we can show `alt` here
44
+ const {pickedSrc} = state;
45
+ const {size} = pickedSrc;
46
+ const sizeMessage = size ? ` (${bytesToSize(size)})` : '';
47
+ return translate(
48
+ {
49
+ id: 'theme.IdealImageMessage.load',
50
+ message: 'Click to load{sizeMessage}',
51
+ description:
52
+ 'To prompt users to load the full image. sizeMessage is a parenthesized size figure.',
53
+ },
54
+ {sizeMessage},
55
+ );
56
+ }
57
+ case 'offline':
58
+ return translate({
59
+ id: 'theme.IdealImageMessage.offline',
60
+ message: 'Your browser is offline. Image not loaded',
61
+ description: 'When the user is viewing an offline document',
62
+ });
63
+ case 'error': {
64
+ const {loadInfo} = state;
65
+ if (loadInfo === 404) {
66
+ return translate({
67
+ id: 'theme.IdealImageMessage.404error',
68
+ message: '404. Image not found',
69
+ description: 'When the image is not found',
70
+ });
71
+ }
72
+ return translate({
73
+ id: 'theme.IdealImageMessage.error',
74
+ message: 'Error. Click to reload',
75
+ description: 'When the image fails to load for unknown error',
76
+ });
77
+ }
78
+ default:
79
+ throw new Error(`Wrong icon: ${icon}`);
80
+ }
81
+ };
82
+
83
+ export default function IdealImage(props: Props): JSX.Element {
84
+ const {alt, className, img} = props;
85
+
86
+ // In dev env just use regular img with original file
87
+ if (typeof img === 'string' || 'default' in img) {
88
+ return (
89
+ <img
90
+ src={typeof img === 'string' ? img : img.default}
91
+ className={className}
92
+ alt={alt}
93
+ {...props}
94
+ />
95
+ );
96
+ }
97
+
98
+ return (
99
+ <ReactIdealImage
100
+ {...props}
101
+ alt={alt}
102
+ className={className}
103
+ height={img.src.height || 100}
104
+ width={img.src.width || 100}
105
+ placeholder={{lqip: img.preSrc}}
106
+ src={img.src.src}
107
+ srcSet={img.src.images.map((image) => ({
108
+ ...image,
109
+ src: image.path,
110
+ }))}
111
+ getMessage={getMessage}
112
+ />
113
+ );
114
+ }