Skip to content

Commit

Permalink
feat: 添加model对象
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeard30 committed Mar 10, 2024
1 parent 663cbf9 commit c031b7d
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/app/components/widgets/ghost/ghost.widget.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ChangeDetectionStrategy, Component, Optional } from '@angular/core';
import { usePrefix } from '../../../utils';
import { NodeTitleWidget } from '../node-title/node-title.widget';
import { Engine } from '../../../core/models';
import { Cursor, CursorStatus } from '../../../core/models/cursor';

@Component({
selector: 'app-ghost',
standalone: true,
template: ``,
template: `
@if (cursor.status === CursorStatus.Dragging) {
<div class="{{ prefix }}">
<span style="white-space: nowrap">
<app-node-title-widget></app-node-title-widget>
</span>
</div>
}
`,
imports: [NodeTitleWidget],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class GhostWidget {}
export class GhostWidget {
prefix = usePrefix('ghost');
constructor(
@Optional() public designer: Engine,
@Optional() public cursor: Cursor
) {}

protected readonly CursorStatus = CursorStatus;
}
27 changes: 27 additions & 0 deletions src/app/components/widgets/node-title/node-title.widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { TreeNode } from '../../../core/models';

@Component({
selector: 'app-node-title-widget',
standalone: true,
template: ` {{ node.getMessage('title') || node.componentName }} `,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NodeTitleWidget implements OnChanges {
@Input() node: TreeNode;

currentNode: TreeNode;

ngOnChanges(changes: SimpleChanges): void {
if (changes.node && changes.node.currentValue) {
this.currentNode = this.takeNode(this.node);
}
}

private takeNode(node: TreeNode) {
if (node.componentName === '$$ResourceNode$$') {
return node.children[0];
}
return node;
}
}
50 changes: 50 additions & 0 deletions src/app/core/models/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,54 @@ export class Cursor {
constructor(engine: Engine) {
this.engine = engine;
}

get speed() {
return Math.sqrt(Math.pow(this.dragAtomDelta.clientX, 2) + Math.pow(this.dragAtomDelta.clientY, 2));
}

setStatus(status: CursorStatus) {
this.status = status;
}

setType(type: CursorType | string) {
this.type = type;
}

setDragType(type: CursorDragType | string) {
this.dragType = type;
}

setStyle(style: string) {
this.engine.workbench.eachWorkspace(workspace => {
setCursorStyle(workspace.viewport.contentWindow, style);
});
}

setPosition(position?: ICursorPosition) {
this.dragAtomDelta = calcPositionDelta(this.position, position);
this.position = { ...position };
if (this.status === CursorStatus.Dragging) {
this.dragStartToCurrentDelta = calcPositionDelta(this.position, this.dragStartPosition);
}
}

setDragStartPosition(position?: ICursorPosition) {
if (position) {
this.dragStartPosition = { ...position };
} else {
this.dragStartPosition = null;
this.dragStartToCurrentDelta = DEFAULT_POSITION;
}
}

setDragEndPosition(position?: ICursorPosition) {
if (!this.dragStartPosition) return;
if (position) {
this.dragEndPosition = { ...position };
this.dragStartToEndDelta = calcPositionDelta(this.dragStartPosition, this.dragEndPosition);
} else {
this.dragEndPosition = null;
this.dragStartToEndDelta = DEFAULT_POSITION;
}
}
}
13 changes: 13 additions & 0 deletions src/app/core/models/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IEngineProps } from '@/app/shared/types';
import { Cursor } from '@/app/core/models/cursor';
import { Screen } from '@/app/core/models/screen';
import { uid } from '@/app/shared/uid';
import { TreeNode } from '@/app/core/models/tree-node';

export class Engine {
id: string;
Expand All @@ -28,4 +29,16 @@ export class Engine {
this.cursor = new Cursor(this);
// this.keyboard = new Keyboard(this)
}

findMovingNodes(): TreeNode[] {
const results = [];
this.workbench.eachWorkspace(workspace => {
workspace.operation.moveHelper.dragNodes?.forEach(node => {
if (!results.includes(node)) {
results.push(node);
}
});
});
return results;
}
}
4 changes: 4 additions & 0 deletions src/app/core/models/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ export class Workbench {
// setWorkbenchType: action
});
}

eachWorkspace<T>(callbackFn: (value: Workspace, index: number) => T) {
this.workspaces.forEach(callbackFn);
}
}
Loading

0 comments on commit c031b7d

Please sign in to comment.