fix:调整删除逻辑

This commit is contained in:
lhj
2024-11-15 00:31:19 +08:00
parent 670e71cd7b
commit f01bdced44
3 changed files with 39 additions and 16 deletions

View File

@ -77,7 +77,7 @@ let componentsList = [];
const store = useSchemeStore(); const store = useSchemeStore();
watch(store, (n) => { watch(store, (n) => {
console.log("store发生了变化", n); // console.log("store发生了变化", n);
}); });
const baseScheme = { const baseScheme = {
@ -158,7 +158,7 @@ function clone(element: Record<'name' | 'id' | 'type' | 'props' | 'class' | 'tex
const onEnd = (event: DraggableEvent) => { const onEnd = (event: DraggableEvent) => {
// console.log("onEnd", event); // console.log("onEnd", event);
store.nowComponentsData = event.clonedData; store.updateNowScheme(event.clonedData);
}; };
const onStart = (event) => { const onStart = (event) => {

View File

@ -76,7 +76,8 @@ const isHovered = ref(false);
const handleClick = () => { const handleClick = () => {
const currentComponent = getCurrentSchemeObj(); const currentComponent = getCurrentSchemeObj();
if (currentComponent) { if (currentComponent) {
store.nowComponentsData = currentComponent
store.updateNowScheme(currentComponent);
console.log(`Component with id ${currentComponent?.id} was clicked.`); console.log(`Component with id ${currentComponent?.id} was clicked.`);
// 你可以在这里执行更多的逻辑,例如发出一个事件或调用一个方法 // 你可以在这里执行更多的逻辑,例如发出一个事件或调用一个方法
} }
@ -137,7 +138,7 @@ const adjustHeaderPosition = () => {
}; };
onMounted(() => { onMounted(() => {
console.log(props.componentData); console.log("组件挂载后",props.componentData);
}); });
watch(() => componentSelected.value, () => { watch(() => componentSelected.value, () => {

View File

@ -1,16 +1,27 @@
import { defineStore } from 'pinia'; import {defineStore} from 'pinia';
import { IPageComponent } from '@/type/IPageComponent'; import {IPageComponent} from '@/type/IPageComponent';
import { sha256 } from 'js-sha256'; import {sha256} from 'js-sha256';
// 缓存对象 // 缓存对象
const idToObjectCache = new Map<string, IPageComponent>(); const idToObjectCache = new Map<string, IPageComponent>();
// 栈维护函数
function pushToStack(stack: string[], id: string): void {
if (!stack.includes(id)) {
stack.push(id);
}
}
function popFromStack(stack: string[]): string | undefined {
return stack.pop();
}
function findObjectById(obj: any, targetId: string) { function findObjectById(obj: any, targetId: string) {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) { for (let i = 0; i < obj.length; i++) {
const item = obj[i]; const item = obj[i];
if (item.id === targetId) { if (item.id === targetId) {
return { item, index: i, parent: obj }; return {item, index: i, parent: obj};
} }
const found = findObjectById(item, targetId); const found = findObjectById(item, targetId);
if (found) return found; if (found) return found;
@ -38,6 +49,7 @@ export const useSchemeStore = defineStore('scheme', {
currentIndex: -1, // 当前索引 currentIndex: -1, // 当前索引
currentLength: 0, // 当前队列中有效元素的数量 currentLength: 0, // 当前队列中有效元素的数量
currentHash: '', // 当前状态的哈希值 currentHash: '', // 当前状态的哈希值
selectedComponentStack: [] as string[], // 最近选择组件栈
}), }),
getters: { getters: {
canUndo: (state) => state.currentIndex >= 0, canUndo: (state) => state.currentIndex >= 0,
@ -76,23 +88,33 @@ export const useSchemeStore = defineStore('scheme', {
} }
this.saveNewState(currentStateHash); this.saveNewState(currentStateHash);
}, },
updateNowScheme(updates: Partial<IPageComponent>) {
this.nowComponentsData = updates;
pushToStack(this.selectedComponentStack, this.nowComponentsData.id);
},
deleteScheme(id: string) { deleteScheme(id: string) {
console.log("删除前,组件id",id)
console.log("删除前,栈",this.selectedComponentStack)
const currentStateHash = this.currentHash; const currentStateHash = this.currentHash;
const objToDelete = findObjectById(this.previewScheme, id); const objToDelete = findObjectById(this.previewScheme, id);
if (objToDelete) { if (objToDelete) {
const { item, index, parent } = objToDelete; const {item, index, parent} = objToDelete;
parent.splice(index, 1); parent.splice(index, 1);
idToObjectCache.delete(id); // 从缓存中删除 idToObjectCache.delete(id); // 从缓存中删除
// 清理 nowComponentsData
if (this.nowComponentsData.id === id) {
this.nowComponentsData = {} as IPageComponent;
}
// 清理 history 中的引用 // 清理 history 中的引用
this.cleanupReferences(id); this.cleanupReferences(id);
} }
this.saveNewState(currentStateHash); this.saveNewState(currentStateHash);
popFromStack(this.selectedComponentStack);
const preComponentId=popFromStack(this.selectedComponentStack)
console.log("删除后,栈",this.selectedComponentStack)
console.log("删除后,前一个组件id",preComponentId)
const preObj=this.getSchemeObj(preComponentId)
console.log("删除后,前一个组件",preObj)
this.nowComponentsData = preObj;
}, },
cleanupReferences(id: string) { cleanupReferences(id: string) {
// 清理 nowComponentsData // 清理 nowComponentsData
@ -106,7 +128,7 @@ export const useSchemeStore = defineStore('scheme', {
if (historyState) { if (historyState) {
const obj = findObjectById(historyState, id); const obj = findObjectById(historyState, id);
if (obj) { if (obj) {
const { parent, index } = obj; const {parent, index} = obj;
parent.splice(index, 1); parent.splice(index, 1);
} }
} }