-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathcontent-tab.component.ts
More file actions
131 lines (112 loc) · 3.58 KB
/
Copy pathcontent-tab.component.ts
File metadata and controls
131 lines (112 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
OnInit,
Output,
SimpleChanges,
ViewChild
} from '@angular/core';
import { View, ViewAction } from '@app/model';
@Component({
standalone: false,
selector: 'elements-content-tab',
templateUrl: 'content-tab.component.html',
styleUrls: ['content-tab.component.scss']
})
export class ElementContentTabComponent implements OnInit, OnChanges {
@Input() view: View;
@Output() onAction: EventEmitter<ViewAction> = new EventEmitter<ViewAction>();
@ViewChild('inputElement', { static: false }) inputElement: ElementRef;
public iconCls: string[] = [];
private shouldFocusInput = false;
private clickTimeout: any;
private static faIconCache = new Map<string, boolean>();
ngOnInit(): void {
this.updateIconClasses();
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.view) {
this.updateIconClasses();
}
}
close() {
const action = new ViewAction(this.view, 'close');
this.onAction.emit(action);
}
setActive() {
if (this.clickTimeout) {
clearTimeout(this.clickTimeout);
this.clickTimeout = null;
}
// 点击时需要额外确定当前是否是在编辑状态
if (this.view.editable) {
return;
}
// 延迟300毫秒,检查是否有双击事件
this.clickTimeout = setTimeout(() => {
const action = new ViewAction(this.view, 'active');
this.onAction.emit(action);
this.clickTimeout = null;
}, 100);
}
onDoubleClick() {
if (this.clickTimeout) {
clearTimeout(this.clickTimeout);
this.clickTimeout = null;
}
this.view.editable = true;
this.shouldFocusInput = true;
}
onBlur() {
setTimeout(() => {
this.view.editable = false;
}, 200);
}
private updateIconClasses(): void {
const iconKey = this.view?.asset?.type?.value || 'linux';
const faClass = `fa-${iconKey}`;
const fallbackClass = this.getFallbackIconClass(iconKey);
const hasFontAwesome = this.hasFontAwesomeIcon(faClass);
this.iconCls = hasFontAwesome ? ['fa', faClass] : [fallbackClass];
}
private getFallbackIconClass(iconKey: string): string {
const normalizedKey = iconKey || 'linux';
const overrides: Record<string, string> = {
general: 'switch_ico_docu',
website: 'website_ico_docu',
sqlserver: 'sqlserver_ico_docu',
postgresql: 'postgresql_ico_docu',
mysql: 'mysql_ico_docu',
mariadb: 'mariadb_ico_docu',
mongodb: 'mongodb_ico_docu',
webcloud: 'WebCloud_ico_docu',
web_cloud: 'WebCloud_ico_docu',
'web-cloud': 'WebCloud_ico_docu'
};
return overrides[normalizedKey] || `${normalizedKey}_ico_docu`;
}
private hasFontAwesomeIcon(iconClass: string): boolean {
const cached = ElementContentTabComponent.faIconCache.get(iconClass);
if (cached !== undefined) {
return cached;
}
if (typeof document === 'undefined' || typeof window === 'undefined') {
return true;
}
const el = document.createElement('i');
el.className = `view_icon fa ${iconClass}`;
el.style.position = 'absolute';
el.style.opacity = '0';
el.style.pointerEvents = 'none';
document.body.appendChild(el);
const content = window.getComputedStyle(el, '::before').getPropertyValue('content');
document.body.removeChild(el);
const normalizedContent = (content || '').replace(/['"]/g, '');
const hasContent = normalizedContent !== '' && normalizedContent !== 'none' && normalizedContent !== 'normal';
ElementContentTabComponent.faIconCache.set(iconClass, hasContent);
return hasContent;
}
}