autopair 1.1.1 → 1.2.1

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/README.md CHANGED
@@ -57,7 +57,7 @@ npm install autopair
57
57
  '*': '*' // For markdown italics
58
58
  });
59
59
 
60
- // Teardown, ie remove autopair.js functionality
60
+ // Teardown, ie remove autopair.js functionality from an element
61
61
  let teardown = autopair(textarea);
62
62
  teardown();
63
63
  </script>
package/index.html CHANGED
@@ -26,7 +26,8 @@
26
26
  '‘': '’',
27
27
  '“': '”',
28
28
  '*': '*',
29
- '_': '_'
29
+ '_': '_',
30
+ '`': '`'
30
31
  });
31
32
  </script>
32
33
  </body>
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "autopair",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "Automatically closes parentheses and similar characters.",
5
- "main": "index.js",
5
+ "main": "src/autopair.js",
6
6
  "author": "Dennis Hackethal <engineering@dennishackethal.com>",
7
7
  "license": "Adjusted MIT",
8
8
  "type": "module",
package/src/autopair.js CHANGED
@@ -55,11 +55,30 @@ export default function autopair(textarea, pairs = {
55
55
  return;
56
56
  }
57
57
 
58
- // Only autopair if next char is whitespace, punctuation, or a closing of the same type
59
58
  const nextChar = value[end] || '';
59
+ const prevChar = value[start - 1] || '';
60
+
60
61
  const insidePair = closing === nextChar;
61
62
  const safeNext = !isWordChar.test(nextChar) || punctuation.test(nextChar);
62
- if (!insidePair && !safeNext) return;
63
+ const isSymmetric = evt.key === closing;
64
+
65
+ // Autoclose only when allowed
66
+ if (
67
+ !insidePair &&
68
+ (
69
+ !safeNext ||
70
+ (
71
+ isSymmetric &&
72
+ start === end &&
73
+ (
74
+ isWordChar.test(prevChar) || // end of word
75
+ prevChar === evt.key // repeated quote
76
+ )
77
+ )
78
+ )
79
+ ) {
80
+ return;
81
+ }
63
82
 
64
83
  evt.preventDefault();
65
84
  textarea.selectionStart = textarea.selectionEnd = start;