Last active
November 3, 2022 22:04
-
-
Save mrts/cda0ac4f10de3e220ee99fb205b769db to your computer and use it in GitHub Desktop.
A simple app for creating slide presentations with Vue.js
This file contains hidden or 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
<script setup lang="ts"> | |
import { $ref } from "vue/macros"; | |
import { onMounted, onUnmounted } from "vue"; | |
import { SLIDE_TEXTS } from "./assets/slides/slideTexts"; | |
import PresentationSlide from "./components/PresentationSlide.vue"; | |
// Width of the slide images in pixels, change this if the images are of different width | |
const IMAGE_WIDTH_PX = 1024; | |
const MAX_INDEX = Object.keys(SLIDE_TEXTS).length; | |
let currentSlideIndex = 1; | |
let imageUrl = $ref(""); | |
let text = $ref(""); | |
function changeSlideToIndex(index: number) { | |
imageUrl = new URL(`./assets/slides/${index}.jpg`, import.meta.url).href; | |
text = SLIDE_TEXTS[index]; | |
} | |
changeSlideToIndex(currentSlideIndex); | |
function changeSlide(forward: boolean) { | |
let index = forward ? ++currentSlideIndex : --currentSlideIndex; | |
if (currentSlideIndex < 1 || currentSlideIndex > MAX_INDEX) { | |
currentSlideIndex = index = 1; | |
} | |
changeSlideToIndex(index); | |
console.log(currentSlideIndex); | |
} | |
function changeSlideWithMouse(e: MouseEvent) { | |
changeSlide(e.offsetX >= IMAGE_WIDTH_PX / 2); | |
} | |
function handleKeydown(e: KeyboardEvent) { | |
switch (e.key) { | |
case "ArrowLeft": | |
changeSlide(false); | |
break; | |
case "ArrowRight": | |
changeSlide(true); | |
break; | |
} | |
} | |
onMounted(() => { | |
window.addEventListener("keydown", handleKeydown, undefined); | |
}); | |
onUnmounted(() => { | |
window.removeEventListener("keydown", handleKeydown); | |
}); | |
</script> | |
<template> | |
<main> | |
<PresentationSlide | |
:image-url="imageUrl" | |
:text="text" | |
@click="changeSlideWithMouse" | |
/> | |
</main> | |
</template> |
This file contains hidden or 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
<script setup lang="ts"> | |
defineProps<{ | |
imageUrl: string; | |
text: string; | |
}>(); | |
</script> | |
<template> | |
<div> | |
<div><img :src="imageUrl" /></div> | |
<p v-html="text"></p> | |
</div> | |
</template> |
This file contains hidden or 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
// save this to src/assets/slides/ | |
export const SLIDE_TEXTS: {[name: number]: string} = { | |
1: "First slide text", | |
2: "Second <b>slide</b> text with HTML tags" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be used as a simple SlideShare clone.
Usage:
Installation:
$ref()
by addingplugins: [vue({ reactivityTransform: true })]
tovite.config.ts
.App.vue
with the version above, changeIMAGE_WIDTH_PX
if your images are not 1024 pixels wide.Slide.vue
tosrc/components/
.src/assets/slides/
, use slide numbers as filenames.slideTexts.ts
tosrc/assets/slides/
and fill in the slide texts for each slide, using slide numbers as text indexes.npm install && npm run dev
to start the server ornpm run build
to compile for production.