-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app-page-builder): expose a useDeleteElement hook
- Loading branch information
Showing
4 changed files
with
95 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/app-page-builder/src/editor/hooks/useDeleteElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useCallback } from "react"; | ||
import { plugins } from "@webiny/plugins"; | ||
import { useEventActionHandler, useFindElementBlock, useUpdateElement } from "~/editor"; | ||
import { DeleteElementActionEvent } from "~/editor/recoil/actions"; | ||
import type { PbBlockVariable, PbEditorElement, PbEditorPageElementPlugin } from "~/types"; | ||
|
||
const removeVariableFromBlock = (block: PbEditorElement, variableId: string) => { | ||
const variables = block.data.variables ?? []; | ||
|
||
const updatedVariables = variables.filter( | ||
(variable: PbBlockVariable) => variable.id.split(".")[0] !== variableId | ||
); | ||
|
||
return { | ||
...block, | ||
data: { | ||
...block.data, | ||
variables: updatedVariables | ||
} | ||
}; | ||
}; | ||
|
||
export const useDeleteElement = () => { | ||
const eventActionHandler = useEventActionHandler(); | ||
const updateElement = useUpdateElement(); | ||
const { findElementBlock } = useFindElementBlock(); | ||
|
||
const canDeleteElement = useCallback((element: PbEditorElement) => { | ||
const plugin = plugins | ||
.byType<PbEditorPageElementPlugin>("pb-editor-page-element") | ||
.find(pl => pl.elementType === element.type); | ||
|
||
if (!plugin) { | ||
return false; | ||
} | ||
|
||
if (typeof plugin.canDelete === "function") { | ||
if (!plugin.canDelete({ element })) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}, []); | ||
|
||
const deleteElement = useCallback(async (element: PbEditorElement): Promise<void> => { | ||
const block = await findElementBlock(element.id); | ||
|
||
// We need to remove element variable from block if it exists | ||
if (element.data?.variableId && block) { | ||
const updatedBlock = removeVariableFromBlock(block, element.data.variableId); | ||
|
||
updateElement(updatedBlock); | ||
} | ||
|
||
eventActionHandler.trigger( | ||
new DeleteElementActionEvent({ | ||
element | ||
}) | ||
); | ||
}, []); | ||
|
||
return { canDeleteElement, deleteElement }; | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/app-page-builder/src/editor/hooks/useFindElementBlock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useCallback } from "react"; | ||
import { useRecoilCallback } from "recoil"; | ||
import { blockByElementSelector } from "~/editor/hooks/useCurrentBlockElement"; | ||
|
||
/** | ||
* Exposes a getter which traverses the element tree upwards from the given element id, and returns an element | ||
* of type "block", if found. | ||
*/ | ||
export const useFindElementBlock = () => { | ||
const findBlock = useRecoilCallback(({ snapshot }) => async (id: string) => { | ||
return await snapshot.getPromise(blockByElementSelector(id)); | ||
}); | ||
|
||
const findElementBlock = useCallback( | ||
async (elementId: string) => { | ||
return findBlock(elementId); | ||
}, | ||
[findBlock] | ||
); | ||
|
||
return { findElementBlock }; | ||
}; |
59 changes: 7 additions & 52 deletions
59
packages/app-page-builder/src/editor/plugins/elementSettings/delete/DeleteAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters