Skip to content

Commit 48499fa

Browse files
authored
feat: add refresh material api (#1320)
1 parent c3ae3b0 commit 48499fa

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

packages/plugins/materials/src/composable/useMaterial.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ const addComponentSnippets = (componentSnippets: Snippet[] | undefined, snippets
285285
if (snippetsMap.has(snippetGroup.group)) {
286286
snippetsMap.get(snippetGroup.group)!.children.push(...snippetGroup.children)
287287
} else {
288-
snippetsData.push(snippetGroup)
288+
const snippetGroupClone = deepClone(snippetGroup)
289+
snippetsData.push(snippetGroupClone)
290+
snippetsMap.set(snippetGroup.group, snippetGroupClone)
289291
}
290292
})
291293

@@ -500,6 +502,12 @@ const initMaterial = ({ isInit = true, appData = {} }: InitMaterialOptions = {})
500502
}
501503
}
502504

505+
const refreshMaterial = async () => {
506+
clearMaterials()
507+
initMaterial()
508+
await fetchMaterial()
509+
}
510+
503511
/**
504512
* 根据组名获取指定分组组件
505513
* @param components 所有组件
@@ -540,6 +548,7 @@ export default function () {
540548
getBlockCompileRes,
541549
addBlockResources,
542550
updateBlockCompileCache,
543-
getComponentsByGroup
551+
getComponentsByGroup,
552+
refreshMaterial
544553
}
545554
}

packages/plugins/materials/src/meta/component/src/Main.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</template>
3636

3737
<script lang="ts">
38-
import { inject, onMounted, reactive, ref, watchEffect } from 'vue'
38+
import { inject, onMounted, reactive, ref, watch, watchEffect } from 'vue'
3939
import { Collapse, CollapseItem, Search } from '@opentiny/vue'
4040
import { SearchEmpty, CanvasDragItem } from '@opentiny/tiny-engine-common'
4141
import i18n from '@opentiny/tiny-engine-common/js/i18n'
@@ -57,7 +57,6 @@ export default {
5757
const { generateNode, materialState, getComponentsByGroup } = useMaterial()
5858
const gridTemplateColumns = ref(COMPONENT_PANEL_COLUMNS)
5959
const panelState = inject('panelState', {})
60-
const { components } = materialState
6160
const { locale } = i18n.global
6261
6362
const fetchComponents = (components, name) => {
@@ -91,10 +90,10 @@ export default {
9190
const initComponents = () => {
9291
const groupName = panelState.materialGroup
9392
if (groupName) {
94-
return getComponentsByGroup(components, groupName)
93+
return getComponentsByGroup(materialState.components, groupName)
9594
}
9695
97-
return components
96+
return materialState.components
9897
}
9998
10099
const state = reactive({
@@ -104,13 +103,23 @@ export default {
104103
})
105104
106105
watchEffect(() => {
107-
state.activeName = [...Array(components.length).keys()]
106+
state.activeName = [...Array(materialState.components.length).keys()]
108107
})
109108
110109
const change = (value) => {
111-
state.components = fetchComponents(components, value)
110+
state.components = fetchComponents(materialState.components, value)
112111
}
113112
113+
watch(
114+
() => materialState.components,
115+
(value) => {
116+
state.components = fetchComponents(value, state.searchValue)
117+
},
118+
{
119+
deep: true
120+
}
121+
)
122+
114123
const componentClick = (data) => {
115124
const { isShortcutPanel, emitEvent } = panelState
116125
const { addComponent } = useCanvas().canvasApi.value

0 commit comments

Comments
 (0)