Skip to content

feat: show view size while dragging split pane #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Repl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ provide('clear-console', toRef(props, 'clearConsole'))
provide('preview-options', props.previewOptions)
provide('theme', toRef(props, 'theme'))
provide('preview-theme', toRef(props, 'previewTheme'))
provide('preview-ref', () => outputRef.value?.previewRef?.container)

/**
* Reload the preview iframe
*/
Expand Down
38 changes: 37 additions & 1 deletion src/SplitPane.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<script setup lang="ts">
import { computed, inject, reactive, ref, toRef } from 'vue'
import {
type MaybeRefOrGetter,
computed,
inject,
reactive,
ref,
toRef,
toValue,
} from 'vue'
import { injectKeyStore } from './types'

const props = defineProps<{ layout?: 'horizontal' | 'vertical' }>()
const isVertical = computed(() => props.layout === 'vertical')

const container = ref()
const previewRef = inject<MaybeRefOrGetter<HTMLDivElement>>('preview-ref')!

// mobile only
const store = inject(injectKeyStore)!
Expand All @@ -14,6 +23,8 @@ const showOutput = toRef(store, 'showOutput')
const state = reactive({
dragging: false,
split: 50,
viewHeight: 0,
viewWidth: 0,
})

const boundSplit = computed(() => {
Expand All @@ -28,6 +39,8 @@ function dragStart(e: MouseEvent) {
state.dragging = true
startPosition = isVertical.value ? e.pageY : e.pageX
startSplit = boundSplit.value

changeViewSize()
}

function dragMove(e: MouseEvent) {
Expand All @@ -38,12 +51,24 @@ function dragMove(e: MouseEvent) {
: container.value.offsetWidth
const dp = position - startPosition
state.split = startSplit + +((dp / totalSize) * 100).toFixed(2)

changeViewSize()
}
}

function dragEnd() {
state.dragging = false
}

function changeViewSize() {
const el = toValue(previewRef)
state.viewHeight = el.offsetHeight
state.viewWidth = el.offsetWidth
}

defineExpose({
changeViewSize,
})
</script>

<template>
Expand All @@ -70,6 +95,9 @@ function dragEnd() {
class="right"
:style="{ [isVertical ? 'height' : 'width']: 100 - boundSplit + '%' }"
>
<div class="view-size" v-show="state.dragging">
{{ `${state.viewWidth}px x ${state.viewHeight}px` }}
</div>
<slot name="right" />
</div>

Expand Down Expand Up @@ -97,6 +125,14 @@ function dragEnd() {
position: relative;
height: 100%;
}
.view-size {
position: absolute;
top: 40px;
left: 10px;
font-size: 12px;
color: var(--text-light);
z-index: 100;
}
.left {
border-right: 1px solid var(--border);
}
Expand Down
2 changes: 1 addition & 1 deletion src/output/Output.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function reload() {
previewRef.value?.reload()
}

defineExpose({ reload })
defineExpose({ reload, previewRef })
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion src/output/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function reload() {
sandbox.contentWindow?.location.reload()
}

defineExpose({ reload })
defineExpose({ reload, container })
</script>

<template>
Expand Down