Skip to content

Commit f563e30

Browse files
committed
Allow custom menu & expose more methods
1 parent 6d2b9e8 commit f563e30

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

src/App.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref } from "vue";
2+
import { computed, ref } from "vue";
33
import LocalGridPlan from './components/GridPlan.vue';
44
import { GridPlanItem } from "grid-plan";
55
import { GridPlanItemType } from "grid-plan";
@@ -102,6 +102,7 @@ const config = ref({
102102
boxThickness: 0.3,
103103
boxColor: '#5A5A5A',
104104
boxHeight: 1,
105+
showMenu: false,
105106
})
106107
107108
// setTimeout(() => {
@@ -138,6 +139,14 @@ function createdItem(item) {
138139
// console.log('CREATED',item)
139140
}
140141
142+
const utils = computed(() => {
143+
const ready = !!plan.value
144+
return {
145+
setActiveType: ready ? plan.value.setActiveType : (_) => {},
146+
147+
}
148+
})
149+
141150
</script>
142151

143152
<template>
@@ -183,6 +192,12 @@ function createdItem(item) {
183192
</svg>
184193
</template>
185194
</GridPlan> -->
195+
196+
<div style="padding:12px; background: #5A5A5A">
197+
<div v-for="type in types" @click="utils.setActiveType(type)">
198+
{{ type.description }}
199+
</div>
200+
</div>
186201

187202
<LocalGridPlan
188203
:availableTypes="types"

src/components/GridPlan.vue

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,14 @@ function getFocusState(item) {
157157
}
158158
159159
defineExpose({
160+
deleteItem,
161+
focusItem,
162+
getActiveItem: () => entity.value,
163+
getFocusState,
160164
getItems,
161-
unselect
165+
selectItem,
166+
setActiveType,
167+
unselect,
162168
})
163169
164170
</script>
@@ -177,22 +183,24 @@ defineExpose({
177183
@unselect="unselect"
178184
/>
179185
180-
<details v-if="finalConfig.useAccordionMenu" class="grid-plan-menu">
181-
<summary class="grid-plan-menu__summary">
182-
{{ finalConfig.accordionMenuTitle }}
183-
</summary>
184-
<div class="grid-plan-menu__body">
186+
<template v-if="finalConfig.showMenu">
187+
<details v-if="finalConfig.useAccordionMenu" class="grid-plan-menu">
188+
<summary class="grid-plan-menu__summary">
189+
{{ finalConfig.accordionMenuTitle }}
190+
</summary>
191+
<div class="grid-plan-menu__body">
192+
<div v-for="t in availableTypes" @click="setActiveType(t)">
193+
<slot name="availableType" v-bind="{ availableType: t }"/>
194+
</div>
195+
</div>
196+
</details>
197+
198+
<div v-else class="grid-plan-menu">
185199
<div v-for="t in availableTypes" @click="setActiveType(t)">
186200
<slot name="availableType" v-bind="{ availableType: t }"/>
187201
</div>
188202
</div>
189-
</details>
190-
191-
<div v-else class="grid-plan-menu">
192-
<div v-for="t in availableTypes" @click="setActiveType(t)">
193-
<slot name="availableType" v-bind="{ availableType: t }"/>
194-
</div>
195-
</div>
203+
</template>
196204
197205
<details v-if="finalConfig.showInventory && items.length" class="grid-plan-inventory">
198206
<summary class="grid-plan-inventory__summary">
@@ -204,6 +212,7 @@ defineExpose({
204212
</div>
205213
</div>
206214
</details>
215+
207216
<Grid
208217
:readonly="readonly"
209218
:key="`2d_${step}`"

src/default_config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
"showBox": true,
2929
"boxThickness": 0.3,
3030
"boxHeight": 1,
31-
"boxColor": "#5A5A5A"
31+
"boxColor": "#5A5A5A",
32+
"showMenu": true
3233
}

types/grid-plan.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ declare module "grid-plan" {
99
export type ItemStateGetter<T extends GridPlanItem = GridPlanItem> =
1010
((item: Readonly<T>) => boolean) & (() => boolean);
1111

12+
export type GridPlanExpose = {
13+
deleteItem(item: GridPlanItem): void
14+
focusItem(item: GridPlanItem): void
15+
getActiveItem(): GridPlanItem
16+
getFocusState(item: GridPlanItem): boolean
17+
getItems(): GridPlanItem[]
18+
selectItem(item: GridPlanItem): void
19+
setActiveType(t: GridPlanItemType): void
20+
unselect(): void
21+
}
22+
1223
export const GridPlan: DefineComponent<{
1324
placedItems: GridPlanItem[];
1425
availableTypes: GridPlanItemType[];
1526
readonly?: boolean;
1627
config?: GridPlanConfig;
17-
}>;
28+
}, GridPlanExpose>;
1829

1930
export type Coordinates = {
2031
x?: number;
@@ -79,6 +90,7 @@ declare module "grid-plan" {
7990
boxThickness?: number;
8091
boxHeight?: number;
8192
boxColor?: string;
93+
showMenu?: boolean;
8294
};
8395

8496
/**

0 commit comments

Comments
 (0)