Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double file browser #2793

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/adapter-react-v5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If you want to create the configuration page with ReactJS:
- Change `name` from `src` to `ADAPTERNAME-admin` (Of course replace `ADAPTERNAME` with yours)
- Add to devDependencies:
```json
"@iobroker/adapter-react-v5": "^7.2.5",
"@iobroker/adapter-react-v5": "^7.2.6",
```
Versions can be higher.
So your `src/package.json` should look like:
Expand All @@ -24,7 +24,7 @@ If you want to create the configuration page with ReactJS:
"version": "0.1.0",
"private": true,
"dependencies": {
"@iobroker/adapter-react-v5": "^7.2.5",
"@iobroker/adapter-react-v5": "^7.2.6",
"@iobroker/build-tools": "^1.0.0",
"@iobroker/eslint-config": "^0.1.2",
"@mui/material": "^6.0.2",
Expand Down
16 changes: 9 additions & 7 deletions packages/adapter-react-v5/src/Components/FileBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { Icon } from './Icon';
import { withWidth } from './withWidth';
import type { ThemeName, ThemeType, Translate, IobTheme } from '../types';

import { FileViewer, EXTENSIONS } from './FileViewer';
import { FileViewer, EXTENSIONS, type FileViewerProps } from './FileViewer';

const ROW_HEIGHT = 32;
const BUTTON_WIDTH = 32;
Expand Down Expand Up @@ -455,7 +455,7 @@ export interface FileBrowserProps {
lang: ioBroker.Languages;
/** The socket connection. */
socket: Connection;
/** Is the component data ready. */
/** Shows if the component data ready. */
ready?: boolean;
/** Is expert mode enabled? (default: false) */
expertMode?: boolean;
Expand Down Expand Up @@ -506,6 +506,8 @@ export interface FileBrowserProps {
allowNonRestricted?: boolean;

showTypeSelector?: boolean;

FileViewer?: React.FC<FileViewerProps>;
}

export interface FolderOrFileItem {
Expand Down Expand Up @@ -856,7 +858,7 @@ export class FileBrowserClass extends Component<FileBrowserProps, FileBrowserSta

this.browseList[0].processing = true;
this.props.socket
.readDir(this.browseList[0].adapter, this.browseList[0].relPath as string)
.readDir(this.browseList[0].adapter, this.browseList[0].relPath)
.then(files => {
if (this.browseList) {
// if component still mounted
Expand Down Expand Up @@ -2294,7 +2296,7 @@ export class FileBrowserClass extends Component<FileBrowserProps, FileBrowserSta
aria-labelledby="ar_dialog_file_delete_title"
>
<DialogTitle id="ar_dialog_file_delete_title">
{this.props.t('ra_Confirm deletion of %s', this.state.deleteItem.split('/').pop() as string)}
{this.props.t('ra_Confirm deletion of %s', this.state.deleteItem.split('/').pop())}
</DialogTitle>
<DialogContent>
<DialogContentText>{this.props.t('ra_Are you sure?')}</DialogContentText>
Expand Down Expand Up @@ -2333,8 +2335,10 @@ export class FileBrowserClass extends Component<FileBrowserProps, FileBrowserSta
}

renderViewDialog(): JSX.Element | null {
const FileViewerComponent = this.props.FileViewer || FileViewer;

return this.state.viewer ? (
<FileViewer
<FileViewerComponent
supportSubscribes={this.supportSubscribes}
key={this.state.viewer}
href={this.state.viewer}
Expand All @@ -2344,8 +2348,6 @@ export class FileBrowserClass extends Component<FileBrowserProps, FileBrowserSta
getStyleBackgroundImage={this.getStyleBackgroundImage}
t={this.props.t}
socket={this.props.socket}
lang={this.props.lang}
expertMode={this.state.expertMode}
onClose={() => this.setState({ viewer: '', formatEditFile: '' })}
/>
) : null;
Expand Down
86 changes: 28 additions & 58 deletions packages/adapter-react-v5/src/Components/FileViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ import type { Connection } from '@iobroker/socket-client';
import { IconNoIcon } from '../icons/IconNoIcon';
import { withWidth } from './withWidth';
import { Utils } from './Utils';
import type { Translate } from '../types';
import type { ThemeType, Translate } from '../types';
import { Icon } from './Icon';
// File viewer in adapter-react does not use ace editor
// import * as ace from 'ace-builds';
// import 'ace-builds/src-noconflict/ext-modelist';
// import Editor from './Editor';

// const modelist = ace.require('ace/ext/modelist');

const styles: Record<string, React.CSSProperties> = {
dialog: {
Expand Down Expand Up @@ -65,23 +59,23 @@ function bufferToBase64(buffer: Buffer, isFull?: boolean): string {
return window.btoa(binary);
}

interface FileViewerProps {
export interface FileViewerProps {
/** Translation function */
t: Translate;
/** Callback when the viewer is closed. */
onClose: () => void;
/** The URL (file path) to the file to be displayed. */
href: string;
// formatEditFile?: string;
formatEditFile?: string;
socket: Connection;
setStateBackgroundImage: () => void;
// themeType: ThemeType;
themeType: ThemeType;
getStyleBackgroundImage: () => React.CSSProperties | null;
/** Flag is the js-controller support subscribe on file */
supportSubscribes?: boolean;
}

interface FileViewerState {
export interface FileViewerState {
text: string | null;
code: string | null;
ext: string | null;
Expand Down Expand Up @@ -220,37 +214,21 @@ export class FileViewerClass extends Component<FileViewerProps, FileViewerState>
}
};

// eslint-disable-next-line class-methods-use-this
writeFile64 = (): void => {
/*
// File viewer in adapter-react does not support write
const parts = this.props.href.split('/');
const data = this.state.editingValue;
parts.splice(0, 2);
const adapter = parts[0];
const name = parts.splice(1).join('/');
this.props.socket.writeFile64(adapter, name, Buffer.from(data).toString('base64'))
.then(() => this.props.onClose())
.catch(e => window.alert(`Cannot write file: ${e}`));
*/
};

static getEditFile(ext: string | null): 'json' | 'json5' | 'javascript' | 'html' | 'text' {
switch (ext) {
case 'json':
return 'json';
case 'json5':
return 'json5';
case 'js':
return 'javascript';
case 'html':
return 'html';
case 'txt':
return 'html';
default:
// e.g. ace/mode/text
return 'text';
}
getEditorOrViewer(): JSX.Element {
return (
<TextField
variant="standard"
style={styles.textarea}
multiline
value={this.state.editingValue || this.state.code || this.state.text}
// onChange={newValue => this.setState({ editingValue: newValue, changed: true })}
slotProps={{
htmlInput: {
readOnly: !this.state.editing,
},
}}
/>
);
}

getContent(): React.JSX.Element | null {
Expand Down Expand Up @@ -315,29 +293,21 @@ export class FileViewerClass extends Component<FileViewerProps, FileViewerState>
if (this.state.code !== null || this.state.text !== null || this.state.editing) {
// File viewer in adapter-react does not support write
// return <Editor
// mode={FileViewerClass.getEditFile(this.props.formatEditFile)}
// mode={this.getEditFile(this.props.formatEditFile)}
// themeType={this.props.themeType}
// value={this.state.editingValue || this.state.code || this.state.text}
// onChange={this.state.editing ? newValue => this.setState({ editingValue: newValue, changed: true }) : undefined}
// />;
return (
<TextField
variant="standard"
style={styles.textarea}
multiline
value={this.state.editingValue || this.state.code || this.state.text}
// onChange={newValue => this.setState({ editingValue: newValue, changed: true })}
slotProps={{
htmlInput: {
readOnly: !this.state.editing,
},
}}
/>
);
return this.getEditorOrViewer();
}
return null;
}

// eslint-disable-next-line class-methods-use-this
onSave(): void {
// Do nothing as the file viewer in adapter-react does not support writing
}

render(): JSX.Element {
return (
<Dialog
Expand Down Expand Up @@ -389,7 +359,7 @@ export class FileViewerClass extends Component<FileViewerProps, FileViewerState>
this.state.editingValue === this.state.text
}
variant="contained"
onClick={this.writeFile64}
onClick={() => this.onSave()}
startIcon={<SaveIcon />}
>
{this.props.t('Save')}
Expand Down
59 changes: 26 additions & 33 deletions packages/adapter-react-v5/src/Components/ObjectBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@
}
if (!filteredOut && filters.role && common) {
if (common) {
filteredOut = !(common.role && common.role.startsWith(context.role as string));
filteredOut = !(common.role && common.role.startsWith(context.role));
} else {
filteredOut = true;
}
Expand Down Expand Up @@ -1703,7 +1703,7 @@
cRoot = _cRoot;
info.ids.push(curPath); // IDs will be added by alphabet
} else if (cRoot.children) {
cRoot = cRoot.children.find(item => item.data.name === parts[k]) as TreeItem;
cRoot = cRoot.children.find(item => item.data.name === parts[k]);
}
}
}
Expand Down Expand Up @@ -1774,8 +1774,8 @@
}

info.roomEnums.sort((a, b) => {
const aName: string = getName(objects[a]?.common?.name, options.lang) || (a.split('.').pop() as string);
const bName: string = getName(objects[b]?.common?.name, options.lang) || (b.split('.').pop() as string);
const aName: string = getName(objects[a]?.common?.name, options.lang) || a.split('.').pop();
const bName: string = getName(objects[b]?.common?.name, options.lang) || b.split('.').pop();
if (aName > bName) {
return 1;
}
Expand All @@ -1785,8 +1785,8 @@
return 0;
});
info.funcEnums.sort((a, b) => {
const aName: string = getName(objects[a]?.common?.name, options.lang) || (a.split('.').pop() as string);
const bName: string = getName(objects[b]?.common?.name, options.lang) || (b.split('.').pop() as string);
const aName: string = getName(objects[a]?.common?.name, options.lang) || a.split('.').pop();
const bName: string = getName(objects[b]?.common?.name, options.lang) || b.split('.').pop();
if (aName > bName) {
return 1;
}
Expand Down Expand Up @@ -1820,7 +1820,7 @@
if (_id === _path) {
found = root.children[i];
break;
} else if (_id > (_path as string)) {
} else if (_id > _path) {
break;
}
}
Expand Down Expand Up @@ -3068,7 +3068,7 @@
this.systemConfig =
this.systemConfig ||
(objects?.['system.config'] as ioBroker.SystemConfigObject) ||
((await props.socket.getObject('system.config')) as ioBroker.SystemConfigObject);
(await props.socket.getObject('system.config'));

this.systemConfig.common = this.systemConfig.common || ({} as ioBroker.SystemConfigCommon);
this.systemConfig.common.defaultNewAcl = this.systemConfig.common.defaultNewAcl || {
Expand Down Expand Up @@ -5249,14 +5249,16 @@
this.setState({ expanded });
}

private onCopy(e: React.MouseEvent, text: string): void {
private onCopy(e: React.MouseEvent, text: string | undefined): void {
e.stopPropagation();
e.preventDefault();
Utils.copyToClipboard(text);
if (text.length < 50) {
this.setState({ toast: this.props.t('ra_Copied %s', text) });
} else {
this.setState({ toast: this.props.t('ra_Copied') });
if (text) {
Utils.copyToClipboard(text);
if (text.length < 50) {
this.setState({ toast: this.props.t('ra_Copied %s', text) });
Dismissed Show dismissed Hide dismissed
} else {
this.setState({ toast: this.props.t('ra_Copied') });
Dismissed Show dismissed Hide dismissed
}
}
}

Expand Down Expand Up @@ -5771,7 +5773,7 @@

info.style = getValueStyle({ state, isExpertMode: this.state.filter.expertMode, isButton: item.data.button });

let val: JSX.Element[] = info.valTextRx as JSX.Element[];
let val: JSX.Element[] = info.valTextRx;
if (!this.state.filter.expertMode) {
if (item.data.button) {
val = [
Expand Down Expand Up @@ -5892,7 +5894,7 @@
}
const type = this.state.enumDialog.type;
const item = this.state.enumDialog.item;
const itemEnums: string[] = this.state.enumDialogEnums as string[];
const itemEnums: string[] = this.state.enumDialogEnums;
const enumsOriginal = this.state.enumDialog.enumsOriginal;

const enums = (type === 'room' ? this.info.roomEnums : this.info.funcEnums)
Expand Down Expand Up @@ -6010,7 +6012,7 @@
roles={this.info.roles}
onClose={(obj?: ioBroker.Object | null) => {
if (obj) {
this.info.objects[this.state.roleDialog as string] = obj;
this.info.objects[this.state.roleDialog] = obj;
}
this.setState({ roleDialog: null });
}}
Expand Down Expand Up @@ -6043,14 +6045,7 @@
this.props.socket
.getObject(this.state.columnsEditCustomDialog?.obj?._id || '')
.then(obj => {
if (
obj &&
ObjectBrowserClass.setCustomValue(
obj,
this.state.columnsEditCustomDialog?.it as AdapterColumn,
value,
)
) {
if (obj && ObjectBrowserClass.setCustomValue(obj, this.state.columnsEditCustomDialog?.it, value)) {
return this.props.socket.setObject(obj._id, obj);
}
throw new Error(this.props.t('ra_Cannot update attribute, because not found in the object'));
Expand Down Expand Up @@ -6825,7 +6820,7 @@
<IconCopy
className="copyButton"
style={styles.cellCopyButton}
onClick={e => this.onCopy(e, item.data?.title as string)}
onClick={e => this.onCopy(e, item.data?.title)}
/>
</Box>
) : null}
Expand Down Expand Up @@ -7235,7 +7230,7 @@
<IconCopy
className="copyButton"
style={styles.cellCopyButtonInDetails}
onClick={e => this.onCopy(e, item.data?.title as string)}
onClick={e => this.onCopy(e, item.data?.title)}
/>
) : null}
</div>
Expand Down Expand Up @@ -7433,7 +7428,7 @@
}
}

return items as JSX.Element[];
return items;
}

private calculateColumnsVisibility(
Expand Down Expand Up @@ -7681,9 +7676,7 @@
if ((e.target as HTMLDivElement).dataset.left === 'true') {
this.resizeLeft = true;
this.resizerNextDiv = this.resizerActiveDiv.previousElementSibling as HTMLDivElement;
let handle: HTMLDivElement | null = this.resizerNextDiv.querySelector(
'.iob-ob-resize-handler',
) as HTMLDivElement;
let handle: HTMLDivElement | null = this.resizerNextDiv.querySelector('.iob-ob-resize-handler');
while (this.resizerNextDiv && !handle && i < 10) {
this.resizerNextDiv = this.resizerNextDiv.previousElementSibling as HTMLDivElement;
handle = this.resizerNextDiv.querySelector('.iob-ob-resize-handler');
Expand All @@ -7702,8 +7695,8 @@
}
this.resizerNextName = this.resizerNextDiv.dataset.name || null;

this.resizerMin = parseInt(this.resizerActiveDiv.dataset.min as string, 10) || 0;
this.resizerNextMin = parseInt(this.resizerNextDiv.dataset.min as string, 10) || 0;
this.resizerMin = parseInt(this.resizerActiveDiv.dataset.min, 10) || 0;
this.resizerNextMin = parseInt(this.resizerNextDiv.dataset.min, 10) || 0;

this.resizerPosition = e.clientX;

Expand Down
Loading
Loading