"`;
4 |
--------------------------------------------------------------------------------
/packages/codemirror/test/createReadOnly.test.tsx:
--------------------------------------------------------------------------------
1 | import { describe, expect, it, vi, afterEach } from "vitest";
2 | import { screen, render } from "solid-testing-library";
3 | import { CodeMirror } from "../src";
4 |
5 | describe("createReadOnly", () => {
6 | afterEach(() => {
7 | vi.restoreAllMocks();
8 | });
9 |
10 | it("is default false", async () => {
11 | const { unmount } = render(() => );
12 |
13 | const div = (await screen.findByRole("textbox")) as HTMLDivElement;
14 | expect(div.getAttribute("contenteditable")).toBe("true");
15 |
16 | unmount();
17 | });
18 |
19 | it("can be set to true", async () => {
20 | const { unmount } = render(() => );
21 | const div = (await screen.findByRole("textbox")) as HTMLDivElement;
22 | expect(div.getAttribute("contenteditable")).toBe("false");
23 |
24 | unmount();
25 | });
26 | });
27 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 |
8 | concurrency: ${{ github.workflow }}-${{ github.ref }}
9 |
10 | jobs:
11 | release:
12 | name: Release
13 | runs-on: ubuntu-latest
14 | steps:
15 | - name: Checkout Repo
16 | uses: actions/checkout@v2
17 | with:
18 | fetch-depth: 0
19 |
20 | - name: Setup Node.js 16.x
21 | uses: actions/setup-node@v2
22 | with:
23 | node-version: 16.x
24 |
25 | - name: Install pnpm
26 | run: npm i pnpm@latest -g
27 |
28 | - name: Install Dependencies
29 | run: pnpm install
30 |
31 | - name: Create Release Pull Request or Publish to npm
32 | id: changesets
33 | uses: changesets/action@v1
34 | with:
35 | publish: pnpm release
36 | env:
37 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
39 |
--------------------------------------------------------------------------------
/packages/codemirror/src/utils/createWrapLine.ts:
--------------------------------------------------------------------------------
1 | import type { Extension } from "@codemirror/state";
2 | import { EditorView } from "@codemirror/view";
3 | import { createEffect, mergeProps, on } from "solid-js";
4 |
5 | export interface WrapLineProps {
6 | /**
7 | * Whether to wrap lines
8 | * @default false
9 | */
10 | wrapLine?: boolean;
11 | }
12 |
13 | export function createWrapLine(
14 | props: WrapLineProps,
15 | createExtension: (extension: Extension) => (extension: Extension) => void
16 | ) {
17 | const merged = mergeProps({ wrapLine: false }, props);
18 |
19 | const reconfigureWrapLine = createExtension(getWrapLine(merged.wrapLine));
20 |
21 | createEffect(
22 | on(
23 | () => merged.wrapLine,
24 | (wrapLine) => {
25 | reconfigureWrapLine(getWrapLine(wrapLine));
26 | },
27 | { defer: true }
28 | )
29 | );
30 | }
31 |
32 | function getWrapLine(wrapLine: boolean) {
33 | return wrapLine ? EditorView.lineWrapping : [];
34 | }
35 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Nimesh Nayaju
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/packages/codemirror/src/utils/createLineNumbers.ts:
--------------------------------------------------------------------------------
1 | import type { Extension } from "@codemirror/state";
2 | import { lineNumbers } from "@codemirror/view";
3 | import { createEffect, mergeProps, on } from "solid-js";
4 |
5 | export interface LineNumbersProps {
6 | /**
7 | * Whether to display line numbers
8 | * @default true
9 | */
10 | showLineNumbers?: boolean;
11 | }
12 |
13 | export function createLineNumbers(
14 | props: LineNumbersProps,
15 | createExtension: (extension: Extension) => (extension: Extension) => void
16 | ) {
17 | const merged = mergeProps({ showLineNumbers: true }, props);
18 |
19 | const reconfigureLineNumbers = createExtension(
20 | getLineNumbers(merged.showLineNumbers)
21 | );
22 |
23 | createEffect(
24 | on(
25 | () => merged.showLineNumbers,
26 | (showLineNumbers) => {
27 | reconfigureLineNumbers(getLineNumbers(showLineNumbers));
28 | },
29 | { defer: true }
30 | )
31 | );
32 | }
33 |
34 | function getLineNumbers(showLineNumbers: boolean) {
35 | return showLineNumbers ? lineNumbers() : [];
36 | }
37 |
--------------------------------------------------------------------------------
/packages/core/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Nimesh Nayaju
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/packages/codemirror/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Nimesh Nayaju
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/packages/codemirror/src/utils/createReadOnly.ts:
--------------------------------------------------------------------------------
1 | import type { Extension } from "@codemirror/state";
2 | import {
3 | EditorView,
4 | highlightActiveLine,
5 | highlightActiveLineGutter,
6 | } from "@codemirror/view";
7 | import { createEffect, mergeProps, on } from "solid-js";
8 |
9 | export interface ReadOnlyProps {
10 | /**
11 | * Whether to set the editor to read-only
12 | * @default false
13 | */
14 | readOnly?: boolean;
15 | }
16 |
17 | export function createReadOnly(
18 | props: ReadOnlyProps,
19 | createExtension: (extension: Extension) => (extension: Extension) => void
20 | ) {
21 | const merged = mergeProps({ readOnly: false }, props);
22 |
23 | const reconfigureLineNumbers = createExtension(getReadOnly(merged.readOnly));
24 |
25 | createEffect(
26 | on(
27 | () => merged.readOnly,
28 | (readOnly) => {
29 | reconfigureLineNumbers(getReadOnly(readOnly));
30 | },
31 | { defer: true }
32 | )
33 | );
34 | }
35 |
36 | function getReadOnly(readOnly: boolean) {
37 | return readOnly
38 | ? EditorView.editable.of(false)
39 | : [highlightActiveLine(), highlightActiveLineGutter()];
40 | }
41 |
--------------------------------------------------------------------------------
/apps/demo/README.md:
--------------------------------------------------------------------------------
1 | ## Usage
2 |
3 | Those templates dependencies are maintained via [pnpm](https://pnpm.io) via `pnpm up -Lri`.
4 |
5 | This is the reason you see a `pnpm-lock.yaml`. That being said, any package manager will work. This file can be safely be removed once you clone a template.
6 |
7 | ```bash
8 | $ npm install # or pnpm install or yarn install
9 | ```
10 |
11 | ### Learn more on the [Solid Website](https://solidjs.com) and come chat with us on our [Discord](https://discord.com/invite/solidjs)
12 |
13 | ## Available Scripts
14 |
15 | In the project directory, you can run:
16 |
17 | ### `npm dev` or `npm start`
18 |
19 | Runs the app in the development mode.
20 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
21 |
22 | The page will reload if you make edits.
23 |
24 | ### `npm run build`
25 |
26 | Builds the app for production to the `dist` folder.
27 | It correctly bundles Solid in production mode and optimizes the build for the best performance.
28 |
29 | The build is minified and the filenames include the hashes.
30 | Your app is ready to be deployed!
31 |
32 | ## Deployment
33 |
34 | You can deploy the `dist` folder to any static host provider (netlify, surge, now, etc.)
35 |
--------------------------------------------------------------------------------
/apps/demo/src/CodeMirror2.tsx:
--------------------------------------------------------------------------------
1 | import { createEffect, mergeProps, on, splitProps } from "solid-js";
2 | import type { JSX } from "solid-js/jsx-runtime";
3 | import { mergeRefs } from "@solid-primitives/refs";
4 | import { type CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
5 | import { lineNumbers } from "@codemirror/view";
6 |
7 | export function CodeMirror2(
8 | props: CodeMirrorProps & JSX.HTMLAttributes & { showLineNumbers?: boolean },
9 | ) {
10 | let ref: HTMLDivElement | undefined;
11 |
12 | const [codemirrorProps, local, others] = splitProps(props, ["value", "onValueChange"], ["showLineNumbers"]);
13 | const { createExtension } = createCodeMirror(codemirrorProps, () => ref);
14 |
15 | const merged = mergeProps({ showLineNumbers: true, readOnly: false }, local);
16 |
17 | const reconfigureLineNumbers = createExtension(getLineNumbers(merged.showLineNumbers));
18 |
19 | createEffect(on(() => merged.showLineNumbers, (showLineNumbers) => {
20 | reconfigureLineNumbers(getLineNumbers(showLineNumbers));
21 | }, { defer: true }));
22 |
23 | return
4 |
5 | # @solid-codemirror/core
6 |
7 | Provides a `createCodeMirror` function that takes in a `ref` object and attaches a `CodeMirror` view to it.
8 |
9 | ## Demo
10 |
11 | https://solid-codemirror.vercel.app/
12 |
13 | ## Installation
14 |
15 | ```bash
16 | yarn add @solid-codemirror/core
17 | # or
18 | npm i @solid-codemirror/core
19 | ```
20 |
21 | > **Note** The [@codemirror/state](https://github.com/codemirror/state) and [@codemirror/view](https://github.com/codemirror/state) libraries are flagged as peer dependencies and are recommeneded to be installed alongside this package.
22 |
23 | ## `createCodeMirror`
24 |
25 | Attaches a `CodeMirror` view to the specified `ref` object and returns a object with a `createExtension` method to add extension compartments to the codemirror state instance.
26 |
27 | ## Basic Usage
28 |
29 | ```tsx
30 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
31 |
32 | export default function CodeMirror(props: CodeMirrorProps) {
33 | let ref: HTMLDivElement | undefined;
34 |
35 | createCodeMirror(props, () => ref);
36 |
37 | return ;
38 | }
39 | ```
40 |
41 | ## Add Extension
42 |
43 | ```tsx
44 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
45 | import { lineNumbers } from "@codemirror/view";
46 |
47 | export default function App(props: CodeMirrorProps) {
48 | let ref: HTMLDivElement | undefined;
49 |
50 | const { createExtension } = createCodeMirror(props, () => ref);
51 |
52 | createExtension(lineNumbers());
53 |
54 | return ;
55 | }
56 | ```
57 |
58 | ## Reconfigure Extension
59 |
60 | ```tsx
61 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
62 | import { lineNumbers } from "@codemirror/view";
63 |
64 | export default function App(props: CodeMirrorProps) {
65 | let ref: HTMLDivElement | undefined;
66 |
67 | const { createExtension } = createCodeMirror(props, () => ref);
68 |
69 | const reconfigureLineNumbers = createExtension(lineNumbers());
70 |
71 | return (
72 |
73 |
74 |
75 | {/* Buttons to show/hide line numbers */}
76 |
77 |
80 |
83 |
84 |
85 | );
86 | }
87 | ```
88 |
89 | > **Info** Extensions in `@codemirror/core` are wrapped inside an editor [Comparment](https://codemirror.net/docs/ref/#state.Compartment). Compartments enable [dynamic reconfiguration](https://codemirror.net/examples/config/) (partially reconfigure a tree of extensions) of the editor.
90 |
91 | ## `CodeMirrorProps`
92 |
93 | You can control the CodeMirror editor instance through the following props. **All props are optional.**
94 |
95 | | Prop | Type | Description |
96 | | --------------- | ------------------------------ | ------------------------------------------------------------------------------ |
97 | | `value` | `string` | The initial value of the editor |
98 | | `onValueChange` | `(value: string) => void` | Called whenever the editor code value changes |
99 | | `onEditorMount` | `(editor: EditorView) => void` | Called when the editor first mounts, receiving the current EditorView instance |
100 |
101 | ### Definition
102 |
103 | ```ts
104 | function createCodeMirror(
105 | props: {
106 | value?: string;
107 | onValueChange?: (value: string) => void;
108 | onEditorMount?: (editor: EditorView) => void;
109 | }
110 | ref: Accessor
111 | ): {
112 | createExtension: (extension: Extension) => (extension: Extension) => void;
113 | };
114 | ```
115 |
116 | > **Note** Extensions in `@codemirror/core` are wrapped inside an editor [Comparment](https://codemirror.net/docs/ref/#state.Compartment). Compartments enable [dynamic reconfiguration](https://codemirror.net/examples/config/) (partially reconfigure a tree of extensions) of the editor.
117 |
118 | > **Note** The `@solid-codemirror/codemirror` package is based on `@codemirror/core`. You can view the [source code](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/codemirror) of the library here.
119 |
120 | ## License
121 |
122 | This project is licensed under MIT.
123 |
124 | ## Author
125 |
126 | - [@nayajunimesh](https://twitter.com/nayajunimesh)
127 |
--------------------------------------------------------------------------------
/packages/codemirror/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | # @solid-codemirror/codemirror
6 |
7 | CodeMirror 6 component for SolidJS
8 |
9 | ## Demo
10 |
11 | https://solid-codemirror.vercel.app/
12 |
13 | ## Installation
14 |
15 | ```bash
16 | yarn add @solid-codemirror/codemirror
17 | # or
18 | npm i @solid-codemirror/codemirror
19 | ```
20 |
21 | > **Note** The [@codemirror/state](https://github.com/codemirror/state) and [@codemirror/view](https://github.com/codemirror/state) libraries are flagged as peer dependencies and are recommeneded to be installed alongside this package.
22 |
23 | ## Known issue with `Vite`
24 |
25 | > **Warning** You may encounter the following error if you're using Vite as your bundling tool:
26 | >
27 | > ```bash
28 | > Error: Unrecognized extension value in extension set ([object Object]). This sometimes happens because multipleinstances of @codemirror/state are loaded, breaking instanceof checks.
29 | > ```
30 | >
31 | > This error can be fixed by adding the following configuration option to your `vite.config.{js,ts}` file.
32 | >
33 | > ```typescript
34 | > export default defineConfig({
35 | > // Your configuration
36 | > optimizeDeps: {
37 | > // Add both @codemirror/state and @codemirror/view to included deps for optimization
38 | > include: ["@codemirror/state", "@codemirror/view"],
39 | > },
40 | > });
41 | > ```
42 |
43 | ## Basic Usage
44 |
45 | ```tsx
46 | import { CodeMirror } from "@solid-codemirror/codemirror";
47 |
48 | export default function App() {
49 | return ;
50 | }
51 | ```
52 |
53 | ## Controlling the `CodeMirror` component
54 |
55 | You can control the `CodeMirror` component through the following props. **All props are optional.**
56 |
57 | | Prop | Type | Description |
58 | | ----------------- | ------------------------------ | ------------------------------------------------------------------------------ |
59 | | `value` | `string` | The initial value of the editor |
60 | | `onValueChange` | `(value: string) => void` | Called whenever the editor code value changes |
61 | | `onEditorMount` | `(editor: EditorView) => void` | Called when the editor first mounts, receiving the current EditorView instance |
62 | | `showLineNumbers` | `boolean` | Whether to display line numbers |
63 | | `wrapLine` | `boolean` | Whether to wrap lines |
64 | | `readOnly` | `boolean` | Whether to set the editor to read-only |
65 | | `theme` | `Extension` | The CodeMirror theme extension to use |
66 | | `extensions` | `Extension[]` | An array of CodeMirror extensions to use |
67 |
68 | ## Configure Line Numbers / Read Only / Line Wrapping
69 |
70 | ```tsx
71 | import { CodeMirror } from "@solid-codemirror/codemirror";
72 |
73 | export default function App() {
74 | return ;
75 | }
76 | ```
77 |
78 | ## Configure theme
79 |
80 | ```tsx
81 | import { CodeMirror } from "@solid-codemirror/codemirror";
82 | import { oneDark } from "@codemirror/theme-one-dark";
83 |
84 | export default function App() {
85 | return ;
86 | }
87 | ```
88 |
89 | ## Configure Extensions
90 |
91 | ```tsx
92 | import { CodeMirror } from "@solid-codemirror/codemirror";
93 | import { basicSetup } from "codemirror";
94 | import { python } from "@codemirror/lang-python";
95 | import { oneDark } from "@codemirror/theme-one-dark";
96 |
97 | export default function App() {
98 | return ;
99 | }
100 | ```
101 |
102 | ## Register callbacks on editor value change or editor mount
103 |
104 | ```tsx
105 | import { CodeMirror } from "@solid-codemirror/codemirror";
106 | import type { EditorView } from "@codemirror/view";
107 |
108 | export default function App() {
109 | const onValueChange = (value: string) => {
110 | console.log(value);
111 | };
112 |
113 | const onEditorMount = (view: EditorView) => {
114 | console.log(view);
115 | };
116 |
117 | return (
118 |
119 | );
120 | }
121 | ```
122 |
123 | ## License
124 |
125 | This project is licensed under MIT.
126 |
127 | ## Author
128 |
129 | - [@nayajunimesh](https://twitter.com/nayajunimesh)
130 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | # solid-codemirror
6 |
7 | A set of libraries to integrate CodeMirror to any SolidJS app. This repository contains two packages:
8 |
9 | - [@solid-codemirror/core](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/core)
10 |
11 | - [@solid-codemirror/codemirror](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/codemirror)
12 |
13 | ## Demo
14 |
15 | https://solid-codemirror.vercel.app/
16 |
17 | ## Installation
18 |
19 | ```bash
20 | yarn add @solid-codemirror/codemirror
21 | # or
22 | npm i @solid-codemirror/codemirror
23 | ```
24 |
25 | > **Note** The [@codemirror/state](https://github.com/codemirror/state) and [@codemirror/view](https://github.com/codemirror/state) libraries are flagged as peer dependencies and are recommeneded to be installed alongside this package.
26 |
27 | ## Known issue with `Vite`
28 |
29 | > **Warning** You may encounter the following error if you're using Vite as your bundling tool:
30 | >
31 | > ```bash
32 | > Error: Unrecognized extension value in extension set ([object Object]). This sometimes happens because multipleinstances of @codemirror/state are loaded, breaking instanceof checks.
33 | > ```
34 | >
35 | > This error can be fixed by adding the following configuration option to your `vite.config.{js,ts}` file.
36 | >
37 | > ```typescript
38 | > export default defineConfig({
39 | > // Your configuration
40 | > optimizeDeps: {
41 | > // Add both @codemirror/state and @codemirror/view to included deps for optimization
42 | > include: ["@codemirror/state", "@codemirror/view"],
43 | > },
44 | > });
45 | > ```
46 |
47 | ## Basic Usage
48 |
49 | ```tsx
50 | import { CodeMirror } from "@solid-codemirror/codemirror";
51 |
52 | export default function App() {
53 | return ;
54 | }
55 | ```
56 |
57 | ## Configure Line Numbers / Read Only / Line Wrapping
58 |
59 | ```tsx
60 | import { CodeMirror } from "@solid-codemirror/codemirror";
61 |
62 | export default function App() {
63 | return ;
64 | }
65 | ```
66 |
67 | ## Configure theme
68 |
69 | ```tsx
70 | import { CodeMirror } from "@solid-codemirror/codemirror";
71 | import { oneDark } from "@codemirror/theme-one-dark";
72 |
73 | export default function App() {
74 | return ;
75 | }
76 | ```
77 |
78 | ## Configure Extensions
79 |
80 | ```tsx
81 | import { CodeMirror } from "@solid-codemirror/codemirror";
82 | import { basicSetup } from "codemirror";
83 | import { python } from "@codemirror/lang-python";
84 | import { oneDark } from "@codemirror/theme-one-dark";
85 |
86 | export default function App() {
87 | return ;
88 | }
89 | ```
90 |
91 | ## Register callbacks on editor value change or editor mount
92 |
93 | ```tsx
94 | import { CodeMirror } from "@solid-codemirror/codemirror";
95 | import type { EditorView } from "@codemirror/view";
96 |
97 | export default function App() {
98 | const onValueChange = (value: string) => {
99 | console.log(value);
100 | };
101 |
102 | const onEditorMount = (view: EditorView) => {
103 | console.log(view);
104 | };
105 |
106 | return (
107 |
108 | );
109 | }
110 | ```
111 |
112 | ## Controlling the `CodeMirror` component
113 |
114 | You can control the `CodeMirror` component through the following props. **All props are optional.**
115 |
116 | | Prop | Type | Description |
117 | | ----------------- | ------------------------------ | ------------------------------------------------------------------------------ |
118 | | `value` | `string` | The initial value of the editor |
119 | | `onValueChange` | `(value: string) => void` | Called whenever the editor code value changes |
120 | | `onEditorMount` | `(editor: EditorView) => void` | Called when the editor first mounts, receiving the current EditorView instance |
121 | | `showLineNumbers` | `boolean` | Whether to display line numbers |
122 | | `wrapLine` | `boolean` | Whether to wrap lines |
123 | | `readOnly` | `boolean` | Whether to set the editor to read-only |
124 | | `theme` | `Extension` | The CodeMirror theme extension to use |
125 | | `extensions` | `Extension[]` | An array of CodeMirror extensions to use |
126 |
127 | For more information on the usage of the `CodeMirror` component, check out [@solid-codemirror/codemirror](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/codemirror).
128 | \
129 |
130 | \
131 |
132 |
133 | # Advanced usage
134 |
135 | ### Want more control over your `CodeMirror` component? Create your custom component using the `createCodeMirror` function.
136 |
137 | ## Installation
138 |
139 | ```bash
140 | yarn add @solid-codemirror/core @codemirror/state @codemirror/view
141 | # or
142 | npm i @solid-codemirror/core @codemirror/state @codemirror/view
143 | ```
144 |
145 | ## `createCodeMirror`
146 |
147 | Attaches a `CodeMirror` view to the specified `ref` object and returns a object with a `createExtension` method to add extension compartments to the codemirror state instance.
148 |
149 | ## Basic Usage
150 |
151 | ```tsx
152 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
153 |
154 | export default function CodeMirror(props: CodeMirrorProps) {
155 | let ref: HTMLDivElement | undefined;
156 |
157 | createCodeMirror(props, () => ref);
158 |
159 | return ;
160 | }
161 | ```
162 |
163 | ## Add Extension
164 |
165 | ```tsx
166 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
167 | import { lineNumbers } from "@codemirror/view";
168 |
169 | export default function App(props: CodeMirrorProps) {
170 | let ref: HTMLDivElement | undefined;
171 |
172 | const { createExtension } = createCodeMirror(props, () => ref);
173 |
174 | createExtension(lineNumbers());
175 |
176 | return ;
177 | }
178 | ```
179 |
180 | ## Reconfigure Extension
181 |
182 | ```tsx
183 | import { CodeMirrorProps, createCodeMirror } from "@solid-codemirror/core";
184 | import { lineNumbers } from "@codemirror/view";
185 |
186 | export default function App(props: CodeMirrorProps) {
187 | let ref: HTMLDivElement | undefined;
188 |
189 | const { createExtension } = createCodeMirror(props, () => ref);
190 |
191 | const reconfigureLineNumbers = createExtension(lineNumbers());
192 |
193 | return (
194 |
195 |
196 |
197 | {/* Buttons to show/hide line numbers */}
198 |
199 |
202 |
205 |
206 |
207 | );
208 | }
209 | ```
210 |
211 | > **Note** Extensions in `@codemirror/core` are wrapped inside an editor [Comparment](https://codemirror.net/docs/ref/#state.Compartment). Compartments enable [dynamic reconfiguration](https://codemirror.net/examples/config/) (partially reconfigure a tree of extensions) of the editor.
212 |
213 | > **Note** The `@solid-codemirror/codemirror` package is based on `@codemirror/core`. You can view the [source code](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/codemirror) of the library here.
214 |
215 | For more information on the usage of the `createCodeMirror` function, check out [@solid-codemirror/core](https://github.com/nimeshnayaju/solid-codemirror/tree/main/packages/core).
216 |
217 | ## License
218 |
219 | This project is licensed under MIT.
220 |
221 | ## Author
222 |
223 | - [@nayajunimesh](https://twitter.com/nayajunimesh)
224 |
--------------------------------------------------------------------------------