Skip to content

Commit

Permalink
🦄 refactor: remove nodeLinkedListChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Apr 18, 2024
1 parent 0516ec6 commit 99a61c5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 81 deletions.
12 changes: 11 additions & 1 deletion packages/chili-core/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IApplication } from "./application";
import { History, IDisposable, IPropertyChanged, ObservableCollection } from "./foundation";
import {
History,
IDisposable,
INodeChangedObserver,
IPropertyChanged,
NodeRecord,
ObservableCollection,
} from "./foundation";
import { Material } from "./material";
import { INode, INodeLinkedList } from "./model/node";
import { ISelection } from "./selection";
Expand All @@ -19,6 +26,9 @@ export interface IDocument extends IPropertyChanged, IDisposable, ISerialize {
readonly application: IApplication;
materials: ObservableCollection<Material>;
addNode(...nodes: INode[]): void;
addNodeObserver(observer: INodeChangedObserver): void;
removeNodeObserver(observer: INodeChangedObserver): void;
notifyNodeChanged(records: NodeRecord[]): void;
save(): Promise<void>;
close(): Promise<void>;
serialize(): Serialized;
Expand Down
4 changes: 4 additions & 0 deletions packages/chili-core/src/foundation/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export interface NodeRecord {
newPrevious?: INode;
}

export interface INodeChangedObserver {
handleNodeChanged(records: NodeRecord[]): void;
}

export class NodeLinkedListHistoryRecord implements IHistoryRecord {
readonly name: string;
constructor(readonly records: NodeRecord[]) {
Expand Down
2 changes: 0 additions & 2 deletions packages/chili-core/src/foundation/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { ObjectSnapType } from "../snapType";
import { CursorType, IView } from "../visual";
import { AsyncController } from "./asyncController";
import { IDisposable } from "./disposable";
import { NodeRecord } from "./history";
import { MessageType } from "./messageType";
import { IPropertyChanged } from "./observer";
import { Result } from "./result";

export interface PubSubEventMap {
executeCommand: (commandName: CommandKeys) => void;
nodeLinkedListChanged: (document: IDocument, records: NodeRecord[]) => void;
activeViewChanged: (view: IView | undefined) => void;
viewClosed: (view: IView) => void;
modelUpdate: (model: IModel) => void;
Expand Down
25 changes: 6 additions & 19 deletions packages/chili-core/src/model/nodeLinkedList.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "../document";
import {
Id,
Logger,
NodeAction,
NodeLinkedListHistoryRecord,
NodeRecord,
PubSub,
Transaction,
} from "../foundation";
import { Id, Logger, NodeAction, NodeRecord } from "../foundation";
import { Serializer } from "../serialize";
import { INode, INodeLinkedList, Node } from "./node";

Expand Down Expand Up @@ -56,12 +48,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
this._count++;
});

this.handlePubAndHistory(records);
}

private handlePubAndHistory(records: NodeRecord[]) {
Transaction.add(this.document, this.document.history, new NodeLinkedListHistoryRecord(records));
PubSub.default.pub("nodeLinkedListChanged", this.document, records);
this.document.notifyNodeChanged(records);
}

private ensureIsChild(item: INode) {
Expand Down Expand Up @@ -107,7 +94,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
});
this.removeNode(item, true);
});
this.handlePubAndHistory(records);
this.document.notifyNodeChanged(records);
}

private removeNode(node: INode, nullifyParent: boolean) {
Expand Down Expand Up @@ -159,7 +146,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
}
}
this._count++;
this.handlePubAndHistory([record]);
this.document.notifyNodeChanged([record]);
}

insertAfter(target: INode | undefined, node: INode): void {
Expand All @@ -173,7 +160,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
node,
};
NodeLinkedList.insertNodeAfter(this, target, node);
this.handlePubAndHistory([record]);
this.document.notifyNodeChanged([record]);
}

private static insertNodeAfter(parent: NodeLinkedList, target: INode | undefined, node: INode) {
Expand Down Expand Up @@ -210,7 +197,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
this.removeNode(child, false);
NodeLinkedList.insertNodeAfter(newParent, previousSibling, child);

this.handlePubAndHistory([record]);
this.document.notifyNodeChanged([record]);
}

protected onVisibleChanged() {
Expand Down
4 changes: 2 additions & 2 deletions packages/chili-core/src/visual/visualContext.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDisposable } from "../foundation";
import { IDisposable, INodeChangedObserver } from "../foundation";
import { IModel } from "../model";
import { ShapeMeshData } from "../shape";
import { IVisualObject } from "./visualObject";
import { IVisualGeometry } from "./visualShape";

export interface IVisualContext extends IDisposable {
export interface IVisualContext extends IDisposable, INodeChangedObserver {
get shapeCount(): number;
addMesh(meshData: ShapeMeshData): IVisualObject;
addVisualObject(object: IVisualObject): void;
Expand Down
18 changes: 18 additions & 0 deletions packages/chili-three/src/threeVisualContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
LineType,
Material,
MathUtils,
NodeAction,
NodeRecord,
ShapeMeshData,
ShapeType,
VertexMeshData,
Expand Down Expand Up @@ -52,6 +54,7 @@ export class ThreeVisualContext implements IVisualContext {
this.visualShapes = new Group();
this.tempShapes = new Group();
scene.add(this.visualShapes, this.tempShapes);
visual.document.addNodeObserver(this);
visual.document.materials.onCollectionChanged(this.onMaterialsChanged);
}

Expand Down Expand Up @@ -122,6 +125,20 @@ export class ThreeVisualContext implements IVisualContext {
}
};

handleNodeChanged = (records: NodeRecord[]) => {
let adds: INode[] = [],
rms: INode[] = [];
records.forEach((x) => {
if (x.action === NodeAction.add) {
INode.addNodeOrChildrenToNodes(adds, x.node);
} else if (x.action === NodeAction.remove) {
INode.addNodeOrChildrenToNodes(rms, x.node);
}
});
this.addModel(adds.filter((x) => !INode.isLinkedListNode(x)) as IModel[]);
this.removeModel(rms.filter((x) => !INode.isLinkedListNode(x)) as IModel[]);
};

addMesh(data: ShapeMeshData): IVisualObject {
let shape: ThreeVisualObject | undefined = undefined;
if (ShapeMeshData.isVertex(data)) {
Expand Down Expand Up @@ -156,6 +173,7 @@ export class ThreeVisualContext implements IVisualContext {
x.removePropertyChanged(this.onMaterialPropertyChanged),
);
this.visual.document.materials.removeCollectionChanged(this.onMaterialsChanged);
this.visual.document.removeNodeObserver(this);
this.materialMap.forEach((x) => x.dispose());
this.materialMap.clear();
this.visualShapes.clear();
Expand Down
11 changes: 11 additions & 0 deletions packages/chili-three/test/testDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
IApplication,
IDocument,
INode,
INodeChangedObserver,
INodeLinkedList,
ISelection,
ISerialize,
IView,
Material,
NodeRecord,
ObservableCollection,
PropertyChangedHandler,
Serialized,
Expand Down Expand Up @@ -57,6 +59,15 @@ export class TestDocument implements IDocument, ISerialize {
this.rootNode = {} as any;
this.application = { views: [] } as any;
}
addNodeObserver(observer: INodeChangedObserver): void {
throw new Error("Method not implemented.");
}
removeNodeObserver(observer: INodeChangedObserver): void {
throw new Error("Method not implemented.");
}
notifyNodeChanged(records: NodeRecord[]): void {
throw new Error("Method not implemented.");
}
addNode(...nodes: INode[]): void {
throw new Error("Method not implemented.");
}
Expand Down
40 changes: 22 additions & 18 deletions packages/chili-ui/src/project/tree/tree.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument, INode, INodeLinkedList, NodeRecord, PubSub, ShapeType, Transaction } from "chili-core";
import {
IDocument,
INode,
INodeChangedObserver,
INodeLinkedList,
NodeRecord,
PubSub,
ShapeType,
Transaction,
} from "chili-core";
import { Control } from "../../components";
import style from "./tree.module.css";
import { TreeItem } from "./treeItem";
import { TreeGroup } from "./treeItemGroup";
import { TreeModel } from "./treeModel";
import { SelectionHandler } from "chili-vis";

export class Tree extends Control {
export class Tree extends Control implements INodeChangedObserver {
private readonly nodeMap = new WeakMap<INode, TreeItem>();
private lastClicked: INode | undefined;
private readonly selectedNodes: Set<INode> = new Set();
Expand All @@ -18,12 +27,12 @@ export class Tree extends Control {
super(style.panel);
this.addAllNodes(document, this, document.rootNode);
this.addDisconnectedCallback(() => {
this.document.removeNodeObserver(this);
PubSub.default.remove("selectionChanged", this.handleSelectionChanged);
PubSub.default.remove("nodeLinkedListChanged", this.handleNodeLinkedChanged);
});
this.addConnectedCallback(() => {
this.document.addNodeObserver(this);
PubSub.default.sub("selectionChanged", this.handleSelectionChanged);
PubSub.default.sub("nodeLinkedListChanged", this.handleNodeLinkedChanged);
});
this.addEvents(this);
}
Expand All @@ -38,28 +47,23 @@ export class Tree extends Control {
this.dragging = undefined;
this.selectedNodes.clear();
this.removeEvents(this);
this.document.removeNodeObserver(this);
PubSub.default.remove("selectionChanged", this.handleSelectionChanged);
PubSub.default.remove("nodeLinkedListChanged", this.handleNodeLinkedChanged);
}

private handleNodeLinkedChanged = (document: IDocument, records: NodeRecord[]) => {
if (this.document !== document) return;
handleNodeChanged(records: NodeRecord[]) {
this.ensureHasHTML(records);
for (const record of records) {
let ele = this.nodeMap.get(record.node);
if (ele === undefined) continue;
if (record.oldParent !== undefined) {
this.nodeMap.get(record.oldParent)?.removeChild(ele);
}
if (record.newParent !== undefined) {
let parent = this.nodeMap.get(record.newParent);
if (parent !== undefined && parent instanceof TreeGroup) {
let pre = record.newPrevious === undefined ? null : this.nodeMap.get(record.newPrevious);
parent.insertAfter(ele, pre ?? null);
}
ele?.remove();
if (ele === undefined || record.newParent === undefined) continue;
let parent = this.nodeMap.get(record.newParent);
if (parent instanceof TreeGroup) {
let pre = record.newPrevious === undefined ? null : this.nodeMap.get(record.newPrevious);
parent.insertAfter(ele, pre ?? null);
}
}
};
}

private handleSelectionChanged = (document: IDocument, selected: INode[], unselected: INode[]) => {
unselected.forEach((x) => {
Expand Down
Loading

0 comments on commit 99a61c5

Please sign in to comment.