Skip to content

Commit 6939a99

Browse files
authored
fix: allow inserting into textual collection items (#4305)
Fixes #4294
1 parent 7d111f4 commit 6939a99

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

apps/builder/app/shared/instance-utils.test.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@webstudio-is/react-sdk";
88
import type { Project } from "@webstudio-is/project";
99
import { createDefaultPages } from "@webstudio-is/project-build";
10-
import { $, renderJsx } from "@webstudio-is/sdk/testing";
10+
import { $, ws, renderJsx } from "@webstudio-is/sdk/testing";
1111
import { parseCss } from "@webstudio-is/css-data";
1212
import * as defaultMetas from "@webstudio-is/sdk-components-react/metas";
1313
import type {
@@ -589,6 +589,27 @@ describe("find closest droppable target", () => {
589589
)
590590
).toEqual(undefined);
591591
});
592+
593+
test("allow inserting into collection item", () => {
594+
const { instances } = renderJsx(
595+
<$.Body ws:id="bodyId">
596+
<ws.collection ws:id="collectionId">
597+
<$.Box ws:id="boxId"></$.Box>
598+
</ws.collection>
599+
</$.Body>
600+
);
601+
expect(
602+
findClosestDroppableTarget(
603+
defaultMetasMap,
604+
instances,
605+
["collectionId[1]", "collectionId", "bodyId"],
606+
emptyInsertConstraints
607+
)
608+
).toEqual({
609+
parentSelector: ["collectionId", "bodyId"],
610+
position: "end",
611+
});
612+
});
592613
});
593614

594615
describe("insert instance children", () => {
@@ -3185,7 +3206,12 @@ describe("copy paste", () => {
31853206
const styleSources: StyleSources = new Map();
31863207
const styles: Styles = new Map();
31873208
const parsed = mapGroupBy(
3188-
parseCss(cssString),
3209+
parseCss(cssString).map((styleDecl) => ({
3210+
...styleDecl,
3211+
selector: styleDecl.selector.startsWith("__")
3212+
? styleDecl.selector.slice(2)
3213+
: styleDecl.selector,
3214+
})),
31893215
(parsedStyleDecl) => parsedStyleDecl.selector
31903216
);
31913217
for (const [selector, parsedStyles] of parsed) {
@@ -3298,7 +3324,8 @@ describe("copy paste", () => {
32983324
boxId__boxlocalid {
32993325
color: red;
33003326
}
3301-
${newBoxId}__${newBoxLocalId} {
3327+
/* escape potential leading digit */
3328+
__${newBoxId}__${newBoxLocalId} {
33023329
color: red;
33033330
}
33043331
`);

apps/builder/app/shared/instance-utils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ export const findClosestDroppableTarget = (
400400
return;
401401
}
402402

403+
const dropTargetParentInstance = instances.get(
404+
instanceSelector[droppableIndex + 1]
405+
);
406+
let dropTargetInstance = instances.get(instanceSelector[droppableIndex]);
407+
// skip collection item when inserting something and go straight into collection instance
408+
if (
409+
dropTargetInstance === undefined &&
410+
dropTargetParentInstance?.component === collectionComponent
411+
) {
412+
instanceSelector = instanceSelector.slice(1);
413+
dropTargetInstance = dropTargetParentInstance;
414+
}
415+
if (dropTargetInstance === undefined) {
416+
return;
417+
}
418+
403419
if (droppableIndex === 0) {
404420
return {
405421
parentSelector: instanceSelector,
@@ -408,8 +424,6 @@ export const findClosestDroppableTarget = (
408424
}
409425

410426
const dropTargetSelector = instanceSelector.slice(droppableIndex);
411-
const dropTargetInstanceId = instanceSelector[droppableIndex];
412-
const dropTargetInstance = instances.get(dropTargetInstanceId);
413427
if (dropTargetInstance === undefined) {
414428
return;
415429
}

0 commit comments

Comments
 (0)