Skip to content

Commit

Permalink
implemented correct cloning of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Nov 23, 2023
1 parent 0e05d6c commit 36b43cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
Message as MessageDialog,
SelectFile as SelectFileDialog, Icon,
} from '@iobroker/adapter-react-v5';
import { isGroup } from './Utils/utils';
import { isGroup, deepClone } from './Utils/utils';
import { recalculateFields, store, updateProject } from './Store';

import Attributes from './Attributes';
Expand Down Expand Up @@ -684,6 +684,20 @@ class App extends Runtime {
});

const newKey = isGroup(newWidget) ? this.getNewGroupId(store.getState().visProject) : this.getNewWidgetId(store.getState().visProject);

if (isGroup(newWidget)) {
// copy all members
for (let i = 0; i < newWidget.data.members.length; i++) {
const wid = newWidget.data.members[i];
const newMember = deepClone(widgets[wid]);

const newMemberId = this.getNewWidgetId(store.getState().visProject, i);
widgets[newMemberId] = newMember;

newWidget.data.members[i] = newMemberId;
}
}

widgets[newKey] = newWidget;
newKeys.push(newKey);
});
Expand Down
23 changes: 21 additions & 2 deletions src/src/Utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ export interface ProjectSettings {
statesDebounceTime: number;
}

export interface Widget {
interface SingleWidget {
data: Record<string, unknown>;
style: Record<string, unknown>;
tpl: string;
widgetSet: string;
}

interface Group extends SingleWidget {
tpl: '_tplGroup';
data: {
members: string[];
[other: string]: unknown
}
}

export type Widget = SingleWidget | Group;

export interface View {
activeWidgets: string[];
filterList: string[];
Expand All @@ -29,6 +39,15 @@ export interface View {
*
* @param widget widget to check
*/
export function isGroup(widget: Widget): boolean {
export function isGroup(widget: Widget): widget is Group {
return widget.tpl === '_tplGroup';
}

/**
* Stringify-parse copy with type inference
*
* @param object The object which should be cloned
*/
export function deepClone<T extends Record<string, unknown>>(object: T): T {
return JSON.parse(JSON.stringify(object));
}

0 comments on commit 36b43cd

Please sign in to comment.