Skip to content

Commit 4d0ee37

Browse files
LuciNyanthegreatercurve
authored andcommitted
fix(lexical-playground): code lang display (#2658)
1 parent 9d53e66 commit 4d0ee37

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

packages/lexical-code/src/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ type SerializedCodeHighlightNode = Spread<
8282
SerializedTextNode
8383
>;
8484

85+
export const CODE_LANGUAGE_FRIENDLY_NAME_MAP: Record<string, string> = {
86+
c: 'C',
87+
clike: 'C-like',
88+
css: 'CSS',
89+
html: 'HTML',
90+
js: 'JavaScript',
91+
markdown: 'Markdown',
92+
objc: 'Objective-C',
93+
plain: 'Plain Text',
94+
py: 'Python',
95+
rust: 'Rust',
96+
sql: 'SQL',
97+
swift: 'Swift',
98+
xml: 'XML',
99+
};
100+
101+
export const CODE_LANGUAGE_MAP: Record<string, string> = {
102+
javascript: 'js',
103+
md: 'markdown',
104+
plaintext: 'plain',
105+
python: 'py',
106+
text: 'plain',
107+
};
108+
109+
export function getLanguageFriendlyName(lang: string) {
110+
const _lang = CODE_LANGUAGE_MAP[lang] || lang;
111+
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
112+
}
113+
85114
const mapToPrismLanguage = (
86115
language: string | null | undefined,
87116
): string | null | undefined => {

packages/lexical-playground/src/plugins/CodeActionMenuPlugin.css

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
height: 35.8px;
33
font-size: 10px;
44
color: rgba(0, 0, 0, 0.5);
5-
text-transform: uppercase;
65
position: absolute;
76
display: flex;
87
align-items: center;

packages/lexical-playground/src/plugins/CodeActionMenuPlugin.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import './CodeActionMenuPlugin.css';
1010

11-
import {$isCodeNode, CodeNode} from '@lexical/code';
11+
import {$isCodeNode, CodeNode, getLanguageFriendlyName} from '@lexical/code';
1212
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
1313
import {
1414
$getNearestNodeFromDOMNode,
@@ -149,11 +149,13 @@ function CodeActionMenuContainer(): JSX.Element {
149149
});
150150
}
151151

152+
const codeFriendlyName = getLanguageFriendlyName(lang);
153+
152154
return (
153155
<>
154156
{isShown ? (
155157
<div className="code-action-menu-container" style={{...position}}>
156-
<div className="code-highlight-language">{lang}</div>
158+
<div className="code-highlight-language">{codeFriendlyName}</div>
157159
<button className="menu-item" onClick={handleClick} aria-label="copy">
158160
{isCopyCompleted ? (
159161
<i className="format success" />

packages/lexical-playground/src/plugins/ToolbarPlugin.tsx

+19-25
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import type {
1616

1717
import './ToolbarPlugin.css';
1818

19-
import {$createCodeNode, $isCodeNode} from '@lexical/code';
19+
import {
20+
$createCodeNode,
21+
$isCodeNode,
22+
CODE_LANGUAGE_FRIENDLY_NAME_MAP,
23+
CODE_LANGUAGE_MAP,
24+
} from '@lexical/code';
2025
import {$isLinkNode, TOGGLE_LINK_COMMAND} from '@lexical/link';
2126
import {
2227
$isListNode,
@@ -109,30 +114,19 @@ const blockTypeToBlockName = {
109114
quote: 'Quote',
110115
};
111116

112-
const CODE_LANGUAGE_OPTIONS: [string, string][] = [
113-
['', '- Select language -'],
114-
['c', 'C'],
115-
['clike', 'C-like'],
116-
['css', 'CSS'],
117-
['html', 'HTML'],
118-
['js', 'JavaScript'],
119-
['markdown', 'Markdown'],
120-
['objc', 'Objective-C'],
121-
['plain', 'Plain Text'],
122-
['py', 'Python'],
123-
['rust', 'Rust'],
124-
['sql', 'SQL'],
125-
['swift', 'Swift'],
126-
['xml', 'XML'],
127-
];
128-
129-
const CODE_LANGUAGE_MAP = {
130-
javascript: 'js',
131-
md: 'markdown',
132-
plaintext: 'plain',
133-
python: 'py',
134-
text: 'plain',
135-
};
117+
function getCodeLanguageOptions(): [string, string][] {
118+
const options: [string, string][] = [['', '- Select language -']];
119+
120+
for (const [lang, friendlyName] of Object.entries(
121+
CODE_LANGUAGE_FRIENDLY_NAME_MAP,
122+
)) {
123+
options.push([lang, friendlyName]);
124+
}
125+
126+
return options;
127+
}
128+
129+
const CODE_LANGUAGE_OPTIONS = getCodeLanguageOptions();
136130

137131
function getSelectedNode(selection: RangeSelection): TextNode | ElementNode {
138132
const anchor = selection.anchor;

0 commit comments

Comments
 (0)