@iobroker/adapter-react-v5 6.1.9 → 7.0.1

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.
Files changed (210) hide show
  1. package/Components/404.js +13 -13
  2. package/Components/FileBrowser.js +24 -19
  3. package/Components/FileViewer.js +14 -5
  4. package/Components/Loader.js +223 -223
  5. package/Components/Loaders/PT.css +108 -108
  6. package/Components/Loaders/PT.js +103 -103
  7. package/Components/Loaders/Vendor.css +13 -13
  8. package/Components/Loaders/Vendor.js +7 -7
  9. package/Components/ObjectBrowser.d.ts +2 -0
  10. package/Components/ObjectBrowser.js +214 -115
  11. package/Components/UploadImage.js +305 -305
  12. package/Components/loader.css +221 -221
  13. package/Components/types.d.ts +82 -82
  14. package/GenericApp.js +49 -49
  15. package/LICENSE +22 -22
  16. package/Prompt.js +7 -7
  17. package/README.md +1004 -1008
  18. package/Theme.js +8 -7
  19. package/assets/devices/Alarm Systems.svg +18 -18
  20. package/assets/devices/Amplifier.svg +21 -21
  21. package/assets/devices/Awnings.svg +4 -4
  22. package/assets/devices/Battery Status.svg +4 -4
  23. package/assets/devices/Ceiling Spotlights.svg +15 -15
  24. package/assets/devices/Chandelier.svg +6 -6
  25. package/assets/devices/Climate.svg +11 -11
  26. package/assets/devices/Coffee Makers.svg +5 -5
  27. package/assets/devices/Cold Water.svg +31 -31
  28. package/assets/devices/Computer.svg +21 -21
  29. package/assets/devices/Consumption.svg +7 -7
  30. package/assets/devices/Curtains.svg +43 -43
  31. package/assets/devices/Dishwashers.svg +11 -11
  32. package/assets/devices/Doors.svg +5 -5
  33. package/assets/devices/Doorstep.svg +35 -35
  34. package/assets/devices/Dryer.svg +13 -13
  35. package/assets/devices/Fan.svg +20 -20
  36. package/assets/devices/Floor Lamps.svg +4 -4
  37. package/assets/devices/Garage Doors.svg +9 -9
  38. package/assets/devices/Gates.svg +32 -32
  39. package/assets/devices/Hairdryer.svg +23 -23
  40. package/assets/devices/Handle.svg +6 -6
  41. package/assets/devices/Hanging Lamps.svg +8 -8
  42. package/assets/devices/Heater.svg +44 -44
  43. package/assets/devices/Hoods.svg +11 -11
  44. package/assets/devices/Hot Water.svg +9 -9
  45. package/assets/devices/Humidity.svg +41 -41
  46. package/assets/devices/Iron.svg +4 -4
  47. package/assets/devices/Irrigation.svg +22 -22
  48. package/assets/devices/Led Strip.svg +30 -30
  49. package/assets/devices/Light.svg +29 -29
  50. package/assets/devices/Lightings.svg +46 -46
  51. package/assets/devices/Lock.svg +19 -19
  52. package/assets/devices/Louvre.svg +6 -6
  53. package/assets/devices/Mowing Machine.svg +8 -8
  54. package/assets/devices/Music.svg +12 -12
  55. package/assets/devices/Outdoor Blinds.svg +6 -6
  56. package/assets/devices/People.svg +19 -19
  57. package/assets/devices/Pool.svg +7 -7
  58. package/assets/devices/Power Consumption.svg +12 -12
  59. package/assets/devices/Printer.svg +9 -9
  60. package/assets/devices/Pump.svg +9 -9
  61. package/assets/devices/Receiver.svg +18 -18
  62. package/assets/devices/Sconces.svg +9 -9
  63. package/assets/devices/Security.svg +34 -34
  64. package/assets/devices/Shading.svg +4 -4
  65. package/assets/devices/Shutters.svg +10 -10
  66. package/assets/devices/SmokeDetector.svg +12 -12
  67. package/assets/devices/Sockets.svg +13 -13
  68. package/assets/devices/Speaker.svg +35 -35
  69. package/assets/devices/Stove.svg +11 -11
  70. package/assets/devices/Table Lamps.svg +11 -11
  71. package/assets/devices/Temperature Sensors.svg +28 -28
  72. package/assets/devices/Tv.svg +7 -7
  73. package/assets/devices/Vacuum Cleaner.svg +15 -15
  74. package/assets/devices/Ventilation.svg +12 -12
  75. package/assets/devices/Washing Machines.svg +15 -15
  76. package/assets/devices/Water Consumption.svg +5 -5
  77. package/assets/devices/Water Heater.svg +8 -8
  78. package/assets/devices/Water.svg +40 -40
  79. package/assets/devices/Weather.svg +28 -28
  80. package/assets/devices/Window.svg +7 -7
  81. package/assets/lamp_ceiling.svg +8 -8
  82. package/assets/lamp_table.svg +7 -7
  83. package/assets/no_icon.svg +9 -9
  84. package/assets/rooms/Anteroom.svg +52 -52
  85. package/assets/rooms/Attic.svg +21 -21
  86. package/assets/rooms/Balcony.svg +12 -12
  87. package/assets/rooms/Barn.svg +5 -5
  88. package/assets/rooms/Basement.svg +4 -4
  89. package/assets/rooms/Bathroom.svg +38 -38
  90. package/assets/rooms/Bedroom.svg +5 -5
  91. package/assets/rooms/Boiler Room.svg +12 -12
  92. package/assets/rooms/Carport.svg +17 -17
  93. package/assets/rooms/Cellar.svg +89 -89
  94. package/assets/rooms/Chamber.svg +9 -9
  95. package/assets/rooms/Corridor.svg +52 -52
  96. package/assets/rooms/Dining Area.svg +37 -37
  97. package/assets/rooms/Dining Room.svg +37 -37
  98. package/assets/rooms/Dining.svg +37 -37
  99. package/assets/rooms/Dressing Room.svg +4 -4
  100. package/assets/rooms/Driveway.svg +14 -14
  101. package/assets/rooms/Entrance.svg +44 -44
  102. package/assets/rooms/Equipment Room.svg +14 -14
  103. package/assets/rooms/Front Yard.svg +64 -64
  104. package/assets/rooms/Gallery.svg +13 -13
  105. package/assets/rooms/Garage.svg +20 -20
  106. package/assets/rooms/Garden.svg +12 -12
  107. package/assets/rooms/Ground Floor.svg +95 -95
  108. package/assets/rooms/Guest Bathroom.svg +32 -32
  109. package/assets/rooms/Guest Room.svg +5 -5
  110. package/assets/rooms/Gym.svg +4 -4
  111. package/assets/rooms/Hall.svg +19 -19
  112. package/assets/rooms/Home Theater.svg +7 -7
  113. package/assets/rooms/Kitchen.svg +17 -17
  114. package/assets/rooms/Laundry Room.svg +11 -11
  115. package/assets/rooms/Living Area.svg +10 -10
  116. package/assets/rooms/Living Room.svg +10 -10
  117. package/assets/rooms/Locker Room.svg +16 -16
  118. package/assets/rooms/Nursery.svg +4 -4
  119. package/assets/rooms/Office.svg +8 -8
  120. package/assets/rooms/Outdoors.svg +7 -7
  121. package/assets/rooms/Playroom.svg +5 -5
  122. package/assets/rooms/Pool.svg +7 -7
  123. package/assets/rooms/Rear Wall.svg +30 -30
  124. package/assets/rooms/Second Floor.svg +95 -95
  125. package/assets/rooms/Shed.svg +16 -16
  126. package/assets/rooms/Sleeping Area.svg +22 -22
  127. package/assets/rooms/Stairway.svg +4 -4
  128. package/assets/rooms/Stairwell.svg +15 -15
  129. package/assets/rooms/Storeroom.svg +4 -4
  130. package/assets/rooms/Summer House.svg +27 -27
  131. package/assets/rooms/Swimming Pool.svg +21 -21
  132. package/assets/rooms/Terrace.svg +6 -6
  133. package/assets/rooms/Toilet.svg +10 -10
  134. package/assets/rooms/Upstairs.svg +5 -5
  135. package/assets/rooms/Wardrobe.svg +60 -60
  136. package/assets/rooms/Washroom.svg +19 -19
  137. package/assets/rooms/Wc.svg +10 -10
  138. package/assets/rooms/Windscreen.svg +60 -60
  139. package/assets/rooms/Workshop.svg +22 -22
  140. package/assets/rooms/Workspace.svg +8 -8
  141. package/craco-module-federation.js +71 -71
  142. package/icons/IconFx.js +1 -1
  143. package/icons/IconLogout.js +1 -1
  144. package/index.css +54 -54
  145. package/modulefederation.admin.config.js +31 -31
  146. package/package.json +9 -9
  147. package/src/AdminConnection.tsx +3 -3
  148. package/src/Components/404.tsx +121 -121
  149. package/src/Components/ColorPicker.tsx +315 -315
  150. package/src/Components/ComplexCron.tsx +507 -507
  151. package/src/Components/CopyToClipboard.tsx +165 -165
  152. package/src/Components/CustomModal.tsx +163 -163
  153. package/src/Components/FileBrowser.tsx +2414 -2394
  154. package/src/Components/FileViewer.tsx +393 -384
  155. package/src/Components/Icon.tsx +210 -210
  156. package/src/Components/IconPicker.tsx +149 -149
  157. package/src/Components/IconSelector.tsx +2202 -2202
  158. package/src/Components/Image.tsx +176 -176
  159. package/src/Components/Loader.tsx +304 -304
  160. package/src/Components/Logo.tsx +166 -166
  161. package/src/Components/MDUtils.tsx +100 -100
  162. package/src/Components/ObjectBrowser.tsx +8032 -7915
  163. package/src/Components/Router.tsx +90 -90
  164. package/src/Components/SaveCloseButtons.tsx +113 -113
  165. package/src/Components/Schedule.tsx +1724 -1724
  166. package/src/Components/SelectWithIcon.tsx +197 -197
  167. package/src/Components/TabContainer.tsx +55 -55
  168. package/src/Components/TabContent.tsx +37 -37
  169. package/src/Components/TabHeader.tsx +19 -19
  170. package/src/Components/TableResize.tsx +259 -259
  171. package/src/Components/TextWithIcon.tsx +148 -148
  172. package/src/Components/ToggleThemeMenu.tsx +34 -34
  173. package/src/Components/TreeTable.tsx +919 -919
  174. package/src/Components/UploadImage.tsx +599 -599
  175. package/src/Components/Utils.tsx +1794 -1794
  176. package/src/Components/loader.css +221 -221
  177. package/src/Components/withWidth.tsx +21 -21
  178. package/src/Connection.tsx +7 -7
  179. package/src/Dialogs/ComplexCron.tsx +129 -129
  180. package/src/Dialogs/Confirm.tsx +162 -162
  181. package/src/Dialogs/Cron.tsx +182 -182
  182. package/src/Dialogs/Error.tsx +72 -72
  183. package/src/Dialogs/Message.tsx +71 -71
  184. package/src/Dialogs/SelectFile.tsx +270 -270
  185. package/src/Dialogs/SelectID.tsx +298 -298
  186. package/src/Dialogs/SimpleCron.tsx +100 -100
  187. package/src/Dialogs/TextInput.tsx +107 -107
  188. package/src/GenericApp.tsx +976 -976
  189. package/src/LegacyConnection.tsx +3589 -3589
  190. package/src/Prompt.tsx +20 -20
  191. package/src/Theme.tsx +479 -479
  192. package/src/icons/IconAdapter.tsx +20 -20
  193. package/src/icons/IconAlias.tsx +20 -20
  194. package/src/icons/IconChannel.tsx +21 -21
  195. package/src/icons/IconClearFilter.tsx +22 -22
  196. package/src/icons/IconClosed.tsx +17 -17
  197. package/src/icons/IconCopy.tsx +16 -16
  198. package/src/icons/IconDevice.tsx +27 -27
  199. package/src/icons/IconDocument.tsx +17 -17
  200. package/src/icons/IconDocumentReadOnly.tsx +18 -18
  201. package/src/icons/IconExpert.tsx +18 -18
  202. package/src/icons/IconFx.tsx +36 -36
  203. package/src/icons/IconInstance.tsx +20 -20
  204. package/src/icons/IconLogout.tsx +30 -30
  205. package/src/icons/IconNoIcon.tsx +19 -19
  206. package/src/icons/IconOpen.tsx +17 -17
  207. package/src/icons/IconProps.tsx +15 -15
  208. package/src/icons/IconState.tsx +17 -17
  209. package/src/index.css +54 -54
  210. package/types.d.ts +134 -134
@@ -1,384 +1,393 @@
1
- // File viewer in adapter-react does not support write
2
- // import { Buffer } from 'buffer';
3
- import React, { Component } from 'react';
4
-
5
- import {
6
- TextField,
7
- Button,
8
- Dialog,
9
- DialogActions,
10
- DialogContent,
11
- DialogTitle,
12
- IconButton,
13
- } from '@mui/material';
14
-
15
- // Icons
16
- import { FaCopy as CopyIcon } from 'react-icons/fa';
17
- import {
18
- Close as CloseIcon,
19
- Save as SaveIcon,
20
- Brightness6 as Brightness5Icon,
21
- } from '@mui/icons-material';
22
-
23
- import type { Connection } from '@iobroker/socket-client';
24
-
25
- import IconNoIcon from '../icons/IconNoIcon';
26
- import withWidth from './withWidth';
27
- import Utils from './Utils';
28
- import { Translate } from '../types';
29
- import Icon from './Icon';
30
- // File viewer in adapter-react does not use ace editor
31
- // import * as ace from 'ace-builds';
32
- // import 'ace-builds/src-noconflict/ext-modelist';
33
- // import Editor from './Editor';
34
-
35
- // const modelist = ace.require('ace/ext/modelist');
36
-
37
- const styles: Record<string, React.CSSProperties> = {
38
- dialog: {
39
- height: '100%',
40
- },
41
- paper: {
42
- height: 'calc(100% - 64px)',
43
- },
44
- content: {
45
- textAlign: 'center',
46
- },
47
- textarea: {
48
- width: '100%',
49
- height: '100%',
50
- },
51
- img: {
52
- width: 'auto',
53
- height: 'calc(100% - 5px)',
54
- objectFit: 'contain',
55
- },
56
- dialogTitle: {
57
- justifyContent: 'space-between',
58
- display: 'flex',
59
- },
60
- };
61
-
62
- export const EXTENSIONS = {
63
- images: ['png', 'jpg', 'svg', 'jpeg', 'bmp', 'gif', 'apng', 'avif', 'webp'],
64
- code: ['js', 'json', 'json5', 'md'],
65
- txt: ['log', 'txt', 'html', 'css', 'xml', 'ics'],
66
- audio: ['mp3', 'wav', 'ogg', 'acc'],
67
- video: ['mp4', 'mov', 'avi'],
68
- };
69
-
70
- function bufferToBase64(buffer: Buffer, isFull?: boolean) {
71
- let binary = '';
72
- const bytes = new Uint8Array(buffer);
73
- const len = bytes.byteLength;
74
- for (let i = 0; i < len && (isFull || i < 50); i++) {
75
- binary += String.fromCharCode(bytes[i]);
76
- }
77
- return window.btoa(binary);
78
- }
79
-
80
- interface FileViewerProps {
81
- /** Translation function */
82
- t: Translate;
83
- /** Callback when the viewer is closed. */
84
- onClose: () => void;
85
- /** The URL (file path) to the file to be displayed. */
86
- href: string;
87
- // formatEditFile?: string;
88
- socket: Connection;
89
- setStateBackgroundImage: () => void;
90
- // themeType: ThemeType;
91
- getStyleBackgroundImage: () => React.CSSProperties | null;
92
- /** Flag is the js-controller support subscribe on file */
93
- supportSubscribes?: boolean;
94
- }
95
-
96
- interface FileViewerState {
97
- text: string | null;
98
- code: string | null;
99
- ext: string | null;
100
- editing: boolean;
101
- editingValue: string | null;
102
- copyPossible: boolean;
103
- forceUpdate: number;
104
- changed: boolean;
105
- imgError: boolean;
106
- }
107
-
108
- class FileViewer extends Component<FileViewerProps, FileViewerState> {
109
- private timeout: ReturnType<typeof setTimeout> | null = null;
110
-
111
- constructor(props: FileViewerProps) {
112
- super(props);
113
- const ext = Utils.getFileExtension(props.href);
114
-
115
- this.state = {
116
- text: null,
117
- code: null,
118
- ext,
119
- // File viewer in adapter-react does not support write
120
- editing: /* !!this.props.formatEditFile || */ false,
121
- editingValue: null,
122
- copyPossible: !!ext && (EXTENSIONS.code.includes(ext) || EXTENSIONS.txt.includes(ext)),
123
- forceUpdate: Date.now(),
124
- changed: false,
125
- imgError: false,
126
- };
127
- }
128
-
129
- readFile() {
130
- if (this.props.href) {
131
- const parts = this.props.href.split('/');
132
- parts.splice(0, 2);
133
- const adapter = parts[0];
134
- const name = parts.splice(1).join('/');
135
-
136
- this.props.socket.readFile(adapter, name)
137
- .then((data: { data: Buffer; type: string } | { file: string; mimeType: string }) => {
138
- let fileData: string = '';
139
- if ((data as { file: string; mimeType: string }).file !== undefined) {
140
- fileData = (data as { file: string; mimeType: string }).file;
141
- }
142
-
143
- const newState: Partial<FileViewerState> = { copyPossible: this.state.copyPossible, ext: this.state.ext };
144
- // try to detect valid extension
145
- if ((data as { data: Buffer; type: string }).type === 'Buffer') {
146
- if (name.toLowerCase().endsWith('.json5')) {
147
- newState.ext = 'json5';
148
- newState.copyPossible = true;
149
- try {
150
- fileData = atob(bufferToBase64((data as { data: Buffer; type: string }).data, true));
151
- } catch (e) {
152
- console.error('Cannot convert base64 to string');
153
- fileData = '';
154
- }
155
- } else {
156
- const ext = Utils.detectMimeType(bufferToBase64((data as { data: Buffer; type: string }).data));
157
- if (ext) {
158
- newState.ext = ext;
159
- newState.copyPossible = EXTENSIONS.code.includes(ext) || EXTENSIONS.txt.includes(ext);
160
- }
161
- }
162
- }
163
-
164
- if (newState.copyPossible) {
165
- if (newState.ext && EXTENSIONS.txt.includes(newState.ext)) {
166
- newState.text = fileData;
167
- newState.editingValue = fileData;
168
- } else if (newState.ext && EXTENSIONS.code.includes(newState.ext)) {
169
- newState.code = fileData;
170
- newState.editingValue = fileData;
171
- }
172
- }
173
-
174
- this.setState(newState as FileViewerState);
175
- })
176
- .catch(e => window.alert(`Cannot read file: ${e}`));
177
- }
178
- }
179
-
180
- componentDidMount() {
181
- this.readFile();
182
-
183
- const parts = this.props.href.split('/');
184
- parts.splice(0, 2);
185
- const adapter = parts[0];
186
- const name = parts.splice(1).join('/');
187
-
188
- this.props.supportSubscribes && this.props.socket.subscribeFiles(adapter, name, this.onFileChanged);
189
- }
190
-
191
- componentWillUnmount() {
192
- this.timeout && clearTimeout(this.timeout);
193
- const parts = this.props.href.split('/');
194
- parts.splice(0, 2);
195
- const adapter = parts[0];
196
- const name = parts.splice(1).join('/');
197
- this.props.supportSubscribes && this.props.socket.subscribeFiles(adapter, name, this.onFileChanged);
198
- }
199
-
200
- onFileChanged = (id: string, fileName: string, size: number | null) => {
201
- if (!this.state.changed) {
202
- this.timeout && clearTimeout(this.timeout);
203
- this.timeout = setTimeout(() => {
204
- this.timeout = null;
205
- if (size === null) {
206
- window.alert('Show file was deleted!');
207
- } else if (this.state.text !== null || this.state.code !== null) {
208
- this.readFile();
209
- } else {
210
- this.setState({ forceUpdate: Date.now() });
211
- }
212
- }, 300);
213
- }
214
- };
215
-
216
- // eslint-disable-next-line class-methods-use-this
217
- writeFile64 = () => {
218
- /*
219
- // File viewer in adapter-react does not support write
220
- const parts = this.props.href.split('/');
221
- const data = this.state.editingValue;
222
- parts.splice(0, 2);
223
- const adapter = parts[0];
224
- const name = parts.splice(1).join('/');
225
- this.props.socket.writeFile64(adapter, name, Buffer.from(data).toString('base64'))
226
- .then(() => this.props.onClose())
227
- .catch(e => window.alert(`Cannot write file: ${e}`));
228
- */
229
- };
230
-
231
- static getEditFile(ext: string | null) {
232
- switch (ext) {
233
- case 'json':
234
- return 'json';
235
- case 'json5':
236
- return 'json5';
237
- case 'js':
238
- return 'javascript';
239
- case 'html':
240
- return 'html';
241
- case 'txt':
242
- return 'html';
243
- default:
244
- // e.g. ace/mode/text
245
- return 'text';
246
- }
247
- }
248
-
249
- getContent() {
250
- if (this.state.ext && EXTENSIONS.images.includes(this.state.ext)) {
251
- if (this.state.imgError) {
252
- return <IconNoIcon style={{ ...styles.img, ...this.props.getStyleBackgroundImage() }} />;
253
- }
254
- return <Icon
255
- onError={e => {
256
- (e.target as HTMLImageElement).onerror = null;
257
- this.setState({ imgError: true });
258
- }}
259
- style={{ ...styles.img, ...this.props.getStyleBackgroundImage() }}
260
- src={`${this.props.href}?ts=${this.state.forceUpdate}`}
261
- alt={this.props.href}
262
- />;
263
- }
264
- if (this.state.ext && EXTENSIONS.audio.includes(this.state.ext)) {
265
- return <div
266
- style={{
267
- width: '100%',
268
- height: '100%',
269
- display: 'flex',
270
- justifyContent: 'center',
271
- alignItems: 'center',
272
- }}
273
- >
274
- {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
275
- <audio
276
- style={{ width: '100%' }}
277
- src={this.props.href}
278
- controls
279
- ></audio>
280
- </div>;
281
- }
282
- if (this.state.ext && EXTENSIONS.video.includes(this.state.ext)) {
283
- return (
284
- <div
285
- style={{
286
- width: '100%',
287
- height: '100%',
288
- display: 'flex',
289
- justifyContent: 'center',
290
- alignItems: 'center',
291
- }}
292
- >
293
- {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
294
- <video style={{ width: '100%', height: '100%' }} controls>
295
- <source src={this.props.href} type={`video/${this.state.ext}}`} />
296
- </video>
297
- </div>
298
- );
299
- }
300
- if (this.state.code !== null || this.state.text !== null || this.state.editing) {
301
- // File viewer in adapter-react does not support write
302
- // return <Editor
303
- // mode={FileViewer.getEditFile(this.props.formatEditFile)}
304
- // themeType={this.props.themeType}
305
- // value={this.state.editingValue || this.state.code || this.state.text}
306
- // onChange={this.state.editing ? newValue => this.setState({ editingValue: newValue, changed: true }) : undefined}
307
- // />;
308
- return <TextField
309
- variant="standard"
310
- style={styles.textarea}
311
- multiline
312
- value={this.state.editingValue || this.state.code || this.state.text}
313
- // onChange={newValue => this.setState({ editingValue: newValue, changed: true })}
314
- InputProps={{ readOnly: !this.state.editing }}
315
- />;
316
- }
317
- return null;
318
- }
319
-
320
- render() {
321
- return <Dialog
322
- sx={{
323
- '&.MuiDialog-scrollPaper': styles.dialog,
324
- '& .MuiDialog-paper': styles.paper,
325
- }}
326
- scroll="paper"
327
- open={!!this.props.href}
328
- onClose={() => this.props.onClose()}
329
- fullWidth
330
- maxWidth="xl"
331
- aria-labelledby="ar_dialog_file_view_title"
332
- >
333
- <div style={styles.dialogTitle}>
334
- <DialogTitle id="ar_dialog_file_view_title">{`${this.props.t(this.state.editing ? 'Edit' : 'View')}: ${this.props.href}`}</DialogTitle>
335
- {this.state.ext && EXTENSIONS.images.includes(this.state.ext) && <div>
336
- <IconButton
337
- size="large"
338
- color="inherit"
339
- onClick={this.props.setStateBackgroundImage}
340
- >
341
- <Brightness5Icon />
342
- </IconButton>
343
- </div>}
344
- </div>
345
- <DialogContent style={styles.content}>
346
- {this.getContent()}
347
- </DialogContent>
348
- <DialogActions>
349
- {this.state.copyPossible ?
350
- <Button
351
- color="grey"
352
- onClick={e => {
353
- e.stopPropagation();
354
- e.preventDefault();
355
- Utils.copyToClipboard(this.state.text || this.state.code || '');
356
- }}
357
- startIcon={<CopyIcon />}
358
- >
359
- {this.props.t('Copy content')}
360
- </Button> : null}
361
- {this.state.editing ?
362
- <Button
363
- color="grey"
364
- disabled={this.state.editingValue === this.state.code || this.state.editingValue === this.state.text}
365
- variant="contained"
366
- onClick={this.writeFile64}
367
- startIcon={<SaveIcon />}
368
- >
369
- {this.props.t('Save')}
370
- </Button> : null}
371
- <Button
372
- variant="contained"
373
- onClick={() => this.props.onClose()}
374
- color="primary"
375
- startIcon={<CloseIcon />}
376
- >
377
- {this.props.t('Close')}
378
- </Button>
379
- </DialogActions>
380
- </Dialog>;
381
- }
382
- }
383
-
384
- export default withWidth()(FileViewer);
1
+ // File viewer in adapter-react does not support write
2
+ // import { Buffer } from 'buffer';
3
+ import React, { Component } from 'react';
4
+
5
+ import {
6
+ TextField,
7
+ Button,
8
+ Dialog,
9
+ DialogActions,
10
+ DialogContent,
11
+ DialogTitle,
12
+ IconButton,
13
+ } from '@mui/material';
14
+
15
+ // Icons
16
+ import { FaCopy as CopyIcon } from 'react-icons/fa';
17
+ import {
18
+ Close as CloseIcon,
19
+ Save as SaveIcon,
20
+ Brightness6 as Brightness5Icon,
21
+ } from '@mui/icons-material';
22
+
23
+ import type { Connection } from '@iobroker/socket-client';
24
+
25
+ import IconNoIcon from '../icons/IconNoIcon';
26
+ import withWidth from './withWidth';
27
+ import Utils from './Utils';
28
+ import { Translate } from '../types';
29
+ import Icon from './Icon';
30
+ // File viewer in adapter-react does not use ace editor
31
+ // import * as ace from 'ace-builds';
32
+ // import 'ace-builds/src-noconflict/ext-modelist';
33
+ // import Editor from './Editor';
34
+
35
+ // const modelist = ace.require('ace/ext/modelist');
36
+
37
+ const styles: Record<string, React.CSSProperties> = {
38
+ dialog: {
39
+ height: '100%',
40
+ },
41
+ paper: {
42
+ height: 'calc(100% - 64px)',
43
+ },
44
+ content: {
45
+ textAlign: 'center',
46
+ },
47
+ textarea: {
48
+ width: '100%',
49
+ height: '100%',
50
+ },
51
+ img: {
52
+ width: 'auto',
53
+ height: 'calc(100% - 5px)',
54
+ objectFit: 'contain',
55
+ },
56
+ dialogTitle: {
57
+ justifyContent: 'space-between',
58
+ display: 'flex',
59
+ },
60
+ };
61
+
62
+ export const EXTENSIONS = {
63
+ images: ['png', 'jpg', 'svg', 'jpeg', 'bmp', 'gif', 'apng', 'avif', 'webp'],
64
+ code: ['js', 'json', 'json5', 'md'],
65
+ txt: ['log', 'txt', 'html', 'css', 'xml', 'ics'],
66
+ audio: ['mp3', 'wav', 'ogg', 'acc'],
67
+ video: ['mp4', 'mov', 'avi'],
68
+ };
69
+
70
+ function bufferToBase64(buffer: Buffer, isFull?: boolean) {
71
+ let binary = '';
72
+ const bytes = new Uint8Array(buffer);
73
+ const len = bytes.byteLength;
74
+ for (let i = 0; i < len && (isFull || i < 50); i++) {
75
+ binary += String.fromCharCode(bytes[i]);
76
+ }
77
+ return window.btoa(binary);
78
+ }
79
+
80
+ interface FileViewerProps {
81
+ /** Translation function */
82
+ t: Translate;
83
+ /** Callback when the viewer is closed. */
84
+ onClose: () => void;
85
+ /** The URL (file path) to the file to be displayed. */
86
+ href: string;
87
+ // formatEditFile?: string;
88
+ socket: Connection;
89
+ setStateBackgroundImage: () => void;
90
+ // themeType: ThemeType;
91
+ getStyleBackgroundImage: () => React.CSSProperties | null;
92
+ /** Flag is the js-controller support subscribe on file */
93
+ supportSubscribes?: boolean;
94
+ }
95
+
96
+ interface FileViewerState {
97
+ text: string | null;
98
+ code: string | null;
99
+ ext: string | null;
100
+ editing: boolean;
101
+ editingValue: string | null;
102
+ copyPossible: boolean;
103
+ forceUpdate: number;
104
+ changed: boolean;
105
+ imgError: boolean;
106
+ }
107
+
108
+ class FileViewer extends Component<FileViewerProps, FileViewerState> {
109
+ private timeout: ReturnType<typeof setTimeout> | null = null;
110
+
111
+ constructor(props: FileViewerProps) {
112
+ super(props);
113
+ const ext = Utils.getFileExtension(props.href);
114
+
115
+ this.state = {
116
+ text: null,
117
+ code: null,
118
+ ext,
119
+ // File viewer in adapter-react does not support write
120
+ editing: /* !!this.props.formatEditFile || */ false,
121
+ editingValue: null,
122
+ copyPossible: !!ext && (EXTENSIONS.code.includes(ext) || EXTENSIONS.txt.includes(ext)),
123
+ forceUpdate: Date.now(),
124
+ changed: false,
125
+ imgError: false,
126
+ };
127
+ }
128
+
129
+ readFile() {
130
+ if (this.props.href) {
131
+ const parts = this.props.href.split('/');
132
+ parts.splice(0, 2);
133
+ const adapter = parts[0];
134
+ const name = parts.splice(1).join('/');
135
+
136
+ this.props.socket.readFile(adapter, name)
137
+ .then((data: { data: Buffer; type: string } | { file: string; mimeType: string }) => {
138
+ let fileData: string = '';
139
+ if ((data as { file: string; mimeType: string }).file !== undefined) {
140
+ fileData = (data as { file: string; mimeType: string }).file;
141
+ }
142
+
143
+ const newState: Partial<FileViewerState> = { copyPossible: this.state.copyPossible, ext: this.state.ext };
144
+ // try to detect valid extension
145
+ if ((data as { data: Buffer; type: string }).type === 'Buffer') {
146
+ if (name.toLowerCase().endsWith('.json5')) {
147
+ newState.ext = 'json5';
148
+ newState.copyPossible = true;
149
+ try {
150
+ fileData = atob(bufferToBase64((data as { data: Buffer; type: string }).data, true));
151
+ } catch {
152
+ console.error('Cannot convert base64 to string');
153
+ fileData = '';
154
+ }
155
+ } else {
156
+ const ext = Utils.detectMimeType(bufferToBase64((data as { data: Buffer; type: string }).data));
157
+ if (ext) {
158
+ newState.ext = ext;
159
+ newState.copyPossible = EXTENSIONS.code.includes(ext) || EXTENSIONS.txt.includes(ext);
160
+ }
161
+ }
162
+ }
163
+
164
+ if (newState.copyPossible) {
165
+ if (newState.ext && EXTENSIONS.txt.includes(newState.ext)) {
166
+ newState.text = fileData;
167
+ newState.editingValue = fileData;
168
+ } else if (newState.ext && EXTENSIONS.code.includes(newState.ext)) {
169
+ newState.code = fileData;
170
+ newState.editingValue = fileData;
171
+ }
172
+ }
173
+
174
+ this.setState(newState as FileViewerState);
175
+ })
176
+ .catch(e => window.alert(`Cannot read file: ${e}`));
177
+ }
178
+ }
179
+
180
+ componentDidMount() {
181
+ this.readFile();
182
+
183
+ const parts = this.props.href.split('/');
184
+ parts.splice(0, 2);
185
+ const adapter = parts[0];
186
+ const name = parts.splice(1).join('/');
187
+
188
+ if (this.props.supportSubscribes) {
189
+ this.props.socket.subscribeFiles(adapter, name, this.onFileChanged);
190
+ }
191
+ }
192
+
193
+ componentWillUnmount() {
194
+ if (this.timeout) {
195
+ clearTimeout(this.timeout);
196
+ this.timeout = null;
197
+ }
198
+ const parts = this.props.href.split('/');
199
+ parts.splice(0, 2);
200
+ const adapter = parts[0];
201
+ const name = parts.splice(1).join('/');
202
+ if (this.props.supportSubscribes) {
203
+ this.props.socket.subscribeFiles(adapter, name, this.onFileChanged);
204
+ }
205
+ }
206
+
207
+ onFileChanged = (id: string, fileName: string, size: number | null) => {
208
+ if (!this.state.changed) {
209
+ if (this.timeout) {
210
+ clearTimeout(this.timeout);
211
+ }
212
+ this.timeout = setTimeout(() => {
213
+ this.timeout = null;
214
+ if (size === null) {
215
+ window.alert('Show file was deleted!');
216
+ } else if (this.state.text !== null || this.state.code !== null) {
217
+ this.readFile();
218
+ } else {
219
+ this.setState({ forceUpdate: Date.now() });
220
+ }
221
+ }, 300);
222
+ }
223
+ };
224
+
225
+ // eslint-disable-next-line class-methods-use-this
226
+ writeFile64 = () => {
227
+ /*
228
+ // File viewer in adapter-react does not support write
229
+ const parts = this.props.href.split('/');
230
+ const data = this.state.editingValue;
231
+ parts.splice(0, 2);
232
+ const adapter = parts[0];
233
+ const name = parts.splice(1).join('/');
234
+ this.props.socket.writeFile64(adapter, name, Buffer.from(data).toString('base64'))
235
+ .then(() => this.props.onClose())
236
+ .catch(e => window.alert(`Cannot write file: ${e}`));
237
+ */
238
+ };
239
+
240
+ static getEditFile(ext: string | null) {
241
+ switch (ext) {
242
+ case 'json':
243
+ return 'json';
244
+ case 'json5':
245
+ return 'json5';
246
+ case 'js':
247
+ return 'javascript';
248
+ case 'html':
249
+ return 'html';
250
+ case 'txt':
251
+ return 'html';
252
+ default:
253
+ // e.g. ace/mode/text
254
+ return 'text';
255
+ }
256
+ }
257
+
258
+ getContent() {
259
+ if (this.state.ext && EXTENSIONS.images.includes(this.state.ext)) {
260
+ if (this.state.imgError) {
261
+ return <IconNoIcon style={{ ...styles.img, ...this.props.getStyleBackgroundImage() }} />;
262
+ }
263
+ return <Icon
264
+ onError={e => {
265
+ (e.target as HTMLImageElement).onerror = null;
266
+ this.setState({ imgError: true });
267
+ }}
268
+ style={{ ...styles.img, ...this.props.getStyleBackgroundImage() }}
269
+ src={`${this.props.href}?ts=${this.state.forceUpdate}`}
270
+ alt={this.props.href}
271
+ />;
272
+ }
273
+ if (this.state.ext && EXTENSIONS.audio.includes(this.state.ext)) {
274
+ return <div
275
+ style={{
276
+ width: '100%',
277
+ height: '100%',
278
+ display: 'flex',
279
+ justifyContent: 'center',
280
+ alignItems: 'center',
281
+ }}
282
+ >
283
+ {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
284
+ <audio
285
+ style={{ width: '100%' }}
286
+ src={this.props.href}
287
+ controls
288
+ ></audio>
289
+ </div>;
290
+ }
291
+ if (this.state.ext && EXTENSIONS.video.includes(this.state.ext)) {
292
+ return (
293
+ <div
294
+ style={{
295
+ width: '100%',
296
+ height: '100%',
297
+ display: 'flex',
298
+ justifyContent: 'center',
299
+ alignItems: 'center',
300
+ }}
301
+ >
302
+ {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
303
+ <video style={{ width: '100%', height: '100%' }} controls>
304
+ <source src={this.props.href} type={`video/${this.state.ext}}`} />
305
+ </video>
306
+ </div>
307
+ );
308
+ }
309
+ if (this.state.code !== null || this.state.text !== null || this.state.editing) {
310
+ // File viewer in adapter-react does not support write
311
+ // return <Editor
312
+ // mode={FileViewer.getEditFile(this.props.formatEditFile)}
313
+ // themeType={this.props.themeType}
314
+ // value={this.state.editingValue || this.state.code || this.state.text}
315
+ // onChange={this.state.editing ? newValue => this.setState({ editingValue: newValue, changed: true }) : undefined}
316
+ // />;
317
+ return <TextField
318
+ variant="standard"
319
+ style={styles.textarea}
320
+ multiline
321
+ value={this.state.editingValue || this.state.code || this.state.text}
322
+ // onChange={newValue => this.setState({ editingValue: newValue, changed: true })}
323
+ InputProps={{ readOnly: !this.state.editing }}
324
+ />;
325
+ }
326
+ return null;
327
+ }
328
+
329
+ render() {
330
+ return <Dialog
331
+ sx={{
332
+ '&.MuiDialog-scrollPaper': styles.dialog,
333
+ '& .MuiDialog-paper': styles.paper,
334
+ }}
335
+ scroll="paper"
336
+ open={!!this.props.href}
337
+ onClose={() => this.props.onClose()}
338
+ fullWidth
339
+ maxWidth="xl"
340
+ aria-labelledby="ar_dialog_file_view_title"
341
+ >
342
+ <div style={styles.dialogTitle}>
343
+ <DialogTitle id="ar_dialog_file_view_title">{`${this.props.t(this.state.editing ? 'Edit' : 'View')}: ${this.props.href}`}</DialogTitle>
344
+ {this.state.ext && EXTENSIONS.images.includes(this.state.ext) && <div>
345
+ <IconButton
346
+ size="large"
347
+ color="inherit"
348
+ onClick={this.props.setStateBackgroundImage}
349
+ >
350
+ <Brightness5Icon />
351
+ </IconButton>
352
+ </div>}
353
+ </div>
354
+ <DialogContent style={styles.content}>
355
+ {this.getContent()}
356
+ </DialogContent>
357
+ <DialogActions>
358
+ {this.state.copyPossible ?
359
+ <Button
360
+ color="grey"
361
+ onClick={e => {
362
+ e.stopPropagation();
363
+ e.preventDefault();
364
+ Utils.copyToClipboard(this.state.text || this.state.code || '');
365
+ }}
366
+ startIcon={<CopyIcon />}
367
+ >
368
+ {this.props.t('Copy content')}
369
+ </Button> : null}
370
+ {this.state.editing ?
371
+ <Button
372
+ color="grey"
373
+ disabled={this.state.editingValue === this.state.code || this.state.editingValue === this.state.text}
374
+ variant="contained"
375
+ onClick={this.writeFile64}
376
+ startIcon={<SaveIcon />}
377
+ >
378
+ {this.props.t('Save')}
379
+ </Button> : null}
380
+ <Button
381
+ variant="contained"
382
+ onClick={() => this.props.onClose()}
383
+ color="primary"
384
+ startIcon={<CloseIcon />}
385
+ >
386
+ {this.props.t('Close')}
387
+ </Button>
388
+ </DialogActions>
389
+ </Dialog>;
390
+ }
391
+ }
392
+
393
+ export default withWidth()(FileViewer);