Skip to content

Commit

Permalink
Follow up #427
Browse files Browse the repository at this point in the history
- Fix locked dragging state when dropped splitter to the out of window
- Improve interaction of the splitter
- Leave full width of splitter if it was in the edge
- Prettify the code
  • Loading branch information
yhatt committed Feb 23, 2022
1 parent 0b95928 commit 0f2f4ba
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
17 changes: 11 additions & 6 deletions src/templates/bespoke/bespoke.scss
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,13 @@ $progress-height: 5px;

// Grid layout for presenter view
display: grid;

--bespoke-marp-presenter-split-ratio: 66%;

grid-template:
'current dragbar next' minmax(140px, 1fr)
'current dragbar note' 2fr
'info dragbar note' 3em;
grid-template-columns: var(--bespoke-marp-presenter-split-ratio) 0 1fr;
grid-template-columns:
minmax(3px, var(--bespoke-marp-presenter-split-ratio, 66%))
0 minmax(3px, 1fr);

.bespoke-marp-parent {
grid-area: current;
Expand All @@ -227,17 +226,22 @@ $progress-height: 5px;
.bespoke-marp-presenter-dragbar-container {
grid-area: dragbar;
background: #0288d1; // Marp brand color
cursor: ew-resize;
cursor: col-resize;
width: 6px;
margin-left: -3px;
position: relative;
z-index: 1;
z-index: 10;
opacity: 0;
transition: opacity 0.4s linear 0.1s;

&:hover {
opacity: 1;
}

&.active {
opacity: 1;
transition-delay: 0s;
}
}

// Next slide view
Expand Down Expand Up @@ -274,6 +278,7 @@ $progress-height: 5px;
color: $text-color;
position: relative;
grid-area: note;
z-index: 1;

button {
@extend %button;
Expand Down
57 changes: 34 additions & 23 deletions src/templates/bespoke/presenter/presenter-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,37 @@ const presenterView = (deck) => {
}

const subscribe = (deck) => {
// Splitter
let isDragging = false

const startDragging = () => {
isDragging = true
$(classes.dragbar).classList.add('active')
}

const endDragging = () => {
isDragging = false
$(classes.dragbar).classList.remove('active')
}

const onDragging = (event: MouseEvent) => {
if (!isDragging) return

const splitRatio =
(event.clientX / document.documentElement.clientWidth) * 100

$(classes.container).style.setProperty(
'--bespoke-marp-presenter-split-ratio',
`${Math.max(0, Math.min(100, splitRatio))}%`
)
}

$(classes.dragbar).addEventListener('mousedown', startDragging)
window.addEventListener('mouseup', endDragging)
window.addEventListener('mousemove', onDragging)

// Next slide view
$(classes.nextContainer).addEventListener('click', () => deck.next())
$(classes.dragbar).addEventListener('mousedown', startDragging)
$(classes.container).addEventListener('mouseup', endDragging)
$(classes.container).addEventListener('mousemove', onDragging)
$(classes.infoTimer).addEventListener(
'click',
() => (startTime = new Date())
)

const nextIframe = $(classes.next) as HTMLIFrameElement
const nextNav = createNavigateFunc(nextIframe)
Expand Down Expand Up @@ -202,8 +224,9 @@ const presenterView = (deck) => {
fragmentIndex === fragments.length - 1
})

// Current time
// Current time and presenter timer
let startTime = new Date()

const update = () => {
const time = new Date()

Expand All @@ -222,22 +245,10 @@ const presenterView = (deck) => {

update()
setInterval(update, 250)
}

let isDragging = false

const startDragging = () => (isDragging = true)
const endDragging = () => (isDragging = false)

const onDragging = (event: MouseEvent) => {
if (!isDragging) return

$(classes.container).style.setProperty(
'--bespoke-marp-presenter-split-ratio',
`${(event.clientX / document.documentElement.clientWidth) * 100}%`
)

event.preventDefault()
$(classes.infoTimer).addEventListener('click', () => {
startTime = new Date()
})
}

document.body.appendChild(buildContainer(deck.parent))
Expand Down
21 changes: 12 additions & 9 deletions test/templates/bespoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,32 +992,35 @@ describe("Bespoke template's browser context", () => {

describe('Drag resize', () => {
let spy

beforeAll(() => {
spy = jest
.spyOn(document.documentElement, 'clientWidth', 'get')
.mockReturnValue(1000)
})

afterAll(() => {
spy.mockRestore()
})

it('resizes on drag', () =>
testPresenterView(({ deck }) => {
testPresenterView(() => {
$p(classes.dragbar).dispatchEvent(new MouseEvent('mousedown'))
$p(classes.container).dispatchEvent(
new MouseEvent('mousemove', { clientX: 200 })
)
window.dispatchEvent(new MouseEvent('mousemove', { clientX: 200 }))

expect(
$p(classes.container).style.getPropertyValue(
'--bespoke-marp-presenter-split-ratio'
)
).toBe('20%')
$p(classes.container).dispatchEvent(new MouseEvent('mouseup'))

window.dispatchEvent(new MouseEvent('mouseup'))
}))

it('no resize without drag', () =>
testPresenterView(({ deck }) => {
$p(classes.container).dispatchEvent(
new MouseEvent('mousemove', { clientX: 200 })
)
testPresenterView(() => {
window.dispatchEvent(new MouseEvent('mousemove', { clientX: 200 }))

expect(
$p(classes.container).style.getPropertyValue(
'--bespoke-marp-presenter-split-ratio'
Expand Down

0 comments on commit 0f2f4ba

Please sign in to comment.