-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(setting): add split preview setting
- Loading branch information
1 parent
1b15781
commit d17bab4
Showing
5 changed files
with
542 additions
and
206 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
const MIN_WIDTH = 240; | ||
|
||
const addDraggable = (tabId: string) => { | ||
const settings = document.querySelector(`#${tabId}_settings`) as HTMLDivElement; | ||
const checkDraggableLine = document.querySelector( | ||
`#tab_${tabId} .draggable-line`, | ||
) as HTMLDivElement; | ||
if (!settings || checkDraggableLine) return; | ||
|
||
settings.style.minWidth = `min(${MIN_WIDTH}px, 100%)`; | ||
|
||
const lineWrapper = document.createElement('div'); | ||
lineWrapper.classList.add('draggable-line'); | ||
|
||
settings.after(lineWrapper); | ||
|
||
const container: HTMLElement | any = settings.parentElement; | ||
container.classList.add('draggable-container'); | ||
|
||
let results: HTMLDivElement = document.querySelector(`#${tabId}_results`) as HTMLDivElement; | ||
|
||
if (!results) return; | ||
|
||
if (tabId === 'extras') results = results.parentElement as HTMLDivElement; | ||
|
||
results.style.minWidth = `${MIN_WIDTH}px`; | ||
|
||
let linePosition = 50; | ||
settings.style.flexBasis = `${linePosition}%`; | ||
results.style.flexBasis = `${100 - linePosition}%`; | ||
|
||
let isDragging = false; | ||
|
||
lineWrapper.addEventListener('mousedown', (e) => { | ||
isDragging = true; | ||
e.preventDefault(); | ||
}); | ||
|
||
document.addEventListener('mousemove', (event) => { | ||
if (!isDragging) return; | ||
|
||
const tab = document.querySelector(`#tab_${tabId}`) as HTMLDivElement; | ||
if (!tab) return; | ||
|
||
let offsetX = tab.offsetLeft; | ||
let parent = tab.offsetParent as HTMLDivElement; | ||
|
||
while (parent) { | ||
offsetX += parent.offsetLeft; | ||
parent = parent.offsetParent as HTMLDivElement; | ||
} | ||
|
||
const containerWidth = container.offsetWidth; | ||
const mouseX = event.clientX; | ||
const linePosition = ((mouseX - offsetX) / containerWidth) * 100; | ||
|
||
if (linePosition <= (MIN_WIDTH / containerWidth) * 100) return; | ||
if (linePosition >= (1 - MIN_WIDTH / containerWidth) * 100) return; | ||
|
||
settings.style.flexBasis = `${linePosition}%`; | ||
results.style.flexBasis = `${100 - linePosition}%`; | ||
}); | ||
|
||
document.addEventListener('mouseup', () => { | ||
isDragging = false; | ||
}); | ||
}; | ||
|
||
export default () => { | ||
addDraggable('txt2img'); | ||
addDraggable('img2img'); | ||
const extrasSetting = document.querySelector('#extras_results')?.parentElement | ||
?.previousElementSibling as HTMLDivElement; | ||
if (extrasSetting) { | ||
extrasSetting.id = 'extras_settings'; | ||
addDraggable('extras'); | ||
} | ||
}; |