Remove whitespace when blocks are renamed (#884)

This commit is contained in:
Kerem Yilmaz
2024-09-25 07:50:35 -07:00
committed by GitHub
parent 2e496e3992
commit 9f98fc94af
9 changed files with 39 additions and 28 deletions

View File

@@ -14,7 +14,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { CodeBlockNode } from "./types"; import type { CodeBlockNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -54,8 +54,9 @@ function CodeBlockNode({ id, data }: NodeProps<CodeBlockNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -14,7 +14,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { DownloadNode } from "./types"; import type { DownloadNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -51,8 +51,9 @@ function DownloadNode({ id, data }: NodeProps<DownloadNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -13,7 +13,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { FileParserNode } from "./types"; import type { FileParserNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -53,8 +53,9 @@ function FileParserNode({ id, data }: NodeProps<FileParserNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -15,7 +15,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { LoopNode } from "./types"; import type { LoopNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -81,8 +81,9 @@ function LoopNode({ id, data }: NodeProps<LoopNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -15,7 +15,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { SendEmailNode } from "./types"; import type { SendEmailNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -66,8 +66,9 @@ function SendEmailNode({ id, data }: NodeProps<SendEmailNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -24,7 +24,7 @@ import {
import { useState } from "react"; import { useState } from "react";
import { AppNode } from ".."; import { AppNode } from "..";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getOutputParameterKey, getOutputParameterKey,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
@@ -423,8 +423,9 @@ function TaskNode({ id, data }: NodeProps<TaskNode>) {
editable={editable} editable={editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -17,7 +17,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { TextPromptNode } from "./types"; import type { TextPromptNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -59,8 +59,9 @@ function TextPromptNode({ id, data }: NodeProps<TextPromptNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -14,7 +14,7 @@ import { NodeActionMenu } from "../NodeActionMenu";
import type { UploadNode } from "./types"; import type { UploadNode } from "./types";
import { useState } from "react"; import { useState } from "react";
import { import {
getLabelForExistingNode, getUniqueLabelForExistingNode,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
} from "../../workflowEditorUtils"; } from "../../workflowEditorUtils";
import { AppNode } from ".."; import { AppNode } from "..";
@@ -51,8 +51,9 @@ function UploadNode({ id, data }: NodeProps<UploadNode>) {
editable={data.editable} editable={data.editable}
onChange={(value) => { onChange={(value) => {
const existingLabels = nodes.map((n) => n.data.label); const existingLabels = nodes.map((n) => n.data.label);
const newLabel = getLabelForExistingNode( const labelWithoutWhitespace = value.replace(/\s+/g, "_");
value, const newLabel = getUniqueLabelForExistingNode(
labelWithoutWhitespace,
existingLabels, existingLabels,
); );
setLabel(newLabel); setLabel(newLabel);

View File

@@ -30,7 +30,7 @@ import { textPromptNodeDefaultData } from "./nodes/TextPromptNode/types";
import { uploadNodeDefaultData } from "./nodes/UploadNode/types"; import { uploadNodeDefaultData } from "./nodes/UploadNode/types";
import type { Node } from "@xyflow/react"; import type { Node } from "@xyflow/react";
export const NEW_NODE_LABEL_PREFIX = "Block "; export const NEW_NODE_LABEL_PREFIX = "block_";
function layoutUtil( function layoutUtil(
nodes: Array<AppNode>, nodes: Array<AppNode>,
@@ -754,12 +754,15 @@ function getAdditionalParametersForEmailBlock(
return sendEmailParameters; return sendEmailParameters;
} }
function getLabelForExistingNode(label: string, existingLabels: Array<string>) { function getUniqueLabelForExistingNode(
label: string,
existingLabels: Array<string>,
) {
if (!existingLabels.includes(label)) { if (!existingLabels.includes(label)) {
return label; return label;
} }
for (let i = 2; i < existingLabels.length + 1; i++) { for (let i = 2; i < existingLabels.length + 1; i++) {
const candidate = `${label} (${i})`; const candidate = `${label}_${i}`;
if (!existingLabels.includes(candidate)) { if (!existingLabels.includes(candidate)) {
return candidate; return candidate;
} }
@@ -801,7 +804,7 @@ export {
getOutputParameterKey, getOutputParameterKey,
getUpdatedNodesAfterLabelUpdateForParameterKeys, getUpdatedNodesAfterLabelUpdateForParameterKeys,
getAdditionalParametersForEmailBlock, getAdditionalParametersForEmailBlock,
getLabelForExistingNode, getUniqueLabelForExistingNode,
isOutputParameterKey, isOutputParameterKey,
getBlockNameOfOutputParameterKey, getBlockNameOfOutputParameterKey,
getDefaultValueForParameterType, getDefaultValueForParameterType,