Skip to content

Commit 9ba856b

Browse files
committed
fix: fixed highlight
1 parent 4b7b017 commit 9ba856b

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

.gitattribute

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js linguist-detectable=false

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hackdoor/codebox",
3-
"version": "1.0.11",
3+
"version": "1.0.16",
44
"license": "ISC",
55
"description": "Code syntax highlighting tool for Editor.js",
66
"keywords": [
@@ -23,12 +23,13 @@
2323
"url": "git+https://github.com/BomdiZane/codebox.git"
2424
},
2525
"scripts": {
26-
"build": "esbuild --bundle --outfile=\"./dist/index.min.js\" --global-name=\"CodeBox\" --platform=\"browser\" ./src/index.ts && cp ./src/style.css ./dist/style.css",
26+
"build": "esbuild --bundle --outfile=\"./dist/index.min.js\" --global-name=\"CodeBox\" --format=\"cjs\" --platform=\"browser\" ./src/index.ts && cp ./src/style.css ./dist/style.css",
2727
"dev": "nodemon --watch ./src/*.ts npm run build",
2828
"start": "node test.js",
2929
"prepublish": "npm run build"
3030
},
3131
"devDependencies": {
32+
"@types/node": "^14.11.2",
3233
"css-loader": "^1.0.0",
3334
"esbuild": "^0.7.9",
3435
"eslint": "^6.8.0",
@@ -39,8 +40,5 @@
3940
"dist",
4041
"src"
4142
],
42-
"main": "dist/index.min.js",
43-
"dependencies": {
44-
"rangy": "^1.3.0"
45-
}
43+
"main": "dist/index.min.js"
4644
}

src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
* @author - Adombang Munang Mbomndih (Bomdi) <[email protected]> (https://bomdisoft.com)
88
*/
99

10-
import rangy from 'rangy/lib/rangy-core.js'
11-
import 'rangy/lib/rangy-textrange.js'
12-
import 'rangy/lib/rangy-selectionsaverestore.js'
13-
1410
import { DEFAULT_THEMES, COMMON_LANGUAGES } from './constants';
1511

1612
type CodeboxConfig = {
@@ -156,9 +152,26 @@ export default class CodeBox {
156152
}
157153

158154
_highlightCodeArea(event){
159-
const savedSel = rangy.saveSelection();
160155
hljs.highlightBlock(this.codeArea);
161-
rangy.restoreSelection(savedSel);
156+
this._placeCaretAtEnd(this.codeArea)
157+
}
158+
159+
_placeCaretAtEnd(el) {
160+
el.focus();
161+
if (typeof window.getSelection != "undefined" &&
162+
typeof document.createRange != "undefined") {
163+
let range = document.createRange();
164+
range.selectNodeContents(el);
165+
range.collapse(false);
166+
let sel = window.getSelection();
167+
sel.removeAllRanges();
168+
sel.addRange(range);
169+
} else if (typeof document.body.createTextRange != "undefined") {
170+
let textRange = document.body.createTextRange();
171+
textRange.moveToElementText(el);
172+
textRange.collapse(false);
173+
textRange.select();
174+
}
162175
}
163176

164177
_handleCodeAreaPaste(event){

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"incremental": true,
44
"target": "ESNext",
5-
"module": "UMD",
5+
"module": "ESNext",
66
"allowJs": true,
77
"checkJs": true,
88
"strict": true,

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
4141
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
4242

43+
"@types/node@^14.11.2":
44+
version "14.11.2"
45+
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.2.tgz#2de1ed6670439387da1c9f549a2ade2b0a799256"
46+
integrity sha512-jiE3QIxJ8JLNcb1Ps6rDbysDhN4xa8DJJvuC9prr6w+1tIh+QAbYyNF3tyiZNLDBIuBCf4KEcV2UvQm/V60xfA==
47+
4348
abbrev@1:
4449
version "1.1.1"
4550
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"

0 commit comments

Comments
 (0)