Skip to content

Commit eaec1bf

Browse files
Refactored
1 parent 84022dc commit eaec1bf

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

example/example.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
inlineCode: {
5555
class: InlineCode,
5656
shortcut: 'CMD+SHIFT+M',
57+
config: {
58+
containerSelector: "#editorjs"
59+
}
5760
},
5861
},
5962
data: example_data, // Imported from example_data.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/inline-code",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"keywords": [
55
"codex editor",
66
"inline",

src/index.js

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import './index.css';
55
import { IconInlineCode } from '@codexteam/icons'
6-
6+
import { isTextEmpty, isCaretAtLastPosition, isTextNode } from './utils'
77
/**
88
* Inline Code Tool for the Editor.js
99
*
@@ -21,7 +21,7 @@ export default class InlineCode {
2121

2222
/**
2323
*/
24-
constructor({api}) {
24+
constructor({api, config}) {
2525
this.api = api;
2626

2727
/**
@@ -47,45 +47,23 @@ export default class InlineCode {
4747
active: this.api.styles.inlineToolButtonActive
4848
};
4949

50-
window.addEventListener("keydown", this.keyListener)
50+
document.querySelector(config.containerSelector)?.addEventListener("keydown", this.keyListener)
5151
}
5252

5353

54-
isTextEmpty = (textContent) => {
55-
return /^\s*$/.test(textContent)
56-
}
57-
5854
keyListener = (e) => {
5955
const codeWrapper = this.api.selection.findParentTag(this.tag, InlineCode.CSS)
60-
if (e.key === "ArrowRight" && codeWrapper) {
61-
if (!this.isCaretAtLastPosition(codeWrapper)) return
56+
if (e.key !== "ArrowRight" || !codeWrapper) return
57+
if (!isCaretAtLastPosition(codeWrapper)) return
6258

63-
if (codeWrapper.nextElementSibling) return
64-
65-
if (!codeWrapper.nextSibling) {
66-
const emptyChar = document.createTextNode("\u00A0")
67-
codeWrapper.parentElement.appendChild(emptyChar)
68-
69-
} else {
70-
if (this.isTextEmpty(codeWrapper.nextSibling.data)) {
71-
codeWrapper.nextSibling.insertData(0, "\u00A0")
72-
}
59+
if (!isTextNode(codeWrapper.nextSibling)) {
60+
codeWrapper.insertAdjacentText("afterend", "\u00A0")
61+
} else {
62+
if (isTextEmpty(codeWrapper.nextSibling.data)) {
63+
codeWrapper.nextSibling.replaceWith("\u00A0")
7364
}
7465
}
7566
}
76-
77-
isCaretAtLastPosition(element) {
78-
// Get the selection object
79-
const selection = window.getSelection();
80-
81-
if (selection.rangeCount === 0) return false;
82-
83-
// Get the range object
84-
const range = selection.getRangeAt(0);
85-
86-
// Check if the end offset of the range is equal to the length of the text content
87-
return range.endOffset === element.textContent.length;
88-
}
8967
/**
9068
* Specifies Tool as Inline Toolbar Tool
9169
*

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const isTextEmpty = (textContent) => {
2+
return /^\s*$/.test(textContent)
3+
}
4+
5+
export const isCaretAtLastPosition = (element) => {
6+
// Get the selection object
7+
const selection = window.getSelection();
8+
9+
if (selection.rangeCount === 0) return false;
10+
11+
// Get the range object
12+
const range = selection.getRangeAt(0);
13+
14+
// Check if the end offset of the range is equal to the length of the text content
15+
return range.endOffset === element.textContent.length;
16+
}
17+
18+
export const isTextNode = (node) => {
19+
return node && node.nodeType === 3;
20+
}

0 commit comments

Comments
 (0)