diff --git a/src/lib/utils/draggable.ts b/src/lib/utils/draggable.ts index 1f758f6a..ce6781dc 100644 --- a/src/lib/utils/draggable.ts +++ b/src/lib/utils/draggable.ts @@ -842,6 +842,9 @@ export class Draggable { let bBottom = b.bottom let bLeft = b.left + let hitX = false + let hitY = false + for (const obstacle of this.obstacleEls) { const o = obstacle.getBoundingClientRect() @@ -859,13 +862,15 @@ export class Draggable { if (this.node === obstacle) continue //· Check X ··································¬ + // too high || too low if (!(top > oBottom || bottom < oTop)) { // Is obstacle on the right or left? - const overRight = right > oLeft && left <= oRight + const overRight = right >= oLeft && left <= oRight if (overRight) { bRight = Math.min(bRight, oLeft) + break } } //⌟ @@ -874,11 +879,16 @@ export class Draggable { // too far left || too far right if (!(left >= oRight || right <= oLeft)) { + //! This fixes the ice-block bug, but only for x-resizing. + // if (!(left >= oRight || right <= oLeft)) { // Is obstacle on top or bottom? const overBottom = bottom > oTop && top <= oBottom if (overBottom) { - bBottom = Math.min(bBottom, oTop) + bBottom = Math.min(bBottom, oTop) + break + + // bRight = b.right } } //⌟ @@ -898,13 +908,15 @@ export class Draggable { let change = false // Move if overflown. - if (overflowX !== 0 && Math.abs(overflowX) <= Math.abs(overflowY)) { + // if (overflowX > 0 || (overflowX < 0 && Math.abs(overflowX) <= Math.abs(overflowY))) { + if (overflowX !== 0) { targetX = Math.min(targetX + overflowX, this.position.x) // Only move if we're not already there. - change = targetX !== this.x + change = targetX !== this.x || targetX !== this.position.x } - if (overflowY !== 0 && Math.abs(overflowY) <= Math.abs(overflowX)) { + // if (overflowY > 0 || (overflowY > 0 && Math.abs(overflowY) <= Math.abs(overflowX))) { + if (overflowY !== 0) { targetY = Math.min(targetY + overflowY, this.position.y) // Only move if we're not already there. change = change || targetY !== this.y @@ -916,6 +928,15 @@ export class Draggable { y: Math.round(targetY), }) } + + if (DEV) { + const xdev = this.node.querySelector('.content') as HTMLElement + if (xdev) { + xdev.innerText = `x: ${this.position.x}, y: ${this.position.y}\n + overflowX: ${overflowX}, overflowY: ${overflowY}\n + targetX: ${targetX}, targetY: ${targetY}\n` + } + } } updatePosition = (targetX: number, targetY: number) => { @@ -1001,6 +1022,19 @@ export class Draggable { } } + // if (DEV) { + // const s = (n = 1) => ' '.repeat(n) + // this.node.innerHTML = ` + // x: ${this.x}, y: ${this.y} + //
rect: { + //
${s(4)}top: ${this.rect.top}, + //
${s(4)}right: ${this.rect.right}, + //
${s(4)}bottom: ${this.rect.bottom}, + //
${s(4)}left: ${this.rect.left}, + //
} + // ` + // } + this.#fireUpdateEvent() } diff --git a/src/routes/demo/window-manager/+page.svelte b/src/routes/demo/window-manager/+page.svelte index 0538bd4c..736a1ba9 100644 --- a/src/routes/demo/window-manager/+page.svelte +++ b/src/routes/demo/window-manager/+page.svelte @@ -5,23 +5,22 @@ const windowManager = new WindowManager({ draggable: { - obstacles: ['.window-4', '.sidebar'], + obstacles: ['.window','.sidebar'], }, resizable: { - // visible: true, sides: ['top', 'right', 'bottom', 'left'], }, animation: { scale: 1.025, duration: 75, }, - // keepZ: true }) // let windows = [1, 2, 3, 4, 5] - let windows = [4, 5] + // let windows = [4, 5] + let windows = [] as number[] - let deleted = windows.map(() => false) + // let deleted = windows.map(() => false)
@@ -29,7 +28,26 @@ - {#each windows as i} +
+

Window 1

+
+ +
+

Window 2

+
+ +
@@ -61,7 +79,7 @@ } .sidebar { - background: var(--bg-b); + background: var(--bg-a); height: 20rem; margin: auto 0; } @@ -75,15 +93,21 @@ width: 240px; height: 120px; + padding: 0.5rem; background-color: rgba(var(--bg-a-rgb), 0.5); backdrop-filter: blur(0.25rem); border: 1px solid var(--bg-b); border-radius: var(--radius); box-shadow: var(--shadow-lg); + + font-size: var(--font-xs); + font-family: var(--font-b); + + overflow: hidden; } - .window-4 { + .window-2 { color: black; background-color: lightslategrey; } @@ -100,25 +124,25 @@ } } - .delete { - all: unset; - position: absolute; - top: 0.5rem; - right: 0.5rem; - // padding: 0.5rem !important; - cursor: pointer; - width: 10px; - height: 10px; - border-radius: 50%; - background: tomato; - filter: brightness(0.25) saturate(0.25); - transition: 0.25s ease-in; - &:hover { - filter: brightness(0.5); - // opacity: 0.5; - } - z-index: 999; - } + // .delete { + // all: unset; + // position: absolute; + // top: 0.5rem; + // right: 0.5rem; + // // padding: 0.5rem !important; + // cursor: pointer; + // width: 10px; + // height: 10px; + // border-radius: 50%; + // background: tomato; + // filter: brightness(0.25) saturate(0.25); + // transition: 0.25s ease-in; + // &:hover { + // filter: brightness(0.5); + // // opacity: 0.5; + // } + // z-index: 999; + // } button { margin: 1rem auto;