@webcoder49/code-input 2.5.1 → 2.6.2
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 +70 -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/README.md +23 -0
- package/esm/code-input.d.mts +154 -0
- package/esm/code-input.mjs +997 -0
- package/esm/plugins/auto-close-brackets.d.mts +15 -0
- package/esm/plugins/auto-close-brackets.mjs +84 -0
- package/esm/plugins/autocomplete.d.mts +14 -0
- package/esm/plugins/autocomplete.mjs +93 -0
- package/esm/plugins/autodetect.d.mts +11 -0
- package/esm/plugins/autodetect.mjs +35 -0
- package/esm/plugins/find-and-replace.d.mts +43 -0
- package/esm/plugins/find-and-replace.mjs +777 -0
- package/esm/plugins/go-to-line.d.mts +29 -0
- package/esm/plugins/go-to-line.mjs +217 -0
- package/esm/plugins/indent.d.mts +22 -0
- package/esm/plugins/indent.mjs +359 -0
- package/esm/plugins/select-token-callbacks.d.mts +51 -0
- package/esm/plugins/select-token-callbacks.mjs +296 -0
- package/esm/plugins/special-chars.d.mts +25 -0
- package/esm/plugins/special-chars.mjs +207 -0
- package/esm/plugins/test.d.mts +16 -0
- package/esm/plugins/test.mjs +56 -0
- package/esm/templates/hljs.d.mts +16 -0
- package/esm/templates/hljs.mjs +28 -0
- package/esm/templates/prism.d.mts +16 -0
- package/esm/templates/prism.mjs +25 -0
- package/package.json +83 -7
- package/plugins/README.md +2 -0
- package/plugins/auto-close-brackets.js +2 -0
- 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 +28 -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 +39 -6
- package/plugins/go-to-line.min.css +1 -1
- package/plugins/go-to-line.min.js +1 -1
- package/plugins/indent.js +4 -2
- 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 -600
- package/tests/tester.min.js +0 -21
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'Internationalising code-input.js'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# Internationalising code-input.js
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
## Languages
|
|
10
|
+
|
|
11
|
+
code-input.js defaults to using English text for its interface, but when you want it in a different language you can provide the translated text:
|
|
12
|
+
|
|
13
|
+
**The core of code-input.js** contains one message shown only when CSS is supported but JavaScript has not been used to register the element. Translate it by writing the following CSS rule, replacing the text with its localised version.
|
|
14
|
+
|
|
15
|
+
```css
|
|
16
|
+
code-input:not(.code-input_registered)::after {
|
|
17
|
+
content: "No highlighting. JavaScript support is disabled or insufficient, or codeInput.registerTemplate has not been called."!important;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
It is only present for debugging and explanatory purposes when highlighting cannot be seen, and should not contain important or specific information about the editor state; if you need such information, especially for screen reader users, add separate text to your application which disappears after registering the code-input without errors.
|
|
22
|
+
|
|
23
|
+
**Plugins** sometimes come with user interface features (e.g. the find-and-replace dialog) which contain text to be translated. The text is provided as an extra argument to the plugin constructor containing translated strings or functions to produce them for each translation key, with the keys and their English values found in either the `code-input.d.ts` or the plugin's source code file. Here's an example:
|
|
24
|
+
```javascript
|
|
25
|
+
// CC-BY; Attribution: Translated by Oliver Geer with some help from English Wiktionary
|
|
26
|
+
let findAndReplaceTranslations = {
|
|
27
|
+
start: "Buscar términos en su código.",
|
|
28
|
+
none: "No hay sucesos",
|
|
29
|
+
oneFound: "1 suceso encontrado.",
|
|
30
|
+
matchIndex: (index, count) => `${index} de ${count} sucesos.`,
|
|
31
|
+
error: (message) => `Error: ${message}`,
|
|
32
|
+
infiniteLoopError: "Causa un ciclo infinito",
|
|
33
|
+
closeDialog: "Cerrar el Diálogo y Regresar al Editor",
|
|
34
|
+
findPlaceholder: "Buscar",
|
|
35
|
+
findCaseSensitive: "Prestar atención a las minúsculas/mayúsculas",
|
|
36
|
+
findRegExp: "Utilizar expresión regular de JavaScript",
|
|
37
|
+
replaceTitle: "Reemplazar",
|
|
38
|
+
replacePlaceholder: "Reemplazar con",
|
|
39
|
+
findNext: "Buscar Suceso Próximo",
|
|
40
|
+
findPrevious: "Buscar Suceso Previo",
|
|
41
|
+
replaceActionShort: "Reemplazar",
|
|
42
|
+
replaceAction: "Reemplazar este Suceso",
|
|
43
|
+
replaceAllActionShort: "Reemplazar Todos",
|
|
44
|
+
replaceAllAction: "Reemplazar Todos los Sucesos"
|
|
45
|
+
};
|
|
46
|
+
// ...
|
|
47
|
+
// passed when the plugin is constructed:
|
|
48
|
+
new codeInput.plugins.FindAndReplace(true, true, findAndReplaceTranslations),
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Other
|
|
52
|
+
* code-input.js has supported text bidirectionality using the HTML `dir` attribute since version 2.5.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'Styling `code-input` elements with CSS'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# Styling `code-input` elements with CSS
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
`code-input` elements can be styled like `textarea` elements in most cases; however, there are some exceptions:
|
|
10
|
+
* The CSS variable `--padding` should be used rather than the property `padding` (e.g. `<code-input style="--padding: 10px;">...`)
|
|
11
|
+
* Background colours set on `code-input` elements will not work with highlighters that set background colours themselves - use `(code-input's selector) pre[class*="language-"]` for Prism.js or `.hljs` for highlight.js to target the highlighted element with higher specificity than the highlighter's theme. You may also set the `background-color` of the code-input element for its appearance when its template is unregistered / there is no JavaScript.
|
|
12
|
+
* For now, elements on top of `code-input` elements should have a CSS `z-index` at least 3 greater than the `code-input` element.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = '`code-input` in HTML5 Forms'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# `code-input` in HTML5 Forms
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
`code-input` elements support [HTML5 forms](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content/HTML_forms#the_anatomy_of_a_form). Form-related HTML5 attributes and events on them will be passed to a `textarea` element inside them when they are registered, and whenever the attributes or events are later changed.
|
|
10
|
+
|
|
11
|
+
This is a good time to make use of the fallback textarea which is used when JavaScript is disabled; the following code will send data to the HTML5 form correctly whether or not JavaScript is enabled:
|
|
12
|
+
```html
|
|
13
|
+
<form>
|
|
14
|
+
<code-input><textarea data-code-input-fallback name="code"></textarea></code-input>
|
|
15
|
+
<input type="submit"/>
|
|
16
|
+
</form>
|
|
17
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = '`code-input` vs the `textarea` in JavaScript'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# `code-input` vs the `textarea` in JavaScript
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
Once registered, `code-input` elements support most of the JavaScript properties and events used with a `textarea` element, because they are built around them. Try swapping out your `textarea` element in your JavaScript application for a `code-input`! If it doesn't work, [submit a bug report](https://github.com/WebCoder49/code-input/issues).
|
|
10
|
+
|
|
11
|
+
If you want to replace a `textarea` with a `code-input` in an application that doesn't need JavaScript, [look here](../forms). We support HTML5 forms, and progressive enhancement so JavaScript isn't needed!
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'Using code-input.js with custom highlighting algorihms in projects which use modules or frameworks'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# Using code-input.js with custom highlighting algorithms 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 custom highlighting algorithms](../../#playground-preset-custom).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'Using code-input.js with highlight.js in projects which use modules or frameworks'
|
|
3
|
+
+++
|
|
4
|
+
# Using code-input.js with highlight.js in projects which use modules or frameworks
|
|
5
|
+
|
|
6
|
+
> Contributors: 2025 Oliver Geer
|
|
7
|
+
|
|
8
|
+
## Quickstart with Frameworks
|
|
9
|
+
* [Vue](vue) (with ESM)
|
|
10
|
+
* [Nuxt](nuxt) (with ESM)
|
|
11
|
+
|
|
12
|
+
## Quickstart with Module Systems
|
|
13
|
+
* [ECMAScript Modules (ESM)](esm)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'How to use code-input and highlight.js with ECMAScript Modules (not framework-specific)'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# How to use code-input and highlight.js with ECMAScript Modules (not framework-specific)
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Oliver Geer
|
|
8
|
+
|
|
9
|
+
These instructions will work anywhere [ECMAScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) are supported. If you want a tutorial specific to a web framework, please [look here](..).
|
|
10
|
+
|
|
11
|
+
This is a client-side library so will not work in Node.js or similar environments. It is assumed that you have already researched and run an example frontend ECMAScript module (ESM) project using either a bundler like [esbuild](https://esbuild.github.io/) or [webpack](https://webpack.js.org), or newer browsers' direct ESM support (less browser compatibility but easier when debugging); setting them up is not described here. You can [learn about ESM from the very basics here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).
|
|
12
|
+
|
|
13
|
+
## Building
|
|
14
|
+
|
|
15
|
+
The ECMAScript modules in version 2 of the code-input.js library need to be "built" from the source files, because the source files are not in the ESM format.
|
|
16
|
+
|
|
17
|
+
For builds from package managers like Yarn and NPM, the ESM files have already been built and are in the `esm` directory.
|
|
18
|
+
|
|
19
|
+
When the code is downloaded or cloned from `git` source, the ESM directory contains files to do the building but not the built modules yet; follow the instructions in `esm/README.md` to run the build.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Here's how to use code-input and highlight.js with ECMAScript modules.
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// Access the registerTemplate function and CodeInput (extends HTMLElement),
|
|
27
|
+
// Template (custom highlighter) and Plugin (abstract class) classes inside the
|
|
28
|
+
// imported codeInput object.
|
|
29
|
+
import codeInput from "@webcoder49/code-input/code-input.mjs";
|
|
30
|
+
|
|
31
|
+
// Alternatively, use:
|
|
32
|
+
// import { registerTemplate, CodeInput, Template, Plugin } from "@webcoder49/code-input/code-input.mjs"
|
|
33
|
+
|
|
34
|
+
// Templates must be imported separately to avoid redundant code being imported
|
|
35
|
+
// Use any name for the default import, not just "Template"
|
|
36
|
+
import Template from "@webcoder49/code-input/templates/hljs.mjs";
|
|
37
|
+
|
|
38
|
+
// Plugins must be imported separately to avoid redundant code being imported
|
|
39
|
+
// See them all at https://code-input-js.org/plugins
|
|
40
|
+
// Use any name for the default import, not just "Indent"
|
|
41
|
+
import Indent from "@webcoder49/code-input/plugins/indent.mjs";
|
|
42
|
+
|
|
43
|
+
// As per https://highlightjs.org/usage
|
|
44
|
+
// You can import and register (in onMounted below) whichever languages you will use,
|
|
45
|
+
// or if you will use many import all, following the instructions at https://highlightjs.org/#usage.
|
|
46
|
+
import hljs from 'highlight.js/lib/core';
|
|
47
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
48
|
+
// Set up the highlighting engine first
|
|
49
|
+
hljs.registerLanguage('javascript', javascript);
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// Register using the imported objects
|
|
53
|
+
codeInput.registerTemplate("code-editor", new Template(hljs, [new Indent()]));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Note the `.mjs` extensions on code-input.js import paths. They are needed, and also make the next part easier:
|
|
57
|
+
|
|
58
|
+
## Import Paths
|
|
59
|
+
|
|
60
|
+
The import paths above assume a package manager and `package.json` export paths are being used.
|
|
61
|
+
|
|
62
|
+
In some setups, this will not work. You have two options (replace `node_modules/@webcoder49/code-input` with the relative path to the library, if it is different):
|
|
63
|
+
* use relative paths instead: replace `"@webcoder49/code-input/path/to/file.mjs"` with `"node_modules/@webcoder49/code-input/esm/path/to/file.mjs"` (note the `esm` directory). Also replace `"@webcoder49/code-input/path/to/file.mjs"` with `"node_modules/@highlightjs/cdn-assets/es/path/to/file.mjs"` (note the `es` directory) *If you're not using an import map yet, I recommend this option because import maps are not supported on as many browsers.*
|
|
64
|
+
* If you're using an [import map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#importing_modules_using_import_maps), add the following to it:
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"imports": {
|
|
68
|
+
"@webcoder49/code-input": "./node_modules/@webcoder49/code-input/esm/",
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'How to use code-input and highlight.js with Nuxt'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# How to use code-input and highlight.js with Nuxt
|
|
6
|
+
|
|
7
|
+
> Contributors: 2025 Paul Rosen, 2025 Oliver Geer; **code samples licensed [MIT/Expat](https://spdx.org/licenses/MIT)**
|
|
8
|
+
|
|
9
|
+
[Vue](../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.
|
|
10
|
+
|
|
11
|
+
## 1. Create a Nuxt app
|
|
12
|
+
|
|
13
|
+
First, create a Nuxt project. (If you already have a Nuxt 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:
|
|
14
|
+
```bash
|
|
15
|
+
yarn create nuxt syntax-highlighter
|
|
16
|
+
# OR
|
|
17
|
+
npm create nuxt@latest syntax-highlighter
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
At the time this tutorial was created, the output was the following:
|
|
21
|
+
```plaintext
|
|
22
|
+
Need to install the following packages:
|
|
23
|
+
create-nuxt@3.27.0
|
|
24
|
+
Ok to proceed? (y) y
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
> npx
|
|
28
|
+
> "create-nuxt" syntax-highlighter
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
.d$b.
|
|
32
|
+
i$$A$$L .d$b
|
|
33
|
+
.$$F` `$$L.$$A$$.
|
|
34
|
+
j$$' `4$$:` `$$.
|
|
35
|
+
j$$' .4$: `$$.
|
|
36
|
+
j$$` .$$: `4$L
|
|
37
|
+
:$$:____.d$$: _____.:$$:
|
|
38
|
+
`4$$$$$$$$P` .i$$$$$$$$P`
|
|
39
|
+
|
|
40
|
+
ℹ Welcome to Nuxt!
|
|
41
|
+
ℹ Creating a new project in syntax-highlighter.
|
|
42
|
+
|
|
43
|
+
✔ Which package manager would you like to use?
|
|
44
|
+
npm
|
|
45
|
+
◐ Installing dependencies...
|
|
46
|
+
|
|
47
|
+
> postinstall
|
|
48
|
+
> nuxt prepare
|
|
49
|
+
|
|
50
|
+
✔ Types generated in .nuxt
|
|
51
|
+
|
|
52
|
+
added 882 packages, and audited 884 packages in 5m
|
|
53
|
+
|
|
54
|
+
185 packages are looking for funding
|
|
55
|
+
run `npm fund` for details
|
|
56
|
+
|
|
57
|
+
found 0 vulnerabilities
|
|
58
|
+
✔ Installation completed.
|
|
59
|
+
|
|
60
|
+
✔ Initialize git repository?
|
|
61
|
+
No
|
|
62
|
+
|
|
63
|
+
✔ Would you like to install any of the official modules?
|
|
64
|
+
Yes
|
|
65
|
+
|
|
66
|
+
✔ Pick the modules to install:
|
|
67
|
+
none
|
|
68
|
+
|
|
69
|
+
✨ Nuxt project has been created with the v4 template. Next steps:
|
|
70
|
+
› cd syntax-highlighter
|
|
71
|
+
› Start development server with npm run dev
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
And just like the above instructions mention, do the following:
|
|
76
|
+
```bash
|
|
77
|
+
cd syntax-highlighter
|
|
78
|
+
|
|
79
|
+
yarn run dev
|
|
80
|
+
# OR
|
|
81
|
+
npm run dev
|
|
82
|
+
# or your package manager's equivalent command
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
You should be able to open your browser to the path that it prints out and see a working Nuxt app. If so, congratulations! Hit Ctrl-C in the command line to stop it.
|
|
86
|
+
|
|
87
|
+
## 2. Add dependencies
|
|
88
|
+
|
|
89
|
+
> This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed.
|
|
90
|
+
|
|
91
|
+
Type these 2 commands:
|
|
92
|
+
```bash
|
|
93
|
+
yarn add @webcoder49/code-input
|
|
94
|
+
# OR
|
|
95
|
+
npm install @webcoder49/code-input
|
|
96
|
+
# or your package manager's equivalent command
|
|
97
|
+
|
|
98
|
+
yarn add highlight.js
|
|
99
|
+
# OR
|
|
100
|
+
npm install highlight.js
|
|
101
|
+
# or your package manager's equivalent command
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
In the file `nuxt.config.ts`, after the line `compatibilityDate`, add this so that Vue knows that `code-input` is not a Vue component:
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
vue: {
|
|
108
|
+
compilerOptions: {
|
|
109
|
+
isCustomElement: (tag) => tag === "code-input",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
> You might want to replace the second file with your own theme, but you need the first file.
|
|
115
|
+
|
|
116
|
+
## 3. Initialize a code-input element
|
|
117
|
+
|
|
118
|
+
Create a component with whatever name you want. Perhaps `app/components/CodeEditor.vue`. Read the following code then paste it into the file:
|
|
119
|
+
|
|
120
|
+
```html
|
|
121
|
+
<template>
|
|
122
|
+
<!--Attributes that make sense on a
|
|
123
|
+
textarea element are both on the code-
|
|
124
|
+
input element for when full
|
|
125
|
+
functionality is present, and on the
|
|
126
|
+
fallback textarea for when it's not
|
|
127
|
+
(e.g. bug or outdated browser).-->
|
|
128
|
+
<code-input
|
|
129
|
+
ref="elem"
|
|
130
|
+
template="code-editor"
|
|
131
|
+
:name="name"
|
|
132
|
+
:value="value"
|
|
133
|
+
:language="language"
|
|
134
|
+
@input="value = elem.value; emit('input', elem.value)"
|
|
135
|
+
@code-input_load="loaded"
|
|
136
|
+
>
|
|
137
|
+
<textarea
|
|
138
|
+
ref="fallback"
|
|
139
|
+
:name="name"
|
|
140
|
+
:value="value"
|
|
141
|
+
@input="value = fallback.value; emit('input', fallback.value)"
|
|
142
|
+
data-code-input-fallback
|
|
143
|
+
></textarea>
|
|
144
|
+
</code-input>
|
|
145
|
+
</template>
|
|
146
|
+
|
|
147
|
+
<script lang="ts" setup>
|
|
148
|
+
// For loading a highlighting engine - this example uses highlight.js
|
|
149
|
+
import hljs from 'highlight.js/lib/core';
|
|
150
|
+
// You can import and register (in onBeforeMount below) whichever languages you will use,
|
|
151
|
+
// or if you will use many import all, following the instructions at https://highlightjs.org/#usage.
|
|
152
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
153
|
+
|
|
154
|
+
// The following are optional.
|
|
155
|
+
const emit = defineEmits<{
|
|
156
|
+
// If you want a listener when the user changes the contents.
|
|
157
|
+
(e: "input", value: string): void;
|
|
158
|
+
// If you want to do more initialization after code-input is ready.
|
|
159
|
+
(e: "ready", element: HTMLElement): void;
|
|
160
|
+
}>();
|
|
161
|
+
|
|
162
|
+
const props = defineProps<{
|
|
163
|
+
value: string; // The starting value for the code-input element
|
|
164
|
+
name: string; // The name that is used when the code-input element is in a form
|
|
165
|
+
language: string; // The programming language name given to highlight.js, which must also be imported above and registered below with highlight.js.
|
|
166
|
+
}>();
|
|
167
|
+
|
|
168
|
+
// This contains the HTMLElement of the code-input component
|
|
169
|
+
const elem = ref()
|
|
170
|
+
|
|
171
|
+
// Before it appears on the page, code-input needs to be initialized.
|
|
172
|
+
// This must be onBeforeMount and not onMount!
|
|
173
|
+
onBeforeMount(async () => {
|
|
174
|
+
// Only if we're in the client
|
|
175
|
+
// so that no server-side rendering is done on the code-input component
|
|
176
|
+
if (import.meta.browser) {
|
|
177
|
+
// Dynamically import code-input so that it is only in the browser
|
|
178
|
+
const codeInput = await import("@webcoder49/code-input");
|
|
179
|
+
const Template = (await import("@webcoder49/code-input/templates/hljs.mjs")).default;
|
|
180
|
+
// Set up highlight.js
|
|
181
|
+
hljs.registerLanguage('javascript', javascript);
|
|
182
|
+
// Register that engine with code-input
|
|
183
|
+
// You can also add plugins as shown below, and at https://code-input-js.org/plugins
|
|
184
|
+
const Indent = (await import("@webcoder49/code-input/plugins/indent.mjs")).default;
|
|
185
|
+
codeInput.registerTemplate("code-editor", new Template(hljs, [new Indent()]));
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
function loaded() {
|
|
190
|
+
// This is called after the code-input is initialized.
|
|
191
|
+
// If you have some further initialization for the code-input element, then do it in this event.
|
|
192
|
+
emit("ready", elem)
|
|
193
|
+
}
|
|
194
|
+
</script>
|
|
195
|
+
|
|
196
|
+
<style scoped>
|
|
197
|
+
.rich-editor {
|
|
198
|
+
border: 1px solid #bbbbbb;
|
|
199
|
+
}
|
|
200
|
+
code-input {
|
|
201
|
+
resize: both; /* if you want the resizing control that textarea has */
|
|
202
|
+
margin: 0; /* you can override other styles */
|
|
203
|
+
font-family: monospace;
|
|
204
|
+
|
|
205
|
+
background: #f1f1f1; /* As explained below, this is required to set the colour of the code-input element if it
|
|
206
|
+
falls back to having no highlighting, and while it loads. */
|
|
207
|
+
}
|
|
208
|
+
</style>
|
|
209
|
+
|
|
210
|
+
<style>
|
|
211
|
+
/* These are necessary styles to make code-input work */
|
|
212
|
+
@import '@webcoder49/code-input/code-input.css';
|
|
213
|
+
/* This is one possibility of styles to use for highlighting */
|
|
214
|
+
@import 'highlight.js/styles/default.min.css';
|
|
215
|
+
|
|
216
|
+
/* Notice that these styles aren't scoped */
|
|
217
|
+
.hljs {
|
|
218
|
+
background: #f1f1f1; /* This is a style specific to highlighted code, so needs to use highlight.js' selector.
|
|
219
|
+
A side effect of this is that it will not affect unregistered/unloaded code-input elements. */
|
|
220
|
+
}
|
|
221
|
+
/* If you want to change the color of selected text during editing */
|
|
222
|
+
code-input textarea::selection {
|
|
223
|
+
background: #6781ef;
|
|
224
|
+
color: #ffffff;
|
|
225
|
+
}
|
|
226
|
+
</style>
|
|
227
|
+
```
|
|
228
|
+
## 4. Using the component
|
|
229
|
+
|
|
230
|
+
In the generated file `app.vue`, place the following line after the "NuxtRouteAnnouncer" line:
|
|
231
|
+
```html
|
|
232
|
+
<CodeEditor value="function hello() { console.log('world'); }" name="myEditor" />
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Nuxt will have already imported it by default, so you don't need to import the component in JavaScript.
|
|
236
|
+
|
|
237
|
+
Restart the server:
|
|
238
|
+
```bash
|
|
239
|
+
yarn run dev
|
|
240
|
+
# OR
|
|
241
|
+
npm run dev
|
|
242
|
+
# or your package manager's equivalent command
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
If all went well, you should see the following in the browser:
|
|
246
|
+
|
|
247
|
+

|
|
248
|
+
|
|
249
|
+
## Please Note
|
|
250
|
+
* 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
|