Skip to content

Commit

Permalink
fix(codemirror): watch extensions changes from props (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed May 25, 2022
1 parent eca9f5b commit 10ac96a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
30 changes: 19 additions & 11 deletions sandpack-react/src/components/CodeEditor/CodeEditor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,25 @@ export const ClosableTabs: React.FC = () => (
</SandpackProvider>
);

export const ExtensionAutocomplete: React.FC = () => (
<SandpackProvider template="react">
<SandpackThemeProvider>
<SandpackCodeEditor
extensions={[autocompletion()]}
extensionsKeymap={[completionKeymap]}
id="extensions"
/>
</SandpackThemeProvider>
</SandpackProvider>
);
export const ExtensionAutocomplete: React.FC = () => {
const [active, setActive] = React.useState(false);
return (
<>
<button onClick={(): void => setActive((prev): boolean => !prev)}>
Toggle
</button>
<SandpackProvider template="react">
<SandpackThemeProvider>
<SandpackCodeEditor
extensions={active ? [autocompletion()] : []}
extensionsKeymap={active ? [completionKeymap] : []}
id="extensions"
/>
</SandpackThemeProvider>
</SandpackProvider>
</>
);
};

export const ReadOnly: React.FC = () => {
return (
Expand Down
21 changes: 20 additions & 1 deletion sandpack-react/src/components/CodeEditor/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { lineNumbers } from "@codemirror/gutter";
import { defaultHighlightStyle } from "@codemirror/highlight";
import { history, historyKeymap } from "@codemirror/history";
import { bracketMatching } from "@codemirror/matchbrackets";
import { EditorState, EditorSelection } from "@codemirror/state";
import { EditorState, EditorSelection, StateEffect } from "@codemirror/state";
import type { Annotation, Extension } from "@codemirror/state";
import {
highlightSpecialChars,
Expand Down Expand Up @@ -295,6 +295,25 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
readOnly,
]);

React.useEffect(
function applyExtensions() {
const view = cmView.current;

if (view) {
view.dispatch({
effects: StateEffect.appendConfig.of(extensions),
});

view.dispatch({
effects: StateEffect.appendConfig.of(
keymap.of([...extensionsKeymap] as unknown as KeyBinding[])
),
});
}
},
[extensions, extensionsKeymap]
);

React.useEffect(() => {
// When the user clicks on a tab button on a larger screen
// Avoid autofocus on mobile as it leads to a bad experience and an unexpected layout shift
Expand Down
3 changes: 2 additions & 1 deletion sandpack-react/src/contexts/sandpackContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { create } from "react-test-renderer";

import { SandpackProvider } from "./sandpackContext";

import { create } from "react-test-renderer";
describe(SandpackProvider, () => {
it("updates a file", () => {
const root = create(<SandpackProvider template="react" />);
Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/contexts/sandpackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
} from "@codesandbox/sandpack-client";
import isEqual from "lodash.isequal";
import * as React from "react";
import { SandpackFiles } from "..";

import type { SandpackFiles } from "..";
import type {
SandpackContext,
SandboxEnvironment,
Expand Down

0 comments on commit 10ac96a

Please sign in to comment.