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 31, 2024
1 parent 6098935 commit dadcee1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 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
11 changes: 10 additions & 1 deletion 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 @@ -524,7 +525,14 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
}
}
}),
HighlighterPlugin({ name: this.name, defaultLanguage: "txt" })
HighlighterPlugin({
name: this.name,
defaultLanguage: () => {
const cachedLanguage =
config.get<(typeof Languages)[number]>("codeBlockLanguage");
return cachedLanguage?.filename ?? "Plaintext";
}
})
];
},

Expand All @@ -536,6 +544,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
l.filename === node.attrs.language ||
l.alias?.some((a) => a === node.attrs.language)
);

const content = document.createElement("pre");
content.classList.add("node-content-wrapper");
content.classList.add(
Expand Down
6 changes: 5 additions & 1 deletion 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 Down Expand Up @@ -215,6 +216,10 @@ export function CodeblockComponent(
<LanguageSelector
selectedLanguage={languageDefinition?.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 +292,6 @@ function LanguageSelector(props: LanguageSelectorProps) {
variant={"menuitem"}
sx={{
textAlign: "left",
py: 1,
display: "flex",
justifyContent: "space-between",
alignItems: "center"
Expand Down
22 changes: 17 additions & 5 deletions packages/editor/src/extensions/code-block/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ function getDecorations({
defaultLanguage
}: {
block: NodeWithPos;
defaultLanguage: string | null | undefined;
defaultLanguage: () => string | null | undefined;
}) {
const decorations: Decoration[] = [];
const languages = refractor.listLanguages();

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

const language = node.attrs.language || defaultLanguage;
const language = node.attrs.language || defaultLanguage();
const nodes = languages.includes(language)
? getHighlightNodes(refractor.highlight(code, language))
: null;
Expand Down Expand Up @@ -107,7 +107,7 @@ export function HighlighterPlugin({
defaultLanguage
}: {
name: string;
defaultLanguage: string | null | undefined;
defaultLanguage: () => string | null | undefined;
}) {
const HIGHLIGHTER_PLUGIN_KEY = new PluginKey<HighlighterState>("highlighter");
const HIGHLIGHTED_BLOCKS: Set<string> = new Set();
Expand Down Expand Up @@ -270,7 +270,13 @@ export function HighlighterPlugin({
},
appendTransaction(transactions, oldState, newState) {
const isDocChanged = transactions.some((tr) => tr.docChanged);
return updateSelection(name, oldState, newState, isDocChanged);
return updateSelection(
name,
oldState,
newState,
isDocChanged,
defaultLanguage
);
}
});
}
Expand All @@ -279,7 +285,8 @@ function updateSelection(
name: string,
oldState: EditorState,
newState: EditorState,
isDocChanged: boolean
isDocChanged: boolean,
defaultLanguage: () => string | null | undefined
) {
const oldNodeName = oldState.selection.$head.parent.type.name;
const newNodeName = newState.selection.$head.parent.type.name;
Expand Down Expand Up @@ -307,6 +314,11 @@ function updateSelection(
isDocChanged ? toCodeLines(node.textContent, pos) : undefined
);
attributes.caretPosition = position;
console.log({
attrs: node.attrs,
defaultLanguage: defaultLanguage()
});
attributes.language = node.attrs.language ?? defaultLanguage();

const { tr } = newState;
tr.setMeta("preventUpdate", true);
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/extensions/code-block/languages.json

Large diffs are not rendered by default.

0 comments on commit dadcee1

Please sign in to comment.