diff --git a/components/Blocks/BlockCommands.tsx b/components/Blocks/BlockCommands.tsx index 326defaa..2062527b 100644 --- a/components/Blocks/BlockCommands.tsx +++ b/components/Blocks/BlockCommands.tsx @@ -32,6 +32,7 @@ import { ListUnorderedSmall } from "components/Toolbar/ListToolbar"; import { BlockMathSmall } from "components/Icons/BlockMathSmall"; import { BlockCodeSmall } from "components/Icons/BlockCodeSmall"; import { QuoteSmall } from "components/Icons/QuoteSmall"; +import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage"; type Props = { parent: string; @@ -310,7 +311,15 @@ export const blockCommands: Command[] = [ type: "block", hiddenInPublication: false, onSelect: async (rep, props) => { - createBlockWithType(rep, props, "code"); + let entity = await createBlockWithType(rep, props, "code"); + let lastLang = localStorage.getItem(LAST_USED_CODE_LANGUAGE_KEY); + if (lastLang) { + await rep.mutate.assertFact({ + entity, + attribute: "block/code-language", + data: { type: "string", value: lastLang }, + }); + } }, }, diff --git a/components/Blocks/CodeBlock.tsx b/components/Blocks/CodeBlock.tsx index 984f7e0c..d42f1180 100644 --- a/components/Blocks/CodeBlock.tsx +++ b/components/Blocks/CodeBlock.tsx @@ -13,6 +13,7 @@ import { BaseTextareaBlock } from "./BaseTextareaBlock"; import { useEntitySetContext } from "components/EntitySetProvider"; import { flushSync } from "react-dom"; import { elementId } from "src/utils/elementId"; +import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage"; export function CodeBlock(props: BlockProps) { let { rep, rootEntity } = useReplicache(); @@ -100,6 +101,7 @@ export function CodeBlock(props: BlockProps) { }} value={lang} onChange={async (e) => { + localStorage.setItem(LAST_USED_CODE_LANGUAGE_KEY, e.target.value); await rep?.mutate.assertFact({ attribute: "block/code-language", entity: props.entityID, diff --git a/components/Blocks/TextBlock/inputRules.ts b/components/Blocks/TextBlock/inputRules.ts index eb33bafd..0ff60e27 100644 --- a/components/Blocks/TextBlock/inputRules.ts +++ b/components/Blocks/TextBlock/inputRules.ts @@ -11,6 +11,7 @@ import { focusBlock } from "src/utils/focusBlock"; import { schema } from "./schema"; import { useUIState } from "src/useUIState"; import { flushSync } from "react-dom"; +import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage"; export const inputrules = ( propsRef: MutableRefObject, repRef: MutableRefObject | null>, @@ -108,13 +109,21 @@ export const inputrules = ( // Code Block new InputRule(/^```\s$/, (state, match) => { - flushSync(() => + flushSync(() => { repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/type", data: { type: "block-type-union", value: "code" }, - }), - ); + }); + let lastLang = localStorage.getItem(LAST_USED_CODE_LANGUAGE_KEY); + if (lastLang) { + repRef.current?.mutate.assertFact({ + entity: propsRef.current.entityID, + attribute: "block/code-language", + data: { type: "string", value: lastLang }, + }); + } + }); setTimeout(() => { focusBlock({ ...propsRef.current, type: "code" }, { type: "start" }); }, 20); diff --git a/src/utils/codeLanguageStorage.ts b/src/utils/codeLanguageStorage.ts new file mode 100644 index 00000000..2738a143 --- /dev/null +++ b/src/utils/codeLanguageStorage.ts @@ -0,0 +1 @@ +export const LAST_USED_CODE_LANGUAGE_KEY = "lastUsedCodeLanguage";