Improvements and cleanup
This commit is contained in:
parent
8f2586d4b0
commit
3ce55e450d
@ -11,7 +11,7 @@ class TpHSplitter extends LitElement {
|
|||||||
css`
|
css`
|
||||||
:host {
|
:host {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: 1fr 5px 1fr;
|
grid-template-rows: 1fr var(--tp-splitter-width, 5px) 1fr;
|
||||||
}
|
}
|
||||||
|
|
||||||
:host([tophidden]),
|
:host([tophidden]),
|
||||||
@ -62,9 +62,8 @@ class TpHSplitter extends LitElement {
|
|||||||
return {
|
return {
|
||||||
topHidden: { type: Boolean, reflect: true },
|
topHidden: { type: Boolean, reflect: true },
|
||||||
bottomHidden: { type: Boolean, reflect: true },
|
bottomHidden: { type: Boolean, reflect: true },
|
||||||
lateResize: { type: Boolean },
|
|
||||||
rows: { type: String },
|
|
||||||
dragging: { type: Boolean, reflect: true },
|
dragging: { type: Boolean, reflect: true },
|
||||||
|
minSideHeight: { type: Number },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,15 +72,6 @@ class TpHSplitter extends LitElement {
|
|||||||
|
|
||||||
this._disableDrag = this._disableDrag.bind(this);
|
this._disableDrag = this._disableDrag.bind(this);
|
||||||
this._resize = this._resize.bind(this);
|
this._resize = this._resize.bind(this);
|
||||||
this._bufferedDelta = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
shouldUpdate(changes) {
|
|
||||||
if (changes.has('rows')) {
|
|
||||||
this.style.gridTemplateRows = this.rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get top() {
|
get top() {
|
||||||
@ -96,6 +86,13 @@ class TpHSplitter extends LitElement {
|
|||||||
return this.shadowRoot.querySelector('#splitter')
|
return this.shadowRoot.querySelector('#splitter')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disconnectedCallback() {
|
||||||
|
super.disconnectedCallback();
|
||||||
|
document.removeEventListener('mouseup', this._disableDrag)
|
||||||
|
document.removeEventListener('mousemove', this._resize);
|
||||||
|
document.body.style['userSelect'] = '';
|
||||||
|
}
|
||||||
|
|
||||||
_enableDrag(e) {
|
_enableDrag(e) {
|
||||||
document.addEventListener('mouseup', this._disableDrag)
|
document.addEventListener('mouseup', this._disableDrag)
|
||||||
document.addEventListener('mousemove', this._resize);
|
document.addEventListener('mousemove', this._resize);
|
||||||
@ -110,24 +107,21 @@ class TpHSplitter extends LitElement {
|
|||||||
document.removeEventListener('mousemove', this._resize);
|
document.removeEventListener('mousemove', this._resize);
|
||||||
document.body.style['userSelect'] = '';
|
document.body.style['userSelect'] = '';
|
||||||
this.dragging = false;
|
this.dragging = false;
|
||||||
|
|
||||||
if (this.lateResize && this._bufferedDelta) {
|
|
||||||
this.splitter.style.transform = '';
|
|
||||||
this.style.gridTemplateRows = `${this._topHeight + this._bufferedDelta}px 5px 1fr`;
|
|
||||||
this._bufferedDelta = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateRows, bubbles: true, composed: true }));
|
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateRows, bubbles: true, composed: true }));
|
||||||
}
|
}
|
||||||
|
|
||||||
_resize(e) {
|
_resize(e) {
|
||||||
if (this.lateResize) {
|
|
||||||
this._bufferedDelta = e.clientY - this._startY;
|
|
||||||
this.splitter.style.transform = `translateY(${this._bufferedDelta}px)`;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const delta = e.clientY - this._startY;
|
const delta = e.clientY - this._startY;
|
||||||
this.style.gridTemplateRows = `${this._topHeight + delta}px 5px 1fr`;
|
const totalHeight = this.offsetHeight;
|
||||||
|
const minHeight = this.minSideHeight || 0;
|
||||||
|
|
||||||
|
// Get splitter width from CSS variable, fallback to 5px.
|
||||||
|
const splitterWidth = parseInt(getComputedStyle(this).getPropertyValue('--tp-splitter-width')) || 5;
|
||||||
|
|
||||||
|
// Calculate new top height while ensuring it stays within bounds.
|
||||||
|
let newTopHeight = Math.max(minHeight, Math.min(this._topHeight + delta, totalHeight - splitterWidth - minHeight));
|
||||||
|
|
||||||
|
this.style.gridTemplateRows = `${newTopHeight}px ${splitterWidth}px 1fr`;
|
||||||
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,16 @@ class TpVSplitter extends LitElement {
|
|||||||
css`
|
css`
|
||||||
:host {
|
:host {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 1fr 5px 1fr;
|
grid-template-columns: 1fr var(--tp-splitter-width, 5px) 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
:host([lefthidden]),
|
||||||
|
:host([righthidden]) {
|
||||||
|
grid-template-rows: 1fr !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
[hidden] {
|
||||||
|
display: none !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
#left,
|
#left,
|
||||||
@ -20,13 +29,13 @@ class TpVSplitter extends LitElement {
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
.splitter {
|
#splitter {
|
||||||
background: var(--tp-splitter-line-color, #3b3b3b);
|
background: var(--tp-splitter-line-color, #3b3b3b);
|
||||||
cursor: col-resize;
|
cursor: col-resize;
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.splitter:hover {
|
#splitter:hover {
|
||||||
background: var(--tp-splitter-line-color-hover, #007dd1);
|
background: var(--tp-splitter-line-color-hover, #007dd1);
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
@ -35,17 +44,21 @@ class TpVSplitter extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const { leftHidden, rightHidden } = this;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div id="left"><slot name="left"></slot></div>
|
<div id="left" part="left" ?hidden=${leftHidden}><slot name="left"></slot></div>
|
||||||
<div class="splitter" @mousedown=${this._enableDrag}></div>
|
<div id="splitter" part="splitter" @mousedown=${this._enableDrag}></div>
|
||||||
<div id="right"><slot name="right"></slot></div>
|
<div id="right" part="right" ?hidden=${rightHidden}><slot name="right"></slot></div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
columns: { type: String },
|
leftHidden: { type: Boolean },
|
||||||
_dragging: { type: Boolean },
|
rightHidden: { type: Boolean },
|
||||||
|
dragging: { type: Boolean },
|
||||||
|
minSideWidth: { type: Number },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,12 +77,11 @@ class TpVSplitter extends LitElement {
|
|||||||
return this.shadowRoot.querySelector('#right');
|
return this.shadowRoot.querySelector('#right');
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldUpdate(changes) {
|
disconnectedCallback() {
|
||||||
if (changes.has('columns')) {
|
super.disconnectedCallback();
|
||||||
this.style.gridTemplateColumns = this.columns;
|
document.removeEventListener('mouseup', this._disableDrag)
|
||||||
}
|
document.removeEventListener('mousemove', this._resize);
|
||||||
|
document.body.style['userSelect'] = '';
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_enableDrag(e) {
|
_enableDrag(e) {
|
||||||
@ -77,21 +89,30 @@ class TpVSplitter extends LitElement {
|
|||||||
document.addEventListener('mousemove', this._resize);
|
document.addEventListener('mousemove', this._resize);
|
||||||
document.body.style['userSelect'] = 'none';
|
document.body.style['userSelect'] = 'none';
|
||||||
this._startX = e.clientX;
|
this._startX = e.clientX;
|
||||||
this._topWidth = this.left.offsetWidth;
|
this._leftWidth = this.left.offsetWidth;
|
||||||
this._dragging = true;
|
this.dragging = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_disableDrag() {
|
_disableDrag() {
|
||||||
document.removeEventListener('mouseup', this._disableDrag)
|
document.removeEventListener('mouseup', this._disableDrag)
|
||||||
document.removeEventListener('mousemove', this._resize);
|
document.removeEventListener('mousemove', this._resize);
|
||||||
document.body.style['userSelect'] = '';
|
document.body.style['userSelect'] = '';
|
||||||
this._dragging = false;
|
this.dragging = false;
|
||||||
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateColumns, bubbles: true, composed: true }));
|
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateColumns, bubbles: true, composed: true }));
|
||||||
}
|
}
|
||||||
|
|
||||||
_resize(e) {
|
_resize(e) {
|
||||||
const delta = e.clientX - this._startX;
|
const delta = e.clientX - this._startX;
|
||||||
this.style.gridTemplateColumns = `${this._topWidth + delta}px 5px 1fr`;
|
const totalWidth = this.offsetWidth;
|
||||||
|
const minWidth = this.minSideWidth || 0; // Minimum width in pixels for each side
|
||||||
|
|
||||||
|
// Get splitter width from CSS variable or default to 5 pixels.
|
||||||
|
const splitterWidth = parseInt(getComputedStyle(this).getPropertyValue('--tp-splitter-width')) || 5;
|
||||||
|
|
||||||
|
// Calculate new left width while ensuring it stays within bounds.
|
||||||
|
let newLeftWidth = Math.max(minWidth, Math.min(this._leftWidth + delta, totalWidth - splitterWidth - minWidth));
|
||||||
|
|
||||||
|
this.style.gridTemplateColumns = `${newLeftWidth}px ${splitterWidth}px 1fr`;
|
||||||
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user