Skip to content

Commit 0691380

Browse files
committed
Release 4.0.0
1 parent 747bd71 commit 0691380

File tree

7 files changed

+105
-234
lines changed

7 files changed

+105
-234
lines changed

README.md

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<a href="https://webpod.dev/?from=codejar"><img src="https://webpod.dev/img/banner.png" alt="Webpod - deploy JavaScript apps" width="190" align="right"></a>
2-
31
<p align="center"><a href="https://medv.io/codejar/"><img src="https://medv.io/assets/codejar.svg" width="72"></a></p>
42
<h3 align="center">CodeJar – an embeddable code editor for the browser</h3>
53
<p align="center"><a href="https://medv.io/codejar/"><img src="https://medv.io/assets/codejar/screenshot.png" width="709"></a></p>
@@ -9,7 +7,7 @@
97

108
## Features
119

12-
* Lightweight (**2 kB** only)
10+
* Lightweight (**3 kB** only)
1311
* Preserves indentation on a new line
1412
* Adds closing brackets, quotes
1513
* Indents line with the **Tab** key
@@ -23,33 +21,25 @@ Install CodeJar 🍯 &nbsp; via npm:
2321
npm i codejar
2422
```
2523

26-
CodeJar 🍯 &nbsp; can be used via modules:
27-
28-
```html
29-
<script type="module">
30-
import {CodeJar} from 'https://medv.io/codejar/codejar.js'
31-
</script>
32-
```
33-
3424
Create an element and init the CodeJar 🍯:
3525

3626
```html
3727
<div class="editor"></div>
3828
<script>
39-
let jar = CodeJar(document.querySelector('.editor'), hljs.highlightElement)
29+
let jar = CodeJar(document.querySelector('.editor'), highlight)
4030
</script>
4131
```
4232

43-
Second argument to `CodeJar` is a highlighting function (in this example [highlight.js](https://highlightjs.org)), but any function may be used:
33+
Second argument to `CodeJar` is a highlighting function (like Prism.js, highlight.js):
4434

4535
```ts
4636
const highlight = (editor: HTMLElement) => {
4737
const code = editor.textContent
48-
// Do something with code and set html.
38+
code = code.replace('foo', '<span style="color: red">foo</span>')
4939
editor.innerHTML = code
5040
}
5141

52-
let jar = CodeJar(editor, highlight)
42+
const jar = CodeJar(editor, highlight)
5343
```
5444

5545
Third argument to `CodeJar` is options:
@@ -66,29 +56,12 @@ Third argument to `CodeJar` is options:
6656

6757

6858
```js
69-
let options = {
59+
const options = {
7060
tab: ' '.repeat(4), // default is '\t'
7161
indentOn: /[(\[]$/, // default is /{$/
7262
}
7363

74-
let jar = CodeJar(editor, highlight, options)
75-
```
76-
77-
Some styles may be applied to our editor to make it better looking:
78-
79-
```css
80-
.editor {
81-
border-radius: 6px;
82-
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
83-
font-family: 'Source Code Pro', monospace;
84-
font-size: 14px;
85-
font-weight: 400;
86-
height: 340px;
87-
letter-spacing: normal;
88-
line-height: 20px;
89-
padding: 10px;
90-
tab-size: 4;
91-
}
64+
const jar = CodeJar(editor, highlight, options)
9265
```
9366

9467
## API
@@ -154,8 +127,8 @@ Removes event listeners from editor.
154127

155128
## Related
156129

157-
* [react-codejar](https://github.com/guilhermelimak/react-codejar) - a react wrapper for CodeJar.
158-
* [ngx-codejar](https://github.com/julianpoemp/ngx-codejar) - an angular wrapper for CodeJar.
130+
* [react-codejar](https://github.com/guilhermelimak/react-codejar) - a React wrapper for CodeJar.
131+
* [ngx-codejar](https://github.com/julianpoemp/ngx-codejar) - an Angular wrapper for CodeJar.
159132

160133
## License
161134

codejar.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
321321
function handleSelfClosingCharacters(event: KeyboardEvent) {
322322
const open = `([{'"`
323323
const close = `)]}'"`
324-
const codeAfter = afterCursor()
325-
const codeBefore = beforeCursor()
326-
const escapeCharacter = codeBefore.substr(codeBefore.length - 1) === '\\'
327-
const charAfter = codeAfter.substr(0, 1)
328-
if (close.includes(event.key) && !escapeCharacter && charAfter === event.key) {
329-
// We already have closing char next to cursor.
330-
// Move one char to right.
331-
const pos = save()
332-
preventDefault(event)
333-
pos.start = ++pos.end
334-
restore(pos)
335-
} else if (
336-
open.includes(event.key)
337-
&& !escapeCharacter
338-
&& (`"'`.includes(event.key) || ['', ' ', '\n'].includes(charAfter))
339-
) {
324+
if (open.includes(event.key)) {
340325
preventDefault(event)
341326
const pos = save()
342327
const wrapText = pos.start == pos.end ? '' : getSelection().toString()
@@ -422,7 +407,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
422407
const text = ((event as any).originalEvent || event)
423408
.clipboardData
424409
.getData('text/plain')
425-
.replace(/\r/g, '')
410+
.replace(/\r\n?/g, '\n')
426411
const pos = save()
427412
insert(text)
428413
highlight(editor)
@@ -450,18 +435,12 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
450435

451436
function visit(editor: HTMLElement, visitor: (el: Node) => 'stop' | undefined) {
452437
const queue: Node[] = []
453-
454438
if (editor.firstChild) queue.push(editor.firstChild)
455-
456439
let el = queue.pop()
457-
458440
while (el) {
459-
if (visitor(el) === 'stop')
460-
break
461-
441+
if (visitor(el) === 'stop') break
462442
if (el.nextSibling) queue.push(el.nextSibling)
463443
if (el.firstChild) queue.push(el.firstChild)
464-
465444
el = queue.pop()
466445
}
467446
}

demo.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>CodeJar 🍯</title>
6+
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
7+
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet">
8+
<style>
9+
* {
10+
margin: 0;
11+
padding: 0;
12+
box-sizing: border-box;
13+
}
14+
15+
body {
16+
min-height: 100vh;
17+
background: #F6F8F8;
18+
}
19+
20+
main {
21+
max-width: 800px;
22+
margin: 40px auto;
23+
padding: 20px;
24+
}
25+
26+
.editor {
27+
background: #fff;
28+
border-radius: 6px;
29+
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
30+
font-family: "Source Code Pro", monospace;
31+
font-size: 14px;
32+
font-weight: 400;
33+
min-height: 240px;
34+
letter-spacing: normal;
35+
line-height: 20px;
36+
padding: 10px;
37+
tab-size: 4;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<main>
43+
<div class="editor" data-manual data-gramm="false"></div>
44+
</main>
45+
<script type="module">
46+
import {CodeJar} from './dist/codejar.js'
47+
const editor = document.querySelector('.editor')
48+
49+
const highlight = editor => {
50+
editor.innerHTML = Prism.highlight(editor.textContent, Prism.languages.javascript, 'javascript')
51+
}
52+
53+
const jar = CodeJar(editor, highlight, {
54+
tab: ' ',
55+
})
56+
57+
jar.updateCode(localStorage.getItem('code'))
58+
jar.onUpdate(code => {
59+
localStorage.setItem('code', code)
60+
})
61+
</script>
62+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
63+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-javascript.min.js"></script>
64+
</body>
65+
</html>

index.html

Lines changed: 0 additions & 67 deletions
This file was deleted.

linenumbers.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)