mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-04 22:31:33 +09:00
## Description ref #4595 Search is working <img width="246" alt="image" src="https://github.com/user-attachments/assets/5dc850e4-6a36-4996-9aaa-f051ac7e977f" /> Arrow keys, enter, mouse click is working. https://p-15889dd9-ed47-46db-9411-fa18c1efb2fe-dot-edit.development.webstudio.is/ - [x] - Enter and click - [ ] - Repeat > N then close menu (later) - [x] - In case of select item clear cmd `/blabla` - [x] - Put cursor inside 1st editable block of the new instance - [x] - Should work only inside editable content ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
183 lines
5.1 KiB
TypeScript
183 lines
5.1 KiB
TypeScript
import { test, expect } from "vitest";
|
|
import type {
|
|
Instance,
|
|
Prop,
|
|
StyleDecl,
|
|
Styles,
|
|
StyleSource,
|
|
StyleSourceSelection,
|
|
} from "@webstudio-is/sdk";
|
|
import { getStyleDeclKey } from "@webstudio-is/sdk";
|
|
import {
|
|
type InstanceSelector,
|
|
cloneStyles,
|
|
findLocalStyleSourcesWithinInstances,
|
|
getAncestorInstanceSelector,
|
|
insertPropsCopyMutable,
|
|
isDescendantOrSelf,
|
|
} from "./tree-utils";
|
|
|
|
const expectString = expect.any(String) as unknown as string;
|
|
|
|
const createProp = (id: string, instanceId: string, value?: string): Prop => {
|
|
return {
|
|
id,
|
|
instanceId,
|
|
type: "string",
|
|
name: "prop",
|
|
value: value ?? "value",
|
|
};
|
|
};
|
|
|
|
const createStyleSource = (
|
|
type: StyleSource["type"],
|
|
id: StyleSource["id"]
|
|
): StyleSource => {
|
|
if (type === "token") {
|
|
return {
|
|
type,
|
|
id,
|
|
name: id,
|
|
};
|
|
}
|
|
return {
|
|
type,
|
|
id,
|
|
};
|
|
};
|
|
|
|
const createStyleSourceSelection = (
|
|
instanceId: Instance["id"],
|
|
values: StyleSource["id"][]
|
|
): StyleSourceSelection => {
|
|
return {
|
|
instanceId,
|
|
values,
|
|
};
|
|
};
|
|
|
|
const createStyleDecl = (
|
|
styleSourceId: string,
|
|
breakpointId: string,
|
|
value?: string
|
|
): StyleDecl => {
|
|
return {
|
|
styleSourceId,
|
|
breakpointId,
|
|
property: "width",
|
|
value: {
|
|
type: "keyword",
|
|
value: value ?? "value",
|
|
},
|
|
};
|
|
};
|
|
|
|
const createStyleDeclPair = (
|
|
styleSourceId: string,
|
|
breakpointId: string,
|
|
state?: string,
|
|
value?: string
|
|
) => {
|
|
return [
|
|
getStyleDeclKey({
|
|
styleSourceId,
|
|
breakpointId,
|
|
state,
|
|
property: "width",
|
|
}),
|
|
createStyleDecl(styleSourceId, breakpointId, value),
|
|
] as const;
|
|
};
|
|
|
|
test("get ancestor instance selector", () => {
|
|
const instanceSelector: InstanceSelector = ["4", "3", "2", "1"];
|
|
expect(getAncestorInstanceSelector(instanceSelector, "2")).toEqual([
|
|
"2",
|
|
"1",
|
|
]);
|
|
expect(getAncestorInstanceSelector(instanceSelector, "-1")).toEqual(
|
|
undefined
|
|
);
|
|
expect(getAncestorInstanceSelector(instanceSelector, "1")).toEqual(["1"]);
|
|
});
|
|
|
|
test("insert props copy with new ids and apply new instance ids", () => {
|
|
const props = new Map([
|
|
["prop1", createProp("prop1", "instance1")],
|
|
["prop2", createProp("prop2", "instance2")],
|
|
["prop3", createProp("prop3", "instance3")],
|
|
["existingSharedProp", createProp("existingSharedProp", "instance3")],
|
|
]);
|
|
const copiedProps = [
|
|
createProp("prop2", "instance2"),
|
|
createProp("sharedProp", "instance3", "newValue"),
|
|
createProp("existingSharedProp", "instance3", "newValue"),
|
|
];
|
|
const clonedInstanceIds = new Map<Instance["id"], Instance["id"]>([
|
|
["instance2", "newInstance2"],
|
|
]);
|
|
insertPropsCopyMutable(props, copiedProps, clonedInstanceIds);
|
|
expect(Array.from(props.entries())).toEqual([
|
|
["prop1", createProp("prop1", "instance1")],
|
|
["prop2", createProp("prop2", "instance2")],
|
|
["prop3", createProp("prop3", "instance3")],
|
|
// shared prop is not overriden
|
|
["existingSharedProp", createProp("existingSharedProp", "instance3")],
|
|
// new props are copied
|
|
[expectString, createProp(expectString, "newInstance2")],
|
|
// shared prop inserted without changes
|
|
["sharedProp", createProp("sharedProp", "instance3", "newValue")],
|
|
]);
|
|
});
|
|
|
|
test("clone styles with appled new style source ids", () => {
|
|
const styles: Styles = new Map([
|
|
createStyleDeclPair("styleSource1", "bp1"),
|
|
createStyleDeclPair("styleSource2", "bp2"),
|
|
createStyleDeclPair("styleSource1", "bp3"),
|
|
createStyleDeclPair("styleSource3", "bp4"),
|
|
createStyleDeclPair("styleSource1", "bp5"),
|
|
createStyleDeclPair("styleSource3", "bp6"),
|
|
]);
|
|
const clonedStyleSourceIds = new Map<StyleSource["id"], StyleSource["id"]>();
|
|
clonedStyleSourceIds.set("styleSource2", "newStyleSource2");
|
|
clonedStyleSourceIds.set("styleSource3", "newStyleSource3");
|
|
expect(cloneStyles(styles, clonedStyleSourceIds)).toEqual([
|
|
createStyleDecl("newStyleSource2", "bp2"),
|
|
createStyleDecl("newStyleSource3", "bp4"),
|
|
createStyleDecl("newStyleSource3", "bp6"),
|
|
]);
|
|
});
|
|
|
|
test("find local style sources within instances", () => {
|
|
const instanceIds = new Set(["instance2", "instance4"]);
|
|
const styleSources = [
|
|
createStyleSource("local", "local1"),
|
|
createStyleSource("local", "local2"),
|
|
createStyleSource("token", "token3"),
|
|
createStyleSource("local", "local4"),
|
|
createStyleSource("token", "token5"),
|
|
createStyleSource("local", "local6"),
|
|
];
|
|
const styleSourceSelections = [
|
|
createStyleSourceSelection("instance1", ["local1"]),
|
|
createStyleSourceSelection("instance2", ["local2"]),
|
|
createStyleSourceSelection("instance3", ["token3"]),
|
|
createStyleSourceSelection("instance4", ["local4", "token5"]),
|
|
createStyleSourceSelection("instance5", ["local6"]),
|
|
];
|
|
expect(
|
|
findLocalStyleSourcesWithinInstances(
|
|
styleSources,
|
|
styleSourceSelections,
|
|
instanceIds
|
|
)
|
|
).toEqual(new Set(["local2", "local4"]));
|
|
});
|
|
|
|
test("is descendant or self", () => {
|
|
expect(isDescendantOrSelf(["1", "2", "3"], ["1", "2", "3"])).toBe(true);
|
|
expect(isDescendantOrSelf(["0", "1", "2", "3"], ["1", "2", "3"])).toBe(true);
|
|
expect(isDescendantOrSelf(["1", "2", "3"], ["0", "1", "2", "3"])).toBe(false);
|
|
});
|