Skip to content

Commit 064a6b3

Browse files
committed
editor: add code block language to local storage
* also add plaintext language option * also fix spacing issues in language selector list Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
1 parent 6098935 commit 064a6b3

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

packages/editor/scripts/langen.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,17 @@ export async function langen(rootDirectory) {
5656
: undefined
5757
});
5858
}
59+
languages.push({
60+
filename: "Plaintext",
61+
title: "Plaintext"
62+
});
5963

6064
const languageIndex = `/* !!! THIS IS A GENERATED FILE. DO NOT EDIT !!! */
6165
export async function loadLanguage(language: string) {
6266
switch (language) {
6367
${languages
6468
.map(({ filename, alias }) => {
69+
if (filename === "Plaintext") return "";
6570
return [
6671
...(alias || []).map((a) => `case "${a}":`),
6772
`case "${filename}":`,

packages/editor/src/extensions/code-block/code-block.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import stripIndent from "strip-indent";
3636
import { nanoid } from "nanoid";
3737
import Languages from "./languages.json";
3838
import { CaretPosition, CodeLine } from "./utils.js";
39+
import { config } from "../../utils/config.js";
3940

4041
interface Indent {
4142
type: "tab" | "space";
@@ -463,6 +464,17 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
463464
event.clipboardData
464465
);
465466

467+
if (language) {
468+
const languageDefinition = Languages.find(
469+
(l) =>
470+
l.filename === language ||
471+
l.alias?.some((a) => a === language)
472+
);
473+
if (languageDefinition) {
474+
config.set("codeBlockLanguage", languageDefinition);
475+
}
476+
}
477+
466478
const isInsideCodeBlock = this.editor.isActive(this.type.name);
467479
if (!isInsideCodeBlock && !isCode) {
468480
return false;

packages/editor/src/extensions/code-block/component.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CodeBlockAttributes } from "./code-block.js";
3030
import Languages from "./languages.json";
3131
import { useThemeEngineStore } from "@notesnook/theme";
3232
import { strings } from "@notesnook/intl";
33+
import { config } from "../../utils/config.js";
3334

3435
export function CodeblockComponent(
3536
props: ReactNodeViewProps<CodeBlockAttributes>
@@ -49,6 +50,9 @@ export function CodeblockComponent(
4950
(l) => l.filename === language || l.alias?.some((a) => a === language)
5051
);
5152

53+
const cachedLanguage =
54+
config.get<(typeof Languages)[number]>("codeBlockLanguage");
55+
5256
return (
5357
<>
5458
<Flex
@@ -155,7 +159,9 @@ export function CodeblockComponent(
155159
title={strings.changeLanguage()}
156160
>
157161
<Text variant={"subBody"} spellCheck={false}>
158-
{languageDefinition?.title || "Plaintext"}
162+
{languageDefinition?.title ||
163+
cachedLanguage?.title ||
164+
"Plaintext"}
159165
</Text>
160166
</Button>
161167

@@ -213,8 +219,16 @@ export function CodeblockComponent(
213219
title={strings.selectLanguage()}
214220
>
215221
<LanguageSelector
216-
selectedLanguage={languageDefinition?.filename || "Plaintext"}
222+
selectedLanguage={
223+
languageDefinition?.filename ||
224+
cachedLanguage?.filename ||
225+
"Plaintext"
226+
}
217227
onLanguageSelected={(language) => {
228+
config.set(
229+
"codeBlockLanguage",
230+
Languages.find((l) => l.filename === language)
231+
);
218232
updateAttributes(
219233
{ language },
220234
{ addToHistory: true, preventUpdate: false }
@@ -287,7 +301,6 @@ function LanguageSelector(props: LanguageSelectorProps) {
287301
variant={"menuitem"}
288302
sx={{
289303
textAlign: "left",
290-
py: 1,
291304
display: "flex",
292305
justifyContent: "space-between",
293306
alignItems: "center"

0 commit comments

Comments
 (0)