Skip to content

Commit

Permalink
editor: add code block language to local storage
Browse files Browse the repository at this point in the history
* also add plaintext language option
* also fix spacing issues in language selector list

Signed-off-by: 01zulfi <[email protected]>
  • Loading branch information
01zulfi committed Dec 16, 2024
1 parent 6098935 commit 064a6b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/editor/scripts/langen.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ export async function langen(rootDirectory) {
: undefined
});
}
languages.push({
filename: "Plaintext",
title: "Plaintext"
});

const languageIndex = `/* !!! THIS IS A GENERATED FILE. DO NOT EDIT !!! */
export async function loadLanguage(language: string) {
switch (language) {
${languages
.map(({ filename, alias }) => {
if (filename === "Plaintext") return "";
return [
...(alias || []).map((a) => `case "${a}":`),
`case "${filename}":`,
Expand Down
12 changes: 12 additions & 0 deletions packages/editor/src/extensions/code-block/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import stripIndent from "strip-indent";
import { nanoid } from "nanoid";
import Languages from "./languages.json";
import { CaretPosition, CodeLine } from "./utils.js";
import { config } from "../../utils/config.js";

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

if (language) {
const languageDefinition = Languages.find(
(l) =>
l.filename === language ||
l.alias?.some((a) => a === language)
);
if (languageDefinition) {
config.set("codeBlockLanguage", languageDefinition);
}
}

const isInsideCodeBlock = this.editor.isActive(this.type.name);
if (!isInsideCodeBlock && !isCode) {
return false;
Expand Down
19 changes: 16 additions & 3 deletions packages/editor/src/extensions/code-block/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { CodeBlockAttributes } from "./code-block.js";
import Languages from "./languages.json";
import { useThemeEngineStore } from "@notesnook/theme";
import { strings } from "@notesnook/intl";
import { config } from "../../utils/config.js";

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

const cachedLanguage =
config.get<(typeof Languages)[number]>("codeBlockLanguage");

return (
<>
<Flex
Expand Down Expand Up @@ -155,7 +159,9 @@ export function CodeblockComponent(
title={strings.changeLanguage()}
>
<Text variant={"subBody"} spellCheck={false}>
{languageDefinition?.title || "Plaintext"}
{languageDefinition?.title ||
cachedLanguage?.title ||
"Plaintext"}
</Text>
</Button>

Expand Down Expand Up @@ -213,8 +219,16 @@ export function CodeblockComponent(
title={strings.selectLanguage()}
>
<LanguageSelector
selectedLanguage={languageDefinition?.filename || "Plaintext"}
selectedLanguage={
languageDefinition?.filename ||
cachedLanguage?.filename ||
"Plaintext"
}
onLanguageSelected={(language) => {
config.set(
"codeBlockLanguage",
Languages.find((l) => l.filename === language)
);
updateAttributes(
{ language },
{ addToHistory: true, preventUpdate: false }
Expand Down Expand Up @@ -287,7 +301,6 @@ function LanguageSelector(props: LanguageSelectorProps) {
variant={"menuitem"}
sx={{
textAlign: "left",
py: 1,
display: "flex",
justifyContent: "space-between",
alignItems: "center"
Expand Down
Loading

0 comments on commit 064a6b3

Please sign in to comment.