Skip to content

Commit 971e5e2

Browse files
AareksioAkryum
andcommitted
feat: Add option to show kebab-case component names (#911)
* Replace `classifyComponents` with `componentNameStyle` This change allows for multiple additional styles of component displayed names, most importantly kebab-case * Remove leftover console.log Co-authored-by: Guillaume Chau <[email protected]>
1 parent da80fc5 commit 971e5e2

File tree

10 files changed

+45
-21
lines changed

10 files changed

+45
-21
lines changed

src/backend/highlighter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inDoc, classify, getComponentName } from '../util'
1+
import { inDoc, getComponentName, getComponentDisplayName } from '../util'
22
import { getInstanceName } from './index'
33
import SharedData from 'src/shared-data'
44
import { isBrowser, target } from '../devtools/env'
@@ -46,7 +46,7 @@ export function highlight (instance) {
4646
if (rect) {
4747
const content = []
4848
let name = instance.fnContext ? getComponentName(instance.fnOptions) : getInstanceName(instance)
49-
if (SharedData.classifyComponents) name = classify(name)
49+
name = getComponentDisplayName(name, SharedData.componentNameStyle)
5050
if (name) {
5151
const pre = document.createElement('span')
5252
pre.style.opacity = '0.6'

src/devtools/views/components/ComponentInspector.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import ScrollPane from 'components/ScrollPane.vue'
7070
import ActionHeader from 'components/ActionHeader.vue'
7171
import StateInspector from 'components/StateInspector.vue'
72-
import { searchDeepInObject, sortByKey, classify, openInEditor } from 'src/util'
72+
import { searchDeepInObject, sortByKey, openInEditor, getComponentDisplayName } from 'src/util'
7373
import groupBy from 'lodash.groupby'
7474
7575
export default {
@@ -102,7 +102,7 @@ export default {
102102
},
103103
104104
targetName () {
105-
return this.$shared.classifyComponents ? classify(this.target.name) : this.target.name
105+
return getComponentDisplayName(this.target.name, this.$shared.componentNameStyle)
106106
},
107107
108108
filteredState () {

src/devtools/views/components/ComponentInstance.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102

103103
<script>
104104
import { mapState, mapMutations } from 'vuex'
105-
import { classify, scrollIntoView, UNDEFINED } from '../../../util'
105+
import { getComponentDisplayName, scrollIntoView, UNDEFINED } from '../../../util'
106106
107107
export default {
108108
name: 'ComponentInstance',
@@ -143,7 +143,7 @@ export default {
143143
},
144144
145145
displayName () {
146-
return this.$shared.classifyComponents ? classify(this.instance.name) : this.instance.name
146+
return getComponentDisplayName(this.instance.name, this.$shared.componentNameStyle)
147147
},
148148
149149
componentHasKey () {

src/devtools/views/events/EventsHistory.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import Keyboard, {
8787
} from '../../mixins/keyboard'
8888
import EntryList from '../../mixins/entry-list'
8989
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
90-
import { classify, focusInput } from 'src/util'
90+
import { focusInput, getComponentDisplayName } from 'src/util'
9191
9292
export default {
9393
components: {
@@ -162,7 +162,7 @@ export default {
162162
]),
163163
164164
displayComponentName (name) {
165-
return this.$shared.classifyComponents ? classify(name) : name
165+
return getComponentDisplayName(name, this.$shared.componentNameStyle)
166166
}
167167
}
168168
}

src/devtools/views/events/module.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as storage from 'src/storage'
2-
import { classify } from 'src/util'
2+
import { getComponentDisplayName } from 'src/util'
33
import SharedData from 'src/shared-data'
44

55
const ENABLED_KEY = 'EVENTS_ENABLED'
@@ -46,10 +46,9 @@ const mutations = {
4646
}
4747

4848
const matchingEvent = ({ searchText, searchComponent, regEx }) => e => {
49-
const classifyComponents = SharedData.classifyComponents
49+
const componentNameStyle = SharedData.componentNameStyle
5050
let searchTerm = (searchComponent
51-
? (classifyComponents
52-
? classify(e.instanceName) : e.instanceName)
51+
? getComponentDisplayName(e.instanceName, componentNameStyle)
5352
: e.eventName)
5453

5554
if (regEx) {

src/devtools/views/perf/ComponentRenderDetails.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
</template>
8080

8181
<script>
82-
import { classify } from 'src/util'
82+
import { getComponentDisplayName } from 'src/util'
8383
8484
import ScrollPane from 'components/ScrollPane.vue'
8585
import ActionHeader from 'components/ActionHeader.vue'
@@ -126,7 +126,7 @@ export default {
126126
},
127127
128128
componentName () {
129-
return (this.$shared.classifyComponents ? classify(this.entry.id) : this.entry.id) || 'Anonymous Component'
129+
return getComponentDisplayName(this.entry.id, this.$shared.componentNameStyle) || 'Anonymous Component'
130130
},
131131
132132
highDensity () {

src/devtools/views/perf/ComponentRenderStats.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<script>
6363
import { mapState, mapGetters } from 'vuex'
6464
import { scaleLinear, extent } from 'd3'
65-
import { classify } from 'src/util'
65+
import { getComponentDisplayName } from 'src/util'
6666
6767
import SplitPane from 'components/SplitPane.vue'
6868
import ScrollPane from 'components/ScrollPane.vue'
@@ -138,7 +138,7 @@ export default {
138138
},
139139
140140
getComponentName (entry) {
141-
return (this.$shared.classifyComponents ? classify(entry.id) : entry.id) || 'Anonymous Component'
141+
return getComponentDisplayName(entry.id, this.$shared.componentNameStyle) || 'Anonymous Component'
142142
}
143143
}
144144
}

src/devtools/views/settings/GlobalPreferences.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
<div class="global-preferences preferences">
33
<VueFormField title="Normalize component names">
44
<VueGroup
5-
v-model="$shared.classifyComponents"
5+
v-model="$shared.componentNameStyle"
66
class="extend"
77
>
88
<VueGroupButton
9-
:value="false"
9+
value="original"
1010
label="Original name"
1111
/>
1212
<VueGroupButton
13-
:value="true"
13+
value="class"
1414
label="Pascal case"
1515
/>
16+
<VueGroupButton
17+
value="kebab"
18+
label="Kebab case"
19+
/>
1620
</VueGroup>
1721
</VueFormField>
1822

src/shared-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as storage from './storage'
33
// Initial state
44
const internalSharedData = {
55
openInEditorHost: '/',
6-
classifyComponents: true,
6+
componentNameStyle: 'class',
77
theme: 'auto',
88
displayDensity: 'low',
99
recordVuex: true,
@@ -18,7 +18,7 @@ const internalSharedData = {
1818
}
1919

2020
const persisted = [
21-
'classifyComponents',
21+
'componentNameStyle',
2222
'theme',
2323
'displayDensity',
2424
'recordVuex',

src/util.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,31 @@ export const camelize = cached((str) => {
2727
return str.replace(camelizeRE, toUpper)
2828
})
2929

30+
const kebabizeRE = /([a-z0-9])([A-Z])/g
31+
export const kebabize = cached((str) => {
32+
return str && str
33+
.replace(kebabizeRE, (_, lowerCaseCharacter, upperCaseLetter) => {
34+
return `${lowerCaseCharacter}-${upperCaseLetter}`
35+
})
36+
.toLowerCase()
37+
})
38+
3039
function toUpper (_, c) {
3140
return c ? c.toUpperCase() : ''
3241
}
3342

43+
export function getComponentDisplayName (originalName, style = 'class') {
44+
switch (style) {
45+
case 'class':
46+
return classify(originalName)
47+
case 'kebab':
48+
return kebabize(originalName)
49+
case 'original':
50+
default:
51+
return originalName
52+
}
53+
}
54+
3455
export function inDoc (node) {
3556
if (!node) return false
3657
var doc = node.ownerDocument.documentElement

0 commit comments

Comments
 (0)