Skip to content

Commit 6c17ef7

Browse files
author
winjo
committed
fix: 临时修复新建文件节点消失的问题
1 parent 235a4aa commit 6c17ef7

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

packages/alex/src/core/patch.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StaticServices } from '@ali/monaco-editor-core/esm/vs/editor/standalone
77
import { ModesRegistry } from '@ali/monaco-editor-core/esm/vs/editor/common/modes/modesRegistry';
88
import { DirtyDiffWidget } from '@ali/ide-scm/lib/browser/dirty-diff/dirty-diff-widget';
99
import { AbstractResourcePreferenceProvider } from '@ali/ide-preferences/lib/browser/abstract-resource-preference-provider';
10+
import { CompositeTreeNode, spliceArray } from '@ali/ide-components/lib/recycle-tree/tree/TreeNode';
1011

1112
export const disposableCollection: ((injector: Injector) => void)[] = [];
1213

@@ -33,3 +34,76 @@ Object.defineProperty(AbstractResourcePreferenceProvider.prototype, 'reset', {
3334
value: () => {},
3435
configurable: true,
3536
});
37+
38+
// TODO: kaitian 已修复 https://code.alipay.com/kaitian/ide-framework/pull_requests/578
39+
// 先临时修复,待发版后移除
40+
CompositeTreeNode.prototype.insertItem = function (this: any, item: any) {
41+
if (item.parent !== this) {
42+
item.mv(this, item.name);
43+
return;
44+
}
45+
if (this.children) {
46+
for (let i = 0; i < this.children.length; i++) {
47+
// path / id 是节点唯一标识
48+
if (this.children[i].path === item.path) {
49+
this.children[i] = item;
50+
return;
51+
}
52+
}
53+
}
54+
const branchSizeIncrease =
55+
1 + (item instanceof CompositeTreeNode && item.expanded ? (item as any)._branchSize : 0);
56+
if (this._children) {
57+
this._children.push(item);
58+
// @ts-ignore
59+
this._children.sort(this._tree.sortComparator || CompositeTreeNode.defaultSortComparator);
60+
}
61+
this._branchSize += branchSizeIncrease;
62+
let master: any = this;
63+
// 如果该节点无叶子节点,则继续往上查找合适的插入位置
64+
while (!master._flattenedBranch) {
65+
if (master.parent) {
66+
master = master.parent as CompositeTreeNode;
67+
master._branchSize += branchSizeIncrease;
68+
}
69+
}
70+
if (!this._children) {
71+
return;
72+
}
73+
let relativeInsertionIndex = this._children!.indexOf(item);
74+
let absInsertionIndex;
75+
const leadingSibling = this._children![relativeInsertionIndex - 1];
76+
if (leadingSibling) {
77+
const siblingIdx = master._flattenedBranch.indexOf(leadingSibling.id);
78+
// @ts-ignore
79+
relativeInsertionIndex =
80+
siblingIdx +
81+
(leadingSibling instanceof CompositeTreeNode && leadingSibling.expanded
82+
? (leadingSibling as any)._branchSize
83+
: 0);
84+
} else {
85+
relativeInsertionIndex = master._flattenedBranch.indexOf(this.id);
86+
}
87+
if (relativeInsertionIndex === -1) {
88+
if (this._branchSize === 1) {
89+
// 在空Tree中插入节点时,相对插入位置为0
90+
relativeInsertionIndex = 0;
91+
}
92+
}
93+
// 非空Tree情况下需要+1,为了容纳自身节点位置,在插入节点下方插入新增节点
94+
absInsertionIndex = relativeInsertionIndex + 1;
95+
// 空 Tree 情况下需要重置为 0,避免设置 Uint32Array 时超出范围
96+
if (master._flattenedBranch.length === 0) {
97+
absInsertionIndex = 0;
98+
}
99+
let branch: number[] = [item.id];
100+
101+
// @ts-ignore
102+
if (item instanceof CompositeTreeNode && item.expanded && item._flattenedBranch) {
103+
// @ts-ignore
104+
branch = branch.concat(item._flattenedBranch);
105+
// @ts-ignore
106+
(item as CompositeTreeNode).setFlattenedBranch(null);
107+
}
108+
master.setFlattenedBranch(spliceArray(master._flattenedBranch, absInsertionIndex, 0, branch));
109+
};

0 commit comments

Comments
 (0)