Skip to content

Commit

Permalink
✨ feat: add DataExchange
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Nov 8, 2024
1 parent fa46813 commit aca1d9c
Show file tree
Hide file tree
Showing 22 changed files with 396 additions and 210 deletions.
34 changes: 27 additions & 7 deletions packages/chili-builder/src/appBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Application, CommandService, EditEventHandler, EditorService, HotkeyService } from "chili";
import {
I18n,
IDataExchange,
IDocument,
INode,
IService,
Expand All @@ -11,6 +12,7 @@ import {
IVisualFactory,
IWindow,
Logger,
VisualNode,
} from "chili-core";
import { IAdditionalModule } from "./additionalModule";

Expand Down Expand Up @@ -90,13 +92,31 @@ export class AppBuilder {
}

createApp() {
return new Application(
this._visualFactory!,
this._shapeFactory!,
this.getServices(),
this._storage!,
this._window
);
return new Application({
storage: this._storage!,
shapeFactory: this._shapeFactory!,
visualFactory: this._visualFactory!,
services: this.getServices(),
mainWindow: this._window,
dataExchange: this.initDataExchange(),
});
}

initDataExchange(): IDataExchange {
return {
importFormats(): string[] {
return ["chili3d"];
},
exportFormats(): string[] {
return ["chili3d"];
},
import(document: IDocument, files: FileList | File[]): Promise<void> {
return Promise.reject("not implemented");
},
export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined> {
return Promise.reject("not implemented");
},
};
}

private ensureNecessary() {
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-builder/src/ribbon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const DefaultRibbon: RibbonTab[] = [
},
{
groupName: "ribbon.group.importExport",
items: ["file.import", "file.export.iges", "file.export.stp"],
items: ["file.import", "file.export"],
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions packages/chili-core/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { ICommand } from "./command";
import { IDataExchange } from "./dataExchange";
import { IDocument } from "./document";
import { IStorage, ObservableCollection } from "./foundation";
import { Serialized } from "./serialize";
Expand All @@ -11,6 +12,7 @@ import { IView, IVisualFactory } from "./visual";

export interface IApplication {
readonly mainWindow?: IWindow;
readonly dataExchange: IDataExchange;
readonly visualFactory: IVisualFactory;
readonly shapeFactory: IShapeFactory;
readonly services: IService[];
Expand Down
3 changes: 1 addition & 2 deletions packages/chili-core/src/command/commandKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const COMMAND_KEYS = [
"doc.saveToFile",
"edit.redo",
"edit.undo",
"file.export.iges",
"file.export.stp",
"file.export",
"file.import",
"modify.array",
"modify.break",
Expand Down
11 changes: 11 additions & 0 deletions packages/chili-core/src/dataExchange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "./document";
import { VisualNode } from "./model";

export interface IDataExchange {
importFormats(): string[];
exportFormats(): string[];
import(document: IDocument, files: FileList | File[]): Promise<void>;
export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined>;
}
12 changes: 12 additions & 0 deletions packages/chili-core/src/foundation/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export class Transaction {
}
}

static async excuteAsync(document: IDocument, name: string, action: () => Promise<void>) {
let trans = new Transaction(document, document.history, name);
trans.start();
try {
await action();
trans.commit();
} catch (e) {
trans.rollback();
throw e;
}
}

start(name?: string) {
let transactionName = name ?? this.name;
if (Transaction._transactionMap.get(this.token) !== undefined) {
Expand Down
6 changes: 4 additions & 2 deletions packages/chili-core/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default {
"box.dx": "Length",
"box.dy": "Width",
"box.dz": "Height",
"body.editableShape": "Editable Shape",
"body.meshNode": "Mesh Node",
"circle.center": "Center",
"circle.radius": "Radius",
"command.arc": "Arc",
Expand All @@ -46,8 +48,7 @@ export default {
"command.document.save": "Save",
"command.document.saveAs": "Save As",
"command.document.saveToFile": "Save To File",
"command.export.iges": "Export IGS",
"command.export.step": "Export STP",
"command.export": "Export",
"command.faceable.isFace": "Face",
"command.fuse": "Fuse",
"command.import": "Import",
Expand Down Expand Up @@ -95,6 +96,7 @@ export default {
"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",
"file.format": "Format",
"home.recent": "Recent",
"home.welcome": "Welcome to chili3d",
"items.header": "Items",
Expand Down
6 changes: 4 additions & 2 deletions packages/chili-core/src/i18n/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const I18N_KEYS = [
"body.revol",
"body.sweep",
"body.wire",
"body.editableShape",
"body.meshNode",
"box.dx",
"box.dy",
"box.dz",
Expand All @@ -41,8 +43,7 @@ const I18N_KEYS = [
"command.document.save",
"command.document.saveAs",
"command.document.saveToFile",
"command.export.iges",
"command.export.step",
"command.export",
"command.faceable.isFace",
"command.fuse",
"command.import",
Expand Down Expand Up @@ -90,6 +91,7 @@ const I18N_KEYS = [
"error.input.invalidNumber",
"error.input.threeNumberCanBeInput",
"error.input.unsupportedInputs",
"file.format",
"home.recent",
"home.welcome",
"items.header",
Expand Down
6 changes: 4 additions & 2 deletions packages/chili-core/src/i18n/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default {
"body.revol": "旋转",
"body.sweep": "扫略",
"body.wire": "线框",
"body.editableShape": "可编辑的形状",
"body.meshNode": "网格节点",
"box.dx": "长",
"box.dy": "宽",
"box.dz": "高",
Expand All @@ -46,8 +48,7 @@ export default {
"command.document.save": "保存文档",
"command.document.saveAs": "另存为",
"command.document.saveToFile": "保存到文件",
"command.export.iges": "导出IGS",
"command.export.step": "导出STP",
"command.export": "导出",
"command.faceable.isFace": "面",
"command.fuse": "合并",
"command.import": "导入",
Expand Down Expand Up @@ -95,6 +96,7 @@ export default {
"error.input.invalidNumber": "输入错误,请输入有效的数字,以,分开",
"error.input.threeNumberCanBeInput": "参照点为空,只能输入 3 个数",
"error.input.unsupportedInputs": "超过最大输入数",
"file.format": "文件格式",
"home.recent": "最近使用",
"home.welcome": "欢迎使用 chili3d",
"items.header": "项目",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./application";
export * from "./command";
export * from "./config";
export * from "./constants";
export * from "./dataExchange";
export * from "./document";
export * from "./editor";
export * from "./foundation";
Expand Down
44 changes: 40 additions & 4 deletions packages/chili-core/src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export namespace INode {
}

export abstract class VisualNode extends Node {
abstract display(): I18nKeys;

@Serializer.serialze()
get transform(): Matrix4 {
return this.getPrivateValue("transform", Matrix4.identity());
Expand Down Expand Up @@ -253,6 +255,19 @@ export abstract class VisualNode extends Node {

@Serializer.register(["document", "mesh", "name", "id"])
export class MeshNode extends VisualNode {
override display(): I18nKeys {
return "body.meshNode";
}

@Serializer.serialze()
@Property.define("common.material", { type: "materialId" })
get materialId(): string {
return this.getPrivateValue("materialId");
}
set materialId(value: string) {
this.setProperty("materialId", value);
}

protected _mesh: Mesh;
@Serializer.serialze()
get mesh(): Mesh {
Expand All @@ -262,9 +277,16 @@ export class MeshNode extends VisualNode {
this.setProperty("mesh", value);
}

constructor(document: IDocument, mesh: Mesh, name: string, id: string = Id.generate()) {
constructor(
document: IDocument,
mesh: Mesh,
name: string,
materialId?: string,
id: string = Id.generate(),
) {
super(document, name, id);
this._mesh = mesh;
this.setPrivateValue("materialId", materialId ?? document.materials.at(0)?.id ?? "");
}

override boundingBox(): BoundingBox {
Expand Down Expand Up @@ -379,11 +401,14 @@ export abstract class ParameterShapeNode extends ShapeNode {
}

protected abstract generateShape(): Result<IShape>;
abstract display(): I18nKeys;
}

@Serializer.register(["document", "name", "shape", "materialId", "id"])
export class EditableShapeNode extends ShapeNode {
override display(): I18nKeys {
return "body.editableShape";
}

@Serializer.serialze()
override get shape(): Result<IShape> {
return this._shape;
Expand All @@ -392,9 +417,20 @@ export class EditableShapeNode extends ShapeNode {
this.setShape(shape);
}

constructor(document: IDocument, name: string, shape: IShape, materialId?: string, id?: string) {
constructor(
document: IDocument,
name: string,
shape: IShape | Result<IShape>,
materialId?: string,
id?: string,
) {
super(document, name, materialId, id);
this._shape = Result.ok(shape);

if (shape instanceof Result) {
this._shape = shape;
} else {
this._shape = Result.ok(shape);
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions packages/chili-core/src/ui/combobox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IConverter, Observable, ObservableCollection } from "../foundation";

export class Combobox<T> extends Observable {
constructor(readonly converter?: IConverter<T>) {
super();
}

get selectedIndex(): number {
return this.getPrivateValue("selectedIndex", 0);
}
set selectedIndex(value: number) {
if (value < 0 || value >= this.items.length) {
return;
}

this.setProperty("selectedIndex", value);
}

get selectedItem(): T | undefined {
return this.items.at(this.selectedIndex);
}

readonly items = new ObservableCollection<T>();
}
1 change: 1 addition & 0 deletions packages/chili-core/src/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

export * from "./button";
export * from "./combobox";
export * from "./ribbon";
export * from "./window";
Loading

0 comments on commit aca1d9c

Please sign in to comment.