mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Add types to various low-level functions (#31781)
Adds types to various low-level modules. All changes are type-only, no runtime changes. `tsc` now reports 38 less errors. One problem was that `@types/sortablejs` does not accept promise return in its functions which triggered the linter, so I disabled the rules on those line.
This commit is contained in:
		| @@ -1,7 +1,9 @@ | ||||
| import {isDocumentFragmentOrElementNode} from '../utils/dom.ts'; | ||||
|  | ||||
| type DirElement = HTMLInputElement | HTMLTextAreaElement; | ||||
|  | ||||
| // for performance considerations, it only uses performant syntax | ||||
| function attachDirAuto(el) { | ||||
| function attachDirAuto(el: DirElement) { | ||||
|   if (el.type !== 'hidden' && | ||||
|       el.type !== 'checkbox' && | ||||
|       el.type !== 'radio' && | ||||
| @@ -18,10 +20,12 @@ export function initDirAuto() { | ||||
|       const mutation = mutationList[i]; | ||||
|       const len = mutation.addedNodes.length; | ||||
|       for (let i = 0; i < len; i++) { | ||||
|         const addedNode = mutation.addedNodes[i]; | ||||
|         const addedNode = mutation.addedNodes[i] as HTMLElement; | ||||
|         if (!isDocumentFragmentOrElementNode(addedNode)) continue; | ||||
|         if (addedNode.nodeName === 'INPUT' || addedNode.nodeName === 'TEXTAREA') attachDirAuto(addedNode); | ||||
|         const children = addedNode.querySelectorAll('input, textarea'); | ||||
|         if (addedNode.nodeName === 'INPUT' || addedNode.nodeName === 'TEXTAREA') { | ||||
|           attachDirAuto(addedNode as DirElement); | ||||
|         } | ||||
|         const children = addedNode.querySelectorAll<DirElement>('input, textarea'); | ||||
|         const len = children.length; | ||||
|         for (let childIdx = 0; childIdx < len; childIdx++) { | ||||
|           attachDirAuto(children[childIdx]); | ||||
| @@ -30,7 +34,7 @@ export function initDirAuto() { | ||||
|     } | ||||
|   }); | ||||
|  | ||||
|   const docNodes = document.querySelectorAll('input, textarea'); | ||||
|   const docNodes = document.querySelectorAll<DirElement>('input, textarea'); | ||||
|   const len = docNodes.length; | ||||
|   for (let i = 0; i < len; i++) { | ||||
|     attachDirAuto(docNodes[i]); | ||||
|   | ||||
| @@ -1,4 +1,6 @@ | ||||
| export async function createSortable(el, opts = {}) { | ||||
| import type {SortableOptions} from 'sortablejs'; | ||||
|  | ||||
| export async function createSortable(el, opts: {handle?: string} & SortableOptions = {}) { | ||||
|   const {Sortable} = await import(/* webpackChunkName: "sortablejs" */'sortablejs'); | ||||
|  | ||||
|   return new Sortable(el, { | ||||
| @@ -15,5 +17,5 @@ export async function createSortable(el, opts = {}) { | ||||
|       opts.onUnchoose?.(e); | ||||
|     }, | ||||
|     ...opts, | ||||
|   }); | ||||
|   } satisfies SortableOptions); | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import {reactive} from 'vue'; | ||||
| import type {Reactive} from 'vue'; | ||||
|  | ||||
| let diffTreeStoreReactive; | ||||
| let diffTreeStoreReactive: Reactive<Record<string, any>>; | ||||
| export function diffTreeStore() { | ||||
|   if (!diffTreeStoreReactive) { | ||||
|     diffTreeStoreReactive = reactive(window.config.pageData.diffFileInfo); | ||||
|   | ||||
| @@ -1,16 +1,22 @@ | ||||
| import tippy, {followCursor} from 'tippy.js'; | ||||
| import {isDocumentFragmentOrElementNode} from '../utils/dom.ts'; | ||||
| import {formatDatetime} from '../utils/time.ts'; | ||||
| import type {Content, Instance, Props} from 'tippy.js'; | ||||
|  | ||||
| const visibleInstances = new Set(); | ||||
| type TippyOpts = { | ||||
|   role?: string, | ||||
|   theme?: 'default' | 'tooltip' | 'menu' | 'box-with-header' | 'bare', | ||||
| } & Partial<Props>; | ||||
|  | ||||
| const visibleInstances = new Set<Instance>(); | ||||
| const arrowSvg = `<svg width="16" height="7"><path d="m0 7 8-7 8 7Z" class="tippy-svg-arrow-outer"/><path d="m0 8 8-7 8 7Z" class="tippy-svg-arrow-inner"/></svg>`; | ||||
|  | ||||
| export function createTippy(target, opts = {}) { | ||||
| export function createTippy(target: Element, opts: TippyOpts = {}) { | ||||
|   // the callback functions should be destructured from opts, | ||||
|   // because we should use our own wrapper functions to handle them, do not let the user override them | ||||
|   const {onHide, onShow, onDestroy, role, theme, arrow, ...other} = opts; | ||||
|  | ||||
|   const instance = tippy(target, { | ||||
|   const instance: Instance = tippy(target, { | ||||
|     appendTo: document.body, | ||||
|     animation: false, | ||||
|     allowHTML: false, | ||||
| @@ -18,15 +24,15 @@ export function createTippy(target, opts = {}) { | ||||
|     interactiveBorder: 20, | ||||
|     ignoreAttributes: true, | ||||
|     maxWidth: 500, // increase over default 350px | ||||
|     onHide: (instance) => { | ||||
|     onHide: (instance: Instance) => { | ||||
|       visibleInstances.delete(instance); | ||||
|       return onHide?.(instance); | ||||
|     }, | ||||
|     onDestroy: (instance) => { | ||||
|     onDestroy: (instance: Instance) => { | ||||
|       visibleInstances.delete(instance); | ||||
|       return onDestroy?.(instance); | ||||
|     }, | ||||
|     onShow: (instance) => { | ||||
|     onShow: (instance: Instance) => { | ||||
|       // hide other tooltip instances so only one tooltip shows at a time | ||||
|       for (const visibleInstance of visibleInstances) { | ||||
|         if (visibleInstance.props.role === 'tooltip') { | ||||
| @@ -43,7 +49,7 @@ export function createTippy(target, opts = {}) { | ||||
|     theme: theme || role || 'default', | ||||
|     plugins: [followCursor], | ||||
|     ...other, | ||||
|   }); | ||||
|   } satisfies Partial<Props>); | ||||
|  | ||||
|   if (role === 'menu') { | ||||
|     target.setAttribute('aria-haspopup', 'true'); | ||||
| @@ -58,12 +64,8 @@ export function createTippy(target, opts = {}) { | ||||
|  * If the target element has no content, then no tooltip will be attached, and it returns null. | ||||
|  * | ||||
|  * Note: "tooltip" doesn't equal to "tippy". "tooltip" means a auto-popup content, it just uses tippy as the implementation. | ||||
|  * | ||||
|  * @param target {HTMLElement} | ||||
|  * @param content {null|string} | ||||
|  * @returns {null|tippy} | ||||
|  */ | ||||
| function attachTooltip(target, content = null) { | ||||
| function attachTooltip(target: Element, content: Content = null) { | ||||
|   switchTitleToTooltip(target); | ||||
|  | ||||
|   content = content ?? target.getAttribute('data-tooltip-content'); | ||||
| @@ -84,7 +86,7 @@ function attachTooltip(target, content = null) { | ||||
|     placement: target.getAttribute('data-tooltip-placement') || 'top-start', | ||||
|     followCursor: target.getAttribute('data-tooltip-follow-cursor') || false, | ||||
|     ...(target.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true, aria: {content: 'describedby', expanded: false}} : {}), | ||||
|   }; | ||||
|   } as TippyOpts; | ||||
|  | ||||
|   if (!target._tippy) { | ||||
|     createTippy(target, props); | ||||
| @@ -94,7 +96,7 @@ function attachTooltip(target, content = null) { | ||||
|   return target._tippy; | ||||
| } | ||||
|  | ||||
| function switchTitleToTooltip(target) { | ||||
| function switchTitleToTooltip(target: Element) { | ||||
|   let title = target.getAttribute('title'); | ||||
|   if (title) { | ||||
|     // apply custom formatting to relative-time's tooltips | ||||
| @@ -118,16 +120,15 @@ function switchTitleToTooltip(target) { | ||||
|  * According to https://www.w3.org/TR/DOM-Level-3-Events/#events-mouseevent-event-order , mouseover event is fired before mouseenter event | ||||
|  * Some browsers like PaleMoon don't support "addEventListener('mouseenter', capture)" | ||||
|  * The tippy by default uses "mouseenter" event to show, so we use "mouseover" event to switch to tippy | ||||
|  * @param e {Event} | ||||
|  */ | ||||
| function lazyTooltipOnMouseHover(e) { | ||||
| function lazyTooltipOnMouseHover(e: MouseEvent) { | ||||
|   e.target.removeEventListener('mouseover', lazyTooltipOnMouseHover, true); | ||||
|   attachTooltip(this); | ||||
| } | ||||
|  | ||||
| // Activate the tooltip for current element. | ||||
| // If the element has no aria-label, use the tooltip content as aria-label. | ||||
| function attachLazyTooltip(el) { | ||||
| function attachLazyTooltip(el: Element) { | ||||
|   el.addEventListener('mouseover', lazyTooltipOnMouseHover, {capture: true}); | ||||
|  | ||||
|   // meanwhile, if the element has no aria-label, use the tooltip content as aria-label | ||||
| @@ -140,15 +141,15 @@ function attachLazyTooltip(el) { | ||||
| } | ||||
|  | ||||
| // Activate the tooltip for all children elements. | ||||
| function attachChildrenLazyTooltip(target) { | ||||
|   for (const el of target.querySelectorAll('[data-tooltip-content]')) { | ||||
| function attachChildrenLazyTooltip(target: Element) { | ||||
|   for (const el of target.querySelectorAll<Element>('[data-tooltip-content]')) { | ||||
|     attachLazyTooltip(el); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export function initGlobalTooltips() { | ||||
|   // use MutationObserver to detect new "data-tooltip-content" elements added to the DOM, or attributes changed | ||||
|   const observerConnect = (observer) => observer.observe(document, { | ||||
|   const observerConnect = (observer: MutationObserver) => observer.observe(document, { | ||||
|     subtree: true, | ||||
|     childList: true, | ||||
|     attributeFilter: ['data-tooltip-content', 'title'], | ||||
| @@ -159,7 +160,7 @@ export function initGlobalTooltips() { | ||||
|     for (const mutation of [...mutationList, ...pending]) { | ||||
|       if (mutation.type === 'childList') { | ||||
|         // mainly for Vue components and AJAX rendered elements | ||||
|         for (const el of mutation.addedNodes) { | ||||
|         for (const el of mutation.addedNodes as NodeListOf<Element>) { | ||||
|           if (!isDocumentFragmentOrElementNode(el)) continue; | ||||
|           attachChildrenLazyTooltip(el); | ||||
|           if (el.hasAttribute('data-tooltip-content')) { | ||||
| @@ -167,7 +168,7 @@ export function initGlobalTooltips() { | ||||
|           } | ||||
|         } | ||||
|       } else if (mutation.type === 'attributes') { | ||||
|         attachTooltip(mutation.target); | ||||
|         attachTooltip(mutation.target as Element); | ||||
|       } | ||||
|     } | ||||
|     observerConnect(observer); | ||||
| @@ -177,7 +178,7 @@ export function initGlobalTooltips() { | ||||
|   attachChildrenLazyTooltip(document.documentElement); | ||||
| } | ||||
|  | ||||
| export function showTemporaryTooltip(target, content) { | ||||
| export function showTemporaryTooltip(target: Element, content: Content) { | ||||
|   // if the target is inside a dropdown, don't show the tooltip because when the dropdown | ||||
|   // closes, the tippy would be pushed unsightly to the top-left of the screen like seen | ||||
|   // on the issue comment menu. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user