Skip to content

Commit f7e2b09

Browse files
author
Guillaume Chau
committed
fix(router): format redirect and alias in tree, closes #1027
1 parent e2fc9de commit f7e2b09

File tree

4 files changed

+85
-62
lines changed

4 files changed

+85
-62
lines changed

packages/app-frontend/src/components/DataField.vue

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,15 @@
207207
<script>
208208
import { mapState } from 'vuex'
209209
import {
210-
UNDEFINED,
211-
INFINITY,
212-
NEGATIVE_INFINITY,
213-
NAN,
214210
isPlainObject,
215211
sortByKey,
216212
openInEditor,
217-
escape,
218-
specialTokenToString,
219213
copyToClipboard
220214
} from '@utils/util'
215+
import { formattedValue, valueType } from '@front/filters'
221216
222217
import DataFieldEdit from '@front/mixins/data-field-edit'
223218
224-
const rawTypeRE = /^\[object (\w+)]$/
225-
const specialTypeRE = /^\[native (\w+) (.*)\]$/
226-
227219
function subFieldCount (value) {
228220
if (Array.isArray(value)) {
229221
return value.length
@@ -284,33 +276,7 @@ export default {
284276
285277
valueType () {
286278
const value = this.field.value
287-
const type = typeof value
288-
if (value == null || value === UNDEFINED) {
289-
return 'null'
290-
} else if (
291-
type === 'boolean' ||
292-
type === 'number' ||
293-
value === INFINITY ||
294-
value === NEGATIVE_INFINITY ||
295-
value === NAN
296-
) {
297-
return 'literal'
298-
} else if (value && value._custom) {
299-
return 'custom'
300-
} else if (type === 'string') {
301-
if (specialTypeRE.test(value)) {
302-
const [, type] = specialTypeRE.exec(value)
303-
return `native ${type}`
304-
} else {
305-
return 'string'
306-
}
307-
} else if (Array.isArray(value) || (value && value._isArray)) {
308-
return 'array'
309-
} else if (isPlainObject(value)) {
310-
return 'plain-object'
311-
} else {
312-
return 'unknown'
313-
}
279+
return valueType(value)
314280
},
315281
316282
rawValueType () {
@@ -337,30 +303,11 @@ export default {
337303
338304
formattedValue () {
339305
let value = this.field.value
340-
let result
341306
if (this.fieldOptions.abstract) {
342307
return ''
343-
} else if ((result = specialTokenToString(value))) {
344-
return result
345-
} else if (this.valueType === 'custom') {
346-
return value._custom.display
347-
} else if (this.valueType === 'array') {
348-
return 'Array[' + value.length + ']'
349-
} else if (this.valueType === 'plain-object') {
350-
return 'Object' + (Object.keys(value).length ? '' : ' (empty)')
351-
} else if (this.valueType.includes('native')) {
352-
return escape(specialTypeRE.exec(value)[2])
353-
} else if (typeof value === 'string') {
354-
const typeMatch = value.match(rawTypeRE)
355-
if (typeMatch) {
356-
value = escape(typeMatch[1])
357-
} else {
358-
value = `<span>"</span>${escape(value)}<span>"</span>`
359-
}
360-
value = value.replace(/ /g, '&nbsp;')
361-
.replace(/\n/g, '<span>\\n</span>')
308+
} else {
309+
return formattedValue(value)
362310
}
363-
return value
364311
},
365312
366313
rawValue () {

packages/app-frontend/src/filters.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,75 @@
1+
import {
2+
UNDEFINED,
3+
INFINITY,
4+
NEGATIVE_INFINITY,
5+
NAN,
6+
isPlainObject,
7+
escape,
8+
specialTokenToString
9+
} from '@utils/util'
10+
11+
const rawTypeRE = /^\[object (\w+)]$/
12+
const specialTypeRE = /^\[native (\w+) (.*)\]$/
13+
114
export function formatTime (timestamp, format) {
215
const date = new Date(timestamp)
316
return `${date.toString().match(/\d\d:\d\d:\d\d/)[0]}${format === 'ms' ? '.' + date.getMilliseconds() : ''}`
417
}
18+
19+
export function valueType (value) {
20+
const type = typeof value
21+
if (value == null || value === UNDEFINED) {
22+
return 'null'
23+
} else if (
24+
type === 'boolean' ||
25+
type === 'number' ||
26+
value === INFINITY ||
27+
value === NEGATIVE_INFINITY ||
28+
value === NAN
29+
) {
30+
return 'literal'
31+
} else if (value && value._custom) {
32+
return 'custom'
33+
} else if (type === 'string') {
34+
if (specialTypeRE.test(value)) {
35+
const [, type] = specialTypeRE.exec(value)
36+
return `native ${type}`
37+
} else {
38+
return 'string'
39+
}
40+
} else if (Array.isArray(value) || (value && value._isArray)) {
41+
return 'array'
42+
} else if (isPlainObject(value)) {
43+
return 'plain-object'
44+
} else {
45+
return 'unknown'
46+
}
47+
}
48+
49+
export function formattedValue (value, quotes = true) {
50+
let result
51+
const type = valueType(value)
52+
if ((result = specialTokenToString(value))) {
53+
return result
54+
} else if (type === 'custom') {
55+
return value._custom.display
56+
} else if (type === 'array') {
57+
return 'Array[' + value.length + ']'
58+
} else if (type === 'plain-object') {
59+
return 'Object' + (Object.keys(value).length ? '' : ' (empty)')
60+
} else if (type.includes('native')) {
61+
return escape(specialTypeRE.exec(value)[2])
62+
} else if (typeof value === 'string') {
63+
const typeMatch = value.match(rawTypeRE)
64+
if (typeMatch) {
65+
value = escape(typeMatch[1])
66+
} else if (quotes) {
67+
value = `<span>"</span>${escape(value)}<span>"</span>`
68+
} else {
69+
value = escape(value)
70+
}
71+
value = value.replace(/ /g, '&nbsp;')
72+
.replace(/\n/g, '<span>\\n</span>')
73+
}
74+
return value
75+
}

packages/app-frontend/src/views/routes/RoutesTreeItem.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
v-if="route.alias"
3737
class="info alias"
3838
>
39-
alias: <b>{{ route.alias }}</b>
39+
alias: <b v-html="formattedValue(route.alias, false)" />
4040
</span>
4141
<span
4242
v-if="route.redirect"
4343
class="info redirect"
4444
>
45-
redirect: <b>{{ route.redirect }}</b>
45+
redirect: <b v-html="formattedValue(route.redirect, false)" />
4646
</span>
4747
<span
4848
v-if="isActive"
@@ -65,6 +65,7 @@
6565

6666
<script>
6767
import { mapState, mapMutations, mapGetters } from 'vuex'
68+
import { formattedValue } from '@front/filters'
6869
6970
export default {
7071
name: 'RoutesTreeItem',
@@ -105,9 +106,12 @@ export default {
105106
...mapMutations('routes', {
106107
inspect: 'INSPECT'
107108
}),
109+
108110
toggleExpand () {
109111
this.expanded = !this.expanded
110-
}
112+
},
113+
114+
formattedValue
111115
}
112116
}
113117
</script>

packages/shell-dev/target/router.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ const routes = [
2727
component: RouteWithBeforeEnter,
2828
beforeEnter: (to, from, next) => {
2929
next()
30-
}},
30+
} },
3131
{ path: '/route-with-redirect', redirect: '/route-one' },
32+
{ path: '/route-with-redirect-function', redirect: () => '/route-one' },
3233
{ path: '/route-with-alias', component: RouteWithAlias, alias: '/this-is-the-alias' },
3334
{ path: '/route-with-dynamic-component', component: DynamicComponent, props: true },
3435
{ path: '/route-with-props',
3536
component: RouteWithProps,
3637
props: {
3738
username: 'My Username',
3839
id: 99
39-
}},
40+
} },
4041
{ path: '/route-with-props-default', component: RouteWithProps },
4142
{ path: '/route-parent',
4243
component: ParentRoute,

0 commit comments

Comments
 (0)