@pfmcodes/caret 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/commonjs/editor.js +9 -4
- package/commonjs/theme.js +1 -1
- package/esm/editor.js +3 -4
- package/esm/theme.js +1 -1
- package/package.json +10 -7
- package/types/editor.ts +284 -0
- package/types/esm-highlight.d.ts +4 -0
- package/types/index.d.ts +34 -0
- package/types/index.ts +10 -0
- package/types/languages.ts +98 -0
- package/types/theme.ts +18 -0
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ pnpm add @pfmcodes/caret
|
|
|
73
73
|
<script type="module">
|
|
74
74
|
import editor from './node_modules/@pfmcodes/caret/esm/index.js';
|
|
75
75
|
|
|
76
|
-
const instance = await editor.createEditor(
|
|
76
|
+
const instance = await editor.editor.createEditor(
|
|
77
77
|
document.getElementById('editor'),
|
|
78
78
|
{
|
|
79
79
|
value: 'console.log("Hello, World!");',
|
|
@@ -92,7 +92,7 @@ pnpm add @pfmcodes/caret
|
|
|
92
92
|
import editor from '@pfmcodes/caret';
|
|
93
93
|
|
|
94
94
|
// Create editor instance
|
|
95
|
-
const editorInstance = await editor.createEditor(
|
|
95
|
+
const editorInstance = await editor.editor.createEditor(
|
|
96
96
|
document.getElementById('editor'),
|
|
97
97
|
{
|
|
98
98
|
value: '', // Initial code
|
|
@@ -121,9 +121,9 @@ caret/
|
|
|
121
121
|
### JavaScript Editor
|
|
122
122
|
|
|
123
123
|
```javascript
|
|
124
|
-
import editor from '@pfmcodes/caret';
|
|
124
|
+
import editor from '@pfmcodes/caret'; // auto link to commonjs version
|
|
125
125
|
|
|
126
|
-
const jsEditor = await editor.createEditor(
|
|
126
|
+
const jsEditor = await editor.editor.createEditor(
|
|
127
127
|
document.getElementById('js-editor'),
|
|
128
128
|
{
|
|
129
129
|
value: `function greet(name) {
|
package/commonjs/editor.js
CHANGED
|
@@ -18,6 +18,10 @@ async function createEditor(editor, data) {
|
|
|
18
18
|
highlighted.id = "Caret-highlighted";
|
|
19
19
|
caret.id = "Caret-caret";
|
|
20
20
|
lineCounter.id = "Caret-lineCounter";
|
|
21
|
+
editor1.className = 'dark';
|
|
22
|
+
highlighted.className = 'dark';
|
|
23
|
+
caret.className = 'dark';
|
|
24
|
+
lineCounter.className = 'dark';
|
|
21
25
|
editor1.style.backgroundColor = isDark ? "#222" : "#fff";
|
|
22
26
|
let code = data.value || "";
|
|
23
27
|
let language = data.language;
|
|
@@ -33,10 +37,10 @@ async function createEditor(editor, data) {
|
|
|
33
37
|
const link = document.createElement("link");
|
|
34
38
|
link.rel = "stylesheet";
|
|
35
39
|
link.id = "Caret-theme";
|
|
36
|
-
link.href =
|
|
40
|
+
link.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/${theme}.css`;
|
|
37
41
|
document.head.appendChild(link);
|
|
38
42
|
} else {
|
|
39
|
-
themeLink.href =
|
|
43
|
+
themeLink.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/${theme}.css`;
|
|
40
44
|
}
|
|
41
45
|
} else {
|
|
42
46
|
let themeLink = document.getElementById("Caret-theme");
|
|
@@ -44,7 +48,7 @@ async function createEditor(editor, data) {
|
|
|
44
48
|
const link = document.createElement("link");
|
|
45
49
|
link.rel = "stylesheet";
|
|
46
50
|
link.id = "Caret-theme";
|
|
47
|
-
link.href =
|
|
51
|
+
link.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/hybrid.css`;
|
|
48
52
|
document.head.appendChild(link);
|
|
49
53
|
} else {
|
|
50
54
|
themeLink.href = `./highlight.js/styles/hybrid.css`;
|
|
@@ -139,6 +143,7 @@ async function createEditor(editor, data) {
|
|
|
139
143
|
lineCounter.innerHTML = html;
|
|
140
144
|
}
|
|
141
145
|
|
|
146
|
+
highlighted.style.paddingTop = "12px"
|
|
142
147
|
|
|
143
148
|
function getFontMetrics() {
|
|
144
149
|
const metrics = measureCtx.measureText("Mg");
|
|
@@ -187,7 +192,7 @@ async function createEditor(editor, data) {
|
|
|
187
192
|
(lineHeight - ascent) +
|
|
188
193
|
"px";
|
|
189
194
|
|
|
190
|
-
caret.style.height = `${lineHeight}px`;
|
|
195
|
+
caret.style.height = `${lineHeight - 5}px`;
|
|
191
196
|
}
|
|
192
197
|
const input = () => {
|
|
193
198
|
caret.style.opacity = "1";
|
package/commonjs/theme.js
CHANGED
package/esm/editor.js
CHANGED
|
@@ -22,7 +22,6 @@ async function createEditor(editor, data) {
|
|
|
22
22
|
highlighted.className = 'dark';
|
|
23
23
|
caret.className = 'dark';
|
|
24
24
|
lineCounter.className = 'dark';
|
|
25
|
-
editor.classList.add("");
|
|
26
25
|
editor1.style.backgroundColor = isDark ? "#222" : "#fff";
|
|
27
26
|
let code = data.value || "";
|
|
28
27
|
let language = data.language;
|
|
@@ -38,10 +37,10 @@ async function createEditor(editor, data) {
|
|
|
38
37
|
const link = document.createElement("link");
|
|
39
38
|
link.rel = "stylesheet";
|
|
40
39
|
link.id = "Caret-theme";
|
|
41
|
-
link.href =
|
|
40
|
+
link.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/${theme}.css`;
|
|
42
41
|
document.head.appendChild(link);
|
|
43
42
|
} else {
|
|
44
|
-
themeLink.href =
|
|
43
|
+
themeLink.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/${theme}.css`;
|
|
45
44
|
}
|
|
46
45
|
} else {
|
|
47
46
|
let themeLink = document.getElementById("Caret-theme");
|
|
@@ -49,7 +48,7 @@ async function createEditor(editor, data) {
|
|
|
49
48
|
const link = document.createElement("link");
|
|
50
49
|
link.rel = "stylesheet";
|
|
51
50
|
link.id = "Caret-theme";
|
|
52
|
-
link.href =
|
|
51
|
+
link.href = `https://esm.sh/@pfmcodes/highlight.js@1.0.0/styles/hybrid.css`;
|
|
53
52
|
document.head.appendChild(link);
|
|
54
53
|
} else {
|
|
55
54
|
themeLink.href = `./highlight.js/styles/hybrid.css`;
|
package/esm/theme.js
CHANGED
package/package.json
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pfmcodes/caret",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "The official code editor engine for lexius",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./index.
|
|
7
|
-
"types": "./types/
|
|
6
|
+
"main": "./esm/index.js",
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
10
11
|
"import": "./esm/index.js",
|
|
11
|
-
"require": "./commonjs/index.js"
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
"require": "./commonjs/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./commonjs": "./commonjs/index.js",
|
|
15
|
+
"./esm": "./esm/index.js"
|
|
14
16
|
},
|
|
15
17
|
"repository": {
|
|
16
18
|
"type": "git",
|
|
@@ -24,6 +26,7 @@
|
|
|
24
26
|
"syntax-highlighting",
|
|
25
27
|
"language-server",
|
|
26
28
|
"typescript",
|
|
29
|
+
"python",
|
|
27
30
|
"javascript",
|
|
28
31
|
"ide",
|
|
29
32
|
"browser-editor",
|
|
@@ -35,4 +38,4 @@
|
|
|
35
38
|
"url": "https://github.com/PFMCODES/lexius-editor/issues"
|
|
36
39
|
},
|
|
37
40
|
"homepage": "https://github.com/PFMCODES/lexius-editor#readme"
|
|
38
|
-
}
|
|
41
|
+
}
|
package/types/editor.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import hljs from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/core.js"; // Use default export
|
|
2
|
+
import languages from "./languages.ts";
|
|
3
|
+
|
|
4
|
+
languages.init();
|
|
5
|
+
|
|
6
|
+
async function createEditor(editor: HTMLElement, data: any) {
|
|
7
|
+
const editor1: HTMLTextAreaElement = document.createElement("textarea");
|
|
8
|
+
const highlighted: HTMLPreElement = document.createElement("pre");
|
|
9
|
+
const caret: HTMLDivElement = document.createElement("div");
|
|
10
|
+
const measureCanvas: HTMLCanvasElement = document.createElement("canvas");
|
|
11
|
+
const measureCtx: CanvasRenderingContext2D = measureCanvas.getContext("2d")!;
|
|
12
|
+
const isDark: boolean = data.theme && (data.theme.includes("dark") || data.theme.includes("night"));
|
|
13
|
+
const caretColor: string = isDark ? "#fff" : "#7116d8";
|
|
14
|
+
const lineColor: string = isDark ? "#fff" : "#000";
|
|
15
|
+
const lineCounter: HTMLDivElement = document.createElement("div");
|
|
16
|
+
|
|
17
|
+
editor1.id = "Caret-textarea";
|
|
18
|
+
highlighted.id = "Caret-highlighted";
|
|
19
|
+
caret.id = "Caret-caret";
|
|
20
|
+
lineCounter.id = "Caret-lineCounter";
|
|
21
|
+
editor1.className = 'dark';
|
|
22
|
+
highlighted.className = 'dark';
|
|
23
|
+
caret.className = 'dark';
|
|
24
|
+
lineCounter.className = 'dark';
|
|
25
|
+
editor1.style.backgroundColor = isDark ? "#222" : "#fff";
|
|
26
|
+
let code: string = data.value || "";
|
|
27
|
+
let language: string = data.language;
|
|
28
|
+
let theme: string = data.theme;
|
|
29
|
+
if (!languages.registeredLanguages.includes(language)) {
|
|
30
|
+
const mod = await import(`https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/${language}.js`);
|
|
31
|
+
languages.registerLanguage(language, mod.default);
|
|
32
|
+
languages.registeredLanguages.push(language);
|
|
33
|
+
}
|
|
34
|
+
if (theme) {
|
|
35
|
+
let themeLink = document.getElementById("Caret-theme")
|
|
36
|
+
if (!themeLink) {
|
|
37
|
+
const link = document.createElement("link");
|
|
38
|
+
link.rel = "stylesheet";
|
|
39
|
+
link.id = "Caret-theme";
|
|
40
|
+
link.href = `./highlight.js/styles/${theme}.css`;
|
|
41
|
+
document.head.appendChild(link);
|
|
42
|
+
} else {
|
|
43
|
+
themeLink.href = `./highlight.js/styles/${theme}.css`;
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
let themeLink = document.getElementById("Caret-theme");
|
|
47
|
+
if (!themeLink) {
|
|
48
|
+
const link = document.createElement("link");
|
|
49
|
+
link.rel = "stylesheet";
|
|
50
|
+
link.id = "Caret-theme";
|
|
51
|
+
link.href = `./highlight.js/styles/hybrid.css`;
|
|
52
|
+
document.head.appendChild(link);
|
|
53
|
+
} else {
|
|
54
|
+
themeLink.href = `./highlight.js/styles/hybrid.css`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
editor1.spellcheck = false;
|
|
58
|
+
editor1.autocapitalize = "off";
|
|
59
|
+
editor1.autocomplete = "off";
|
|
60
|
+
(editor1 as any).autocorrect = false;
|
|
61
|
+
editor.style = "position: relative; width: 600px; height: 300px; overflow: hidden; /* 👈 CRITICAL */ font-size: 14px;"
|
|
62
|
+
if (code) {
|
|
63
|
+
editor1.value = code;
|
|
64
|
+
editor1.style.paddingTop = "-9px";
|
|
65
|
+
highlighted.innerHTML = hljs.highlight(code, { language: language }).value;
|
|
66
|
+
}
|
|
67
|
+
const keyDown = (e: any) => {
|
|
68
|
+
if (e.key !== "Tab") return;
|
|
69
|
+
|
|
70
|
+
e.preventDefault();
|
|
71
|
+
|
|
72
|
+
const value = editor1.value;
|
|
73
|
+
const start = editor1.selectionStart;
|
|
74
|
+
const end = editor1.selectionEnd;
|
|
75
|
+
|
|
76
|
+
const indent = " ";
|
|
77
|
+
|
|
78
|
+
// Find line start & end
|
|
79
|
+
const lineStart = value.lastIndexOf("\n", start - 1) + 1;
|
|
80
|
+
const lineEnd = value.indexOf("\n", end);
|
|
81
|
+
const finalLineEnd = lineEnd === -1 ? value.length : lineEnd;
|
|
82
|
+
|
|
83
|
+
const block = value.slice(lineStart, finalLineEnd);
|
|
84
|
+
const lines = block.split("\n");
|
|
85
|
+
|
|
86
|
+
let newLines: any;
|
|
87
|
+
let delta: number = 0;
|
|
88
|
+
|
|
89
|
+
if (e.shiftKey) {
|
|
90
|
+
// UNINDENT
|
|
91
|
+
newLines = lines.map(line => {
|
|
92
|
+
if (line.startsWith(indent)) {
|
|
93
|
+
delta -= indent.length;
|
|
94
|
+
return line.slice(indent.length);
|
|
95
|
+
}
|
|
96
|
+
if (line.startsWith("\t")) {
|
|
97
|
+
delta -= 1;
|
|
98
|
+
return line.slice(1);
|
|
99
|
+
}
|
|
100
|
+
return line;
|
|
101
|
+
});
|
|
102
|
+
} else {
|
|
103
|
+
// INDENT
|
|
104
|
+
newLines = lines.map(line => indent + line);
|
|
105
|
+
delta = indent.length;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const newBlock = newLines.join("\n");
|
|
109
|
+
|
|
110
|
+
editor1.value =
|
|
111
|
+
value.slice(0, lineStart) +
|
|
112
|
+
newBlock +
|
|
113
|
+
value.slice(finalLineEnd);
|
|
114
|
+
|
|
115
|
+
// Fix selection
|
|
116
|
+
editor1.selectionStart = start + delta;
|
|
117
|
+
editor1.selectionEnd =
|
|
118
|
+
end + delta * newLines.length;
|
|
119
|
+
|
|
120
|
+
highlighted.innerHTML = hljs.highlight(editor1.value, { language }).value;
|
|
121
|
+
updateLineNumbers();
|
|
122
|
+
updateCaret();
|
|
123
|
+
}
|
|
124
|
+
editor1.addEventListener("keydown", keyDown);
|
|
125
|
+
editor.appendChild(lineCounter);
|
|
126
|
+
editor.appendChild(highlighted);
|
|
127
|
+
editor.appendChild(editor1);
|
|
128
|
+
editor.appendChild(caret);
|
|
129
|
+
|
|
130
|
+
function updateFontMetrics() {
|
|
131
|
+
const style: any = getComputedStyle(editor1);
|
|
132
|
+
measureCtx.font = `${style.fontSize} ${style.fontFamily}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function updateLineNumbers() {
|
|
136
|
+
const lineCount: number = editor1.value.split("\n").length;
|
|
137
|
+
|
|
138
|
+
let html: string = "";
|
|
139
|
+
for (let i: number = 1; i <= lineCount; i++) {
|
|
140
|
+
html += `<div class="Caret-lineCounter-number" style="color: ${lineColor}">${i}</div>`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
lineCounter.innerHTML = html;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
highlighted.style.paddingTop = "12px"
|
|
147
|
+
|
|
148
|
+
function getFontMetrics() {
|
|
149
|
+
const metrics = measureCtx.measureText("Mg");
|
|
150
|
+
return {
|
|
151
|
+
ascent: metrics.actualBoundingBoxAscent,
|
|
152
|
+
descent: metrics.actualBoundingBoxDescent,
|
|
153
|
+
height: metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const focus = () => {
|
|
157
|
+
caret.style.opacity = "1";
|
|
158
|
+
caret.style.background = caretColor;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
editor1.addEventListener("focus", focus);
|
|
162
|
+
|
|
163
|
+
const blur = () => {
|
|
164
|
+
caret.style.opacity = "0";
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
editor1.addEventListener("blur", blur);
|
|
168
|
+
|
|
169
|
+
function updateCaret() {
|
|
170
|
+
const start = editor1.selectionStart;
|
|
171
|
+
const text = editor1.value.slice(0, start);
|
|
172
|
+
|
|
173
|
+
const lines = text.split("\n");
|
|
174
|
+
const lineIndex = lines.length - 1;
|
|
175
|
+
const lineText = lines[lineIndex].replace(/\t/g, " ");
|
|
176
|
+
|
|
177
|
+
const style = getComputedStyle(editor1);
|
|
178
|
+
const paddingLeft = parseFloat(style.paddingLeft);
|
|
179
|
+
const paddingTop = parseFloat(style.paddingTop);
|
|
180
|
+
const lineHeight = parseFloat(style.lineHeight);
|
|
181
|
+
|
|
182
|
+
updateFontMetrics();
|
|
183
|
+
const metrics = measureCtx.measureText("Mg");
|
|
184
|
+
const ascent = metrics.actualBoundingBoxAscent;
|
|
185
|
+
|
|
186
|
+
caret.style.left =
|
|
187
|
+
paddingLeft + measureCtx.measureText(lineText).width + "px";
|
|
188
|
+
caret.style.top =
|
|
189
|
+
-9 +
|
|
190
|
+
paddingTop +
|
|
191
|
+
lineIndex * lineHeight +
|
|
192
|
+
(lineHeight - ascent) +
|
|
193
|
+
"px";
|
|
194
|
+
|
|
195
|
+
caret.style.height = `${lineHeight - 5}px`;
|
|
196
|
+
}
|
|
197
|
+
const input = () => {
|
|
198
|
+
caret.style.opacity = "1";
|
|
199
|
+
highlighted.innerHTML = hljs.highlight(editor1.value, { language: language }).value;
|
|
200
|
+
updateLineNumbers();
|
|
201
|
+
updateCaret();
|
|
202
|
+
};
|
|
203
|
+
editor1.addEventListener("input", input);
|
|
204
|
+
const scroll = () => {
|
|
205
|
+
const x = -editor1.scrollLeft;
|
|
206
|
+
const y = -editor1.scrollTop;
|
|
207
|
+
|
|
208
|
+
highlighted.style.transform = `translate(${x}px, ${y}px)`;
|
|
209
|
+
caret.style.transform = `translate(${x}px, ${y}px)`;
|
|
210
|
+
lineCounter.style.transform = `translateY(${y}px)`;
|
|
211
|
+
};
|
|
212
|
+
editor1.addEventListener("scroll", scroll);
|
|
213
|
+
|
|
214
|
+
updateFontMetrics();
|
|
215
|
+
getFontMetrics();
|
|
216
|
+
|
|
217
|
+
editor1.addEventListener("click", updateCaret);
|
|
218
|
+
editor1.addEventListener("keyup", updateCaret);
|
|
219
|
+
|
|
220
|
+
// Initial caret position
|
|
221
|
+
updateLineNumbers();
|
|
222
|
+
updateCaret();
|
|
223
|
+
|
|
224
|
+
// Focus the editor
|
|
225
|
+
editor1.focus();
|
|
226
|
+
function destroy() {
|
|
227
|
+
editor1.removeEventListener('click', updateCaret);
|
|
228
|
+
editor1.removeEventListener('keyup', updateCaret);
|
|
229
|
+
editor1.removeEventListener('scroll', scroll);
|
|
230
|
+
editor1.removeEventListener('keydown', keyDown);
|
|
231
|
+
editor.innerHTML = "";
|
|
232
|
+
}
|
|
233
|
+
function refresh() {
|
|
234
|
+
highlighted.innerHTML = hljs.highlight(editor1.value, { language }).value;
|
|
235
|
+
updateLineNumbers();
|
|
236
|
+
updateCaret();
|
|
237
|
+
}
|
|
238
|
+
function getValue() {
|
|
239
|
+
return editor1.value;
|
|
240
|
+
}
|
|
241
|
+
function setValue(i: string) {
|
|
242
|
+
editor1.value = i;
|
|
243
|
+
refresh();
|
|
244
|
+
}
|
|
245
|
+
async function setLanguage(l: string) {
|
|
246
|
+
if (!languages.registeredLanguages.includes(l)) {
|
|
247
|
+
if (l === "html" || l === "svg") {
|
|
248
|
+
language = "xml";
|
|
249
|
+
l = "xml";
|
|
250
|
+
}
|
|
251
|
+
const mod = await import(`https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/${l}.js`);
|
|
252
|
+
|
|
253
|
+
}
|
|
254
|
+
language = l;
|
|
255
|
+
refresh();
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
getValue,
|
|
259
|
+
setValue,
|
|
260
|
+
focus,
|
|
261
|
+
setLanguage,
|
|
262
|
+
destroy
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const editor = {
|
|
267
|
+
createEditor
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export default editor;
|
|
271
|
+
|
|
272
|
+
/*
|
|
273
|
+
|
|
274
|
+
createEditor: creates the main editor, using html Elements like, textarea and etc.
|
|
275
|
+
refresh: refreshs the editor
|
|
276
|
+
getValue: return the current value from the editor
|
|
277
|
+
setValue: sets a certain value to the editor's value
|
|
278
|
+
focus: focusses the editor
|
|
279
|
+
destroy: destroys and removeEventListeners
|
|
280
|
+
updateCaret: updates the caret positon, height and other metrics using math
|
|
281
|
+
updateLineNumbers: just add new line numbers
|
|
282
|
+
getFontMetrics: returns back the font's metrics like height
|
|
283
|
+
updateFontMetrics: update the fontMetrics
|
|
284
|
+
*/
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare module "@pfmcodes/caret" {
|
|
2
|
+
interface CaretEditorAPI {
|
|
3
|
+
createEditor(
|
|
4
|
+
el: HTMLElement,
|
|
5
|
+
data: any
|
|
6
|
+
): Promise<{
|
|
7
|
+
getValue(): string;
|
|
8
|
+
setValue(v: string): void;
|
|
9
|
+
focus(): void;
|
|
10
|
+
setLanguage(l: string): Promise<void>;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface CaretThemeApi {
|
|
16
|
+
setTheme(name: string): void;
|
|
17
|
+
removeTheme(name: string): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface CaretLanguageApi {
|
|
21
|
+
registeredLanguages: string[];
|
|
22
|
+
init(): void;
|
|
23
|
+
registerLanguage(name: string, definition: any): void;
|
|
24
|
+
hljs: any;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const Caret: {
|
|
28
|
+
editor: CaretEditorAPI;
|
|
29
|
+
theme: CaretThemeApi;
|
|
30
|
+
language: CaretLanguageApi;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default Caret;
|
|
34
|
+
}
|
package/types/index.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import javascript from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/javascript.js";
|
|
2
|
+
import xml from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/xml.js";
|
|
3
|
+
import css from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/css.js";
|
|
4
|
+
import python from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/python.js";
|
|
5
|
+
import java from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/java.js";
|
|
6
|
+
import csharp from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/csharp.js";
|
|
7
|
+
import cpp from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/cpp.js";
|
|
8
|
+
import ruby from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/ruby.js";
|
|
9
|
+
import php from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/php.js";
|
|
10
|
+
import go from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/go.js";
|
|
11
|
+
import c from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/c.js";
|
|
12
|
+
import rust from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/rust.js";
|
|
13
|
+
import kotlin from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/kotlin.js";
|
|
14
|
+
import swift from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/swift.js";
|
|
15
|
+
import typescript from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/typescript.js";
|
|
16
|
+
import json from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/json.js";
|
|
17
|
+
import bash from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/bash.js";
|
|
18
|
+
import plaintext from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/languages/plaintext.js";
|
|
19
|
+
import hljs from "https://esm.sh/@pfmcodes/highlight.js@1.0.0/es/core.js";
|
|
20
|
+
|
|
21
|
+
let registeredLanguages: Array<T> = [];
|
|
22
|
+
type T = any
|
|
23
|
+
|
|
24
|
+
function init() {
|
|
25
|
+
// Register all languages
|
|
26
|
+
hljs.registerLanguage("javascript", javascript);
|
|
27
|
+
hljs.registerLanguage("xml", xml);
|
|
28
|
+
hljs.registerLanguage("css", css);
|
|
29
|
+
hljs.registerLanguage("html", xml);
|
|
30
|
+
hljs.registerLanguage("python", python);
|
|
31
|
+
hljs.registerLanguage("java", java);
|
|
32
|
+
hljs.registerLanguage("csharp", csharp);
|
|
33
|
+
hljs.registerLanguage("cpp", cpp);
|
|
34
|
+
hljs.registerLanguage("ruby", ruby);
|
|
35
|
+
hljs.registerLanguage("php", php);
|
|
36
|
+
hljs.registerLanguage("go", go);
|
|
37
|
+
hljs.registerLanguage("c", c);
|
|
38
|
+
hljs.registerLanguage("rust", rust);
|
|
39
|
+
hljs.registerLanguage("kotlin", kotlin);
|
|
40
|
+
hljs.registerLanguage("swift", swift);
|
|
41
|
+
hljs.registerLanguage("typescript", typescript);
|
|
42
|
+
hljs.registerLanguage("json", json);
|
|
43
|
+
hljs.registerLanguage("bash", bash);
|
|
44
|
+
hljs.registerLanguage("shell", bash);
|
|
45
|
+
hljs.registerLanguage("sh", bash);
|
|
46
|
+
hljs.registerLanguage("plaintext", plaintext);
|
|
47
|
+
registeredLanguages = [
|
|
48
|
+
"javascript",
|
|
49
|
+
"js",
|
|
50
|
+
"xml",
|
|
51
|
+
"html",
|
|
52
|
+
"svg",
|
|
53
|
+
"python",
|
|
54
|
+
"java",
|
|
55
|
+
"csharp",
|
|
56
|
+
"cpp",
|
|
57
|
+
"ruby",
|
|
58
|
+
"php",
|
|
59
|
+
"go",
|
|
60
|
+
"c",
|
|
61
|
+
"rust",
|
|
62
|
+
"kotlin",
|
|
63
|
+
"swift",
|
|
64
|
+
"typescript",
|
|
65
|
+
"json",
|
|
66
|
+
"bash",
|
|
67
|
+
"shell",
|
|
68
|
+
"sh",
|
|
69
|
+
"plaintext"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function registerLanguage(name: string, definition: any) {
|
|
74
|
+
hljs.registerLanguage(name, definition);
|
|
75
|
+
if (!registeredLanguages.includes(name)) {
|
|
76
|
+
registeredLanguages.push(name);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const languages = {
|
|
81
|
+
init,
|
|
82
|
+
registeredLanguages,
|
|
83
|
+
registerLanguage,
|
|
84
|
+
hljs
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default languages;
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
|
|
91
|
+
registeredLannguage: added for the editor.js can check if the langauge provided already is regsitered or not
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
init: just registers some languages and updates the registeredLangauges variable
|
|
95
|
+
|
|
96
|
+
registerLanguage: just registers a language
|
|
97
|
+
|
|
98
|
+
*/
|
package/types/theme.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function setTheme(name: string) {
|
|
2
|
+
const link = document.getElementById("Caret-theme") as HTMLLinkElement;
|
|
3
|
+
link.href = `./highlight.js/styles/${name}.css`;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function removeTheme() {
|
|
7
|
+
const link = document.getElementById("Caret-theme") as HTMLLinkElement;
|
|
8
|
+
if (link && link.parentNode) {
|
|
9
|
+
link.parentNode.removeChild(link);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const theme = {
|
|
14
|
+
removeTheme,
|
|
15
|
+
setTheme
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default theme;
|