Skip to content

Commit

Permalink
feat: 添加model对象
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeard30 committed Mar 8, 2024
1 parent 3709e36 commit 663cbf9
Show file tree
Hide file tree
Showing 17 changed files with 655 additions and 2 deletions.
116 changes: 116 additions & 0 deletions src/app/core/models/cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Engine } from './engine';
import { isValidNumber } from '../../shared/types';
import { globalThisPolyfill } from '../../shared/globalThisPolyfill';

export enum CursorStatus {
Normal = 'NORMAL',
DragStart = 'DRAG_START',
Dragging = 'DRAGGING',
DragStop = 'DRAG_STOP'
}

export enum CursorDragType {
Move = 'MOVE',
Resize = 'RESIZE',
Rotate = 'ROTATE',
Scale = 'SCALE',
Translate = 'TRANSLATE',
Round = 'ROUND'
}

export enum CursorType {
Normal = 'NORMAL',
Selection = 'SELECTION',
Sketch = 'SKETCH'
}

export interface ICursorPosition {
pageX?: number;

pageY?: number;

clientX?: number;

clientY?: number;

topPageX?: number;

topPageY?: number;

topClientX?: number;

topClientY?: number;
}

export interface ICursor {
status?: CursorStatus;

position?: ICursorPosition;

dragStartPosition?: ICursorPosition;

dragEndPosition?: ICursorPosition;

view?: Window;
}

const DEFAULT_POSITION = {
pageX: 0,
pageY: 0,
clientX: 0,
clientY: 0,
topPageX: 0,
topPageY: 0,
topClientX: 0,
topClientY: 0
};

const setCursorStyle = (contentWindow: Window, style: string) => {
const currentRoot = document?.getElementsByTagName?.('html')?.[0];
const root = contentWindow?.document?.getElementsByTagName('html')?.[0];
if (root && root.style.cursor !== style) {
root.style.cursor = style;
}
if (currentRoot && currentRoot.style.cursor !== style) {
currentRoot.style.cursor = style;
}
};

const calcPositionDelta = (end: ICursorPosition, start: ICursorPosition): ICursorPosition => {
return Object.keys(end || {}).reduce((buf, key) => {
if (isValidNumber(end?.[key]) && isValidNumber(start?.[key])) {
buf[key] = end[key] - start[key];
} else {
buf[key] = end[key];
}
return buf;
}, {});
};

export class Cursor {
engine: Engine;

type: CursorType | string = CursorType.Normal;

dragType: CursorDragType | string = CursorDragType.Move;

status: CursorStatus = CursorStatus.Normal;

position: ICursorPosition = DEFAULT_POSITION;

dragStartPosition: ICursorPosition;

dragEndPosition: ICursorPosition;

dragAtomDelta: ICursorPosition = DEFAULT_POSITION;

dragStartToCurrentDelta: ICursorPosition = DEFAULT_POSITION;

dragStartToEndDelta: ICursorPosition = DEFAULT_POSITION;

view: Window = globalThisPolyfill;

constructor(engine: Engine) {
this.engine = engine;
}
}
30 changes: 29 additions & 1 deletion src/app/core/models/engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
import { Workbench } from '@/app/core/models/workbench';
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';

export class Engine {
props: any;
id: string;

props: IEngineProps<Engine>;

cursor: Cursor;

workbench: Workbench;

// keyboard: Keyboard

screen: Screen;

constructor() {
this.init();
this.id = uid();
}

init() {
this.workbench = new Workbench(this);
this.screen = new Screen(this);
this.cursor = new Cursor(this);
// this.keyboard = new Keyboard(this)
}
}
27 changes: 27 additions & 0 deletions src/app/core/models/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface IHistoryProps<T> {
onPush?: (item: T) => void;
onRedo?: (item: T) => void;
onUndo?: (item: T) => void;
onGoto?: (item: T) => void;
}

export interface HistoryItem<T> {
data: T;
type?: string;
timestamp: number;
}

export interface ISerializable {
from(json: any): void; //导入数据
serialize(): any; //序列化模型,用于历史记录保存
}

export class History<T extends ISerializable = any> {
context: ISerializable;
props: IHistoryProps<HistoryItem<T>>;
current = 0;
history: HistoryItem<T>[] = [];
updateTimer = null;
maxSize = 100;
locking = false;
}
7 changes: 7 additions & 0 deletions src/app/core/models/hover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Operation } from './operation';
import { TreeNode } from './tree-node';

export class Hover {
node: TreeNode = null;
operation: Operation;
}
66 changes: 66 additions & 0 deletions src/app/core/models/move-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Operation } from './operation';
import { TreeNode } from './tree-node';
import { Viewport } from './viewport';
import { IPoint, Rect } from '../../shared/coordinate';

export enum ClosestPosition {
Before = 'BEFORE',
ForbidBefore = 'FORBID_BEFORE',
After = 'After',
ForbidAfter = 'FORBID_AFTER',
Upper = 'UPPER',
ForbidUpper = 'FORBID_UPPER',
Under = 'UNDER',
ForbidUnder = 'FORBID_UNDER',
Inner = 'INNER',
ForbidInner = 'FORBID_INNER',
InnerAfter = 'INNER_AFTER',
ForbidInnerAfter = 'FORBID_INNER_AFTER',
InnerBefore = 'INNER_BEFORE',
ForbidInnerBefore = 'FORBID_INNER_BEFORE',
Forbid = 'FORBID'
}

export interface IMoveHelperProps {
operation: Operation;
}

export interface IMoveHelperDragStartProps {
dragNodes: TreeNode[];
}

export interface IMoveHelperDragDropProps {
dropNode: TreeNode;
}
export interface IMoveHelperDragMoveProps {
touchNode: TreeNode;
point: IPoint;
}

export class MoveHelper {
operation: Operation;

rootNode: TreeNode;

dragNodes: TreeNode[] = [];

touchNode: TreeNode = null;

closestNode: TreeNode = null;

activeViewport: Viewport = null;

viewportClosestRect: Rect = null;

outlineClosestRect: Rect = null;

viewportClosestOffsetRect: Rect = null;

outlineClosestOffsetRect: Rect = null;

viewportClosestDirection: ClosestPosition = null;

outlineClosestDirection: ClosestPosition = null;

dragging = false;
}
32 changes: 32 additions & 0 deletions src/app/core/models/operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ITreeNode, TreeNode } from './tree-node';
import { Engine } from './engine';
import { Workspace } from './workspace';
import { Selection } from './selection';
import { Hover } from './hover';
import { TransformHelper } from './transform-helper';
import { MoveHelper } from './move-helper';

export interface IOperation {
tree?: ITreeNode;
selected?: string[];
}

export class Operation {
workspace: Workspace;

engine: Engine;

tree: TreeNode;

selection: Selection;

hover: Hover;

transformHelper: TransformHelper;

moveHelper: MoveHelper;

requests = {
snapshot: null
};
}
11 changes: 11 additions & 0 deletions src/app/core/models/selection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Operation } from './operation';

export interface ISelection {
selected?: string[];
operation?: Operation;
}
export class Selection {
operation: Operation;
selected: string[] = [];
indexes: Record<string, boolean> = {};
}
20 changes: 20 additions & 0 deletions src/app/core/models/snapline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TransformHelper } from './transform-helper';
import { TreeNode } from './tree-node';
import { ILineSegment, IPoint } from '../../shared/coordinate';
export type ISnapLineType = 'ruler' | 'space-block' | 'normal';

export type ISnapLine = ILineSegment & {
type?: ISnapLineType;
distance?: number;
id?: string;
refer?: TreeNode;
};
export class SnapLine {
_id: string;
type: ISnapLineType;
distance: number;
refer: TreeNode;
start: IPoint;
end: IPoint;
helper: TransformHelper;
}
22 changes: 22 additions & 0 deletions src/app/core/models/space-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Rect } from '../../shared/coordinate';
import { TransformHelper } from './transform-helper';
import { TreeNode } from './tree-node';
export type ISpaceBlockType = 'top' | 'right' | 'bottom' | 'left' | (string & {});

export interface ISpaceBlock {
id?: string;
refer?: TreeNode;
rect?: Rect;
distance?: number;
type?: ISpaceBlockType;
}

export type AroundSpaceBlock = Record<ISpaceBlockType, SpaceBlock>;
export class SpaceBlock {
_id: string;
distance: number;
refer: TreeNode;
helper: TransformHelper;
rect: Rect;
type: ISpaceBlockType;
}
Loading

0 comments on commit 663cbf9

Please sign in to comment.