|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { afterEach, expect, it, vi } from "vitest"; |
| 4 | +import { savePlainRotation } from "../../hooks/plainRotation"; |
| 5 | +import type { ElementOffsetStagerDeps } from "../../hooks/elementOffsetStager"; |
| 6 | +import type { DomEditSelection } from "./domEditing"; |
| 7 | +import type { GestureState, UseDomEditOverlayGesturesOptions } from "./domEditOverlayGestures"; |
| 8 | +import { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures"; |
| 9 | + |
| 10 | +const ref = <T>(current: T) => ({ current }); |
| 11 | + |
| 12 | +function evt(clientX: number, clientY: number) { |
| 13 | + return { |
| 14 | + clientX, |
| 15 | + clientY, |
| 16 | + pointerId: 1, |
| 17 | + button: 0, |
| 18 | + altKey: false, |
| 19 | + shiftKey: false, |
| 20 | + preventDefault() {}, |
| 21 | + stopPropagation() {}, |
| 22 | + currentTarget: { setPointerCapture() {} }, |
| 23 | + } as unknown as React.PointerEvent<HTMLDivElement>; |
| 24 | +} |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + document.head.innerHTML = ""; |
| 28 | + document.body.innerHTML = ""; |
| 29 | +}); |
| 30 | + |
| 31 | +it("saves an inline span's turn with the inline-block it was drawn with, after the draft and the hold", async () => { |
| 32 | + const sheet = document.head.appendChild(document.createElement("style")); |
| 33 | + sheet.textContent = "#word { display: inline; }"; |
| 34 | + const element = document.body.appendChild(document.createElement("span")); |
| 35 | + element.id = "word"; |
| 36 | + element.textContent = "Word"; |
| 37 | + const selection = { |
| 38 | + element, |
| 39 | + id: "word", |
| 40 | + selector: "#word", |
| 41 | + selectorIndex: 0, |
| 42 | + sourceFile: "index.html", |
| 43 | + tagName: "span", |
| 44 | + label: "Word", |
| 45 | + textContent: "Word", |
| 46 | + textFields: [], |
| 47 | + capabilities: { canMove: true, canApplyManualRotation: true }, |
| 48 | + } as unknown as DomEditSelection; |
| 49 | + const commitPositionPatchToHtml = vi.fn<ElementOffsetStagerDeps["commitPositionPatchToHtml"]>( |
| 50 | + () => Promise.resolve(), |
| 51 | + ); |
| 52 | + const saves: Promise<void>[] = []; |
| 53 | + const opts: UseDomEditOverlayGesturesOptions = { |
| 54 | + overlayRef: ref<HTMLDivElement | null>(document.createElement("div")), |
| 55 | + iframeRef: ref<HTMLIFrameElement | null>(document.createElement("iframe")), |
| 56 | + boxRef: ref<HTMLDivElement | null>(document.createElement("div")), |
| 57 | + selectionRef: ref<DomEditSelection | null>(selection), |
| 58 | + hoverSelectionRef: ref<DomEditSelection | null>(null), |
| 59 | + overlayRectRef: ref({ left: 0, top: 0, width: 200, height: 100, editScaleX: 1, editScaleY: 1 }), |
| 60 | + groupOverlayItemsRef: ref([]), |
| 61 | + gestureRef: ref<GestureState | null>(null), |
| 62 | + groupGestureRef: ref(null), |
| 63 | + blockedMoveRef: ref(null), |
| 64 | + rafPausedRef: ref(false), |
| 65 | + suppressNextBoxClickRef: ref(false), |
| 66 | + setOverlayRect: () => {}, |
| 67 | + setGroupOverlayItems: () => {}, |
| 68 | + onBlockedMoveRef: ref(() => {}), |
| 69 | + onManualDragStartRef: ref(() => {}), |
| 70 | + onPathOffsetCommitRef: ref(() => {}), |
| 71 | + onGroupPathOffsetCommitRef: ref(() => {}), |
| 72 | + onBoxSizeCommitRef: ref(() => {}), |
| 73 | + onRotationCommitRef: ref((s, next) => { |
| 74 | + const saved = savePlainRotation({ commitPositionPatchToHtml }, s, next); |
| 75 | + saves.push(saved); |
| 76 | + return saved; |
| 77 | + }), |
| 78 | + onCanvasPointerMoveRef: ref(() => Promise.resolve(null)), |
| 79 | + onCanvasMouseDown: () => {}, |
| 80 | + snapGuidesRef: ref(null), |
| 81 | + } as unknown as UseDomEditOverlayGesturesOptions; |
| 82 | + const handlers = createDomEditOverlayGestureHandlers(opts); |
| 83 | + |
| 84 | + handlers.startGesture("rotate", evt(200, 50)); |
| 85 | + handlers.onPointerMove(evt(100, 150)); |
| 86 | + handlers.onPointerUp(evt(100, 150)); |
| 87 | + await Promise.all(saves); |
| 88 | + |
| 89 | + expect(saves).toHaveLength(1); |
| 90 | + expect(commitPositionPatchToHtml.mock.calls[0]![1]).toContainEqual({ |
| 91 | + type: "inline-style", |
| 92 | + property: "display", |
| 93 | + value: "inline-block", |
| 94 | + }); |
| 95 | +}); |
0 commit comments