mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-04-23 03:00:31 -04:00
feat(ui): make index optional when adding elements, update tests
This commit is contained in:
@@ -110,6 +110,58 @@ describe('workflow builder form manipulation', () => {
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el1.id);
|
||||
});
|
||||
|
||||
it('should add the element to the end if the index is out of bounds', () => {
|
||||
const form = getDefaultForm();
|
||||
const el = buildText('foo');
|
||||
addElement({
|
||||
form,
|
||||
element: el,
|
||||
parentId: form.rootElementId,
|
||||
index: 100,
|
||||
});
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
|
||||
});
|
||||
|
||||
it('should add the element to the end if the index is undefined', () => {
|
||||
const form = getDefaultForm();
|
||||
const el = buildText('foo');
|
||||
addElement({
|
||||
form,
|
||||
element: el,
|
||||
parentId: form.rootElementId,
|
||||
});
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
|
||||
const el2 = buildText('foo');
|
||||
addElement({
|
||||
form,
|
||||
element: el2,
|
||||
parentId: form.rootElementId,
|
||||
});
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el2.id);
|
||||
});
|
||||
|
||||
it('should add the element counting from the end if the index is negative', () => {
|
||||
const form = getDefaultForm();
|
||||
const el = buildText('foo');
|
||||
addElement({
|
||||
form,
|
||||
element: el,
|
||||
parentId: form.rootElementId,
|
||||
index: -1,
|
||||
});
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
|
||||
const el2 = buildText('foo');
|
||||
addElement({
|
||||
form,
|
||||
element: el2,
|
||||
parentId: form.rootElementId,
|
||||
index: -1,
|
||||
});
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el2.id);
|
||||
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el.id);
|
||||
});
|
||||
|
||||
it('should return true if the element was added', () => {
|
||||
const form = getDefaultForm();
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ export const addElement = (args: {
|
||||
form: BuilderForm;
|
||||
element: FormElement;
|
||||
parentId: string;
|
||||
index: number;
|
||||
index?: number;
|
||||
}): boolean => {
|
||||
const { form, element, parentId, index } = args;
|
||||
const { elements } = form;
|
||||
@@ -137,7 +137,7 @@ export const addElement = (args: {
|
||||
|
||||
element.parentId = parentId;
|
||||
elements[element.id] = element;
|
||||
parent.data.children.splice(index, 0, element.id);
|
||||
parent.data.children.splice(index ?? parent.data.children.length, 0, element.id);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
@@ -154,7 +154,7 @@ export const workflowSlice = createSlice({
|
||||
action: PayloadAction<{
|
||||
element: FormElement;
|
||||
parentId: ElementId;
|
||||
index: number;
|
||||
index?: number;
|
||||
initialValue?: StatefulFieldValue;
|
||||
}>
|
||||
) => {
|
||||
|
||||
Reference in New Issue
Block a user