@haklex/cm-editor 0.0.82 → 0.0.84
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/dist/index.mjs +67 -50
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,65 +1,82 @@
|
|
|
1
|
-
import { LanguageDescription,
|
|
1
|
+
import { LanguageDescription, syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language";
|
|
2
2
|
import { languages } from "@codemirror/language-data";
|
|
3
3
|
import { oneDark } from "@codemirror/theme-one-dark";
|
|
4
4
|
import { EditorView } from "@codemirror/view";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
zsh: ["shell", "bash"]
|
|
5
|
+
const languageAliases = {
|
|
6
|
+
c: ["c"],
|
|
7
|
+
cpp: ["c++", "cpp"],
|
|
8
|
+
cs: ["c#", "csharp"],
|
|
9
|
+
js: ["javascript"],
|
|
10
|
+
jsx: ["javascript jsx", "jsx"],
|
|
11
|
+
md: ["markdown"],
|
|
12
|
+
py: ["python"],
|
|
13
|
+
sh: ["shell", "bash"],
|
|
14
|
+
ts: ["typescript"],
|
|
15
|
+
tsx: ["typescript jsx", "tsx"],
|
|
16
|
+
yml: ["yaml"],
|
|
17
|
+
zsh: ["shell", "bash"]
|
|
19
18
|
};
|
|
20
19
|
function getLanguageCandidates(language) {
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const normalized = language === "plaintext" ? "text" : language;
|
|
21
|
+
return [normalized, ...languageAliases[normalized] ?? []];
|
|
23
22
|
}
|
|
24
23
|
async function loadLanguageExtension(language) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
if (language === "text") return [];
|
|
25
|
+
const candidates = getLanguageCandidates(language);
|
|
26
|
+
for (const candidate of candidates) {
|
|
27
|
+
const matched = LanguageDescription.matchLanguageName(
|
|
28
|
+
languages,
|
|
29
|
+
candidate,
|
|
30
|
+
true
|
|
31
|
+
);
|
|
32
|
+
if (!matched) continue;
|
|
33
|
+
try {
|
|
34
|
+
return await matched.load();
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return [];
|
|
35
39
|
}
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/selection.ts
|
|
38
40
|
function isOnFirstLine(view) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
const { main } = view.state.selection;
|
|
42
|
+
if (!main.empty) return false;
|
|
43
|
+
return view.state.doc.lineAt(main.head).number === 1;
|
|
42
44
|
}
|
|
43
45
|
function isOnLastLine(view) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
const { main } = view.state.selection;
|
|
47
|
+
if (!main.empty) return false;
|
|
48
|
+
return view.state.doc.lineAt(main.head).number === view.state.doc.lines;
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
const baseTheme = EditorView.theme({
|
|
51
|
+
"&": {
|
|
52
|
+
backgroundColor: "transparent"
|
|
53
|
+
},
|
|
54
|
+
"&.cm-editor": {
|
|
55
|
+
outline: "none"
|
|
56
|
+
},
|
|
57
|
+
"&.cm-editor.cm-focused": {
|
|
58
|
+
outline: "none"
|
|
59
|
+
},
|
|
60
|
+
".cm-scroller": {
|
|
61
|
+
overflow: "auto",
|
|
62
|
+
fontFamily: "inherit"
|
|
63
|
+
},
|
|
64
|
+
".cm-content": {
|
|
65
|
+
minHeight: "1.5em"
|
|
66
|
+
},
|
|
67
|
+
".cm-selectionBackground": {
|
|
68
|
+
backgroundColor: "rgba(125, 125, 125, 0.26) !important"
|
|
69
|
+
}
|
|
60
70
|
});
|
|
61
71
|
function getThemeExtensions(colorScheme) {
|
|
62
|
-
|
|
72
|
+
return colorScheme === "dark" ? [oneDark, baseTheme] : [baseTheme, syntaxHighlighting(defaultHighlightStyle, { fallback: true })];
|
|
63
73
|
}
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
export {
|
|
75
|
+
baseTheme,
|
|
76
|
+
getLanguageCandidates,
|
|
77
|
+
getThemeExtensions,
|
|
78
|
+
isOnFirstLine,
|
|
79
|
+
isOnLastLine,
|
|
80
|
+
languageAliases,
|
|
81
|
+
loadLanguageExtension
|
|
82
|
+
};
|