@webcoder49/code-input 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +9 -126
- package/code-input.css +71 -33
- package/code-input.d.ts +135 -59
- package/code-input.js +201 -110
- package/code-input.min.css +1 -1
- package/code-input.min.js +12 -1
- package/docs/LICENSE +3 -0
- package/docs/LICENSE.CC-BY-SA-4.0 +116 -0
- package/docs/LICENSE.CC0-1.0 +30 -0
- package/docs/README.md +5 -0
- package/docs/_index.md +308 -0
- package/docs/i18n/_index.md +52 -0
- package/docs/interface/_index.md +3 -0
- package/docs/interface/css/_index.md +12 -0
- package/docs/interface/forms/_index.md +17 -0
- package/docs/interface/js/_index.md +11 -0
- package/docs/modules-and-frameworks/_index.md +3 -0
- package/docs/modules-and-frameworks/custom/_index.md +9 -0
- package/docs/modules-and-frameworks/hljs/_index.md +13 -0
- package/docs/modules-and-frameworks/hljs/esm/_index.md +71 -0
- package/docs/modules-and-frameworks/hljs/nuxt/_index.md +250 -0
- package/docs/modules-and-frameworks/hljs/nuxt/nuxt-demo-screenshot.png +0 -0
- package/docs/modules-and-frameworks/hljs/vue/_index.md +233 -0
- package/docs/modules-and-frameworks/hljs/vue/vue-demo-screenshot.png +0 -0
- package/docs/modules-and-frameworks/prism/_index.md +14 -0
- package/docs/plugins/_index.md +676 -0
- package/docs/plugins/new/_index.md +52 -0
- package/docs/theory/_index.md +9 -0
- package/esm/.code-input.mjs.kate-swp +0 -0
- package/esm/README.md +23 -0
- package/esm/code-input.mjs +2 -0
- package/package.json +83 -7
- package/plugins/README.md +2 -0
- package/plugins/auto-close-brackets.js +25 -7
- package/plugins/auto-close-brackets.min.js +1 -1
- package/plugins/autocomplete.js +6 -6
- package/plugins/autocomplete.min.js +1 -1
- package/plugins/autodetect.js +4 -2
- package/plugins/autodetect.min.js +1 -1
- package/plugins/find-and-replace.css +0 -4
- package/plugins/find-and-replace.js +34 -8
- package/plugins/find-and-replace.min.css +1 -1
- package/plugins/find-and-replace.min.js +1 -1
- package/plugins/go-to-line.css +10 -5
- package/plugins/go-to-line.js +43 -6
- package/plugins/go-to-line.min.css +1 -1
- package/plugins/go-to-line.min.js +1 -1
- package/plugins/indent.js +29 -4
- package/plugins/indent.min.js +1 -1
- package/plugins/prism-line-numbers.css +14 -5
- package/plugins/prism-line-numbers.min.css +1 -1
- package/plugins/select-token-callbacks.js +3 -1
- package/plugins/select-token-callbacks.min.js +1 -1
- package/plugins/special-chars.css +13 -1
- package/plugins/special-chars.js +14 -4
- package/plugins/special-chars.min.css +1 -1
- package/plugins/special-chars.min.js +1 -1
- package/plugins/test.js +22 -7
- package/plugins/test.min.js +1 -1
- package/.github/workflows/minify.yml +0 -22
- package/.github/workflows/npm-publish.yml +0 -21
- package/CODE_OF_CONDUCT.md +0 -130
- package/CONTRIBUTING.md +0 -35
- package/tests/hljs.html +0 -55
- package/tests/i18n.html +0 -197
- package/tests/prism-match-braces-compatibility.js +0 -215
- package/tests/prism-match-braces-compatibility.min.js +0 -1
- package/tests/prism.html +0 -54
- package/tests/tester.js +0 -593
- package/tests/tester.min.js +0 -21
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'How to use code-input and highlight.js with Vue'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# How to use code-input and highlight.js with Vue
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Paul Rosen, 2025 Oliver Geer; **code samples licensed [MIT/Expat](https://spdx.org/licenses/MIT)**
|
|
8
|
+
|
|
9
|
+
## 1. Create a Vue app
|
|
10
|
+
|
|
11
|
+
First, create a Vue project. (If you already have a Vue project then you can skip this step). On a command line, use your package manager to do so, for example by typing one of these:
|
|
12
|
+
```bash
|
|
13
|
+
yarn create vue
|
|
14
|
+
# OR
|
|
15
|
+
npm create vue@latest
|
|
16
|
+
```
|
|
17
|
+
At the time this tutorial was created, the output was the following, after I named the project `syntax-highlighter` and checked "typescript" (to give the most specific type of code possible in the tutorial; you can use JavaScript just by reading the comments and emitting types from the tutorial's samples):
|
|
18
|
+
```plaintext
|
|
19
|
+
Need to install the following packages:
|
|
20
|
+
create-vue@3.18.0
|
|
21
|
+
Ok to proceed? (y) y
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
> npx
|
|
25
|
+
> "create-vue"
|
|
26
|
+
|
|
27
|
+
┌ Vue.js - The Progressive JavaScript Framework
|
|
28
|
+
│
|
|
29
|
+
◇ Project name (target directory):
|
|
30
|
+
│ syntax-highlighter
|
|
31
|
+
│
|
|
32
|
+
◇ Select features to include in your project: (↑/↓ to navigate, space to select, a to toggle
|
|
33
|
+
all, enter to confirm)
|
|
34
|
+
│ TypeScript
|
|
35
|
+
│
|
|
36
|
+
◇ Select experimental features to include in your project: (↑/↓ to navigate, space to
|
|
37
|
+
select, a to toggle all, enter to confirm)
|
|
38
|
+
│ none
|
|
39
|
+
│
|
|
40
|
+
◇ Skip all example code and start with a blank Vue project?
|
|
41
|
+
│ No
|
|
42
|
+
|
|
43
|
+
Scaffolding project in /srv/app/projects/syntax-highlighter...
|
|
44
|
+
│
|
|
45
|
+
└ Done. Now run:
|
|
46
|
+
|
|
47
|
+
cd syntax-highlighter
|
|
48
|
+
npm install
|
|
49
|
+
npm run dev
|
|
50
|
+
|
|
51
|
+
| Optional: Initialize Git in your project directory with:
|
|
52
|
+
|
|
53
|
+
git init && git add -A && git commit -m "initial commit"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
And just like the above instructions mention, do the following:
|
|
57
|
+
```bash
|
|
58
|
+
cd syntax-highlighter
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
yarn install
|
|
62
|
+
# OR
|
|
63
|
+
npm install
|
|
64
|
+
# or your package manager's equivalent command
|
|
65
|
+
|
|
66
|
+
yarn run dev
|
|
67
|
+
# OR
|
|
68
|
+
npm run dev
|
|
69
|
+
# or your package manager's equivalent command
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
You should be able to open your browser to the path that it prints out and see a working Vue app. If so, congratulations! Hit Ctrl-C in the command line to stop it.
|
|
73
|
+
|
|
74
|
+
## 2. Add dependencies
|
|
75
|
+
|
|
76
|
+
> This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed.
|
|
77
|
+
|
|
78
|
+
Type this:
|
|
79
|
+
```bash
|
|
80
|
+
yarn add @webcoder49/code-input
|
|
81
|
+
# OR
|
|
82
|
+
npm install @webcoder49/code-input
|
|
83
|
+
# or your package manager's equivalent command
|
|
84
|
+
|
|
85
|
+
yarn add highlight.js
|
|
86
|
+
# OR
|
|
87
|
+
npm install highlight.js
|
|
88
|
+
# or your package manager's equivalent command
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
In the file `vite.config.ts`, change the line that contains `vue(),` to this:
|
|
92
|
+
```javascript
|
|
93
|
+
vue({
|
|
94
|
+
template: {
|
|
95
|
+
compilerOptions: {
|
|
96
|
+
isCustomElement: (tag) => tag === 'code-input'
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}),
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
So that Vue knows that `code-input` is not a Vue component.
|
|
103
|
+
|
|
104
|
+
## 3. Initialize a code-input element
|
|
105
|
+
|
|
106
|
+
Create a component with whatever name you want. Perhaps `CodeEditor.vue`. Read the following code then paste it into the file:
|
|
107
|
+
```html
|
|
108
|
+
<template>
|
|
109
|
+
<!--Attributes that make sense on a
|
|
110
|
+
textarea element are both on the code-
|
|
111
|
+
input element for when full
|
|
112
|
+
functionality is present, and on the
|
|
113
|
+
fallback textarea for when it's not
|
|
114
|
+
(e.g. bug or outdated browser).-->
|
|
115
|
+
<code-input
|
|
116
|
+
ref="elem"
|
|
117
|
+
template="code-editor"
|
|
118
|
+
:name="name"
|
|
119
|
+
:value="value"
|
|
120
|
+
:language="language"
|
|
121
|
+
@input="value = elem.value; emit('input', elem.value)"
|
|
122
|
+
@code-input_load="loaded"
|
|
123
|
+
>
|
|
124
|
+
<textarea
|
|
125
|
+
ref="fallback"
|
|
126
|
+
:name="name"
|
|
127
|
+
:value="value"
|
|
128
|
+
@input="value = fallback.value; emit('input', fallback.value)"
|
|
129
|
+
data-code-input-fallback
|
|
130
|
+
></textarea>
|
|
131
|
+
</code-input>
|
|
132
|
+
</template>
|
|
133
|
+
|
|
134
|
+
<script lang="ts" setup>
|
|
135
|
+
import {onMounted, ref} from "vue";
|
|
136
|
+
// For loading the code-input web component
|
|
137
|
+
import codeInput from "@webcoder49/code-input";
|
|
138
|
+
import Template from "@webcoder49/code-input/templates/hljs.mjs";
|
|
139
|
+
// You can import plugins as shown below, and at https://code-input-js.org/plugins
|
|
140
|
+
import Indent from "@webcoder49/code-input/plugins/indent.mjs";
|
|
141
|
+
|
|
142
|
+
// You can import and register (in onMounted below) whichever languages you will use,
|
|
143
|
+
// or if you will use many import all, following the instructions at https://highlightjs.org/#usage.
|
|
144
|
+
import hljs from 'highlight.js/lib/core';
|
|
145
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
146
|
+
|
|
147
|
+
// The following are optional.
|
|
148
|
+
const emit = defineEmits<{
|
|
149
|
+
// If you want a listener when the user changes the contents.
|
|
150
|
+
(e: "input", value: string): void;
|
|
151
|
+
// If you want to do more initialization after code-input is ready.
|
|
152
|
+
(e: "ready", element: HTMLElement): void;
|
|
153
|
+
}>();
|
|
154
|
+
// The JavaScript version of the TypeScript above would be const emit = defineEmits(["input", "ready"]);
|
|
155
|
+
|
|
156
|
+
const props = defineProps<{
|
|
157
|
+
value: string; // The starting value for the code-input element
|
|
158
|
+
name: string; // The name that is used when the code-input element is in a form
|
|
159
|
+
language: string; // The programming language name given to highlight.js, which must also be imported above and registered below with highlight.js.
|
|
160
|
+
}>();
|
|
161
|
+
// The JavaScript version of the TypeScript above would be const props = defineProps({value: String, name: String});
|
|
162
|
+
|
|
163
|
+
// This contains the HTMLElement of the code-input component
|
|
164
|
+
const elem = ref();
|
|
165
|
+
|
|
166
|
+
onMounted(async () => {
|
|
167
|
+
// Set up the highlighting engine first
|
|
168
|
+
hljs.registerLanguage('javascript', javascript);
|
|
169
|
+
// Register that engine with code-input
|
|
170
|
+
codeInput.registerTemplate("code-editor", new Template(hljs, [new Indent()]));
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
function loaded() {
|
|
174
|
+
// This is called after the code-input is initialized.
|
|
175
|
+
// If you have some further initialization for the code-input element, then do it in this event.
|
|
176
|
+
emit("ready", elem);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
</script>
|
|
180
|
+
|
|
181
|
+
<style>
|
|
182
|
+
/* These are necessary styles to make code-input work */
|
|
183
|
+
@import '@webcoder49/code-input/code-input.css';
|
|
184
|
+
/* This is one possibility of styles to use for highlighting */
|
|
185
|
+
@import 'highlight.js/styles/default.min.css';
|
|
186
|
+
|
|
187
|
+
code-input {
|
|
188
|
+
resize: both; /* if you want the resizing control that textarea has */
|
|
189
|
+
margin: 0; /* you can override other styles */
|
|
190
|
+
font-family: monospace;
|
|
191
|
+
|
|
192
|
+
background: #f1f1f1; /* As explained below, this is required to set the colour of the code-input element if it
|
|
193
|
+
falls back to having no highlighting, and while it loads. */
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
.hljs {
|
|
197
|
+
background: #f1f1f1; /* This is a style specific to highlighted code, so needs to use highlight.js' selector.
|
|
198
|
+
A side effect of this is that it will not affect unregistered/unloaded code-input elements. */
|
|
199
|
+
}
|
|
200
|
+
/* If you want to change the color of selected text during editing */
|
|
201
|
+
code-input textarea::selection {
|
|
202
|
+
background: #6781ef;
|
|
203
|
+
color: #ffffff;
|
|
204
|
+
}
|
|
205
|
+
</style>
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## 4. Using the component
|
|
209
|
+
|
|
210
|
+
In the generated file `HelloWorld.vue`, place the following line after the "greetings" line:
|
|
211
|
+
```html
|
|
212
|
+
<CodeEditor value="function hello() { console.log('world'); }" name="myEditor" />
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
And put its import in the `<script>` section:
|
|
216
|
+
```javascript
|
|
217
|
+
import CodeEditor from "@/components/CodeEditor.vue";
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Restart the server:
|
|
221
|
+
```bash
|
|
222
|
+
yarn run dev
|
|
223
|
+
# OR
|
|
224
|
+
npm run dev
|
|
225
|
+
# or your package manager's equivalent command
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
If all went well, you should see the following in the browser:
|
|
229
|
+
|
|
230
|
+

|
|
231
|
+
|
|
232
|
+
## Please Note
|
|
233
|
+
* Hot module replacement (the updating of a running app when files are changed) will not work completely correctly when the `CodeEditor` component is changed but will work when files that use it are changed. This is only important when running a development server.
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'Using code-input.js with Prism.js in projects which use modules or frameworks'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# Using code-input.js with Prism.js in projects which use modules or frameworks
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
We plan to get documentation for this soon (contributions via a pull request are welcome!), but for now please use [the corresponding article for highlight.js](../hljs), and [the demo vanilla code for code-input and Prism.js](../../#playground-preset-prism). <!--Good time to add docs here would be release of Prism v2.-->
|
|
10
|
+
|
|
11
|
+
Bear in mind that the Prism.js library can be imported into ECMAScript modules as follows:
|
|
12
|
+
```javascript
|
|
13
|
+
import Prism from "prismjs";
|
|
14
|
+
```
|