Repository navigation
Expand file tree
/
Copy pathutil.ts
More file actions
341 lines (304 loc) · 10.8 KB
/
Copy pathutil.ts
File metadata and controls
341 lines (304 loc) · 10.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// import Cookies from 'js-cookie'
// cookie保存的天数
import config from '@/config'
import { forEach, objEqual } from '@/libs/tools'
import type { AppRoute, MenuItem, ShowTitleOptions } from '@/types'
const { title } = config
// export const TOKEN_KEY = 'token'
// export const setToken = (token) => {
// Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
// }
// export const getToken = () => {
// const token = Cookies.get(TOKEN_KEY)
// if (token) return token
// else return false
// }
export const hasChild = (item: MenuItem): boolean => {
return !!(item.children && item.children.length !== 0)
}
const showThisMenuEle = (): boolean => {
return true
// if (item.meta && item.meta.access && item.meta.access.length) {
// if (hasOneOf(item.meta.access, access)) return true
// else return false
// } else return true
}
export const getMenuByRouter = (list: AppRoute[], access: string[]): MenuItem[] => {
const res: MenuItem[] = []
forEach(list, (item: AppRoute) => {
if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
const obj: MenuItem = {
name: item.name || '',
path: item.path,
meta: item.meta
}
if ((hasChild(item as MenuItem) || item.meta?.showAlways) && showThisMenuEle(item as MenuItem, access)) {
obj.children = getMenuByRouter(item.children || [], access)
}
if (item.meta?.href) obj.href = item.meta.href
res.push(obj)
}
})
return res
}
export const getBreadCrumbList = (route: { matched?: Array<{ name?: string; meta?: RouteMeta }> }, homeRoute?: AppRoute): MenuItem[] => {
const routeMetched = route.matched || []
// 如果没有提供 homeRoute,只返回非首页的面包屑
if (!homeRoute) {
const res: MenuItem[] = []
for (const item of routeMetched) {
if (item && item.meta && item.meta.title) {
const meta = { ...item.meta }
const obj: MenuItem = {
name: item.name || '',
meta: meta
}
res.push(obj)
}
}
return res
}
const homeItem: MenuItem = {
name: homeRoute.name || '',
path: homeRoute.path,
meta: { ...homeRoute.meta, icon: homeRoute.meta?.icon }
}
// 检查是否应该返回首页
if (routeMetched.some((item: { name?: string }) => item && item.name === homeRoute.name)) {
return [homeItem]
}
// 过滤和映射有效的路由项
const res: MenuItem[] = []
for (const item of routeMetched) {
if (item && item.meta && item.meta.title) {
const meta = { ...item.meta }
const obj: MenuItem = {
name: item.name || '',
meta: meta
}
res.push(obj)
}
}
res.unshift(homeItem)
return res
}
export const getHomeRoute = (routers: AppRoute[], homeName = 'home'): AppRoute | undefined => {
let i = -1
const len = routers.length
let homeRoute: AppRoute | undefined
while (++i < len) {
const item = routers[i]
if (item.children && item.children.length) {
const res = getHomeRoute(item.children, homeName)
if (res) return res
} else {
if (item.name === homeName) homeRoute = item
}
}
return homeRoute
}
export const showTitle = (item: MenuItem, vm?: ShowTitleOptions): string => {
const { title } = item.meta || {}
if (!title) return ''
// 启用多语言支持
if (title.includes('{{') && title.includes('}}')) {
return title.replace(/({{[\s\S]+?}})/, (m, str) => str.split(/[{}]/)[1].trim().replace(/\./g, '-'))
} else if (item.meta?.__titleIsFunction__) {
return (item as MenuItem & { __titleIsFunction__: (vm?: ShowTitleOptions) => string }).__titleIsFunction__(vm)
} else {
// 使用 i18n 翻译
return (vm && (vm as ShowTitleOptions).$t) ? (vm as ShowTitleOptions).$t(title) : title
}
}
/**
* @param {Array} list 标签列表
* @param {String} name 当前关闭的标签的name
*/
export const getNextRoute = (list: MenuItem[], route: AppRoute): AppRoute => {
let res = {} as AppRoute
if (list.length === 2) {
res = getHomeRoute([route]) || {} as AppRoute
} else {
const index = list.findIndex(item => routeEqual(item, route))
if (index === list.length - 1) res = list[list.length - 2] as unknown as AppRoute
else res = list[index + 1] as unknown as AppRoute
}
return res
}
export const routeEqual = (route1: MenuItem, route2: AppRoute): boolean => {
const params1 = route1.params || {}
const params2 = route2.params || {}
const query1 = route1.query || {}
const query2 = route2.query || {}
return (route1.name === route2.name) && objEqual(params1, params2) && objEqual(query1, query2)
}
export const localSave = (key: string, value: string): void => {
localStorage.setItem(key, value)
}
export const localRead = (key: string): string => {
return localStorage.getItem(key) || ''
}
// scrollTop animation
export const scrollTop = (el: HTMLElement | Window, from = 0, to: number, duration = 500, endCallback?: () => void): void => {
if (!window.requestAnimationFrame) {
(window as Window & typeof globalThis & { webkitRequestAnimationFrame?: FrameRequestCallback, mozRequestAnimationFrame?: FrameRequestCallback, msRequestAnimationFrame?: FrameRequestCallback }).requestAnimationFrame = (
(window as Window & typeof globalThis & { webkitRequestAnimationFrame?: FrameRequestCallback, mozRequestAnimationFrame?: FrameRequestCallback, msRequestAnimationFrame?: FrameRequestCallback }).webkitRequestAnimationFrame ||
(window as Window & typeof globalThis & { webkitRequestAnimationFrame?: FrameRequestCallback, mozRequestAnimationFrame?: FrameRequestCallback, msRequestAnimationFrame?: FrameRequestCallback }).mozRequestAnimationFrame ||
(window as Window & typeof globalThis & { webkitRequestAnimationFrame?: FrameRequestCallback, mozRequestAnimationFrame?: FrameRequestCallback, msRequestAnimationFrame?: FrameRequestCallback }).msRequestAnimationFrame ||
function (callback: () => void) {
return window.setTimeout(callback, 1000 / 60)
}
)
}
const difference = Math.abs(from - to)
const step = Math.ceil(difference / duration * 50)
const scroll = (start: number, end: number, stepNum: number): void => {
if (start === end) {
endCallback && endCallback()
return
}
let d = (start + stepNum > end) ? end : start + stepNum
if (start > end) {
d = (start - stepNum < end) ? end : start - stepNum
}
if (el === window) {
(el as Window).scrollTo(d, d)
} else {
(el as HTMLElement).scrollTop = d
}
window.requestAnimationFrame(() => scroll(d, end, stepNum))
}
scroll(from, to, step)
}
/**
* @param {String} url
* @description 从URL中解析参数
*/
export const getParams = (url: string): Record<string, string> => {
const keyValueArr = url.split('?')[1]?.split('&') || []
const paramObj: Record<string, string> = {}
keyValueArr.forEach(item => {
const keyValue = item.split('=')
paramObj[keyValue[0]] = keyValue[1]
})
return paramObj
}
/**
* @param {Array} list 标签列表
* @param {String} name 当前关闭的标签的name
*/
export const getNextName = (list: MenuItem[], name: string): string => {
let res = ''
if (list.length === 2) {
res = 'home'
} else {
const index = list.findIndex(item => item.name === name)
if (index === list.length - 1) res = list[list.length - 2].name
else res = list[index + 1].name
}
return res
}
export const findNodeUpper = (ele: HTMLElement, tag: string): HTMLElement | null => {
if (ele.parentNode) {
if ((ele.parentNode as HTMLElement).tagName === tag.toUpperCase()) {
return ele.parentNode as HTMLElement
} else {
return findNodeUpper(ele.parentNode as HTMLElement, tag)
}
}
return null
}
export const findNodeUpperByClasses = (ele: HTMLElement, classes: string[]): HTMLElement | null => {
const parentNode = ele.parentNode as HTMLElement
if (parentNode) {
const classList = parentNode.classList
if (classList && classes.every(className => classList.contains(className))) {
return parentNode
} else {
return findNodeUpperByClasses(parentNode, classes)
}
}
return null
}
export const findNodeDownward = (ele: HTMLElement, tag: string): HTMLElement | null => {
const tagName = tag.toUpperCase()
if (ele.childNodes && ele.childNodes.length) {
let i = -1
const len = ele.childNodes.length
while (++i < len) {
const child = ele.childNodes[i] as HTMLElement
if (child.tagName === tagName) return child
else return findNodeDownward(child, tag)
}
}
return null
}
export const showByAccess = (): boolean => {
return true // hasOneOf(canAccess, access)
}
export const getRouteTitleHandled = (route: AppRoute): AppRoute => {
const router = { ...route }
const meta: AppRoute['meta'] = { ...(route.meta || {}) }
let title = ''
if (meta?.title) {
if (typeof meta.title === 'function') {
;(meta as RouteMeta & { __titleIsFunction__?: boolean }).__titleIsFunction__ = true
title = (meta.title as (r: AppRoute) => string)(router)
} else title = meta.title as string
}
if (meta) {
meta.title = title
router.meta = meta
}
return router
}
/**
* @description 根据当前跳转的路由设置显示在浏览器标签的title
* @param {Object} routeItem 路由对象
* @param {Object} vm Vue实例
*/
export const setTitle = (routeItem: AppRoute, vm?: ShowTitleOptions): void => {
const handledRoute = getRouteTitleHandled(routeItem)
const menuItem: MenuItem = {
name: handledRoute.name || '',
path: handledRoute.path,
meta: handledRoute.meta
}
const pageTitle = showTitle(menuItem, vm)
const resTitle = pageTitle ? `${pageTitle} - ${title}` : title
window.document.title = resTitle
}
/**
* 轻量级 JSON 语法高亮 - 单次分词扫描版
*/
export const highlightJSON = (json: string | object): string => {
if (typeof json !== 'string') {
json = JSON.stringify(json, null, 2)
}
// 1. 先进行基础 HTML 转义
const escaped = (json as string)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
// 2. 使用单次正则匹配所有 JSON 标记 (字符串/Key, 布尔/Null, 数字, 标点)
// 这个正则保证了匹配的唯一性,字符串内部的内容不会被再次匹配
const regex = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?|([\{\}\[\]]|[,:]))/g
return escaped.replace(regex, (match) => {
let cls = 'hl-punc'
if (match.startsWith('"')) {
if (match.endsWith(':')) {
return `<span class="hl-key">${match.slice(0, -1)}</span><span class="hl-punc">:</span>`
}
cls = 'hl-val'
} else if (/true|false/.test(match)) {
cls = 'hl-bool'
} else if (match === 'null') {
cls = 'hl-null'
} else if (/[0-9]/.test(match)) {
cls = 'hl-num'
} else if (/[\[\]\{\}]/.test(match)) {
cls = 'hl-bracket'
}
return `<span class="${cls}">${match}</span>`
})
}