From 20ecd112f4e79aa78fc35776eaa0b4c1226849c0 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Fri, 7 Aug 2020 14:18:17 +0100 Subject: [PATCH] duplicate name check when renaming component --- .../userInterface/SettingsView.svelte | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/userInterface/SettingsView.svelte b/packages/builder/src/components/userInterface/SettingsView.svelte index 551f81bd2f..22f67e9f66 100644 --- a/packages/builder/src/components/userInterface/SettingsView.svelte +++ b/packages/builder/src/components/userInterface/SettingsView.svelte @@ -4,6 +4,8 @@ import Input from "../common/Input.svelte" import { goto } from "@sveltech/routify" import { excludeProps } from "./propertyCategories.js" + import { store } from "builderStore" + import { walkProps } from "builderStore/storeUtils" export let panelDefinition = [] export let componentDefinition = {} @@ -13,6 +15,7 @@ export let screenOrPageInstance let pageScreenProps = ["title", "favicon", "description", "route"] + let duplicateName = false const propExistsOnComponentDef = prop => pageScreenProps.includes(prop) || prop in componentDefinition.props @@ -33,6 +36,43 @@ $: isPage = screenOrPageInstance && screenOrPageInstance.favicon $: screenOrPageDefinition = isPage ? pageDefinition : screenDefinition + + const isDuplicateName = name => { + let duplicate = false + + const lookForDuplicate = rootPops => { + walkProps(rootPops, (inst, cancel) => { + if (inst._instanceName === name && inst._id !== componentInstance._id) { + duplicate = true + cancel() + } + }) + } + // check page first + lookForDuplicate($store.pages[$store.currentPageName].props) + if (duplicate) return true + + // if viwing screen, check current screen for duplicate + if ($store.currentFrontEndType === "screen") { + lookForDuplicate($store.currentPreviewItem.props) + } else { + // viewing master page - need to dedupe against all screens + for (let screen of $store.screens) { + lookForDuplicate(screen.props) + } + } + + return duplicate + } + + const onInstanceNameChange = (_, name) => { + if (isDuplicateName(name)) { + duplicateName = true + } else { + duplicateName = false + onChange("_instanceName", name) + } + } {#if screenOrPageInstance} @@ -54,7 +94,10 @@ label="Name" key="_instanceName" value={componentInstance._instanceName} - {onChange} /> + onChange={onInstanceNameChange} /> + {#if duplicateName} + Name must be unique + {/if} {/if} {#if panelDefinition && panelDefinition.length > 0} @@ -79,4 +122,9 @@ div { text-align: center; } + + .duplicate-name { + color: var(--red); + font-size: var(--font-size-xs); + }