tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
27
pulls
pipelines
persist last used codeblock language
awarm.space
3 months ago
f7140208
0c4f6b77
+25
-4
4 changed files
expand all
collapse all
unified
split
components
Blocks
BlockCommands.tsx
CodeBlock.tsx
TextBlock
inputRules.ts
src
utils
codeLanguageStorage.ts
+10
-1
components/Blocks/BlockCommands.tsx
···
32
32
import { BlockMathSmall } from "components/Icons/BlockMathSmall";
33
33
import { BlockCodeSmall } from "components/Icons/BlockCodeSmall";
34
34
import { QuoteSmall } from "components/Icons/QuoteSmall";
35
35
+
import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage";
35
36
36
37
type Props = {
37
38
parent: string;
···
310
311
type: "block",
311
312
hiddenInPublication: false,
312
313
onSelect: async (rep, props) => {
313
313
-
createBlockWithType(rep, props, "code");
314
314
+
let entity = await createBlockWithType(rep, props, "code");
315
315
+
let lastLang = localStorage.getItem(LAST_USED_CODE_LANGUAGE_KEY);
316
316
+
if (lastLang) {
317
317
+
await rep.mutate.assertFact({
318
318
+
entity,
319
319
+
attribute: "block/code-language",
320
320
+
data: { type: "string", value: lastLang },
321
321
+
});
322
322
+
}
314
323
},
315
324
},
316
325
+2
components/Blocks/CodeBlock.tsx
···
13
13
import { useEntitySetContext } from "components/EntitySetProvider";
14
14
import { flushSync } from "react-dom";
15
15
import { elementId } from "src/utils/elementId";
16
16
+
import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage";
16
17
17
18
export function CodeBlock(props: BlockProps) {
18
19
let { rep, rootEntity } = useReplicache();
···
100
101
}}
101
102
value={lang}
102
103
onChange={async (e) => {
104
104
+
localStorage.setItem(LAST_USED_CODE_LANGUAGE_KEY, e.target.value);
103
105
await rep?.mutate.assertFact({
104
106
attribute: "block/code-language",
105
107
entity: props.entityID,
+12
-3
components/Blocks/TextBlock/inputRules.ts
···
11
11
import { schema } from "./schema";
12
12
import { useUIState } from "src/useUIState";
13
13
import { flushSync } from "react-dom";
14
14
+
import { LAST_USED_CODE_LANGUAGE_KEY } from "src/utils/codeLanguageStorage";
14
15
export const inputrules = (
15
16
propsRef: MutableRefObject<BlockProps & { entity_set: { set: string } }>,
16
17
repRef: MutableRefObject<Replicache<ReplicacheMutators> | null>,
···
108
109
109
110
// Code Block
110
111
new InputRule(/^```\s$/, (state, match) => {
111
111
-
flushSync(() =>
112
112
+
flushSync(() => {
112
113
repRef.current?.mutate.assertFact({
113
114
entity: propsRef.current.entityID,
114
115
attribute: "block/type",
115
116
data: { type: "block-type-union", value: "code" },
116
116
-
}),
117
117
-
);
117
117
+
});
118
118
+
let lastLang = localStorage.getItem(LAST_USED_CODE_LANGUAGE_KEY);
119
119
+
if (lastLang) {
120
120
+
repRef.current?.mutate.assertFact({
121
121
+
entity: propsRef.current.entityID,
122
122
+
attribute: "block/code-language",
123
123
+
data: { type: "string", value: lastLang },
124
124
+
});
125
125
+
}
126
126
+
});
118
127
setTimeout(() => {
119
128
focusBlock({ ...propsRef.current, type: "code" }, { type: "start" });
120
129
}, 20);
+1
src/utils/codeLanguageStorage.ts
···
1
1
+
export const LAST_USED_CODE_LANGUAGE_KEY = "lastUsedCodeLanguage";