Improvements and cleanup

This commit is contained in:
trading_peter 2025-01-28 11:07:57 +01:00
parent 8f2586d4b0
commit 3ce55e450d
2 changed files with 58 additions and 43 deletions

View File

@ -11,7 +11,7 @@ class TpHSplitter extends LitElement {
css`
:host {
display: grid;
grid-template-rows: 1fr 5px 1fr;
grid-template-rows: 1fr var(--tp-splitter-width, 5px) 1fr;
}
:host([tophidden]),
@ -62,9 +62,8 @@ class TpHSplitter extends LitElement {
return {
topHidden: { type: Boolean, reflect: true },
bottomHidden: { type: Boolean, reflect: true },
lateResize: { type: Boolean },
rows: { type: String },
dragging: { type: Boolean, reflect: true },
minSideHeight: { type: Number },
};
}
@ -73,15 +72,6 @@ class TpHSplitter extends LitElement {
this._disableDrag = this._disableDrag.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() {
@ -96,6 +86,13 @@ class TpHSplitter extends LitElement {
return this.shadowRoot.querySelector('#splitter')
}
disconnectedCallback() {
super.disconnectedCallback();
document.removeEventListener('mouseup', this._disableDrag)
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
}
_enableDrag(e) {
document.addEventListener('mouseup', this._disableDrag)
document.addEventListener('mousemove', this._resize);
@ -110,24 +107,21 @@ class TpHSplitter extends LitElement {
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
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 }));
}
_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;
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 }));
}
}

View File

@ -11,7 +11,16 @@ class TpVSplitter extends LitElement {
css`
:host {
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,
@ -20,13 +29,13 @@ class TpVSplitter extends LitElement {
overflow: hidden;
}
.splitter {
#splitter {
background: var(--tp-splitter-line-color, #3b3b3b);
cursor: col-resize;
opacity: 1;
}
.splitter:hover {
#splitter:hover {
background: var(--tp-splitter-line-color-hover, #007dd1);
opacity: 0.5;
}
@ -35,17 +44,21 @@ class TpVSplitter extends LitElement {
}
render() {
const { leftHidden, rightHidden } = this;
return html`
<div id="left"><slot name="left"></slot></div>
<div class="splitter" @mousedown=${this._enableDrag}></div>
<div id="right"><slot name="right"></slot></div>
<div id="left" part="left" ?hidden=${leftHidden}><slot name="left"></slot></div>
<div id="splitter" part="splitter" @mousedown=${this._enableDrag}></div>
<div id="right" part="right" ?hidden=${rightHidden}><slot name="right"></slot></div>
`;
}
static get properties() {
return {
columns: { type: String },
_dragging: { type: Boolean },
leftHidden: { type: Boolean },
rightHidden: { type: Boolean },
dragging: { type: Boolean },
minSideWidth: { type: Number },
};
}
@ -64,12 +77,11 @@ class TpVSplitter extends LitElement {
return this.shadowRoot.querySelector('#right');
}
shouldUpdate(changes) {
if (changes.has('columns')) {
this.style.gridTemplateColumns = this.columns;
}
return true;
disconnectedCallback() {
super.disconnectedCallback();
document.removeEventListener('mouseup', this._disableDrag)
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
}
_enableDrag(e) {
@ -77,21 +89,30 @@ class TpVSplitter extends LitElement {
document.addEventListener('mousemove', this._resize);
document.body.style['userSelect'] = 'none';
this._startX = e.clientX;
this._topWidth = this.left.offsetWidth;
this._dragging = true;
this._leftWidth = this.left.offsetWidth;
this.dragging = true;
}
_disableDrag() {
document.removeEventListener('mouseup', this._disableDrag)
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
this._dragging = false;
this.dragging = false;
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateColumns, bubbles: true, composed: true }));
}
_resize(e) {
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 }));
}
}