Skip to content

Commit

Permalink
Fix: Fix customize the display of profile classification (#1504)
Browse files Browse the repository at this point in the history
* Fix: Fix customize the display of Profile classification

* Fix: Fix public profile

* Fix: Fix keep the name of the custom directory
  • Loading branch information
dohard-ma authored Mar 11, 2022
1 parent 962ad6c commit cf0bc71
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 107 deletions.
1 change: 0 additions & 1 deletion src/app/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const WHITE_COLOR = '#ffffff';
export const BLACK_COLOR = '#464646';

export const KEY_DEFAULT_CATEGORY_CUSTOM = 'key-default_category-Custom';
export const KEY_DEFAULT_CATEGORY_DEFAULT = 'key-default_category-Default';

export const PRINTING_MATERIAL_CONFIG_COLORS = [
[WHITE_COLOR, BLACK_COLOR],
Expand Down
1 change: 1 addition & 0 deletions src/app/flux/cnc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export const actions = {
definitionsWithSameCategory = toolDefinitions.filter(d => d.category === oldName);
for (const definition of definitionsWithSameCategory) {
definition.category = newName;
definition.i18nCategory = '';
await definitionManager.updateDefinition(definition);
// find the old tool category definition and replace it
const isReplacedDefinition = (d) => d.definitionId === definition.definitionId;
Expand Down
22 changes: 2 additions & 20 deletions src/app/flux/editor/actions-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import { baseActions } from './actions-base';
import { controller } from '../../lib/controller';
import { STEP_STAGE, PROCESS_STAGE } from '../../lib/manager/ProgressManager';
import { DISPLAYED_TYPE_MODEL, DISPLAYED_TYPE_TOOLPATH, HEAD_LASER, KEY_DEFAULT_CATEGORY_CUSTOM, KEY_DEFAULT_CATEGORY_DEFAULT, SELECTEVENT } from '../../constants';
import { DISPLAYED_TYPE_MODEL, DISPLAYED_TYPE_TOOLPATH, HEAD_LASER, SELECTEVENT } from '../../constants';
import { getToolPathType } from '../../toolpaths/utils';

import { toast } from '../../ui/components/Toast';
Expand Down Expand Up @@ -554,27 +554,9 @@ export const processActions = {
name = `#${name}`;
}
definition.name = name;

// Compatible with profiles exported from older versions
definition.category = (
definition.i18nCategory
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_CUSTOM
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_DEFAULT
) ? definition.category : '';

definition.i18nCategory = (
definition.i18nCategory
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_CUSTOM
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_DEFAULT
) ? definition.i18nCategory : '';

definition.i18nName = '';
await definitionManager.updateDefinition({
definitionId: definition.definitionId,
name,
category: definition.category,
i18nCategory: definition.i18nCategory,
i18nName: definition.i18nName
name
});
dispatch(baseActions.updateState(headType, {
toolDefinitions: [...toolDefinitions, definition]
Expand Down
1 change: 1 addition & 0 deletions src/app/flux/laser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export const actions = {
definitionsWithSameCategory = toolDefinitions.filter(d => d.category === oldName);
for (const definition of definitionsWithSameCategory) {
definition.category = newName;
definition.i18nCategory = '';
await definitionManager.updateDefinition(definition);
// find the old tool category definition and replace it
const isReplacedDefinition = (d) => d.definitionId === definition.definitionId;
Expand Down
33 changes: 27 additions & 6 deletions src/app/flux/manager/DefinitionManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { includes } from 'lodash';
import api from '../../api';
import i18n from '../../lib/i18n';
import { HEAD_CNC, RIGHT_EXTRUDER_MAP_NUMBER,
import {
HEAD_CNC, RIGHT_EXTRUDER_MAP_NUMBER,
PRINTING_MATERIAL_CONFIG_KEYS_SINGLE,
MACHINE_EXTRUDER_X,
MACHINE_EXTRUDER_Y
MACHINE_EXTRUDER_Y,
KEY_DEFAULT_CATEGORY_CUSTOM
} from '../../constants';

const primeTowerDefinitionKeys = [
Expand Down Expand Up @@ -83,24 +85,43 @@ class DefinitionManager {
return definition;
}

fillCustomCategory(definition) {
const isCustom = ({ metadata }) => {
if (metadata?.readonly) {
return false;
}
return true;
};
const category = definition.category || i18n._(KEY_DEFAULT_CATEGORY_CUSTOM);
const categoryApplyI18n = definition.i18nCategory ? i18n._(definition.i18nCategory) : category;

definition.category = isCustom(definition) ? category : categoryApplyI18n;
definition.i18nCategory = definition.i18nCategory || '';
return definition;
}

async getConfigDefinitions() {
const res = await api.profileDefinitions.getConfigDefinitions(this.headType, this.configPathname);
const definitions = await this.markDefaultDefinitions(res.body.definitions);
return definitions;
return definitions.map(this.fillCustomCategory);
}

async getDefinitionsByPrefixName(prefix) {
const res = await api.profileDefinitions.getDefinitionsByPrefixName(this.headType, prefix, this.configPathname);
const definitions = await this.markDefaultDefinitions(res.body.definitions);
return definitions;
return definitions.map(this.fillCustomCategory);
}


async createDefinition(definition) {
const res = await api.profileDefinitions.createDefinition(this.headType, definition, this.configPathname);
return res.body.definition;
}

async createTmpDefinition(definition, definitionName) {
const res = await api.profileDefinitions.createTmpDefinition(definition, definitionName);
return res.body.uploadName;
}

async removeDefinition(definition) {
await api.profileDefinitions.removeDefinition(this.headType, definition.definitionId, this.configPathname);
}
Expand All @@ -112,7 +133,7 @@ class DefinitionManager {
console.error(err);
return null;
} else {
return definition;
return this.fillCustomCategory(definition);
}
}

Expand Down
32 changes: 6 additions & 26 deletions src/app/flux/printing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
DUAL_EXTRUDER_TOOLHEAD_FOR_SM2,
DUAL_EXTRUDER_LIMIT_WIDTH_L,
DUAL_EXTRUDER_LIMIT_WIDTH_R, BOTH_EXTRUDER_MAP_NUMBER,
KEY_DEFAULT_CATEGORY_CUSTOM,
KEY_DEFAULT_CATEGORY_DEFAULT,
WHITE_COLOR,
BLACK_COLOR
} from '../../constants';
Expand Down Expand Up @@ -1013,34 +1011,15 @@ export const actions = {
const definitionId = `${type}.${timestamp()}`;
const definition = await definitionManager.uploadDefinition(definitionId, response.uploadName);

// Compatible with profiles exported from older versions
definition.category = (
definition.i18nCategory
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_CUSTOM
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_DEFAULT
) ? definition.category : '';

definition.i18nCategory = (
definition.i18nCategory
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_CUSTOM
&& definition.i18nCategory !== KEY_DEFAULT_CATEGORY_DEFAULT
) ? definition.i18nCategory : '';


let name = definition.name;
const definitionsKey = defaultDefinitionKeys[type].definitions;
const definitions = getState().printing[definitionsKey];
while (definitions.find(e => e.name === name)) {
name = `#${name}`;
}
definition.i18nName = '';
await definitionManager.updateDefinition({
definitionId: definition.definitionId,
name,
// The classification cannot be modified, otherwise there will be duplicate user-defined directories during language switching
category: definition.category,
i18nCategory: definition.i18nCategory,
i18nName: definition.i18nName
name
});
dispatch(actions.updateState({
[definitionsKey]: [...definitions, definition]
Expand Down Expand Up @@ -1072,6 +1051,7 @@ export const actions = {
definitions.forEach((item) => {
if (item.category === oldCategory) {
item.category = name;
item.i18nCategory = '';
definitionManager.updateDefinition(item);
}
});
Expand Down Expand Up @@ -1450,7 +1430,7 @@ export const actions = {
finalDefinition.settings.support_roof_extruder_nr.default_value = supportExtruder;
finalDefinition.settings.support_bottom_extruder_nr.default_value = supportExtruder;

await api.profileDefinitions.createDefinition(CONFIG_HEADTYPE, finalDefinition);
await definitionManager.createDefinition(finalDefinition);

// slice
/*
Expand Down Expand Up @@ -1512,8 +1492,8 @@ export const actions = {
ret.originalName = uploadResult.body.originalName;
}
const definitionName = uploadResult.body.uploadName.replace(/\.stl$/, '');
const definitionRes = await api.profileDefinitions.createTmpDefinition(modelDefinition, definitionName);
ret.definition.push(definitionRes.body.uploadName);
const uploadName = await definitionManager.createTmpDefinition(modelDefinition, definitionName);
ret.definition.push(uploadName);

// upload support of model
if (supportMesh) {
Expand Down Expand Up @@ -2949,7 +2929,7 @@ export const actions = {
operation.state.currentSupport = mesh;
model.meshObject.add(mesh);
resolve();
}, () => {}, (err) => {
}, () => { }, (err) => {
reject(err);
});
} else {
Expand Down
58 changes: 15 additions & 43 deletions src/app/ui/views/ProfileManager/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';
import PropTypes from 'prop-types';
// import { useSelector, shallowEqual } from 'react-redux';
import { isUndefined, cloneDeep, uniqWith } from 'lodash';
import { HEAD_CNC, HEAD_LASER, PRINTING_MANAGER_TYPE_MATERIAL, PRINTING_MANAGER_TYPE_QUALITY, KEY_DEFAULT_CATEGORY_CUSTOM } from '../../../constants';
import { HEAD_CNC, HEAD_LASER, PRINTING_MANAGER_TYPE_MATERIAL, PRINTING_MANAGER_TYPE_QUALITY } from '../../../constants';
import modal from '../../../lib/modal';
import DefinitionCreator from '../DefinitionCreator';
import Anchor from '../../components/Anchor';
Expand Down Expand Up @@ -35,9 +35,6 @@ function creatCateArray(optionList) {
optionList.forEach(option => {
// Make sure that the copied description file is displayed in the correct position after switching the language
const cateItem = cates.find((cate) => {
if (option.i18nCategory) {
return cate.i18nCategory === option.i18nCategory;
}
return cate.category === option.category;
});
if (cateItem) {
Expand Down Expand Up @@ -70,13 +67,8 @@ function useGetDefinitions(allDefinitions, activeDefinitionID, getDefaultDefinit
return {
label: d.name,
value: d.definitionId,
category: d.i18nCategory ? i18n._(d.i18nCategory) : (
d.category || i18n._(KEY_DEFAULT_CATEGORY_CUSTOM)
),
i18nCategory: d.i18nCategory || (
// If it is a custom type, set i18nCategory to null
d.category ? '' : KEY_DEFAULT_CATEGORY_CUSTOM
),
category: d.category,
i18nCategory: d.i18nCategory,
isHidden: !d.settings || Object.keys(d.settings).length === 0,
isDefault: !!d.isDefault,
color: (
Expand Down Expand Up @@ -128,16 +120,13 @@ function ProfileManager({
currentDefinitions.current = allDefinitions;

const actions = {
isCategorySelectedNow: (category, i18nCategory) => {
isCategorySelectedNow: (category) => {
const { definitionForManager, isCategorySelected } = definitionState;
if (!isCategorySelected) {
return false;
}
if (i18nCategory === KEY_DEFAULT_CATEGORY_CUSTOM) {
return definitionForManager.category === '';
} else {
return definitionForManager.category === category;
}

return definitionForManager.category === category;
},
setRenamingStatus: (status) => {
const currentDefinition = definitionState?.definitionForManager;
Expand All @@ -150,10 +139,7 @@ function ProfileManager({
return;
}
if (status) {
const title = definitionState.isCategorySelected ? (
// Assign default values to import type
currentDefinition.category || i18n._(KEY_DEFAULT_CATEGORY_CUSTOM)
) : currentDefinition.name;
const title = definitionState.isCategorySelected ? currentDefinition.category : currentDefinition.name;
setDefinitionState({
selectedName: title,
renamingStatus: status
Expand Down Expand Up @@ -190,7 +176,7 @@ function ProfileManager({
});
}
},
onSelectCategory: (category, i18nCategory) => {
onSelectCategory: (category) => {
const { definitionForManager, isCategorySelected, renamingStatus } = definitionState;
if (isCategorySelected && category === definitionForManager.category) {
return;
Expand All @@ -199,16 +185,12 @@ function ProfileManager({
actions.setRenamingStatus(false);
}
const activeToolCategory = currentDefinitions.current.find(d => {
if (i18nCategory === KEY_DEFAULT_CATEGORY_CUSTOM) {
return d.category === '';
} else {
return d.category === category;
}
return d.category === category;
});
const selectedSettingDefaultValue = outsideActions.getDefaultDefinition(activeToolCategory.definitionId);
setDefinitionState({
definitionForManager: activeToolCategory,
selectedName: activeToolCategory.category || i18n._(KEY_DEFAULT_CATEGORY_CUSTOM),
selectedName: activeToolCategory.category,
isCategorySelected: true,
selectedSettingDefaultValue: selectedSettingDefaultValue
});
Expand Down Expand Up @@ -328,7 +310,7 @@ function ProfileManager({
if (isCategorySelected) {
newName = data.categoryName;
const newDefinition = await outsideActions.onCreateManagerDefinition(newDefinitionForManager, newName, isCategorySelected);
actions.onSelectCategory(newDefinition.category, newDefinition.i18nCategory);
actions.onSelectCategory(newDefinition.category);
} else {
newDefinitionForManager.category = data.categoryName;
newDefinitionForManager.i18nCategory = data.categoryI18n;
Expand All @@ -343,7 +325,7 @@ function ProfileManager({
newName = data.categoryName;
newDefinitionForManager.settings = {};
const newDefinition = await outsideActions.onCreateManagerDefinition(newDefinitionForManager, newName, data.createType === 'Category', isCreate);
actions.onSelectCategory(newDefinition.category, newDefinition.i18nCategory);
actions.onSelectCategory(newDefinition.category);
} else {
newDefinitionForManager.category = data.categoryName;
newDefinitionForManager.i18nCategory = data.categoryI18n;
Expand Down Expand Up @@ -381,10 +363,6 @@ function ProfileManager({
updateCategoryName: async () => {
const definition = definitionState?.definitionForManager;
const selectedName = definitionState.selectedName;
// If it is of import type and the default value is not modified
if (definition.category === '' && selectedName === i18n._(KEY_DEFAULT_CATEGORY_CUSTOM)) {
return;
}
if (selectedName !== definition.category) { // changed
try {
await outsideActions.updateCategoryName(definition, selectedName);
Expand Down Expand Up @@ -453,18 +431,12 @@ function ProfileManager({
)}
<ul className={classNames(styles['manager-name-wrapper'])}>
{(definitionState.cates.map((cate) => {
const isCategorySelected = (() => {
if (cate.i18nCategory === KEY_DEFAULT_CATEGORY_CUSTOM) {
return definitionState?.definitionForManager.category === '';
} else {
return cate.category === definitionState?.definitionForManager.category;
}
})();
const isCategorySelected = cate.category === definitionState?.definitionForManager.category;
return !!cate.items.length && (
<li key={`${cate.category}`}>
<Anchor
className={classNames(styles['manager-btn'], { [styles.selected]: actions.isCategorySelectedNow(cate.category, cate.i18nCategory) })}
onClick={() => actions.onSelectCategory(cate.category, cate.i18nCategory)}
className={classNames(styles['manager-btn'], { [styles.selected]: actions.isCategorySelectedNow(cate.category) })}
onClick={() => actions.onSelectCategory(cate.category)}
onDoubleClick={() => actions.setRenamingStatus(true)}
>
<div className="sm-flex align-center" style={{ paddingRight: '10px' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ function PresentSelector({ toolDefinitions, setCurrentToolDefinition, setCurrent
const checkboxAndSelectGroup = {};
const name = tool.name;
const detailName = '';
// if (tool.settings.angle.default_value !== '180') {
// detailName = `${tool.name} (${tool.settings.angle.default_value}${tool.settings.angle.unit} ${tool.settings.shaft_diameter.default_value}${tool.settings.shaft_diameter.unit})`;
// } else {
// detailName = `${tool.name} (${tool.settings.shaft_diameter.default_value}${tool.settings.shaft_diameter.unit})`;
// }
checkboxAndSelectGroup.name = name;
checkboxAndSelectGroup.definitionId = definitionId;
checkboxAndSelectGroup.label = `${detailName}`;
Expand Down
3 changes: 3 additions & 0 deletions src/server/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ export const DEFINITION_SNAPMAKER_EXTRUDER_0 = 'snapmaker_extruder_0';
export const DEFINITION_SNAPMAKER_EXTRUDER_1 = 'snapmaker_extruder_1';
export const DEFINITION_ACTIVE = 'active';
export const DEFINITION_ACTIVE_FINAL = 'active_final';

export const KEY_DEFAULT_CATEGORY_CUSTOM = 'key-default_category-Custom';
export const KEY_DEFAULT_CATEGORY_DEFAULT = 'key-default_category-Default';
Loading

0 comments on commit cf0bc71

Please sign in to comment.