Skip to content

Commit 7929efa

Browse files
committed
Update documentation and docstrings for plugins, i18n and ESM+hljs
1 parent a73dbea commit 7929efa

File tree

22 files changed

+862
-54
lines changed

22 files changed

+862
-54
lines changed

code-input.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ code-input:not(.code-input_registered) {
138138

139139
code-input:not(.code-input_registered)::after {
140140
/* Display message to register */
141-
content: "No-JavaScript fallback. For highlighting and plugins: as a user use a newer browser/enable JavaScript support; as a developer use codeInput.registerTemplate.";
141+
content: "No highlighting. JavaScript support is disabled or insufficient, or codeInput.registerTemplate has not been called.";
142142
display: block;
143143
position: absolute;
144144
bottom: 0;

code-input.d.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export abstract class Plugin {
4646
* modifications to the `codeInput.Plugin.attributeChanged` method.
4747
*/
4848
observedAttributes: Array<string>
49+
50+
/**
51+
* Replace the values in destination with those from source where the keys match, in-place.
52+
* @param {Object} destination Where to place the translated strings, already filled with the keys pointing to English strings.
53+
* @param {Object} source The same keys, or some of them, mapped to translated strings. Keys not present here will retain the values they are maapped to in destination.
54+
*/
55+
addTranslations(destination: Object, source: Object): void
4956
}
5057

5158
// ESM-SUPPORT-START-NAMESPACE-1 Do not (re)move this - it's needed for ESM generation
@@ -128,8 +135,8 @@ export namespace plugins {
128135
class FindAndReplace extends Plugin {
129136
/**
130137
* Create a find-and-replace command plugin to pass into a template
131-
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
132-
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
138+
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
139+
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
133140
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the find-and-replace.js source code for the English text.
134141
*/
135142
constructor(useCtrlF?: boolean, useCtrlH?: boolean,
@@ -171,7 +178,7 @@ export namespace plugins {
171178
class GoToLine extends Plugin {
172179
/**
173180
* Create a go-to-line command plugin to pass into a template
174-
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
181+
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
175182
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the English text.
176183
*/
177184
constructor(useCtrlG?: boolean,
@@ -269,14 +276,22 @@ export namespace plugins {
269276
/**
270277
* Render special characters and control characters as a symbol with their hex code.
271278
* Files: special-chars.js, special-chars.css
279+
*
280+
* WARNING:
281+
*
282+
* This plugin is currently unstable when used with other plugins,
283+
* Unicode characters, or highlight.js. I hope to fix much of this by
284+
* major version 3, and if you could help that would be amazing!
285+
*
286+
* See https://github.com/WebCoder49/code-input/issues?q=is%3Aissue%20state%3Aopen%20specialchars
272287
*/
273288
class SpecialChars extends Plugin {
274289
/**
275290
* Create a special characters plugin instance.
276291
* Default = covers many non-renderable ASCII characters.
277-
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code
278-
* @param {Boolean} inheritTextColor If `inheritTextColor` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base color of the `pre code` element is used to give contrast to the small characters.
279-
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
292+
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code. Defaults to false.
293+
* @param {Boolean} inheritTextColor If true, forces the color of the hex code to inherit from syntax highlighting. If false, the base color of the `pre code` element is used to give contrast to the small characters. Defaults to false.
294+
* @param {RegExp} specialCharRegExp The regular expression which matches special characters. Defaults to many non-renderable ASCII characters (which characters are renderable depends on the browser and OS).
280295
*/
281296
constructor(colorInSpecialChars?: boolean, inheritTextColor?: boolean, specialCharRegExp?: RegExp);
282297
}

code-input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ var codeInput = {
367367
}
368368

369369
/**
370-
* Replace the keys in destination with any source
370+
* Replace the values in destination with those from source where the keys match, in-place.
371371
* @param {Object} destination Where to place the translated strings, already filled with the keys pointing to English strings.
372-
* @param {Object} source The same keys, or some of them, mapped to translated strings.
372+
* @param {Object} source The same keys, or some of them, mapped to translated strings. Keys not present here will retain the values they are maapped to in destination.
373373
*/
374374
addTranslations(destination, source) {
375375
for(const key in source) {

docs/_index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ console.log("Hello, World!");</textarea></code-input>
4747

4848
### Tutorials by Example
4949

50-
#### Prism.js Code Editor (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](frameworks/prism)) {#playground-preset-prism}
50+
#### Prism.js Code Editor (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](modules-and-frameworks/prism)) {#playground-preset-prism}
5151

5252
```
5353
<!DOCTYPE html>
@@ -103,7 +103,7 @@ console.log("Hello, World!");</textarea></code-input>
103103
</html>
104104
```
105105

106-
#### highlight.js Code Editor (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](frameworks/hljs)) {#playground-preset-hljs}
106+
#### highlight.js Code Editor (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](modules-and-frameworks/hljs)) {#playground-preset-hljs}
107107

108108
```
109109
<!DOCTYPE html>
@@ -160,7 +160,7 @@ console.log("Hello, World!");</textarea></code-input>
160160
</html>
161161
```
162162

163-
#### Editor with Custom Highlighting Algorithm (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](frameworks/custom)) {#playground-preset-custom}
163+
#### Editor with Custom Highlighting Algorithm (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](modules-and-frameworks/custom)) {#playground-preset-custom}
164164

165165
```
166166
<!DOCTYPE html>
@@ -279,7 +279,7 @@ Widely Usable and Progressively Enhanced
279279
</dt>
280280
<dd>
281281

282-
Works on any modern browser independent of whether a framework is used, with the standardised web component API. Integrates well into modular setups and web frameworks with TypeScript definitions, an ECMAScript Module build and tutorials. Falls back to a `textarea` element when there is insufficient JavaScript support. The fallback even works on Lynx.
282+
Works on any modern browser independent of whether a framework is used, with the standardised web component API. Integrates well into modular setups and web frameworks with TypeScript definitions, an ECMAScript Module build and [tutorials](modules-and-frameworks). Falls back to a `textarea` element when there is insufficient JavaScript support. The fallback even works on Lynx.
283283

284284
</dd>
285285
</dl>
@@ -297,4 +297,4 @@ something like [CodeMirror](https://codemirror.net/),
297297
[Monaco](https://microsoft.github.io/monaco-editor/).
298298

299299
## Read Enough?
300-
**If you don't need web framework integration, get started with the commented tutorials by example on this page, for [Prism.js](#playground-preset-prism), [highlight.js](#playground-preset-hljs), or [another highlighter](#playground-preset-custom). If you're using a web framework, start [here](frameworks).**
300+
**If you don't need web framework integration, get started with the commented tutorials by example on this page, for [Prism.js](#playground-preset-prism), [highlight.js](#playground-preset-hljs), or [another highlighter](#playground-preset-custom). If you're using ECMAScript modules or a web framework, start [here](modules-and-frameworks).**

docs/frameworks/hljs/_index.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

docs/i18n/_index.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
+++
2+
title = 'Internationalising code-input.js'
3+
+++
4+
5+
# Internationalising code-input.js
6+
7+
> Contributors: 2021 Oliver Geer
8+
9+
## Languages
10+
11+
code-input.js defaults to using English text, but when want the interface in a different language you can provide text in a different language.
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+
```css
15+
code-input:not(.code-input_registered)::after {
16+
content: "No highlighting. JavaScript support is disabled or insufficient, or codeInput.registerTemplate has not been called."!important;
17+
}
18+
```
19+
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.
20+
21+
**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:
22+
```javascript
23+
// CC-BY; Attribution: Translated by Oliver Geer with some help from English Wiktionary
24+
let findAndReplaceTranslations = {
25+
start: "Buscar términos en su código.",
26+
none: "No hay sucesos",
27+
oneFound: "1 suceso encontrado.",
28+
matchIndex: (index, count) => `${index} de ${count} sucesos.`,
29+
error: (message) => `Error: ${message}`,
30+
infiniteLoopError: "Causa un ciclo infinito",
31+
closeDialog: "Cerrar el Diálogo y Regresar al Editor",
32+
findPlaceholder: "Buscar",
33+
findCaseSensitive: "Prestar atención a las minúsculas/mayúsculas",
34+
findRegExp: "Utilizar expresión regular de JavaScript",
35+
replaceTitle: "Reemplazar",
36+
replacePlaceholder: "Reemplazar con",
37+
findNext: "Buscar Suceso Próximo",
38+
findPrevious: "Buscar Suceso Previo",
39+
replaceActionShort: "Reemplazar",
40+
replaceAction: "Reemplazar este Suceso",
41+
replaceAllActionShort: "Reemplazar Todos",
42+
replaceAllAction: "Reemplazar Todos los Sucesos"
43+
};
44+
// ...
45+
// passed when the plugin is constructed:
46+
new codeInput.plugins.FindAndReplace(true, true, findAndReplaceTranslations),
47+
```
48+
49+
## Other
50+
* code-input.js has supported text bidirectionality using the HTML `dir` attribute since version 2.5.
File renamed without changes.

docs/frameworks/custom/_index.md renamed to docs/modules-and-frameworks/custom/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ title = 'Using code-input.js with custom highlighting algorihms in projects whic
66

77
> Contributors: 2025 Oliver Geer
88
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 frameworkless code for code-input and custom highlighting algorithms](../../#playground-preset-custom).
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).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)