mirror of https://github.com/go-gitea/gitea.git
Refactor global init code and add more comments (#33755)
Follow up #33748 Now there are 3 "global" functions: * registerGlobalSelectorFunc: for all elements matching the selector, eg: `.ui.dropdown` * registerGlobalInitFunc: for `data-global-init="initInputAutoFocusEnd"` * registerGlobalEventFunc: for `data-global-click="onCommentReactionButtonClick"` And introduce `initGlobalInput` to replace old `initAutoFocusEnd` and `attachDirAuto`, use `data-global-init` to replace fragile `.js-autofocus-end` selector. Another benefit is that by the new approach, no matter how many times `registerGlobalInitFunc` is called, we only need to do one "querySelectorAll" in the last step, it could slightly improve the performance.pull/33768/head^2
parent
5cbdf83f70
commit
27bf63ad20
@ -1,20 +1,20 @@
|
||||
{{if .Flash.ErrorMsg}}
|
||||
{{- if .Flash.ErrorMsg -}}
|
||||
<div class="ui negative message flash-message flash-error">
|
||||
<p>{{.Flash.ErrorMsg | SanitizeHTML}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Flash.SuccessMsg}}
|
||||
{{- end -}}
|
||||
{{- if .Flash.SuccessMsg -}}
|
||||
<div class="ui positive message flash-message flash-success">
|
||||
<p>{{.Flash.SuccessMsg | SanitizeHTML}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Flash.InfoMsg}}
|
||||
{{- end -}}
|
||||
{{- if .Flash.InfoMsg -}}
|
||||
<div class="ui info message flash-message flash-info">
|
||||
<p>{{.Flash.InfoMsg | SanitizeHTML}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Flash.WarningMsg}}
|
||||
{{- end -}}
|
||||
{{- if .Flash.WarningMsg -}}
|
||||
<div class="ui warning message flash-message flash-warning">
|
||||
<p>{{.Flash.WarningMsg | SanitizeHTML}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{- end -}}
|
||||
|
@ -1,6 +0,0 @@
|
||||
export function initAutoFocusEnd() {
|
||||
for (const el of document.querySelectorAll<HTMLInputElement>('.js-autofocus-end')) {
|
||||
el.focus(); // expects only one such element on one page. If there are many, then the last one gets the focus.
|
||||
el.setSelectionRange(el.value.length, el.value.length);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
export class InitPerformanceTracer {
|
||||
results: {name: string, dur: number}[] = [];
|
||||
recordCall(name: string, func: ()=>void) {
|
||||
const start = performance.now();
|
||||
func();
|
||||
this.results.push({name, dur: performance.now() - start});
|
||||
}
|
||||
printResults() {
|
||||
this.results = this.results.sort((a, b) => b.dur - a.dur);
|
||||
for (let i = 0; i < 20 && i < this.results.length; i++) {
|
||||
console.info(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function callInitFunctions(functions: (() => any)[]): InitPerformanceTracer | null {
|
||||
// Start performance trace by accessing a URL by "https://localhost/?_ui_performance_trace=1" or "https://localhost/?key=value&_ui_performance_trace=1"
|
||||
// It is a quick check, no side effect so no need to do slow URL parsing.
|
||||
const perfTracer = !window.location.search.includes('_ui_performance_trace=1') ? null : new InitPerformanceTracer();
|
||||
if (perfTracer) {
|
||||
for (const func of functions) perfTracer.recordCall(func.name, func);
|
||||
} else {
|
||||
for (const func of functions) func();
|
||||
}
|
||||
return perfTracer;
|
||||
}
|
Loading…
Reference in New Issue