2020-08-07 13:01:16 +02:00
|
|
|
import { walkProps } from "./storeUtils"
|
|
|
|
import { get_capitalised_name } from "../helpers"
|
2020-11-05 18:47:27 +01:00
|
|
|
import { get } from "svelte/store"
|
|
|
|
import { allScreens } from "builderStore"
|
2020-12-05 13:09:58 +01:00
|
|
|
import { FrontendTypes } from "../constants"
|
2020-12-07 16:27:46 +01:00
|
|
|
import { currentAsset } from "."
|
2020-08-07 13:01:16 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-25 18:56:09 +01:00
|
|
|
// check layouts first
|
|
|
|
for (let layout of state.layouts) {
|
|
|
|
findMatches(layout.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
|
2020-12-05 13:09:58 +01:00
|
|
|
if (state.currentFrontEndType === FrontendTypes.SCREEN) {
|
2020-12-07 16:27:46 +01:00
|
|
|
findMatches(get(currentAsset).props)
|
2020-08-07 15:17:57 +02:00
|
|
|
} else {
|
2020-11-25 18:56:09 +01:00
|
|
|
// viewing a layout - need to find against all screens
|
2020-11-05 18:47:27 +01:00
|
|
|
for (let screen of get(allScreens)) {
|
2020-08-07 15:17:57 +02:00
|
|
|
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) {
|
2020-11-05 12:44:18 +01:00
|
|
|
const tryName = `${capitalised || "Copy"} ${index}`
|
2020-08-07 13:01:16 +02:00
|
|
|
if (!matchingComponents.includes(tryName)) name = tryName
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|