2020-08-07 13:01:16 +02:00
|
|
|
import { walkProps } from "./storeUtils"
|
|
|
|
import { get_capitalised_name } from "../helpers"
|
|
|
|
|
|
|
|
export default function(component, state) {
|
2020-10-28 20:38:11 +01:00
|
|
|
const capitalised = get_capitalised_name(
|
|
|
|
component.name || component._component
|
|
|
|
)
|
2020-08-07 13:01:16 +02:00
|
|
|
|
|
|
|
const matchingComponents = []
|
|
|
|
|
2020-08-07 15:17:57 +02:00
|
|
|
const findMatches = props => {
|
|
|
|
walkProps(props, c => {
|
2020-10-07 23:30:00 +02:00
|
|
|
const thisInstanceName = get_capitalised_name(c._instanceName)
|
|
|
|
if ((thisInstanceName || "").startsWith(capitalised)) {
|
|
|
|
matchingComponents.push(thisInstanceName)
|
2020-08-07 13:01:16 +02:00
|
|
|
}
|
|
|
|
})
|
2020-08-07 15:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check page first
|
|
|
|
findMatches(state.pages[state.currentPageName].props)
|
2020-08-07 13:01:16 +02:00
|
|
|
|
2020-08-07 15:17:57 +02:00
|
|
|
// if viewing screen, check current screen for duplicate
|
|
|
|
if (state.currentFrontEndType === "screen") {
|
|
|
|
findMatches(state.currentPreviewItem.props)
|
|
|
|
} else {
|
|
|
|
// viewing master page - need to find against all screens
|
|
|
|
for (let screen of state.screens) {
|
|
|
|
findMatches(screen.props)
|
2020-08-07 13:01:16 +02:00
|
|
|
}
|
2020-08-07 15:17:57 +02:00
|
|
|
}
|
2020-08-07 13:01:16 +02:00
|
|
|
|
2020-08-07 13:09:48 +02:00
|
|
|
let index = 1
|
2020-08-07 13:01:16 +02:00
|
|
|
let name
|
|
|
|
while (!name) {
|
|
|
|
const tryName = `${capitalised} ${index}`
|
|
|
|
if (!matchingComponents.includes(tryName)) name = tryName
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|