Skip to content

Commit

Permalink
🎈 perf: Added error messages for import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Jan 5, 2025
1 parent 1088c4e commit 892c491
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
24 changes: 17 additions & 7 deletions packages/chili-core/src/dataExchange.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "./document";
import { PubSub } from "./foundation";
import { ShapeNode, VisualNode } from "./model";
import { PubSub, Result } from "./foundation";
import { I18n } from "./i18n";
import { FolderNode, ShapeNode, VisualNode } from "./model";

export interface IDataExchange {
importFormats(): string[];
Expand All @@ -23,13 +24,18 @@ export class DefaultDataExchange implements IDataExchange {
async import(document: IDocument, files: FileList | File[]): Promise<void> {
for (const file of files) {
let content = new Uint8Array(await file.arrayBuffer());
let shape = document.application.shapeFactory.converter.convertFromIGES(document, content);
let shape: Result<FolderNode>;
if (file.name.endsWith(".step") || file.name.endsWith(".stp")) {
shape = document.application.shapeFactory.converter.convertFromSTEP(document, content);
} else if (file.name.endsWith(".iges") || file.name.endsWith(".igs")) {
shape = document.application.shapeFactory.converter.convertFromIGES(document, content);
} else {
alert(I18n.translate("error.import.unsupportedFileType:{0}", file.name));
continue;
}
if (!shape.isOk) {
PubSub.default.pub("showToast", "toast.read.error");
return;
PubSub.default.pub("showToast", "error.default:{0}", shape.error);
continue;
}
shape.value.name = file.name;
document.addNode(shape.value);
Expand All @@ -38,14 +44,18 @@ export class DefaultDataExchange implements IDataExchange {
}

async export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined> {
let shapes = nodes.map((x) => (x as ShapeNode).shape.value);
let shapes = nodes.filter((x) => x instanceof ShapeNode).map((x) => x.shape.value);
if (shapes.length === 0) {
PubSub.default.pub("showToast", "error.export.noNodeCanBeExported");
return undefined;
}
let factory = nodes[0].document.application.shapeFactory.converter.convertToIGES;
if (type === ".step") {
factory = nodes[0].document.application.shapeFactory.converter.convertToSTEP;
}
let shapeString = factory(...shapes);
if (!shapeString.isOk) {
PubSub.default.pub("showToast", "toast.converter.error");
PubSub.default.pub("showToast", "error.default:{0}", shapeString.error);
return undefined;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/chili-core/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ export default {
"common.type": "Type",
"entity.editable": "Editable Entity",
"entity.parameter": "Parameter Entity",
"error.default": "error",
"error.default:{0}": "error: {0}",
"error.input.cannotInputANumber": "Overlap with reference point, 1 number cannot be entered",
"error.input.invalidNumber": "Please enter a valid number, separated by ,",
"error.input.threeNumberCanBeInput": "Reference point is empty, only 3 numbers can be entered",
"error.input.unsupportedInputs": "Exceeds the maximum number of inputs",
"error.import.unsupportedFileType:{0}": "Unsupported file type: {0}",
"error.export.noNodeCanBeExported": "No node can be exported",
"file.format": "Format",
"home.recent": "Recent",
"home.welcome": "Welcome to chili3d",
Expand Down
4 changes: 3 additions & 1 deletion packages/chili-core/src/i18n/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ const I18N_KEYS = [
"common.type",
"entity.editable",
"entity.parameter",
"error.default",
"error.default:{0}",
"error.input.cannotInputANumber",
"error.input.invalidNumber",
"error.input.threeNumberCanBeInput",
"error.input.unsupportedInputs",
"error.import.unsupportedFileType:{0}",
"error.export.noNodeCanBeExported",
"file.format",
"home.recent",
"home.welcome",
Expand Down
4 changes: 3 additions & 1 deletion packages/chili-core/src/i18n/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ export default {
"common.type": "类型",
"entity.editable": "可编辑实体",
"entity.parameter": "参数化实体",
"error.default": "错误",
"error.default:{0}": "错误: {0}",
"error.input.cannotInputANumber": "与参照点重合,无法输入 1 个数",
"error.input.invalidNumber": "输入错误,请输入有效的数字,以,分开",
"error.input.threeNumberCanBeInput": "参照点为空,只能输入 3 个数",
"error.input.unsupportedInputs": "超过最大输入数",
"error.import.unsupportedFileType:{0}": "不支持的文件类型: {0}",
"error.export.noNodeCanBeExported": "没有可导出的节点",
"file.format": "文件格式",
"home.recent": "最近使用",
"home.welcome": "欢迎使用 chili3d",
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-ui/src/property/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class InputProperty extends PropertyBase {
private readonly setValue = (input: HTMLInputElement) => {
let newValue = this.converter?.convertBack?.(input.value);
if (!newValue?.isOk) {
PubSub.default.pub("showToast", "error.default");
PubSub.default.pub("showToast", "error.default:{0}", newValue?.error);
return;
}
Transaction.excute(this.document, "modify property", () => {
Expand Down

0 comments on commit 892c491

Please sign in to comment.