Skip to content

feat: Add option to show kebab-case component names #911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backend/highlighter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { inDoc, classify, getComponentName } from '../util'
import { inDoc, getComponentName, getComponentDisplayName } from '../util'
import { getInstanceName } from './index'
import SharedData from 'src/shared-data'
import { isBrowser, target } from '../devtools/env'
Expand Down Expand Up @@ -46,7 +46,7 @@ export function highlight (instance) {
if (rect) {
const content = []
let name = instance.fnContext ? getComponentName(instance.fnOptions) : getInstanceName(instance)
if (SharedData.classifyComponents) name = classify(name)
name = getComponentDisplayName(name, SharedData.componentNameStyle)
if (name) {
const pre = document.createElement('span')
pre.style.opacity = '0.6'
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/components/ComponentInspector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import ScrollPane from 'components/ScrollPane.vue'
import ActionHeader from 'components/ActionHeader.vue'
import StateInspector from 'components/StateInspector.vue'
import { searchDeepInObject, sortByKey, classify, openInEditor } from 'src/util'
import { searchDeepInObject, sortByKey, openInEditor, getComponentDisplayName } from 'src/util'
import groupBy from 'lodash.groupby'

export default {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default {
},

targetName () {
return this.$shared.classifyComponents ? classify(this.target.name) : this.target.name
return getComponentDisplayName(this.target.name, this.$shared.componentNameStyle)
},

filteredState () {
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/components/ComponentInstance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<script>
import { mapState, mapMutations } from 'vuex'
import { classify, scrollIntoView, UNDEFINED } from '../../../util'
import { getComponentDisplayName, scrollIntoView, UNDEFINED } from '../../../util'

export default {
name: 'ComponentInstance',
Expand Down Expand Up @@ -143,7 +143,7 @@ export default {
},

displayName () {
return this.$shared.classifyComponents ? classify(this.instance.name) : this.instance.name
return getComponentDisplayName(this.instance.name, this.$shared.componentNameStyle)
},

componentHasKey () {
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/events/EventsHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import Keyboard, {
} from '../../mixins/keyboard'
import EntryList from '../../mixins/entry-list'
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
import { classify, focusInput } from 'src/util'
import { focusInput, getComponentDisplayName } from 'src/util'

export default {
components: {
Expand Down Expand Up @@ -162,7 +162,7 @@ export default {
]),

displayComponentName (name) {
return this.$shared.classifyComponents ? classify(name) : name
return getComponentDisplayName(name, this.$shared.componentNameStyle)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/devtools/views/events/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import storage from 'src/storage'
import { classify } from 'src/util'
import { getComponentDisplayName } from 'src/util'
import SharedData from 'src/shared-data'

const ENABLED_KEY = 'EVENTS_ENABLED'
Expand Down Expand Up @@ -47,10 +47,9 @@ const mutations = {
}

const matchingEvent = ({ searchText, searchComponent, regEx }) => e => {
const classifyComponents = SharedData.classifyComponents
const componentNameStyle = SharedData.componentNameStyle
let searchTerm = (searchComponent
? (classifyComponents
? classify(e.instanceName) : e.instanceName)
? getComponentDisplayName(e.instanceName, componentNameStyle)
: e.eventName)

if (regEx) {
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/perf/ComponentRenderDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</template>

<script>
import { classify } from 'src/util'
import { getComponentDisplayName } from 'src/util'

import ScrollPane from 'components/ScrollPane.vue'
import ActionHeader from 'components/ActionHeader.vue'
Expand Down Expand Up @@ -126,7 +126,7 @@ export default {
},

componentName () {
return (this.$shared.classifyComponents ? classify(this.entry.id) : this.entry.id) || 'Anonymous Component'
return getComponentDisplayName(this.entry.id, this.$shared.componentNameStyle) || 'Anonymous Component'
},

highDensity () {
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/perf/ComponentRenderStats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<script>
import { mapState, mapGetters } from 'vuex'
import { scaleLinear, extent } from 'd3'
import { classify } from 'src/util'
import { getComponentDisplayName } from 'src/util'

import SplitPane from 'components/SplitPane.vue'
import ScrollPane from 'components/ScrollPane.vue'
Expand Down Expand Up @@ -138,7 +138,7 @@ export default {
},

getComponentName (entry) {
return (this.$shared.classifyComponents ? classify(entry.id) : entry.id) || 'Anonymous Component'
return getComponentDisplayName(entry.id, this.$shared.componentNameStyle) || 'Anonymous Component'
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/devtools/views/settings/GlobalPreferences.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
<div class="global-preferences preferences">
<VueFormField title="Normalize component names">
<VueGroup
v-model="$shared.classifyComponents"
v-model="$shared.componentNameStyle"
class="extend"
>
<VueGroupButton
:value="false"
value="original"
label="Original name"
/>
<VueGroupButton
:value="true"
value="class"
label="Pascal case"
/>
<VueGroupButton
value="kebab"
label="Kebab case"
/>
</VueGroup>
</VueFormField>

Expand Down
4 changes: 2 additions & 2 deletions src/shared-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import storage from './storage'
// Initial state
const internalSharedData = {
openInEditorHost: '/',
classifyComponents: true,
componentNameStyle: 'class',
theme: 'auto',
displayDensity: 'low',
recordVuex: true,
Expand All @@ -17,7 +17,7 @@ const internalSharedData = {
}

const persisted = [
'classifyComponents',
'componentNameStyle',
'theme',
'displayDensity',
'recordVuex',
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,33 @@ export const camelize = cached((str) => {
return str.replace(camelizeRE, toUpper)
})

const kebabizeRE = /([a-z0-9])([A-Z])/g
export const kebabize = cached((str) => {
console.log('kebabize', str)

return str && str
.replace(kebabizeRE, (_, lowerCaseCharacter, upperCaseLetter) => {
return `${lowerCaseCharacter}-${upperCaseLetter}`
})
.toLowerCase()
})

function toUpper (_, c) {
return c ? c.toUpperCase() : ''
}

export function getComponentDisplayName (originalName, style = 'class') {
switch (style) {
case 'class':
return classify(originalName)
case 'kebab':
return kebabize(originalName)
case 'original':
default:
return originalName
}
}

export function inDoc (node) {
if (!node) return false
var doc = node.ownerDocument.documentElement
Expand Down