Skip to content

Commit

Permalink
Toggle elements in tree when selected on right panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Kévin Lanvin committed Jul 30, 2024
1 parent 203875d commit 21ec78a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 28 additions & 1 deletion _dev/src/components/tree/Element.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ import {
FieldContent,
} from "../../core-logic/entities/ComponentContent";
import { useZoneStore } from "../../core-logic/store/zoneStore.js";
import { ref, nextTick, computed } from "vue";
import { ref, nextTick, computed, watch, onMounted } from "vue";
import Subfields from "./Subfields.vue";
import Icon from "../Icon.vue";
import { PrimitiveFieldType } from "../../core-logic/entities/ElementType";
import { PrimitiveFieldContent } from "../../core-logic/entities/PrimitiveFieldContent";
import { useNavigationStore } from "../../core-logic/store/navigationStore";
import { storeToRefs } from "pinia";
import {
findComponentById,
findComponentByIdInRepeater,
findComponentByIdInSubfields,
SearchComponentResult,
} from "../../core-logic/utils/finder";
import { Repeater } from "../../core-logic/entities/Repeater";
const { element, children, isDeletable, isMovable, isDuplicable } =
defineProps<{
Expand All @@ -97,6 +104,26 @@ const { selectedElement } = storeToRefs(navigationStore);
const isSelected = computed(() => selectedElement.value?.id === element.id);
const checkToggle = () => {
let foundComponent: SearchComponentResult;
if (element.type === "repeater")
foundComponent = findComponentByIdInRepeater(
element as Repeater<FieldContent>,
selectedElement.value?.id
);
if (element.type === "component" || element.type === "block")
foundComponent = findComponentByIdInSubfields(
element as BlockContent | ComponentContent,
selectedElement.value?.id
);
if (foundComponent) {
isCollapsed.value = false;
}
};
watch(selectedElement, checkToggle);
onMounted(checkToggle);
const deleteMap = {
block: zoneStore.deleteBlockById,
component: zoneStore.deleteComponentById,
Expand Down
11 changes: 7 additions & 4 deletions _dev/src/core-logic/utils/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const findComponentByIdInBlock = (
return findComponentByIdInSubfields(tree, id);
};

const findComponentByIdInSubfields = (
export const findComponentByIdInSubfields = (
tree: BlockContent | ComponentContent,
id: string
) => {
Expand Down Expand Up @@ -73,15 +73,18 @@ const findComponentByIdInRepeaterFields = (
}
};

const findComponentByIdInRepeater = (
repeater: Repeater<ComponentContent>,
export const findComponentByIdInRepeater = (
repeater: Repeater<FieldContent>,
id: string
) => {
for (const field of repeater.sub_elements) {
// Check if subfield matches
if (field.id === id) return { node: field, parent: repeater };
// Else check recursively
const foundElement = findComponentByIdInBlock(field, id);
const foundElement = findComponentByIdInBlock(
field as ComponentContent,
id
);
if (foundElement) return foundElement;
}
};
Expand Down

0 comments on commit 21ec78a

Please sign in to comment.