-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7941382
commit f3ede90
Showing
9 changed files
with
550 additions
and
100 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
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
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
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
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,186 @@ | ||
<template> | ||
<div | ||
:class="classnames" | ||
:style="{ cursor, userSelect }" | ||
@mousedown="onMouseDown" | ||
@touchstart="onMouseDown" | ||
> | ||
<slot></slot> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
const LAYOUT_HORIZONTAL = 'horizontal' | ||
const LAYOUT_VERTICAL = 'vertical' | ||
export default { | ||
name: 'multipane', | ||
props: { | ||
layout: { | ||
type: String, | ||
default: LAYOUT_VERTICAL, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
isResizing: false, | ||
} | ||
}, | ||
computed: { | ||
classnames() { | ||
return [ | ||
'multipane', | ||
'layout-' + this.layout.slice(0, 1), | ||
this.isResizing ? 'is-resizing' : '', | ||
] | ||
}, | ||
cursor() { | ||
return this.isResizing | ||
? this.layout == LAYOUT_VERTICAL | ||
? 'col-resize' | ||
: 'row-resize' | ||
: '' | ||
}, | ||
userSelect() { | ||
return this.isResizing ? 'none' : '' | ||
}, | ||
}, | ||
methods: { | ||
getPageXY(e) { | ||
return e.touches?.[0] || e | ||
}, | ||
onMouseDown(e) { | ||
const resizer = e.target | ||
if ( | ||
resizer.className && | ||
resizer.className.match('multipane-resizer') | ||
) { | ||
let { $el: container, layout } = this | ||
const { pageX: initialPageX, pageY: initialPageY } = | ||
this.getPageXY(e) | ||
let pane = resizer.previousElementSibling | ||
let { | ||
offsetWidth: initialPaneWidth, | ||
offsetHeight: initialPaneHeight, | ||
} = pane | ||
let usePercentage = !!(pane.style.width + '').match('%') | ||
const { addEventListener, removeEventListener } = window | ||
const resize = (initialSize, offset = 0) => { | ||
if (layout == LAYOUT_VERTICAL) { | ||
let containerWidth = container.clientWidth | ||
let paneWidth = initialSize + offset | ||
return (pane.style.width = usePercentage | ||
? (paneWidth / containerWidth) * 100 + '%' | ||
: paneWidth + 'px') | ||
} | ||
if (layout == LAYOUT_HORIZONTAL) { | ||
let containerHeight = container.clientHeight | ||
let paneHeight = initialSize + offset | ||
return (pane.style.height = usePercentage | ||
? (paneHeight / containerHeight) * 100 + '%' | ||
: paneHeight + 'px') | ||
} | ||
} | ||
// This adds is-resizing class to container | ||
this.isResizing = true | ||
// Resize once to get current computed size | ||
let size = resize() | ||
// Trigger paneResizeStart event | ||
this.$emit('paneResizeStart', pane, resizer, size) | ||
const onMouseMove = (e) => { | ||
const { pageX, pageY } = this.getPageXY(e) | ||
size = | ||
layout == LAYOUT_VERTICAL | ||
? resize(initialPaneWidth, pageX - initialPageX) | ||
: resize(initialPaneHeight, pageY - initialPageY) | ||
this.$emit('paneResize', pane, resizer, size) | ||
} | ||
const onMouseUp = () => { | ||
// Run resize one more time to set computed width/height. | ||
size = | ||
layout == LAYOUT_VERTICAL | ||
? resize(pane.clientWidth) | ||
: resize(pane.clientHeight) | ||
// This removes is-resizing class to container | ||
this.isResizing = false | ||
removeEventListener('mousemove', onMouseMove) | ||
removeEventListener('mouseup', onMouseUp) | ||
removeEventListener('touchmove', onMouseMove) | ||
removeEventListener('touchend', onMouseUp) | ||
this.$emit('paneResizeStop', pane, resizer, size) | ||
} | ||
addEventListener('mousemove', onMouseMove) | ||
addEventListener('mouseup', onMouseUp) | ||
addEventListener('touchmove', onMouseMove) | ||
addEventListener('touchend', onMouseUp) | ||
} | ||
}, | ||
}, | ||
} | ||
</script> | ||
<style lang="scss"> | ||
.multipane { | ||
display: flex; | ||
&.layout-h { | ||
flex-direction: column; | ||
} | ||
&.layout-v { | ||
flex-direction: row; | ||
} | ||
} | ||
.multipane > div { | ||
position: relative; | ||
z-index: 1; | ||
} | ||
.multipane-resizer { | ||
display: block; | ||
position: relative; | ||
z-index: 2; | ||
margin: 0; | ||
background-color: #ccc; | ||
} | ||
.layout-h > .multipane-resizer { | ||
width: 100%; | ||
height: 10px; | ||
margin-top: -10px; | ||
top: 5px; | ||
cursor: row-resize; | ||
} | ||
.layout-v > .multipane-resizer { | ||
width: 10px; | ||
height: 100%; | ||
margin-left: -10px; | ||
left: 5px; | ||
cursor: col-resize; | ||
} | ||
</style> |
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,3 @@ | ||
<template> | ||
<div class="multipane-resizer"><slot></slot></div> | ||
</template> |
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
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
Oops, something went wrong.