Skip to content

Commit ab52adb

Browse files
author
Humberto Parra Vargas
authored
+ support to extendes autoclose simbols (#122)
* + support to extendes autoclose simbols * + Documentation support autoclose
1 parent 7f17055 commit ab52adb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ Third argument to `CodeJar` is options:
5454
- `addClosing: boolean` automatically adds closing brackets, quotes. Default `true`.
5555
- `history` records history. Default `true`.
5656
- `window` window object. Default: `window`.
57+
- `autoclose` object
58+
- `open string` characters that triggers the autoclose function
59+
- `close string` characters that correspond to the opening ones and close the object.
5760

5861

5962
```js
6063
const options = {
6164
tab: ' '.repeat(4), // default is '\t'
6265
indentOn: /[(\[]$/, // default is /{$/
66+
autoclose: {
67+
open: `([{*`, // default is `([{'"`
68+
close: `)]}*` // default is `)]}'"`
69+
}
6370
}
6471

6572
const jar = CodeJar(editor, highlight, options)

codejar.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type Options = {
1010
addClosing: boolean
1111
history: boolean
1212
window: typeof window
13+
autoclose: {
14+
open: string;
15+
close: string;
16+
}
1317
}
1418

1519
type HistoryRecord = {
@@ -36,6 +40,10 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
3640
addClosing: true,
3741
history: true,
3842
window: globalWindow,
43+
autoclose: {
44+
open: `([{'"`,
45+
close: `)]}'"`
46+
},
3947
...opt,
4048
}
4149

@@ -349,13 +357,13 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
349357
}
350358

351359
function handleSelfClosingCharacters(event: KeyboardEvent) {
352-
const open = `([{'"`
353-
const close = `)]}'"`
360+
const open = options.autoclose.open;
361+
const close = options.autoclose.close;
354362
if (open.includes(event.key)) {
355363
preventDefault(event)
356364
const pos = save()
357365
const wrapText = pos.start == pos.end ? '' : getSelection().toString()
358-
const text = event.key + wrapText + close[open.indexOf(event.key)]
366+
const text = event.key + wrapText + (close[open.indexOf(event.key)] ?? "")
359367
insert(text)
360368
pos.start++
361369
pos.end++

0 commit comments

Comments
 (0)