From 430a6013c494d6b0c5d4b094d57669409dc82199 Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Fri, 12 Aug 2022 16:36:26 -0700 Subject: [PATCH] update code block --- blocks/file-blocks/code/index.tsx | 65 +++++++++++++++---- blocks/file-blocks/code/style.css | 3 +- blocks/file-blocks/code/theme.tsx | 9 +++ blocks/file-blocks/markdown-edit/index.tsx | 73 ++++++++++++---------- package.json | 1 + yarn.lock | 5 ++ 6 files changed, 109 insertions(+), 47 deletions(-) diff --git a/blocks/file-blocks/code/index.tsx b/blocks/file-blocks/code/index.tsx index f34b34f..410054b 100644 --- a/blocks/file-blocks/code/index.tsx +++ b/blocks/file-blocks/code/index.tsx @@ -33,10 +33,13 @@ import { } from "@codemirror/language"; import { languages } from "@codemirror/language-data"; import interact from "@replit/codemirror-interact"; +import { vim } from "@replit/codemirror-vim"; +import { indentationMarkers } from "@replit/codemirror-indentation-markers"; import { theme } from "./theme"; const languageCompartment = new Compartment(); const isEditableCompartment = new Compartment(); +const vimModeCompartment = new Compartment(); const extensions = [ lineNumbers(), @@ -55,6 +58,7 @@ const extensions = [ rectangularSelection(), highlightActiveLine(), highlightSelectionMatches(), + indentationMarkers(), interact({ rules: [ // dragging numbers @@ -84,15 +88,11 @@ const extensions = [ ]; export default function (props: FileBlockProps) { - const { - content, - context: { path }, - isEditable, - onUpdateContent, - } = props; + const { content, context, isEditable, onUpdateContent } = props; const editorRef = React.useRef(null); const viewRef = React.useRef(); + const [isUsingVim, setIsUsingVim] = React.useState(false); React.useEffect(() => { if (viewRef.current || !editorRef.current) return; @@ -100,6 +100,7 @@ export default function (props: FileBlockProps) { const state = EditorState.create({ doc: content, extensions: [ + vimModeCompartment.of(isUsingVim ? vim() : []), extensions, EditorView.updateListener.of((v) => { if ( @@ -137,7 +138,7 @@ export default function (props: FileBlockProps) { if (!viewRef.current) return; const view = viewRef.current; - const language = LanguageDescription.matchFilename(languages, path); + const language = LanguageDescription.matchFilename(languages, context.path); if (language) { language.load().then((lang) => { @@ -146,7 +147,7 @@ export default function (props: FileBlockProps) { }); }); } - }, [path]); + }, [context.path]); React.useEffect(() => { if (!viewRef.current) return; @@ -160,9 +161,49 @@ export default function (props: FileBlockProps) { }, [isEditable]); return ( -
+
+ {isEditable && ( + + )} + +
+
); } diff --git a/blocks/file-blocks/code/style.css b/blocks/file-blocks/code/style.css index f2a1935..b18aeb2 100644 --- a/blocks/file-blocks/code/style.css +++ b/blocks/file-blocks/code/style.css @@ -9,8 +9,7 @@ body, } .cm-editor { - padding: 2em 1em 10em; - max-width: 70em; + padding: 1em 1em 10em; min-height: 100%; margin: 0 auto; outline: none !important; diff --git a/blocks/file-blocks/code/theme.tsx b/blocks/file-blocks/code/theme.tsx index b1bb401..ce92983 100644 --- a/blocks/file-blocks/code/theme.tsx +++ b/blocks/file-blocks/code/theme.tsx @@ -79,6 +79,15 @@ export const oneDarkTheme = EditorView.theme( color: colors.text, }, }, + ".cm-fat-cursor": { + background: `${colors.selectionBg} !important`, + }, + "&:not(.cm-focused) .cm-fat-cursor": { + outline: `solid 1px ${colors.selectionBg} !important`, + }, + ".cm-indentation-marker": { + opacity: 0.3, + }, }, { dark: true } ); diff --git a/blocks/file-blocks/markdown-edit/index.tsx b/blocks/file-blocks/markdown-edit/index.tsx index 0b57d38..9162345 100644 --- a/blocks/file-blocks/markdown-edit/index.tsx +++ b/blocks/file-blocks/markdown-edit/index.tsx @@ -47,7 +47,8 @@ import { images } from "./image-widget"; import { theme } from "./theme"; import "./style.css"; -let vimModeCompartment = new Compartment(); +const vimModeCompartment = new Compartment(); + const extensions = [ // lineNumbers(), highlightActiveLineGutter(), @@ -314,38 +315,44 @@ export default function (props: FileBlockProps) { />
)} - + + {isEditable && ( + + )} +