Skip to content

Commit 459d867

Browse files
zhangtengjinwewoor
authored andcommitted
feat: extract randomId
extract randomId
1 parent b08f56f commit 459d867

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

src/common/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ export function mergeFunctions(...funcs) {
2828
funcs.filter((fn) => !!fn).forEach((fn) => fn?.(...args));
2929
};
3030
}
31+
32+
export function randomId() {
33+
return (Date.now() + Math.round(Math.random() * 1000));
34+
}

src/controller/explorer/folderTree.tsx

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ import { EditorController } from 'mo/controller/editor';
77
import { IMenuItem } from 'mo/components/menu';
88
import Modal from 'mo/components/dialog';
99
import {
10-
IFolderInputEvent, TreeNodeModel,
11-
baseContextMenu, rootFolderContextMenu, fileContextMenu,
12-
NEW_FILE_COMMAND_ID, NEW_FOLDER_COMMAND_ID, RENAME_COMMAND_ID,
13-
REMOVE_COMMAND_ID, DELETE_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID,
14-
ADD_ROOT_FOLDER_COMMAND_ID
10+
IFolderInputEvent,
11+
TreeNodeModel,
12+
baseContextMenu,
13+
rootFolderContextMenu,
14+
fileContextMenu,
15+
NEW_FILE_COMMAND_ID,
16+
NEW_FOLDER_COMMAND_ID,
17+
RENAME_COMMAND_ID,
18+
REMOVE_COMMAND_ID,
19+
DELETE_COMMAND_ID,
20+
OPEN_TO_SIDE_COMMAND_ID,
21+
ADD_ROOT_FOLDER_COMMAND_ID,
1522
} from 'mo/model';
1623
const confirm = Modal.confirm;
1724

@@ -41,7 +48,7 @@ export class FolderTreeController
4148
this.initView();
4249
}
4350

44-
private initView() { }
51+
private initView() {}
4552

4653
public readonly onSelectFile = (file: ITreeNodeItem, isAuto?: boolean) => {
4754
const tabData = {
@@ -164,9 +171,7 @@ export class FolderTreeController
164171
break;
165172
}
166173
case FileTypes.rootFolder: {
167-
menu = baseContextMenu.concat(
168-
rootFolderContextMenu
169-
);;
174+
menu = baseContextMenu.concat(rootFolderContextMenu);
170175
break;
171176
}
172177
default:

src/model/workbench/explorer.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { injectable } from 'tsyringe';
44
import { ITreeNodeItem, FileType, FileTypes } from 'mo/components/tree';
55
import { IMenuItem } from 'mo/components/menu';
66
import { IActivityBarItem } from './activityBar';
7-
7+
import { randomId } from 'mo/common/utils';
88
export enum ExplorerEvent {
99
onClick = 'explorer.onClick',
1010
}
@@ -99,7 +99,7 @@ export const fileContextMenu = [
9999
id: OPEN_TO_SIDE_COMMAND_ID,
100100
name: 'Open to the Side',
101101
},
102-
]
102+
];
103103
// Sample folder panel area ContextMenu
104104
export const folderPanelContextMenu = [
105105
{
@@ -180,13 +180,13 @@ export class TreeNodeModel implements ITreeNodeItem {
180180
icon = '',
181181
modify = false,
182182
} = props;
183-
(this.fileType = fileType),
184-
(this.modify = modify),
185-
(this.name = name),
186-
(this.id = id || Math.random() * 10 + 1),
187-
(this.location = location),
188-
(this.children = children),
189-
(this.icon = icon);
183+
this.fileType = fileType;
184+
this.modify = modify;
185+
this.name = name;
186+
this.id = id || randomId();
187+
this.location = location;
188+
this.children = children;
189+
this.icon = icon;
190190
}
191191
}
192192

src/services/workbench/explorerService.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { TreeViewUtil, searchById } from '../helper';
1010
import { ITreeNodeItem, FileTypes, FileType } from 'mo/components/tree';
1111
import { editorService } from 'mo';
1212
import { TreeNodeModel } from 'mo/model';
13+
import { randomId } from 'mo/common/utils';
1314

1415
export interface IExplorerService extends Component<IExplorer> {
1516
addPanel(panel: IPanelItem | IPanelItem[]): void;
@@ -273,7 +274,7 @@ export class ExplorerService
273274
const tree = new TreeViewUtil(currentRootFolder);
274275
if (!id) {
275276
const tabData = {
276-
id: `${Math.random() * 10 + 1}`,
277+
id: `${randomId()}`,
277278
name: `Untitled`,
278279
data: {
279280
modified: false,

0 commit comments

Comments
 (0)