Skip to content

Commit 628c5ce

Browse files
author
qiaoyuwen
committed
feat: designer
1 parent 951e777 commit 628c5ce

File tree

10 files changed

+325
-1
lines changed

10 files changed

+325
-1
lines changed

packages/react-mobile/src/core/events/index.ts

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TreeNode } from '../../models';
2+
import { IEngineContext } from '../../types';
3+
4+
export interface IMutationNodeEventData {
5+
// 事件发生的数据源
6+
source: TreeNode | TreeNode[];
7+
// 事件发生的目标对象
8+
target: TreeNode | TreeNode[];
9+
// 事件发生的来源对象
10+
originSourceParents?: TreeNode | TreeNode[];
11+
}
12+
13+
export class AbstractMutationNodeEvent {
14+
data: IMutationNodeEventData;
15+
context: IEngineContext;
16+
constructor(data: IMutationNodeEventData) {
17+
this.data = data;
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ICustomEvent } from '@designable/shared';
2+
import { AbstractMutationNodeEvent } from './AbstractMutationNodeEvent';
3+
4+
export class InsertAfterEvent extends AbstractMutationNodeEvent implements ICustomEvent {
5+
type = 'insert:after';
6+
}

packages/react-mobile/src/core/events/mutation/index.ts

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { IEventProps } from '@/shared';
2+
import { Event, uid } from '@/shared';
3+
import type { IEngineProps } from '../types';
4+
5+
export class Engine extends Event {
6+
id: string;
7+
props: IEngineProps<Engine>;
8+
9+
constructor(props: IEngineProps<Engine>) {
10+
super(props as IEventProps);
11+
this.props = {
12+
...Engine.defaultProps,
13+
...props,
14+
};
15+
this.id = uid();
16+
}
17+
18+
static defaultProps: IEngineProps<Engine> = {
19+
effects: [],
20+
drivers: [],
21+
sourceIdAttrName: 'data-designer-source-id',
22+
nodeIdAttrName: 'data-designer-node-id',
23+
contentEditableAttrName: 'data-content-editable',
24+
contentEditableNodeIdAttrName: 'data-content-editable-node-id',
25+
clickStopPropagationAttrName: 'data-click-stop-propagation',
26+
nodeHelpersIdAttrName: 'data-designer-node-helpers-id',
27+
outlineNodeIdAttrName: 'data-designer-outline-node-id',
28+
// shortcuts: [],
29+
// defaultScreenType: ScreenType.PC,
30+
};
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { IEventProps } from '@/shared';
2+
3+
export type IEngineProps<T = Event> = IEventProps<T> & {
4+
sourceIdAttrName?: string; // 拖拽源Id的dom属性名
5+
nodeIdAttrName?: string; // 节点Id的dom属性名
6+
contentEditableAttrName?: string; // 原地编辑属性名
7+
contentEditableNodeIdAttrName?: string; // 原地编辑指定Node Id属性名
8+
clickStopPropagationAttrName?: string; // 点击阻止冒泡属性
9+
outlineNodeIdAttrName?: string; // 大纲树节点ID的dom属性名
10+
nodeHelpersIdAttrName?: string; // 节点工具栏属性名
11+
// defaultScreenType?: ScreenType; // 默认屏幕类型
12+
// shortcuts?: Shortcut[]; // 快捷键
13+
};
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/* eslint-disable consistent-return */
2+
/* eslint-disable @typescript-eslint/no-unused-vars */
3+
/* eslint-disable @typescript-eslint/ban-types */
4+
/* eslint-disable no-restricted-syntax */
5+
/* eslint-disable no-plusplus */
6+
/* eslint-disable no-nested-ternary */
7+
import { isArr, isObj, isStr } from './types';
8+
9+
type EachArrayIterator<T> = (currentValue: T, key: number) => void | boolean;
10+
type EachStringIterator = (currentValue: string, key: number) => void | boolean;
11+
type EachObjectIterator<T = any> = (currentValue: T, key: string) => void | boolean;
12+
type MapArrayIterator<TItem, TResult> = (currentValue: TItem, key: number) => TResult;
13+
type MapStringIterator<TResult> = (currentValue: string, key: number) => TResult;
14+
type MapObjectIterator<TItem, TResult> = (currentValue: TItem, key: string) => TResult;
15+
type MemoArrayIterator<T, U> = (previousValue: U, currentValue: T, key: number) => U;
16+
type MemoStringIterator<T> = (previousValue: T, currentValue: string, key: number) => T;
17+
type MemoObjectIterator<TValue, TResult> = (
18+
previousValue: TResult,
19+
currentValue: TValue,
20+
key: string,
21+
) => TResult;
22+
23+
export const toArr = (val: any): any[] => (isArr(val) ? val : val ? [val] : []);
24+
export function each(val: string, iterator: EachStringIterator, revert?: boolean): void;
25+
export function each<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): void;
26+
export function each<T extends {}, TValue = T[keyof T]>(
27+
val: T,
28+
iterator: EachObjectIterator<TValue>,
29+
revert?: boolean,
30+
): void;
31+
export function each(val: any, iterator: any, revert?: boolean): void {
32+
if (isArr(val) || isStr(val)) {
33+
if (revert) {
34+
for (let i: number = val.length - 1; i >= 0; i--) {
35+
if (iterator(val[i], i) === false) {
36+
return;
37+
}
38+
}
39+
} else {
40+
for (let i = 0; i < val.length; i++) {
41+
if (iterator(val[i], i) === false) {
42+
return;
43+
}
44+
}
45+
}
46+
} else if (isObj(val)) {
47+
let key: string;
48+
for (key in val) {
49+
if (Object.hasOwnProperty.call(val, key)) {
50+
if (iterator(val[key], key) === false) {
51+
return;
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
export function map<T>(val: string, iterator: MapStringIterator<T>, revert?: boolean): any;
59+
export function map<TItem, TResult>(
60+
val: TItem[],
61+
iterator: MapArrayIterator<TItem, TResult>,
62+
revert?: boolean,
63+
): any;
64+
export function map<T extends {}, TResult>(
65+
val: T,
66+
iterator: MapObjectIterator<T[keyof T], TResult>,
67+
revert?: boolean,
68+
): any;
69+
export function map(val: any, iterator: any, revert?: boolean): any {
70+
const res = isArr(val) || isStr(val) ? [] : {};
71+
each(
72+
val,
73+
(item, key) => {
74+
const value = iterator(item, key);
75+
if (isArr(res)) {
76+
(res as any).push(value);
77+
} else {
78+
res[key] = value;
79+
}
80+
},
81+
revert,
82+
);
83+
return res;
84+
}
85+
86+
export function reduce<T, U>(
87+
val: T[],
88+
iterator: MemoArrayIterator<T, U>,
89+
accumulator?: U,
90+
revert?: boolean,
91+
): U;
92+
export function reduce<T>(
93+
val: string,
94+
iterator: MemoStringIterator<T>,
95+
accumulator?: T,
96+
revert?: boolean,
97+
): T;
98+
export function reduce<T extends {}, TValue = T[keyof T], TResult = any>(
99+
val: T,
100+
iterator: MemoObjectIterator<TValue, TResult>,
101+
accumulator?: TResult,
102+
revert?: boolean,
103+
): TResult;
104+
export function reduce(val: any, iterator: any, accumulator?: any, revert?: boolean): any {
105+
let result = accumulator;
106+
each(
107+
val,
108+
(item, key) => {
109+
result = iterator(result, item, key);
110+
},
111+
revert,
112+
);
113+
return result;
114+
}
115+
116+
export function every<T extends string>(
117+
val: T,
118+
iterator: EachStringIterator,
119+
revert?: boolean,
120+
): boolean;
121+
export function every<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean;
122+
export function every<T extends {}, TValue = T[keyof T]>(
123+
val: T,
124+
iterator: EachObjectIterator,
125+
revert?: boolean,
126+
): boolean;
127+
export function every(val: any, iterator: any, revert?: boolean): boolean {
128+
let res = true;
129+
each(
130+
val,
131+
(item, key) => {
132+
if (!iterator(item, key)) {
133+
res = false;
134+
return false;
135+
}
136+
},
137+
revert,
138+
);
139+
return res;
140+
}
141+
142+
export function some<T extends string>(
143+
val: T,
144+
iterator: EachStringIterator,
145+
revert?: boolean,
146+
): boolean;
147+
export function some<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean;
148+
export function some<T extends {}, TValue = T[keyof T]>(
149+
val: T,
150+
iterator: EachObjectIterator,
151+
revert?: boolean,
152+
): boolean;
153+
export function some(val: any, iterator: any, revert?: boolean): boolean {
154+
let res = false;
155+
each(
156+
val,
157+
(item, key) => {
158+
if (iterator(item, key)) {
159+
res = true;
160+
return false;
161+
}
162+
},
163+
revert,
164+
);
165+
return res;
166+
}
167+
168+
export function findIndex<T extends string>(
169+
val: T,
170+
iterator: EachStringIterator,
171+
revert?: boolean,
172+
): number;
173+
export function findIndex<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): number;
174+
export function findIndex<T extends {}, TValue = T[keyof T]>(
175+
val: T,
176+
iterator: EachObjectIterator,
177+
revert?: boolean,
178+
): keyof T;
179+
export function findIndex(val: any, iterator: any, revert?: boolean): string | number {
180+
let res: number | string = -1;
181+
each(
182+
val,
183+
(item, key) => {
184+
if (iterator(item, key)) {
185+
res = key;
186+
return false;
187+
}
188+
},
189+
revert,
190+
);
191+
return res;
192+
}
193+
194+
export function find<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): any;
195+
export function find<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): T;
196+
export function find<T extends {}, TValue = T[keyof T]>(
197+
val: T,
198+
iterator: EachObjectIterator,
199+
revert?: boolean,
200+
): T[keyof T];
201+
export function find(val: any, iterator: any, revert?: boolean): any {
202+
let res: any;
203+
each(
204+
val,
205+
(item, key) => {
206+
if (iterator(item, key)) {
207+
res = item;
208+
return false;
209+
}
210+
},
211+
revert,
212+
);
213+
return res;
214+
}
215+
216+
export function includes<T extends string>(
217+
val: T,
218+
searchElement: string,
219+
revert?: boolean,
220+
): boolean;
221+
export function includes<T>(val: T[], searchElement: T, revert?: boolean): boolean;
222+
export function includes(val: any, searchElement: any, revert?: boolean) {
223+
if (isStr(val)) return val.includes(searchElement);
224+
return some(val, (item) => item === searchElement, revert);
225+
}
226+
227+
export function includesWith<T extends string>(val: T, search: (item: string) => boolean): boolean;
228+
export function includesWith<T>(val: T[], search: (item: T) => boolean): boolean;
229+
export function includesWith(val: any, search: any) {
230+
if (isArr(val)) {
231+
return val.some((item) => search(item));
232+
}
233+
return false;
234+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './event';
2+
export * from './subscribable';
3+
export * from './types';
4+
export * from './uid';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable no-bitwise */
2+
/* eslint-disable no-plusplus */
3+
4+
let IDX = 36;
5+
let HEX = '';
6+
while (IDX--) HEX += IDX.toString(36);
7+
8+
export function uid(len?: number) {
9+
let str = '';
10+
let num = len || 11;
11+
while (num--) str += HEX[(Math.random() * 36) | 0];
12+
return str;
13+
}

packages/react-mobile/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
"noEmit": false,
1313
"declaration": true,
1414
"skipLibCheck": true,
15-
"moduleResolution": "node"
15+
"moduleResolution": "node",
16+
"baseUrl": "./",
17+
"paths": {
18+
"@/*": ["src/*"]
19+
}
1620
},
1721
"include": ["src/**/*", "jest.config.js", ".eslintrc.js"],
1822
"exclude": ["dist"]

0 commit comments

Comments
 (0)