You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Plugins allow you to add extra features to a template, like [automatic indentation](./indent.js) or [support for highlight.js's language autodetection](./autodetect.js). To use them, just:
40
49
- Import the plugins' JS/CSS files (there may only be one of these; import all of the files that exist) after you have imported `code-input` and before registering the template.
Copy file name to clipboardExpand all lines: plugins/special-chars.js
+72-30Lines changed: 72 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/**
2
-
* Render special characters as a symbol with their hex code.
2
+
* Render special characters and control characters as a symbol with their hex code.
3
3
* Files: special-chars.js, special-chars.css
4
4
*/
5
5
@@ -9,30 +9,27 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
9
9
specialCharRegExp;
10
10
11
11
cachedColors;// ascii number > [background color, text color]
12
-
cachedWidths;// character > character width
12
+
cachedWidths;// font > {character > character width}
13
13
canvasContext;
14
14
15
15
/**
16
16
* Create a special characters plugin instance
17
-
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
18
17
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code
18
+
* @param {Boolean} inheritTextColor If `colorInSpecialChars` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base colour of the `pre code` element is used to give contrast to the small characters.
19
+
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
19
20
*/
20
-
constructor(specialCharRegExp=/(?!\n)(?!\t)[\u{0000}-\u{001F}]|[\u{007F}-\u{009F}]|[\u{0200}-\u{FFFF}]/ug,colorInSpecialChars=true){// By default, covers many non-renderable ASCII characters
21
+
constructor(colorInSpecialChars=false,inheritTextColor=false,specialCharRegExp=/(?!\n)(?!\t)[\u{0000}-\u{001F}]|[\u{007F}-\u{009F}]|[\u{0200}-\u{FFFF}]/ug){// By default, covers many non-renderable ASCII characters
21
22
super();
23
+
22
24
this.specialCharRegExp=specialCharRegExp;
23
25
this.colorInSpecialChars=colorInSpecialChars;
26
+
this.inheritTextColor=inheritTextColor;
24
27
25
28
this.cachedColors={};
26
29
this.cachedWidths={};
27
30
28
31
letcanvas=document.createElement("canvas");
29
-
window.addEventListener("load",()=>{
30
-
document.body.appendChild(canvas);
31
-
32
-
});
33
32
this.canvasContext=canvas.getContext("2d");
34
-
this.canvasContext.fillStyle="black";
35
-
this.canvasContext.font="20px 'Consolas'";// TODO: Make dynamic
36
33
}
37
34
38
35
/* Runs before elements are added into a `code-input`; Params: codeInput element) */
@@ -50,10 +47,17 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
0 commit comments