Skip to content

Commit

Permalink
feat(client): add experimental component AutoFitText
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2021
1 parent b5ba710 commit fa342f9
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
87 changes: 87 additions & 0 deletions packages/client/builtin/AutoFitText.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!--
[Expiremental]
Think this component as the TextBox you that will see
in PowerPoint or Keynote. It will automaitcally resize
the font size based on it's content to fit them in.
Usage:
<AutoFitText modelValue="text"/>
or
<AutoFitText :max="80" :min="100" v-model="text"/>
-->

<script setup lang="ts">
import { useElementSize, useVModel } from '@vueuse/core'
import { computed, defineEmit, defineProps, ref, watch } from 'vue'
const emit = defineEmit()
const props = defineProps({
modelValue: {
default: '',
},
max: {
default: 100,
},
min: {
default: 30,
},
})
const container = ref<HTMLDivElement>()
const inner = ref<HTMLDivElement>()
const size = ref(100)
// @ts-ignore
const fontSize = computed(() => `${size.value}px`)
const value = useVModel(props, 'modelValue', emit)
const containerSize = useElementSize(container)
const innerSize = useElementSize(inner)
const wrapLen = ref(0)
const wrap = ref('nowrap')
watch([container, value, containerSize.width, innerSize.width], async() => {
if (!container.value || innerSize.width.value <= 0)
return
const ratio = containerSize.width.value / innerSize.width.value
if (isNaN(ratio) || ratio <= 0)
return
let newSize = size.value * (containerSize.width.value / innerSize.width.value)
if (newSize < props.min) {
wrapLen.value = value.value.length
wrap.value = ''
}
else {
if (value.value.length < wrapLen.value)
wrap.value = 'nowrap'
}
newSize = Math.max(props.min, Math.min(props.max, newSize))
size.value = newSize
})
</script>

<template>
<div ref="container" class="slidev-auto-fit-text">
<div ref="inner" class="slidev-auto-fit-text-inner">
<slot>
{{ value }}
</slot>
</div>
</div>
</template>

<style scoped>
.slidev-auto-fit-text {
overflow: auto;
font-size: v-bind(fontSize);
white-space: v-bind(wrap);
}
.slidev-auto-fit-text-inner {
display: inline-block;
}
</style>
8 changes: 8 additions & 0 deletions packages/client/builtin/CodeHighlightController.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<!--
Usage:
```ts {1,3-5|2,4}
const your_code = 'here'
```
-->

<script setup lang="ts">
import { range, remove } from '@antfu/utils'
import { parseRangeString } from '@slidev/parser/core'
Expand Down
8 changes: 8 additions & 0 deletions packages/client/builtin/Monaco.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<!--
Usage:
```ts {monaco}
const your_code = 'here'
```
-->

<template>
<div ref="el" class="vue-monaco text-base" :style="{ height }"></div>
</template>
Expand Down
10 changes: 10 additions & 0 deletions packages/client/builtin/Transform.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<!--
Apply scaling or transforming to elements.
Usage:
<Transform :scale="0.5">
<YourElements />
</Transform>
-->

<script setup lang="ts">
import { computed, defineProps } from 'vue'
Expand Down

0 comments on commit fa342f9

Please sign in to comment.