Skip to content

Commit

Permalink
allow to customize auto-indent
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 6, 2020
1 parent ea35086 commit 77866d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CodeJar 🍯   can be used via modules:
</script>
```

Create element and init:
Create an element and init the CodeJar 🍯:

```html
<div class="editor"></div>
Expand All @@ -38,7 +38,7 @@ Create element and init:
</script>
```

Second argument to `CodeJar` is highligting function (in this example [PrismJS](https://prismjs.com)), but any function may be used:
Second argument to `CodeJar` is a highligting function (in this example [PrismJS](https://prismjs.com)), but any function may be used:

```ts
const highlight = (editor: HTMLElement) => {
Expand All @@ -51,16 +51,16 @@ let jar = CodeJar(editor, highlight)
```

Third argument to `CodeJar` is options:
- Options.tab replace "tabs" and "auto indent tabs" to given string
- Can use CSS tab-size instead
- If so, "auto indent tabs" can be removed with a single "backspace"
- Options.indentRegex allows indent rule to be customized
- Auto-tab if the last character match the given regex while pressing Enter
- `tab: string` replaces "tabs" with given string. Default: `\t`.
- Note: use css rule `tab-size` to customize size.
- `identOn: RegExp` allows auto indent rule to be customized. Default `{$`
- Auto-tab if the text before cursor match the given regex while pressing Enter.

```js
let options = {
tab: ' '.repeat(4), // default is \t
indentRegex: /[([{'"]/ // default is /[{]/
tab: ' '.repeat(4), // default is '\t'
indentOn: /[(\[]$/, // default is /{$/
}

let jar = CodeJar(editor, highlight, options)
Expand Down
7 changes: 4 additions & 3 deletions codejar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Options = {
tab: string
indentOn: RegExp
}

type HistoryRecord = {
Expand All @@ -16,9 +17,9 @@ type Position = {
export type CodeJar = ReturnType<typeof CodeJar>

export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement) => void, opt: Partial<Options> = {}) {
const options = {
const options: Options = {
tab: "\t",
indentRegex: /[{]/,
indentOn: /{$/,
...opt
}
let listeners: [string, any][] = []
Expand Down Expand Up @@ -217,7 +218,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement) => void

// If last symbol is "{" ident new line
// Allow user defines indent rule
if (before[before.length - 1].match(indentRegex)) {
if (options.indentOn.test(before)) {
newLinePadding += options.tab
}

Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
hljs.highlightBlock(editor)
}

const jar = CodeJar(editor, highlight)
const jar = CodeJar(editor, highlight, {
indentOn: /[(\[{]$/
})

jar.updateCode(localStorage.getItem('code'))
jar.onUpdate(code => {
Expand Down

0 comments on commit 77866d3

Please sign in to comment.