autopair 0.1.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 ADDED
@@ -0,0 +1,27 @@
1
+ Adjusted MIT License
2
+
3
+ Copyright (c) 2025 Dennis Hackethal
4
+
5
+ Permission is hereby granted, free of charge, to any person* obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ * Exception: no one in the public sector may use this software. That means
24
+ no politicians, public-school teachers, police, intelligence services, etc.
25
+ Private recipients of public moneys may not use this software either unless
26
+ they oppose the welfare state and consider such moneys restitution. No one
27
+ doing unpaid work for any government may use this software either.
package/README ADDED
@@ -0,0 +1,45 @@
1
+ # autopair.js
2
+
3
+ Lightweight autopairing + typethrough behavior for HTML `input[type=text]` and `textarea`. No dependencies. Preserves the undo/redo stack.
4
+
5
+ ## Features
6
+
7
+ 1. Automatically closes parentheses, brackets, curly braces, single quotes, and double quotes.
8
+ 2. Wraps selected text. For example, selecting a word and hitting `(` will wrap the word in parentheses.
9
+ 3. Atomically removes pairings. For example, when hitting backspace inside `()`, both characters are removed.
10
+ 4. Types through closing characters. For example, hitting `]` in front of an already typed `]` simply moves the cursor past it.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install autopair
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```html
21
+ <script src="path/to/autopair.js"></script>
22
+ <script>
23
+ const textarea = document.getElementById('editor');
24
+
25
+ // Autopair with default pairings:
26
+ // '(': ')',
27
+ // '[': ']',
28
+ // '{': '}',
29
+ // "'": "'",
30
+ // '"': '"'
31
+ autopair(textarea);
32
+
33
+ // When defining custom pairings, include the defaults
34
+ autopair(textarea, {
35
+ '(': ')',
36
+ '[': ']',
37
+ '{': '}',
38
+ "'": "'",
39
+ '"': '"',
40
+ '‘': '’', // Curly quotes
41
+ '“': '”',
42
+ '*': '*' // For markdown italics
43
+ });
44
+ </script>
45
+ ```
package/index.html ADDED
@@ -0,0 +1,32 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Autopair Test</title>
6
+ </head>
7
+ <body>
8
+ <h1>Autopair Test</h1>
9
+ <textarea id="editor" rows="10" cols="50" autofocus></textarea>
10
+
11
+ <script src="./src/autopair.js"></script>
12
+ <script>
13
+ // Use the autopair module
14
+ const textarea = document.getElementById('editor');
15
+
16
+ // If using Node style exports in browser, we can attach manually for testing:
17
+ // const autopair = window.autopair || module?.exports;
18
+
19
+ autopair(textarea, {
20
+ '(': ')',
21
+ '[': ']',
22
+ '{': '}',
23
+ "'": "'",
24
+ '"': '"',
25
+ '‘': '’',
26
+ '“': '”',
27
+ '*': '*',
28
+ '_': '_'
29
+ });
30
+ </script>
31
+ </body>
32
+ </html>
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "autopair",
3
+ "version": "0.1.0",
4
+ "description": "Automatically close special characters like parentheses.",
5
+ "main": "index.js",
6
+ "author": "Dennis Hackethal <engineering@dennishackethal.com>",
7
+ "license": "Adjusted MIT"
8
+ }
@@ -0,0 +1,79 @@
1
+ function autopair(textarea, pairs = {
2
+ '(': ')',
3
+ '[': ']',
4
+ '{': '}',
5
+ "'": "'",
6
+ '"': '"'
7
+ }) {
8
+ textarea.addEventListener('keydown', (evt) => {
9
+ const { selectionStart: start, selectionEnd: end, value } = textarea;
10
+
11
+ // Typethrough
12
+ if (start === end) {
13
+ const next = value[end];
14
+ const isClosing = Object.values(pairs).includes(evt.key);
15
+
16
+ if (isClosing && next === evt.key) {
17
+ evt.preventDefault();
18
+ textarea.selectionStart = textarea.selectionEnd = end + 1;
19
+ return;
20
+ }
21
+ }
22
+
23
+ // Handle backspace inside a direct pair
24
+ if (evt.key === 'Backspace' && start === end && start > 0) {
25
+ const left = value[start - 1];
26
+ const right = value[start];
27
+ const opening = Object.keys(pairs).find(k => pairs[k] === right);
28
+ if (left === opening) {
29
+ evt.preventDefault();
30
+ // Select the pair and delete in one go
31
+ textarea.selectionStart = start - 1;
32
+ textarea.selectionEnd = start + 1;
33
+ document.execCommand('insertText', false, '');
34
+
35
+ return;
36
+ }
37
+
38
+ return; // normal backspace
39
+ }
40
+
41
+ const closing = pairs[evt.key];
42
+ if (!closing) return;
43
+
44
+ const isWordChar = /[\w]/;
45
+ const punctuation = /[;,.})\]]/;
46
+
47
+ // Wrap selection if present
48
+ if (start !== end) {
49
+ evt.preventDefault();
50
+ textarea.selectionStart = start;
51
+ textarea.selectionEnd = end;
52
+ document.execCommand('insertText', false, evt.key + value.slice(start, end) + closing);
53
+ textarea.selectionStart = start + 1;
54
+ textarea.selectionEnd = end + 1;
55
+ return;
56
+ }
57
+
58
+ // Only autopair if next char is whitespace, punctuation, or a closing of the same type
59
+ const nextChar = value[end] || '';
60
+ const insidePair = closing === nextChar;
61
+ const safeNext = !isWordChar.test(nextChar) || punctuation.test(nextChar);
62
+ if (!insidePair && !safeNext) return;
63
+
64
+ evt.preventDefault();
65
+ textarea.selectionStart = textarea.selectionEnd = start;
66
+ document.execCommand('insertText', false, evt.key + closing);
67
+ textarea.selectionStart = textarea.selectionEnd = start + 1;
68
+ });
69
+ }
70
+
71
+ // Export for Node
72
+ if (typeof module !== 'undefined' && module.exports) {
73
+ module.exports = autopair;
74
+ }
75
+
76
+ // Attach to window for browser
77
+ if (typeof window !== 'undefined') {
78
+ window.autopair = autopair;
79
+ }