Skip to content

Commit

Permalink
fixed crash case when fixing widgets (#323)
Browse files Browse the repository at this point in the history
* fixed bug, that opacity is applied twice on image edit mode overlay

- closes #315

* rm comment

* fixed crash case when fixing widgets

- closes #322

---------

Signed-off-by: Max Hauser <[email protected]>
  • Loading branch information
foxriver76 authored Jan 19, 2024
1 parent 01edb2a commit 6d2332a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ E.g., if it was used in a menu and the menu is red, the circle would be red.
-->
## Changelog
### **WORK IN PROGRESS**
* (foxriver76) fixed crash case when fixing widgets
* (foxriver76) fixed bug, that opacity is applied twice on image edit mode overlay

### 2.9.20 (2024-01-18)
Expand Down
2 changes: 1 addition & 1 deletion src/src/Components/WizardHelpers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,6 @@ export default {
getObjectIcon,
allObjects,
getNewWidgetId,
/** @deprecated use "getNewWidgetInfo" instead, it will give you the full wid like "w000001" */
/** @deprecated use "getNewWidgetId" instead, it will give you the full wid like "w000001" */
getNewWidgetIdNumber,
};
2 changes: 1 addition & 1 deletion src/src/Vis/oldVis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ class Vis {

// Return as array used widgetSets or null if no information about it
getUsedObjectIDs() {
const result = getUsedObjectIDs(this.views, !this.editMode);
const result = getUsedObjectIDs(!this.editMode);
if (!result) {
return result;
}
Expand Down
7 changes: 3 additions & 4 deletions src/src/Vis/visCanWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import { calculateOverflow, isVarFinite } from '@/Utils/utils';
import { calculateOverflow, isVarFinite, deepClone } from '@/Utils/utils';
import {
replaceGroupAttr,
addClass,
Expand Down Expand Up @@ -111,7 +111,7 @@ class VisCanWidget extends VisBaseWidget {
signals: this.props.context.linkContext.signals,
};

getUsedObjectIDsInWidget(this.props.context.views, this.props.view, this.props.id, linkContext);
getUsedObjectIDsInWidget(this.props.view, this.props.id, linkContext);

this.IDs = linkContext.IDs;

Expand Down Expand Up @@ -971,7 +971,7 @@ class VisCanWidget extends VisBaseWidget {

try {
widgetData = { wid, ...(widget.data || {}) };
widgetStyle = JSON.parse(JSON.stringify(newWidgetStyle || widget.style || {}));
widgetStyle = deepClone(newWidgetStyle || widget.style || {});
// Replace _PRJ_NAME
Object.keys(widgetData).forEach(attr => {
if (attr &&
Expand Down Expand Up @@ -1352,7 +1352,6 @@ class VisCanWidget extends VisBaseWidget {
if (this.state.applyBindings && !this.bindingsTimer) {
this.bindingsTimer = setTimeout(() => {
this.bindingsTimer = null;
// console.log(`[${Date.now()}] Widget bindings ${JSON.stringify(this.state.applyBindings)}`);
this.renderWidget(true);
}, 10);
}
Expand Down
4 changes: 3 additions & 1 deletion src/src/Vis/visEngine.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import VisFormatUtils from './visFormatUtils';
import { getUrlParameter, extractBinding } from './visUtils';
import VisWidgetsCatalog from './visWidgetsCatalog';
import { store } from '../Store';
import { deepClone } from '../Utils/utils';

function _translateWord(text, lang, dictionary) {
if (!text) {
Expand Down Expand Up @@ -2055,7 +2056,8 @@ ${this.scripts}
timeStart: this.state.timeStart,
user: this.userName,
userGroups: this.props.userGroups,
views: visProject,
// For internal widgets interact with the store instead
views: deepClone(visProject),
widgetHint: this.props.widgetHint,
registerEditorCallback: this.props.runtime ? null : this.props.registerEditorCallback,
setSelectedGroup: this.props.runtime ? null : this.props.setSelectedGroup,
Expand Down
3 changes: 1 addition & 2 deletions src/src/Vis/visRxWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget {
widgetAttrInfo,
};

getUsedObjectIDsInWidget(props.context.views, props.view, props.id, this.linkContext);
getUsedObjectIDsInWidget(props.view, props.id, this.linkContext);

// do not change it to lambda function as onStateChanged could be inherited.
this.onStateChangedBind = this.onStateChanged.bind(this);
Expand Down Expand Up @@ -445,7 +445,6 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget {

// extract bindings anew as data or style was changes
getUsedObjectIDsInWidget(
context.views,
this.props.view,
this.props.id,
this.linkContext,
Expand Down
35 changes: 14 additions & 21 deletions src/src/Vis/visUtils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
import { I18n } from '@iobroker/adapter-react-v5';
import { deepClone } from '@/Utils/utils';
import { store, updateWidget } from '../Store';
import { store, updateProject, updateWidget } from '../Store';

function replaceGroupAttr(inputStr, groupAttrList) {
let newString = inputStr;
Expand Down Expand Up @@ -278,7 +278,8 @@ function extractBinding(format) {
// visibility: {} //
// signals: {} //
// }
function getUsedObjectIDsInWidget(views, view, wid, linkContext) {
function getUsedObjectIDsInWidget(view, wid, linkContext) {
const views = deepClone(store.getState().visProject);
// Check all attributes
const widget = views[view].widgets[wid];

Expand Down Expand Up @@ -371,7 +372,6 @@ function getUsedObjectIDsInWidget(views, view, wid, linkContext) {
if (!widget.groupid) {
store.dispatch(updateWidget({ viewId: view, widgetId: widget, data: { ...widget, groupid: getWidgetGroup(views, view, wid) } }));
}
// widget.groupid = widget.groupid || getWidgetGroup(views, view, wid);

if (!store.getState().visProject[view].widgets[widget.groupid]) {
store.dispatch(updateWidget({ viewId: view, widgetId: widget, data: { ...widget, groupid: getWidgetGroup(views, view, wid) } }));
Expand Down Expand Up @@ -407,7 +407,7 @@ function getUsedObjectIDsInWidget(views, view, wid, linkContext) {
if (typeof data[attr] === 'string') {
const result = replaceGroupAttr(data[attr], parentWidgetData);
if (result.doesMatch) {
newGroupData = newGroupData || JSON.parse(JSON.stringify(data));
newGroupData = newGroupData || deepClone(data);
newGroupData[attr] = result.newString || '';
}
}
Expand Down Expand Up @@ -614,9 +614,14 @@ function getUsedObjectIDsInWidget(views, view, wid, linkContext) {
}
});
}

// as we are fixing the project in this method, write it back to store
store.dispatch(updateProject(views));
}

function getUsedObjectIDs(views, isByViews) {
function getUsedObjectIDs(isByViews) {
const views = store.getState().visProject;

if (!views) {
console.log('Check why views are not yet loaded!');
return null;
Expand All @@ -643,7 +648,7 @@ function getUsedObjectIDs(views, isByViews) {
linkContext.byViews[view] = [];
}

Object.keys(views[view].widgets).forEach(wid => getUsedObjectIDsInWidget(views, view, wid, linkContext));
Object.keys(views[view].widgets).forEach(wid => getUsedObjectIDsInWidget(view, wid, linkContext));
});

if (isByViews) {
Expand All @@ -662,9 +667,9 @@ function getUsedObjectIDs(views, isByViews) {
if (widget.tpl === 'tplContainerView' && widget.data.contains_view) {
const ids = linkContext.byViews[widget.data.contains_view];
if (ids) {
for (let a = 0; a < ids.length; a++) {
if (ids[a] && !linkContext.byViews[view].includes(ids[a])) {
linkContext.byViews[view].push(ids[a]);
for (const id of ids) {
if (id && !linkContext.byViews[view].includes(id)) {
linkContext.byViews[view].push(id);
changed = true;
}
}
Expand Down Expand Up @@ -1062,18 +1067,6 @@ function findWidgetUsages(views, view, widgetId, _result) {
return result;
}

// module.exports = {
// getUsedObjectIDs,
// extractBinding,
// getWidgetGroup,
// replaceGroupAttr,
// getUsedObjectIDsInWidget,
// getUrlParameter,
// parseDimension,
// addClass,
// removeClass,
// };

export {
getUsedObjectIDs,
extractBinding,
Expand Down

0 comments on commit 6d2332a

Please sign in to comment.