-
Notifications
You must be signed in to change notification settings - Fork 96
feat: shift replace block while dropping #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ruchamahabal
wants to merge
1
commit into
frappe:develop
Choose a base branch
from
ruchamahabal:shift-replace
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,118 @@ | ||
| import { pinia } from "../support/component" | ||
|
|
||
| import { setActivePinia } from "pinia" | ||
| import { createRouter, createMemoryHistory } from "vue-router" | ||
| // @ts-ignore | ||
| import { resourcesPlugin } from "frappe-ui" | ||
| import { spritePlugin } from "frappe-ui/icons" | ||
|
|
||
| import StudioCanvas from "@/components/StudioCanvas.vue" | ||
| import Block from "@/utils/block" | ||
| import { COMPONENTS } from "@/data/components" | ||
| import { getBlockInstance, getComponentBlock } from "@/utils/serializer" | ||
| import getBlockTemplate from "@/utils/blockTemplate" | ||
| import { registerGlobalComponents } from "@/globals" | ||
| import useCanvasStore from "@/stores/canvasStore" | ||
|
|
||
| // drags a component from the panel onto the block rendered at `componentId` | ||
| function dragOnto(componentId: string, componentName: string, shiftKey: boolean) { | ||
| const dataTransfer = new DataTransfer() | ||
|
|
||
| return cy | ||
| .get(`.__studio_component__[data-component-id="${componentId}"]`) | ||
| .first() | ||
| .then(([element]) => { | ||
| const canvasStore = useCanvasStore() | ||
| canvasStore.handleDragStart({ target: element, dataTransfer } as unknown as DragEvent, componentName) | ||
|
|
||
| const { left, top, width, height } = element.getBoundingClientRect() | ||
| const options = { | ||
| dataTransfer, | ||
| shiftKey, | ||
| force: true, | ||
| clientX: left + width / 2, | ||
| clientY: top + height / 2, | ||
| } | ||
| cy.wrap(element).trigger("dragover", options).trigger("drop", options) | ||
| cy.then(() => canvasStore.handleDragEnd()) | ||
| }) | ||
| } | ||
|
|
||
| // a new block is auto-selected on nextTick and its editor overlay would swallow the drop | ||
| function clearSelection(canvas: any) { | ||
| cy.get(".editor").should("exist") | ||
| cy.then(() => canvas.clearSelection()) | ||
| cy.get(".editor").should("not.exist") | ||
| } | ||
|
|
||
| describe("dropping a component on top of another block", () => { | ||
| // exposed StudioCanvas instance (defineExpose) used as canvasStore.activeCanvas | ||
| let canvas: any | ||
|
|
||
| beforeEach(() => { | ||
| // block prop/slot init reads Block.components (done in main.ts in the real app) | ||
| Block.setComponents(COMPONENTS) | ||
|
|
||
| setActivePinia(pinia) | ||
| const router = createRouter({ | ||
| history: createMemoryHistory(), | ||
| routes: [{ path: "/", component: { template: "<div />" } }], | ||
| }) | ||
|
|
||
| const rootBlock = getBlockInstance(getBlockTemplate("body")) | ||
|
|
||
| cy.viewport(1440, 900) | ||
| cy.mount(StudioCanvas as any, { | ||
| props: { componentTree: rootBlock }, | ||
| global: { | ||
| plugins: [pinia, router, resourcesPlugin, spritePlugin, { install: registerGlobalComponents }], | ||
| }, | ||
| }).then(({ wrapper }) => { | ||
| canvas = wrapper.vm | ||
| useCanvasStore().activeCanvas = canvas | ||
| }) | ||
|
|
||
| cy.then(() => { | ||
| canvas.canvasProps.scale = 1 | ||
| canvas.canvasProps.translateX = 0 | ||
| canvas.canvasProps.translateY = 0 | ||
| }) | ||
| }) | ||
|
|
||
| it("replaces the hovered block in place with shift", () => { | ||
| let button: Block, badge: Block | ||
|
|
||
| cy.then(() => { | ||
| button = canvas.rootComponent.addChild(getComponentBlock("Button")) | ||
| badge = canvas.rootComponent.addChild(getComponentBlock("Badge")) | ||
| }) | ||
|
|
||
| clearSelection(canvas) | ||
|
|
||
| cy.then(() => dragOnto(button.componentId, "Avatar", true)) | ||
|
|
||
| cy.then(() => { | ||
| const children = canvas.rootComponent.children | ||
| expect(children.map((child: Block) => child.componentName)).to.deep.equal(["Avatar", "Badge"]) | ||
| expect(canvas.rootComponent.getChildById(button.componentId)).to.be.null | ||
| expect(children[1].componentId).to.equal(badge.componentId) | ||
| }) | ||
| }) | ||
|
|
||
| it("drops into the hovered block without shift", () => { | ||
| let container: Block | ||
|
|
||
| cy.then(() => { | ||
| container = canvas.rootComponent.addChild(getBlockInstance(getBlockTemplate("empty-component"))) | ||
| }) | ||
|
|
||
| clearSelection(canvas) | ||
|
|
||
| cy.then(() => dragOnto(container.componentId, "Avatar", false)) | ||
|
|
||
| cy.then(() => { | ||
| expect(canvas.rootComponent.children).to.have.length(1) | ||
| expect(container.children.map((child: Block) => child.componentName)).to.deep.equal(["Avatar"]) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Shift-replacing a slot block with a fragment-editable component, slot insertion clones
newBlock, but the save callback targets the original instance, causing the edited content not to be applied.Context Used: Guidelines for reviewing Frappe Framework applicat... (source)