|
| 1 | +# Vue/Nuxt Tutorial |
| 2 | + |
| 3 | +Vue and Nuxt have some similarities, but there is one big difference in how they can use this library. In Nuxt there is server side rendering (SSR) that will attempt to create the final HTML before sending the page to the browser. This cannot use any browser-specific things so the `code-input` component must be excluded from rendering until hydration in the browser. |
| 4 | + |
| 5 | +## Vue |
| 6 | + |
| 7 | +### 1. Create a Vue app |
| 8 | + |
| 9 | +First, create a Vue project. (If you already have a Vue project then you can skip this step). On a command line, type this: |
| 10 | +``` |
| 11 | +npm create vue@latest |
| 12 | +``` |
| 13 | +At the time this tutorial was created, the output was the following, after I named the project `syntax-highlighter` and checked "typescript": |
| 14 | +``` |
| 15 | +Need to install the following packages: |
| 16 | + |
| 17 | +Ok to proceed? (y) y |
| 18 | +
|
| 19 | +
|
| 20 | +> npx |
| 21 | +> "create-vue" |
| 22 | +
|
| 23 | +┌ Vue.js - The Progressive JavaScript Framework |
| 24 | +│ |
| 25 | +◇ Project name (target directory): |
| 26 | +│ syntax-highlighter |
| 27 | +│ |
| 28 | +◇ Select features to include in your project: (↑/↓ to navigate, space to select, a to toggle |
| 29 | +all, enter to confirm) |
| 30 | +│ TypeScript |
| 31 | +│ |
| 32 | +◇ Select experimental features to include in your project: (↑/↓ to navigate, space to |
| 33 | +select, a to toggle all, enter to confirm) |
| 34 | +│ none |
| 35 | +│ |
| 36 | +◇ Skip all example code and start with a blank Vue project? |
| 37 | +│ No |
| 38 | +
|
| 39 | +Scaffolding project in /srv/app/projects/syntax-highlighter... |
| 40 | +│ |
| 41 | +└ Done. Now run: |
| 42 | +
|
| 43 | + cd syntax-highlighter |
| 44 | + npm install |
| 45 | + npm run dev |
| 46 | +
|
| 47 | +| Optional: Initialize Git in your project directory with: |
| 48 | +
|
| 49 | + git init && git add -A && git commit -m "initial commit" |
| 50 | +``` |
| 51 | + |
| 52 | +And just like the above instructions mention, do the following: |
| 53 | +```bash |
| 54 | +cd syntax-highlighter |
| 55 | +npm install |
| 56 | +npm run dev |
| 57 | +``` |
| 58 | + |
| 59 | +You should be able to open your browser to the path that it prints out and see a started Vue app. If so, congratulations! Hit Ctrl-C to stop it. |
| 60 | + |
| 61 | +### 2. Add dependencies |
| 62 | + |
| 63 | +This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed. |
| 64 | + |
| 65 | +Type this: |
| 66 | +```bash |
| 67 | +npm install @webcoder49/code-input |
| 68 | +npm install highlight.js |
| 69 | +``` |
| 70 | + |
| 71 | +In the file `vite.config.ts`, change the line that contains `vue()` to this: |
| 72 | +```javascript |
| 73 | +vue({ |
| 74 | + template: { |
| 75 | + compilerOptions: { |
| 76 | + isCustomElement: (tag) => tag === 'code-input' |
| 77 | + } |
| 78 | + } |
| 79 | +}) |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. Initialize the textarea |
| 83 | + |
| 84 | +Create a component with whatever name you want. Perhaps `RichEditor.vue`. Paste the following into it: |
| 85 | +```vue |
| 86 | +<template> |
| 87 | + <code-input name="richText">function hello() { console.log("world"); } |
| 88 | + </code-input> |
| 89 | +</template> |
| 90 | +
|
| 91 | +<script lang="ts" setup> |
| 92 | +import {onMounted} from "vue"; |
| 93 | +// For loading the code-input web component |
| 94 | +import codeInput from "@webcoder49/code-input"; |
| 95 | +import Template from "@webcoder49/code-input/templates/hljs.mjs"; |
| 96 | +// For loading a highlighting engine - this example uses highlight.js |
| 97 | +import hljs from 'highlight.js/lib/core'; |
| 98 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 99 | +
|
| 100 | +onMounted(async () => { |
| 101 | + // Set up the highlighting engine first |
| 102 | + hljs.registerLanguage('javascript', javascript); |
| 103 | + // Register that engine with code-input |
| 104 | + codeInput.registerTemplate("syntax-highlighted", new Template(hljs, [])); |
| 105 | +}) |
| 106 | +
|
| 107 | +</script> |
| 108 | +
|
| 109 | +<style> |
| 110 | +/* These are necessary styles to make code-input work */ |
| 111 | +@import '@webcoder49/code-input/code-input.css'; |
| 112 | +/* This is one possibility of styles to use for highlighting */ |
| 113 | +@import 'highlight.js/styles/default.min.css'; |
| 114 | +
|
| 115 | +code-input { |
| 116 | + resize: both; /* if you want the resizing control that textarea has */ |
| 117 | + margin: 0; /* you can override other styles */ |
| 118 | + font-family: Monaco, monospace; |
| 119 | +} |
| 120 | +
|
| 121 | +.hljs { |
| 122 | + background: #f1f1f1; /* here's how to change the background color. */ |
| 123 | +} |
| 124 | +</style> |
| 125 | +``` |
| 126 | + |
| 127 | +### 4. Using the component |
| 128 | + |
| 129 | +In the generated file `HelloWorld.vue`, place the following line after the "greetings" line: |
| 130 | +```vue |
| 131 | +<RichEditor /> |
| 132 | +``` |
| 133 | + |
| 134 | +And put its input in the `<script>` section: |
| 135 | +```vue |
| 136 | +import RichEditor from "@/components/RichEditor.vue"; |
| 137 | +``` |
| 138 | + |
| 139 | +Restart the server: |
| 140 | +```bash |
| 141 | +npm run dev |
| 142 | +``` |
| 143 | + |
| 144 | +If all went well, you should see the following in the browser: |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +## Nuxt |
| 149 | + |
| 150 | +TODO |
0 commit comments