@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 = 'Creating your own code-input Plugin to add functionality'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# Creating your own code-input Plugin to add functionality
|
|
6
|
+
|
|
7
|
+
If you're writing some code that depends on the code-input editor and isn't very useful independent of it, or integrates other code with code-input, writing a plugin's often the way to go. You can even choose to contribute it back to the code-input library!
|
|
8
|
+
|
|
9
|
+
A very useful source of reference is the code-input.d.ts file in code-input.js' source code, which defines the public JavaScript interface of the code-input.js library including code-input elements.
|
|
10
|
+
|
|
11
|
+
Start with this code, which is also available as the `test` plugin. Afterwards, construct and pass a `new TestPlugin()` into the array when you register a code-input.js template, like any other code-input plugin:
|
|
12
|
+
```javascript
|
|
13
|
+
const TestPlugin = class extends codeInput.Plugin {
|
|
14
|
+
instructions = {
|
|
15
|
+
beforeHighlight: "before highlight",
|
|
16
|
+
afterHighlight: "after highlight",
|
|
17
|
+
beforeElementsAdded: "before elements added",
|
|
18
|
+
afterElementsAdded: "after elements added",
|
|
19
|
+
attributeChanged: (name, oldValue, newValue) => `${name}: '${oldValue}'>'${newValue}'`
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
constructor(instructionTranslations = {}) {
|
|
23
|
+
super(["testattr"]);
|
|
24
|
+
// Array of observed attributes as parameter
|
|
25
|
+
|
|
26
|
+
// instructionTranslations, instructions, and the addTranslations
|
|
27
|
+
// call need not be present if this plugin uses no localisable
|
|
28
|
+
// text.
|
|
29
|
+
this.addTranslations(this.instructions, instructionTranslations);
|
|
30
|
+
}
|
|
31
|
+
/* Runs before code is highlighted; Params: codeInput element) */
|
|
32
|
+
beforeHighlight(codeInput) {
|
|
33
|
+
console.log(codeInput, this.instructions.beforeHighlight);
|
|
34
|
+
}
|
|
35
|
+
/* Runs after code is highlighted; Params: codeInput element) */
|
|
36
|
+
afterHighlight(codeInput) {
|
|
37
|
+
console.log(codeInput, this.instructions.afterHighlight);
|
|
38
|
+
}
|
|
39
|
+
/* Runs before elements are added into a `code-input`; Params: codeInput element) */
|
|
40
|
+
beforeElementsAdded(codeInput) {
|
|
41
|
+
console.log(codeInput, this.instructions.beforeElementsAdded);
|
|
42
|
+
}
|
|
43
|
+
/* Runs after elements are added into a `code-input` (useful for adding events to the textarea); Params: codeInput element) */
|
|
44
|
+
afterElementsAdded(codeInput) {
|
|
45
|
+
console.log(codeInput, this.instructions.afterElementsAdded);
|
|
46
|
+
}
|
|
47
|
+
/* Runs when an observed attribute of a `code-input` is changed (you must add the attribute name in the constructor); Params: codeInput element, name attribute name, oldValue previous value of attribute, newValue changed value of attribute) */
|
|
48
|
+
attributeChanged(codeInput, name, oldValue, newValue) {
|
|
49
|
+
console.log(codeInput, this.instructions.attriibuteChanged(name, oldValue, newValue));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
+++
|
|
2
|
+
title = 'How code-input.js works Behind The Scenes'
|
|
3
|
+
+++
|
|
4
|
+
|
|
5
|
+
# How `code-input.js` works Behind The Scenes
|
|
6
|
+
|
|
7
|
+
> Contributors: 2021 Oliver Geer
|
|
8
|
+
|
|
9
|
+
Bearing in mind that it will teach you to make your own version of the core of <code>code-input.js</code> but you should use the library itself if you want more stability, please see [this CSS-Tricks article](https://css-tricks.com/creating-an-editable-textarea-that-supports-syntax-highlighted-code). It was written by the founder of the library just before development started, so doesn't contain all bugfixes but does contain the core theory and how to create a minimal `code-input` yourself.
|
package/esm/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Autogenerated ECMAScript Modules
|
|
2
|
+
|
|
3
|
+
## Using
|
|
4
|
+
|
|
5
|
+
If you are using Yarn, NPM, or a similar package manager, the files should have been generated before being uploaded to the package repository, or on `pack` if the package manager is fetching from Git.
|
|
6
|
+
|
|
7
|
+
Otherwise, after changing directory to the one containing this file:
|
|
8
|
+
|
|
9
|
+
- If you have Node.js installed, run `node generate.mjs`.
|
|
10
|
+
- If you don't have Node.js installed but are on a POSIX-like system with `bash`/`zsh`, run `sh ./generate.sh`.
|
|
11
|
+
- If neither of the above are true, install Node.js or (slightly harder; look online) a POSIX/"Linux" compatible shell.
|
|
12
|
+
|
|
13
|
+
## Extra Information
|
|
14
|
+
|
|
15
|
+
When code-input was started, it was written and tested only to be imported directly via a `<script>` tag, and it assigned an object to a global `codeInput` variable containing all its functionality. As plugins were added, they were implemented as similar but separate `<script>` tags. However, this limits where `codeInput` can be used, making it difficult to integrate with many larger JavaScript projects and frameworks, and causes code duplication when multiple plugins use the same code.
|
|
16
|
+
|
|
17
|
+
To fix these, code-input is gaining support for ECMAScript Modules (ESM), a standard way to import modules and export from them with JavaScript. ESM can be used directly in NPM/Yarn-led environments, bundled for inclusion in a `<script>` tag in a backwards-compatible way, or imported as a module into a web browser which supports it natively.
|
|
18
|
+
|
|
19
|
+
To ensure backwards compatibility, in the first stage of the transition a process to auto-generate ESM-compatible files from the existing JavaScript files will be created, so existing `<script>` tag users are unaffected.
|
|
20
|
+
|
|
21
|
+
Later in the second stage, `code-input`'s daily-edited source code may be relocated to ESM, using these generated files, and the direct importable files would be produced by a bundler.
|
|
22
|
+
|
|
23
|
+
However, refactoring the core would need quite a lot of work and testing, and the first stage suffices for compatibility with all the examples. This directory will exist from the first stage until the second stage, containing the tools to generate ESM files. After the second stage, it would likely be repurposed as the main source code directory, containing the same files which would become the main developed ones.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// NOTICE: This code is @generated from code outside the esm directory. Please do not edit it to contribute!
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Plugins are imported from the plugins folder. They will then
|
|
6
|
+
* provide custom extra functionality to code-input elements.
|
|
7
|
+
*/
|
|
8
|
+
export abstract class Plugin {
|
|
9
|
+
/**
|
|
10
|
+
* Create a Plugin
|
|
11
|
+
*
|
|
12
|
+
* @param {Array<string>} observedAttributes - The HTML attributes to watch for this plugin, and report any
|
|
13
|
+
* modifications to the `codeInput.Plugin.attributeChanged` method.
|
|
14
|
+
*/
|
|
15
|
+
constructor(observedAttributes: Array<string>)
|
|
16
|
+
/**
|
|
17
|
+
* Runs before code is highlighted.
|
|
18
|
+
* @param {codeInput.CodeInput} codeInput - The codeInput element
|
|
19
|
+
*/
|
|
20
|
+
beforeHighlight(codeInput: CodeInput): void
|
|
21
|
+
/**
|
|
22
|
+
* Runs after code is highlighted.
|
|
23
|
+
* @param {codeInput.CodeInput} codeInput - The codeInput element
|
|
24
|
+
*/
|
|
25
|
+
afterHighlight(codeInput: CodeInput): void
|
|
26
|
+
/**
|
|
27
|
+
* Runs before elements are added into a code-input element.
|
|
28
|
+
* @param {codeInput.CodeInput} codeInput - The codeInput element
|
|
29
|
+
*/
|
|
30
|
+
beforeElementsAdded(codeInput: CodeInput): void
|
|
31
|
+
/**
|
|
32
|
+
* Runs after elements are added into a code-input element (useful for adding events to the textarea).
|
|
33
|
+
* @param {codeInput.CodeInput} codeInput - The codeInput element
|
|
34
|
+
*/
|
|
35
|
+
afterElementsAdded(codeInput: CodeInput): void
|
|
36
|
+
/**
|
|
37
|
+
* Runs when an attribute of a code-input element is changed (you must add the attribute name to `codeInput.Plugin.observedAttributes` first).
|
|
38
|
+
* @param {codeInput.CodeInput} codeInput - The codeInput element
|
|
39
|
+
* @param {string} name - The name of the attribute
|
|
40
|
+
* @param {string} oldValue - The value of the attribute before it was changed
|
|
41
|
+
* @param {string} newValue - The value of the attribute after it is changed
|
|
42
|
+
*/
|
|
43
|
+
attributeChanged(codeInput: CodeInput, name: string, oldValue: string, newValue: string): void
|
|
44
|
+
/**
|
|
45
|
+
* The HTML attributes to watch for this plugin, and report any
|
|
46
|
+
* modifications to the `codeInput.Plugin.attributeChanged` method.
|
|
47
|
+
*/
|
|
48
|
+
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
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* If you're using one of the two named highlighters, please see
|
|
61
|
+
* `codeInput.templates.prism` or `codeInput.templates.hljs`.
|
|
62
|
+
* Otherwise please see this class' constructor.
|
|
63
|
+
* Templates are used in `<code-input>` elements and once registered with
|
|
64
|
+
* `codeInput.registerTemplate` will be in charge of the highlighting
|
|
65
|
+
* algorithm and settings for all code-inputs with a `template` attribute
|
|
66
|
+
* matching the registered name.
|
|
67
|
+
*/
|
|
68
|
+
export class Template {
|
|
69
|
+
/**
|
|
70
|
+
* **When `includeCodeInputInHighlightFunc` is `false`, `highlight` takes only the `<pre><code>` element as a parameter.**
|
|
71
|
+
*
|
|
72
|
+
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
|
|
73
|
+
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
|
|
74
|
+
* @param {(codeElement: HTMLElement) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
|
|
75
|
+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
|
|
76
|
+
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
|
|
77
|
+
* @param {false} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
|
|
78
|
+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
|
|
79
|
+
* @returns template object
|
|
80
|
+
*/
|
|
81
|
+
constructor(highlight?: (codeElement: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, plugins?: Plugin[])
|
|
82
|
+
/**
|
|
83
|
+
* **When `includeCodeInputInHighlightFunc` is `true`, `highlight` takes two parameters: the `<pre><code>` element, and the `<code-input>` element.**
|
|
84
|
+
*
|
|
85
|
+
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
|
|
86
|
+
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
|
|
87
|
+
* @param {(codeElement: HTMLElement, codeInput: CodeInput) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
|
|
88
|
+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
|
|
89
|
+
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
|
|
90
|
+
* @param {true} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
|
|
91
|
+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
|
|
92
|
+
* @returns template object
|
|
93
|
+
*/
|
|
94
|
+
constructor(highlight?: (codeElement: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, plugins?: Plugin[])
|
|
95
|
+
highlight: Function
|
|
96
|
+
preElementStyled: boolean
|
|
97
|
+
isCode: boolean
|
|
98
|
+
includeCodeInputInHighlightFunc: boolean
|
|
99
|
+
plugins: Plugin[]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A `<code-input>` element, an instance of an `HTMLElement`, and the result
|
|
105
|
+
* of `document.createElement("code-input")`. Attributes are only set when
|
|
106
|
+
* the element's template has been registered, and before this are null.
|
|
107
|
+
*/
|
|
108
|
+
export class CodeInput extends HTMLTextAreaElement { // Tries to implement textarea interface
|
|
109
|
+
/**
|
|
110
|
+
* When the code-input's template is registered, this contains its codeInput.Template object.
|
|
111
|
+
*/
|
|
112
|
+
templateObject?: readonly Template
|
|
113
|
+
/**
|
|
114
|
+
* Exposed child textarea element for user to input code in; in this version of code-input you shouldn't need to access
|
|
115
|
+
* it because most textarea functionality is present on the code-input element itself.
|
|
116
|
+
*/
|
|
117
|
+
textareaElement?: HTMLTextAreaElement
|
|
118
|
+
/**
|
|
119
|
+
* Exposed child pre element where syntax-highlighted code is outputted.
|
|
120
|
+
* Contains this.codeElement as its only child.
|
|
121
|
+
*/
|
|
122
|
+
preElement?: HTMLPreElement
|
|
123
|
+
/**
|
|
124
|
+
* Exposed child pre element's child code element where syntax-highlighted code is outputted.
|
|
125
|
+
* Has this.preElement as its parent.
|
|
126
|
+
*/
|
|
127
|
+
codeElement?: HTMLElement
|
|
128
|
+
/**
|
|
129
|
+
* Exposed non-scrolling element designed to contain dialog boxes etc. from plugins,
|
|
130
|
+
* that shouldn't scroll with the code-input element.
|
|
131
|
+
*/
|
|
132
|
+
dialogContainerElement?: HTMLElement
|
|
133
|
+
/**
|
|
134
|
+
* Show some instructions to the user only if they are using keyboard navigation - for example, a prompt on how to navigate with the keyboard if Tab is repurposed.
|
|
135
|
+
* @param {string} instructions The instructions to display only if keyboard navigation is being used. If it's blank, no instructions will be shown.
|
|
136
|
+
* @param {boolean} includeAriaDescriptionFirst Whether to include the aria-description of the code-input element before the keyboard navigation instructions for a screenreader. Keep this as true when the textarea is first focused.
|
|
137
|
+
*/
|
|
138
|
+
setKeyboardNavInstructions(instructions: string, includeAriaDescriptionFirst: boolean): void
|
|
139
|
+
/**
|
|
140
|
+
* Allows plugins to store data in the scope of a single element.
|
|
141
|
+
* Key - name of the plugin, in camelCase
|
|
142
|
+
* Value - object of data to be stored; different plugins may use this differently.
|
|
143
|
+
*/
|
|
144
|
+
pluginData: Object
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Register a template so code-input elements with a template attribute that equals the templateName will use the template.
|
|
149
|
+
* See `codeInput.templates` for constructors to create templates.
|
|
150
|
+
* @param {string} templateName - the name to register the template under
|
|
151
|
+
* @param {Object} template - a Template object instance - see `codeInput.templates`
|
|
152
|
+
*/
|
|
153
|
+
export function registerTemplate(templateName: string, template: Template): void;
|
|
154
|
+
export default { Plugin, Template, CodeInput, registerTemplate };
|