@iobroker/adapter-react-v5 7.0.1 → 7.1.0

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