Skip to content

Commit 2809a7e

Browse files
committed
feat: add onStatistics props.
1 parent e823116 commit 2809a7e

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

core/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ export interface ReactCodeMirrorProps
307307
indentWithTab?: boolean;
308308
/** Fired whenever a change occurs to the document. */
309309
onChange?(value: string, viewUpdate: ViewUpdate): void;
310+
/** Some data on the statistics editor. */
311+
onStatistics?(data: Statistics): void;
310312
/** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
311313
onUpdate?(viewUpdate: ViewUpdate): void;
312314
/**
@@ -359,6 +361,29 @@ export interface BasicSetupOptions {
359361
}
360362
```
361363

364+
```ts
365+
import { EditorSelection, SelectionRange } from '@codemirror/state';
366+
import { ViewUpdate } from '@codemirror/view';
367+
export interface Statistics {
368+
/** Get the number of lines in the editor. */
369+
lineCount: number;
370+
/** Cursor Position */
371+
selection: EditorSelection;
372+
/** Retrieves a list of all current selections. */
373+
ranges: readonly SelectionRange[];
374+
/** Get the currently selected code. */
375+
selectionCode: string;
376+
/**
377+
* The length of the given array should be the same as the number of active selections.
378+
* Replaces the content of the selections with the strings in the array.
379+
*/
380+
selections: string[];
381+
/** Return true if any text is selected. */
382+
selectedText: boolean;
383+
}
384+
export declare const getStatistics: (view: ViewUpdate) => Statistics;
385+
```
386+
362387
## Packages
363388

364389
| Name | NPM Version | Website |

core/src/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import React, { useRef, forwardRef, useImperativeHandle } from 'react';
22
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
33
import { EditorView, ViewUpdate } from '@codemirror/view';
44
import { useCodeMirror } from './useCodeMirror';
5+
import { Statistics } from './utils';
56
import { BasicSetupOptions } from './basicSetup';
67

78
export * from './basicSetup';
89
export * from './useCodeMirror';
10+
export * from './utils';
911

1012
export interface ReactCodeMirrorProps
1113
extends Omit<EditorStateConfig, 'doc' | 'extensions'>,
@@ -49,6 +51,8 @@ export interface ReactCodeMirrorProps
4951
indentWithTab?: boolean;
5052
/** Fired whenever a change occurs to the document. */
5153
onChange?(value: string, viewUpdate: ViewUpdate): void;
54+
/** Some data on the statistics editor. */
55+
onStatistics?(data: Statistics): void;
5256
/** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
5357
onUpdate?(viewUpdate: ViewUpdate): void;
5458
/**
@@ -78,6 +82,7 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
7882
selection,
7983
extensions = [],
8084
onChange,
85+
onStatistics,
8186
onUpdate,
8287
autoFocus,
8388
theme = 'light',
@@ -115,6 +120,7 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
115120
readOnly,
116121
selection,
117122
onChange,
123+
onStatistics,
118124
onUpdate,
119125
extensions,
120126
});

core/src/useCodeMirror.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { indentWithTab } from '@codemirror/commands';
44
import { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';
55
import { oneDark } from '@codemirror/theme-one-dark';
66
import { basicSetup } from './basicSetup';
7+
import { getStatistics } from './utils';
78
import { ReactCodeMirrorProps } from '.';
89

910
export interface UseCodeMirror extends ReactCodeMirrorProps {
@@ -15,6 +16,7 @@ export function useCodeMirror(props: UseCodeMirror) {
1516
value,
1617
selection,
1718
onChange,
19+
onStatistics,
1820
onUpdate,
1921
extensions = [],
2022
autoFocus,
@@ -61,6 +63,7 @@ export function useCodeMirror(props: UseCodeMirror) {
6163
const value = doc.toString();
6264
onChange(value, vu);
6365
}
66+
onStatistics && onStatistics(getStatistics(vu));
6467
});
6568

6669
let getExtensions = [updateListener, defaultThemeOption];

core/src/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { EditorSelection, SelectionRange } from '@codemirror/state';
2+
import { ViewUpdate } from '@codemirror/view';
3+
4+
export interface Statistics {
5+
/** Get the number of lines in the editor. */
6+
lineCount: number;
7+
/** Cursor Position */
8+
selection: EditorSelection;
9+
/** Retrieves a list of all current selections. */
10+
ranges: readonly SelectionRange[];
11+
/** Get the currently selected code. */
12+
selectionCode: string;
13+
/**
14+
* The length of the given array should be the same as the number of active selections.
15+
* Replaces the content of the selections with the strings in the array.
16+
*/
17+
selections: string[];
18+
/** Return true if any text is selected. */
19+
selectedText: boolean;
20+
}
21+
22+
export const getStatistics = (view: ViewUpdate): Statistics => ({
23+
lineCount: view.state.doc.lines,
24+
selection: view.state.selection,
25+
ranges: view.state.selection.ranges,
26+
selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to),
27+
selections: view.state.selection.ranges.map((r) => view.state.sliceDoc(r.from, r.to)),
28+
selectedText: view.state.selection.ranges.some((r) => !r.empty),
29+
});

www/src/pages/home/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export default function App() {
169169
</Github>
170170
<Npm.Version
171171
scope="@uiw"
172-
packageName="react-shields"
172+
packageName="react-codemirror"
173173
href="https://www.npmjs.com/package/@uiw/react-codemirror"
174174
/>
175175
</Footer>

0 commit comments

Comments
 (0)