Skip to content

Commit dadcee1

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 <[email protected]>
1 parent 6098935 commit dadcee1

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
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: 10 additions & 1 deletion
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";
@@ -524,7 +525,14 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
524525
}
525526
}
526527
}),
527-
HighlighterPlugin({ name: this.name, defaultLanguage: "txt" })
528+
HighlighterPlugin({
529+
name: this.name,
530+
defaultLanguage: () => {
531+
const cachedLanguage =
532+
config.get<(typeof Languages)[number]>("codeBlockLanguage");
533+
return cachedLanguage?.filename ?? "Plaintext";
534+
}
535+
})
528536
];
529537
},
530538

@@ -536,6 +544,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
536544
l.filename === node.attrs.language ||
537545
l.alias?.some((a) => a === node.attrs.language)
538546
);
547+
539548
const content = document.createElement("pre");
540549
content.classList.add("node-content-wrapper");
541550
content.classList.add(

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

Lines changed: 5 additions & 1 deletion
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>
@@ -215,6 +216,10 @@ export function CodeblockComponent(
215216
<LanguageSelector
216217
selectedLanguage={languageDefinition?.filename || "Plaintext"}
217218
onLanguageSelected={(language) => {
219+
config.set(
220+
"codeBlockLanguage",
221+
Languages.find((l) => l.filename === language)
222+
);
218223
updateAttributes(
219224
{ language },
220225
{ addToHistory: true, preventUpdate: false }
@@ -287,7 +292,6 @@ function LanguageSelector(props: LanguageSelectorProps) {
287292
variant={"menuitem"}
288293
sx={{
289294
textAlign: "left",
290-
py: 1,
291295
display: "flex",
292296
justifyContent: "space-between",
293297
alignItems: "center"

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ function getDecorations({
6969
defaultLanguage
7070
}: {
7171
block: NodeWithPos;
72-
defaultLanguage: string | null | undefined;
72+
defaultLanguage: () => string | null | undefined;
7373
}) {
7474
const decorations: Decoration[] = [];
7575
const languages = refractor.listLanguages();
7676

7777
const { node, pos } = block;
7878
const code = node.textContent;
7979

80-
const language = node.attrs.language || defaultLanguage;
80+
const language = node.attrs.language || defaultLanguage();
8181
const nodes = languages.includes(language)
8282
? getHighlightNodes(refractor.highlight(code, language))
8383
: null;
@@ -107,7 +107,7 @@ export function HighlighterPlugin({
107107
defaultLanguage
108108
}: {
109109
name: string;
110-
defaultLanguage: string | null | undefined;
110+
defaultLanguage: () => string | null | undefined;
111111
}) {
112112
const HIGHLIGHTER_PLUGIN_KEY = new PluginKey<HighlighterState>("highlighter");
113113
const HIGHLIGHTED_BLOCKS: Set<string> = new Set();
@@ -270,7 +270,13 @@ export function HighlighterPlugin({
270270
},
271271
appendTransaction(transactions, oldState, newState) {
272272
const isDocChanged = transactions.some((tr) => tr.docChanged);
273-
return updateSelection(name, oldState, newState, isDocChanged);
273+
return updateSelection(
274+
name,
275+
oldState,
276+
newState,
277+
isDocChanged,
278+
defaultLanguage
279+
);
274280
}
275281
});
276282
}
@@ -279,7 +285,8 @@ function updateSelection(
279285
name: string,
280286
oldState: EditorState,
281287
newState: EditorState,
282-
isDocChanged: boolean
288+
isDocChanged: boolean,
289+
defaultLanguage: () => string | null | undefined
283290
) {
284291
const oldNodeName = oldState.selection.$head.parent.type.name;
285292
const newNodeName = newState.selection.$head.parent.type.name;
@@ -307,6 +314,11 @@ function updateSelection(
307314
isDocChanged ? toCodeLines(node.textContent, pos) : undefined
308315
);
309316
attributes.caretPosition = position;
317+
console.log({
318+
attrs: node.attrs,
319+
defaultLanguage: defaultLanguage()
320+
});
321+
attributes.language = node.attrs.language ?? defaultLanguage();
310322

311323
const { tr } = newState;
312324
tr.setMeta("preventUpdate", true);

packages/editor/src/extensions/code-block/languages.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)