@uiw/react-codemirror 4.9.6 → 4.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -0
- package/cjs/index.d.ts +4 -0
- package/cjs/index.js +17 -1
- package/cjs/index.js.map +3 -2
- package/cjs/useCodeMirror.js +5 -0
- package/cjs/useCodeMirror.js.map +4 -2
- package/cjs/utils.d.ts +30 -0
- package/cjs/utils.js +29 -0
- package/cjs/utils.js.map +37 -0
- package/dist/mdeditor.js +5 -2
- package/dist/mdeditor.min.js +1 -1
- package/esm/index.d.ts +4 -0
- package/esm/index.js +4 -1
- package/esm/index.js.map +3 -2
- package/esm/useCodeMirror.js +4 -0
- package/esm/useCodeMirror.js.map +4 -2
- package/esm/utils.d.ts +30 -0
- package/esm/utils.js +14 -0
- package/esm/utils.js.map +37 -0
- package/package.json +1 -1
- package/src/index.tsx +6 -0
- package/src/useCodeMirror.ts +3 -0
- package/src/utils.ts +44 -0
package/esm/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
|
|
3
3
|
import { EditorView, ViewUpdate } from '@codemirror/view';
|
|
4
|
+
import { Statistics } from './utils';
|
|
4
5
|
import { BasicSetupOptions } from './basicSetup';
|
|
5
6
|
export * from './basicSetup';
|
|
6
7
|
export * from './useCodeMirror';
|
|
8
|
+
export * from './utils';
|
|
7
9
|
export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'extensions'>, Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {
|
|
8
10
|
/** value of the auto created model in the editor. */
|
|
9
11
|
value?: string;
|
|
@@ -44,6 +46,8 @@ export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'e
|
|
|
44
46
|
indentWithTab?: boolean;
|
|
45
47
|
/** Fired whenever a change occurs to the document. */
|
|
46
48
|
onChange?(value: string, viewUpdate: ViewUpdate): void;
|
|
49
|
+
/** Some data on the statistics editor. */
|
|
50
|
+
onStatistics?(data: Statistics): void;
|
|
47
51
|
/** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
|
|
48
52
|
onUpdate?(viewUpdate: ViewUpdate): void;
|
|
49
53
|
/**
|
package/esm/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/extends";
|
|
2
2
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
|
|
3
|
-
var _excluded = ["className", "value", "selection", "extensions", "onChange", "onUpdate", "autoFocus", "theme", "height", "minHeight", "maxHeight", "width", "minWidth", "maxWidth", "basicSetup", "placeholder", "indentWithTab", "editable", "readOnly", "root"];
|
|
3
|
+
var _excluded = ["className", "value", "selection", "extensions", "onChange", "onStatistics", "onUpdate", "autoFocus", "theme", "height", "minHeight", "maxHeight", "width", "minWidth", "maxWidth", "basicSetup", "placeholder", "indentWithTab", "editable", "readOnly", "root"];
|
|
4
4
|
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
|
|
5
5
|
import { useCodeMirror } from './useCodeMirror';
|
|
6
6
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
7
|
export * from './basicSetup';
|
|
8
8
|
export * from './useCodeMirror';
|
|
9
|
+
export * from './utils';
|
|
9
10
|
var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
|
|
10
11
|
var {
|
|
11
12
|
className,
|
|
@@ -13,6 +14,7 @@ var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
13
14
|
selection,
|
|
14
15
|
extensions = [],
|
|
15
16
|
onChange,
|
|
17
|
+
onStatistics,
|
|
16
18
|
onUpdate,
|
|
17
19
|
autoFocus,
|
|
18
20
|
theme = 'light',
|
|
@@ -56,6 +58,7 @@ var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
56
58
|
readOnly,
|
|
57
59
|
selection,
|
|
58
60
|
onChange,
|
|
61
|
+
onStatistics,
|
|
59
62
|
onUpdate,
|
|
60
63
|
extensions
|
|
61
64
|
});
|
package/esm/index.js.map
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"selection",
|
|
15
15
|
"extensions",
|
|
16
16
|
"onChange",
|
|
17
|
+
"onStatistics",
|
|
17
18
|
"onUpdate",
|
|
18
19
|
"autoFocus",
|
|
19
20
|
"theme",
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
"../src/index.tsx"
|
|
45
46
|
],
|
|
46
47
|
"sourcesContent": [
|
|
47
|
-
"import React, { useRef, forwardRef, useImperativeHandle } from 'react';\nimport { EditorState, EditorStateConfig, Extension } from '@codemirror/state';\nimport { EditorView, ViewUpdate } from '@codemirror/view';\nimport { useCodeMirror } from './useCodeMirror';\nimport { BasicSetupOptions } from './basicSetup';\n\nexport * from './basicSetup';\nexport * from './useCodeMirror';\n\nexport interface ReactCodeMirrorProps\n extends Omit<EditorStateConfig, 'doc' | 'extensions'>,\n Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {\n /** value of the auto created model in the editor. */\n value?: string;\n height?: string;\n minHeight?: string;\n maxHeight?: string;\n width?: string;\n minWidth?: string;\n maxWidth?: string;\n /** focus on the editor. */\n autoFocus?: boolean;\n /** Enables a placeholder—a piece of example content to show when the editor is empty. */\n placeholder?: string | HTMLElement;\n /**\n * `light` / `dark` / `Extension` Defaults to `light`.\n * @default light\n */\n theme?: 'light' | 'dark' | Extension;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n basicSetup?: boolean | BasicSetupOptions;\n /**\n * This disables editing of the editor content by the user.\n * @default true\n */\n editable?: boolean;\n /**\n * This disables editing of the editor content by the user.\n * @default false\n */\n readOnly?: boolean;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n indentWithTab?: boolean;\n /** Fired whenever a change occurs to the document. */\n onChange?(value: string, viewUpdate: ViewUpdate): void;\n /** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */\n onUpdate?(viewUpdate: ViewUpdate): void;\n /**\n * Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.\n * They can either be built-in extension-providing objects,\n * such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of),\n * or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.\n */\n extensions?: Extension[];\n /**\n * If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.\n * Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)\n */\n root?: ShadowRoot | Document;\n}\n\nexport interface ReactCodeMirrorRef {\n editor?: HTMLDivElement | null;\n state?: EditorState;\n view?: EditorView;\n}\n\nconst ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {\n const {\n className,\n value = '',\n selection,\n extensions = [],\n onChange,\n onUpdate,\n autoFocus,\n theme = 'light',\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n root,\n ...other\n } = props;\n const editor = useRef<HTMLDivElement>(null);\n const { state, view, container, setContainer } = useCodeMirror({\n container: editor.current,\n root,\n value,\n autoFocus,\n theme,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n selection,\n onChange,\n onUpdate,\n extensions,\n });\n\n useImperativeHandle(ref, () => ({ editor: editor.current, state: state, view: view }), [\n editor,\n container,\n state,\n view,\n ]);\n\n // check type of value\n if (typeof value !== 'string') {\n throw new Error(`value must be typeof string but got ${typeof value}`);\n }\n\n const defaultClassNames = typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';\n return <div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>;\n});\n\nReactCodeMirror.displayName = 'CodeMirror';\n\nexport default ReactCodeMirror;\n"
|
|
48
|
+
"import React, { useRef, forwardRef, useImperativeHandle } from 'react';\nimport { EditorState, EditorStateConfig, Extension } from '@codemirror/state';\nimport { EditorView, ViewUpdate } from '@codemirror/view';\nimport { useCodeMirror } from './useCodeMirror';\nimport { Statistics } from './utils';\nimport { BasicSetupOptions } from './basicSetup';\n\nexport * from './basicSetup';\nexport * from './useCodeMirror';\nexport * from './utils';\n\nexport interface ReactCodeMirrorProps\n extends Omit<EditorStateConfig, 'doc' | 'extensions'>,\n Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {\n /** value of the auto created model in the editor. */\n value?: string;\n height?: string;\n minHeight?: string;\n maxHeight?: string;\n width?: string;\n minWidth?: string;\n maxWidth?: string;\n /** focus on the editor. */\n autoFocus?: boolean;\n /** Enables a placeholder—a piece of example content to show when the editor is empty. */\n placeholder?: string | HTMLElement;\n /**\n * `light` / `dark` / `Extension` Defaults to `light`.\n * @default light\n */\n theme?: 'light' | 'dark' | Extension;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n basicSetup?: boolean | BasicSetupOptions;\n /**\n * This disables editing of the editor content by the user.\n * @default true\n */\n editable?: boolean;\n /**\n * This disables editing of the editor content by the user.\n * @default false\n */\n readOnly?: boolean;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n indentWithTab?: boolean;\n /** Fired whenever a change occurs to the document. */\n onChange?(value: string, viewUpdate: ViewUpdate): void;\n /** Some data on the statistics editor. */\n onStatistics?(data: Statistics): void;\n /** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */\n onUpdate?(viewUpdate: ViewUpdate): void;\n /**\n * Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.\n * They can either be built-in extension-providing objects,\n * such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of),\n * or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.\n */\n extensions?: Extension[];\n /**\n * If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.\n * Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)\n */\n root?: ShadowRoot | Document;\n}\n\nexport interface ReactCodeMirrorRef {\n editor?: HTMLDivElement | null;\n state?: EditorState;\n view?: EditorView;\n}\n\nconst ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {\n const {\n className,\n value = '',\n selection,\n extensions = [],\n onChange,\n onStatistics,\n onUpdate,\n autoFocus,\n theme = 'light',\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n root,\n ...other\n } = props;\n const editor = useRef<HTMLDivElement>(null);\n const { state, view, container, setContainer } = useCodeMirror({\n container: editor.current,\n root,\n value,\n autoFocus,\n theme,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n selection,\n onChange,\n onStatistics,\n onUpdate,\n extensions,\n });\n\n useImperativeHandle(ref, () => ({ editor: editor.current, state: state, view: view }), [\n editor,\n container,\n state,\n view,\n ]);\n\n // check type of value\n if (typeof value !== 'string') {\n throw new Error(`value must be typeof string but got ${typeof value}`);\n }\n\n const defaultClassNames = typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';\n return <div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>;\n});\n\nReactCodeMirror.displayName = 'CodeMirror';\n\nexport default ReactCodeMirror;\n"
|
|
48
49
|
],
|
|
49
|
-
"mappings": ";;;AAAA,OAAOA,KAAP,IAAgBC,MAAhB,EAAwBC,UAAxB,EAA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;EArB6D,CAAD,CAA9D;EAwBAR,mBAAmB,CAACI,GAAD,EAAM,OAAO;IAAEuB,MAAM,EAAEA,MAAM,CAACK,OAAjB;IAA0BJ,KAAK,EAAEA,KAAjC;IAAwCC,IAAI,EAAEA;EAA9C,CAAP,CAAN,EAAoE,CACrFF,MADqF,EAErFG,SAFqF,EAGrFF,KAHqF,EAIrFC,IAJqF,CAApE,CAAnB,CAlD2F,CAyD3F;;EACA,IAAI,OAAOvB,KAAP,KAAiB,QAArB,EAA+B;IAC7B,MAAM,IAAI2B,KAAJ,0CAAiD,OAAO3B,KAAxD,CAAN;EACD;;EAED,IAAM4B,iBAAiB,GAAG,OAAOrB,KAAP,KAAiB,QAAjB,iBAAwCA,KAAxC,GAAkD,UAA5E;EACA,oBAAO;IAAK,GAAG,EAAEc,MAAV;IAAkB,SAAS,OAAKO,iBAAL,IAAyB7B,SAAS,SAAOA,SAAP,GAAqB,EAAvD;EAA3B,GAA4FqB,KAA5F,EAAP;AACD,CAhEiC,CAAlC;AAkEAxB,eAAe,CAACiC,WAAhB,GAA8B,YAA9B;AAEA,eAAejC,eAAf"
|
|
50
51
|
}
|
package/esm/useCodeMirror.js
CHANGED
|
@@ -4,11 +4,13 @@ import { indentWithTab } from '@codemirror/commands';
|
|
|
4
4
|
import { EditorView, keymap, placeholder } from '@codemirror/view';
|
|
5
5
|
import { oneDark } from '@codemirror/theme-one-dark';
|
|
6
6
|
import { basicSetup } from './basicSetup';
|
|
7
|
+
import { getStatistics } from './utils';
|
|
7
8
|
export function useCodeMirror(props) {
|
|
8
9
|
var {
|
|
9
10
|
value,
|
|
10
11
|
selection,
|
|
11
12
|
onChange,
|
|
13
|
+
onStatistics,
|
|
12
14
|
onUpdate,
|
|
13
15
|
extensions = [],
|
|
14
16
|
autoFocus,
|
|
@@ -54,6 +56,8 @@ export function useCodeMirror(props) {
|
|
|
54
56
|
|
|
55
57
|
onChange(_value, vu);
|
|
56
58
|
}
|
|
59
|
+
|
|
60
|
+
onStatistics && onStatistics(getStatistics(vu));
|
|
57
61
|
});
|
|
58
62
|
var getExtensions = [updateListener, defaultThemeOption];
|
|
59
63
|
|
package/esm/useCodeMirror.js.map
CHANGED
|
@@ -11,11 +11,13 @@
|
|
|
11
11
|
"placeholder",
|
|
12
12
|
"oneDark",
|
|
13
13
|
"basicSetup",
|
|
14
|
+
"getStatistics",
|
|
14
15
|
"useCodeMirror",
|
|
15
16
|
"props",
|
|
16
17
|
"value",
|
|
17
18
|
"selection",
|
|
18
19
|
"onChange",
|
|
20
|
+
"onStatistics",
|
|
19
21
|
"onUpdate",
|
|
20
22
|
"extensions",
|
|
21
23
|
"autoFocus",
|
|
@@ -73,7 +75,7 @@
|
|
|
73
75
|
"../src/useCodeMirror.ts"
|
|
74
76
|
],
|
|
75
77
|
"sourcesContent": [
|
|
76
|
-
"import { useEffect, useState } from 'react';\nimport { EditorState, StateEffect } from '@codemirror/state';\nimport { indentWithTab } from '@codemirror/commands';\nimport { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';\nimport { oneDark } from '@codemirror/theme-one-dark';\nimport { basicSetup } from './basicSetup';\nimport { ReactCodeMirrorProps } from '.';\n\nexport interface UseCodeMirror extends ReactCodeMirrorProps {\n container?: HTMLDivElement | null;\n}\n\nexport function useCodeMirror(props: UseCodeMirror) {\n const {\n value,\n selection,\n onChange,\n onUpdate,\n extensions = [],\n autoFocus,\n theme = 'light',\n height = '',\n minHeight = '',\n maxHeight = '',\n placeholder: placeholderStr = '',\n width = '',\n minWidth = '',\n maxWidth = '',\n editable = true,\n readOnly = false,\n indentWithTab: defaultIndentWithTab = true,\n basicSetup: defaultBasicSetup = true,\n root,\n } = props;\n const [container, setContainer] = useState<HTMLDivElement>();\n const [view, setView] = useState<EditorView>();\n const [state, setState] = useState<EditorState>();\n const defaultLightThemeOption = EditorView.theme(\n {\n '&': {\n backgroundColor: '#fff',\n },\n },\n {\n dark: false,\n },\n );\n const defaultThemeOption = EditorView.theme({\n '&': {\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n },\n });\n const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {\n if (vu.docChanged && typeof onChange === 'function') {\n const doc = vu.state.doc;\n const value = doc.toString();\n onChange(value, vu);\n }\n });\n\n let getExtensions = [updateListener, defaultThemeOption];\n if (defaultIndentWithTab) {\n getExtensions.unshift(keymap.of([indentWithTab]));\n }\n if (defaultBasicSetup) {\n if (typeof defaultBasicSetup === 'boolean') {\n getExtensions.unshift(basicSetup());\n } else {\n getExtensions.unshift(basicSetup(defaultBasicSetup));\n }\n }\n\n if (placeholderStr) {\n getExtensions.unshift(placeholder(placeholderStr));\n }\n\n switch (theme) {\n case 'light':\n getExtensions.push(defaultLightThemeOption);\n break;\n case 'dark':\n getExtensions.push(oneDark);\n break;\n default:\n getExtensions.push(theme);\n break;\n }\n\n if (editable === false) {\n getExtensions.push(EditorView.editable.of(false));\n }\n if (readOnly) {\n getExtensions.push(EditorState.readOnly.of(true));\n }\n\n if (onUpdate && typeof onUpdate === 'function') {\n getExtensions.push(EditorView.updateListener.of(onUpdate));\n }\n getExtensions = getExtensions.concat(extensions);\n\n useEffect(() => {\n if (container && !state) {\n const stateCurrent = EditorState.create({\n doc: value,\n selection,\n extensions: getExtensions,\n });\n setState(stateCurrent);\n if (!view) {\n const viewCurrent = new EditorView({\n state: stateCurrent,\n parent: container,\n root,\n });\n setView(viewCurrent);\n }\n }\n return () => {\n if (view) {\n setState(undefined);\n setView(undefined);\n }\n };\n }, [container, state]);\n\n useEffect(() => setContainer(props.container!), [props.container]);\n\n useEffect(\n () => () => {\n if (view) {\n view.destroy();\n setView(undefined);\n }\n },\n [view],\n );\n\n useEffect(() => {\n if (autoFocus && view) {\n view.focus();\n }\n }, [autoFocus, view]);\n\n useEffect(() => {\n if (view) {\n view.dispatch({ effects: StateEffect.reconfigure.of(getExtensions) });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n theme,\n extensions,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n placeholderStr,\n editable,\n readOnly,\n defaultIndentWithTab,\n defaultBasicSetup,\n onChange,\n onUpdate,\n ]);\n\n useEffect(() => {\n const currentValue = view ? view.state.doc.toString() : '';\n if (view && value !== currentValue) {\n view.dispatch({\n changes: { from: 0, to: currentValue.length, insert: value || '' },\n });\n }\n }, [value, view]);\n\n return { state, setState, view, setView, container, setContainer };\n}\n"
|
|
78
|
+
"import { useEffect, useState } from 'react';\nimport { EditorState, StateEffect } from '@codemirror/state';\nimport { indentWithTab } from '@codemirror/commands';\nimport { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';\nimport { oneDark } from '@codemirror/theme-one-dark';\nimport { basicSetup } from './basicSetup';\nimport { getStatistics } from './utils';\nimport { ReactCodeMirrorProps } from '.';\n\nexport interface UseCodeMirror extends ReactCodeMirrorProps {\n container?: HTMLDivElement | null;\n}\n\nexport function useCodeMirror(props: UseCodeMirror) {\n const {\n value,\n selection,\n onChange,\n onStatistics,\n onUpdate,\n extensions = [],\n autoFocus,\n theme = 'light',\n height = '',\n minHeight = '',\n maxHeight = '',\n placeholder: placeholderStr = '',\n width = '',\n minWidth = '',\n maxWidth = '',\n editable = true,\n readOnly = false,\n indentWithTab: defaultIndentWithTab = true,\n basicSetup: defaultBasicSetup = true,\n root,\n } = props;\n const [container, setContainer] = useState<HTMLDivElement>();\n const [view, setView] = useState<EditorView>();\n const [state, setState] = useState<EditorState>();\n const defaultLightThemeOption = EditorView.theme(\n {\n '&': {\n backgroundColor: '#fff',\n },\n },\n {\n dark: false,\n },\n );\n const defaultThemeOption = EditorView.theme({\n '&': {\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n },\n });\n const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {\n if (vu.docChanged && typeof onChange === 'function') {\n const doc = vu.state.doc;\n const value = doc.toString();\n onChange(value, vu);\n }\n onStatistics && onStatistics(getStatistics(vu));\n });\n\n let getExtensions = [updateListener, defaultThemeOption];\n if (defaultIndentWithTab) {\n getExtensions.unshift(keymap.of([indentWithTab]));\n }\n if (defaultBasicSetup) {\n if (typeof defaultBasicSetup === 'boolean') {\n getExtensions.unshift(basicSetup());\n } else {\n getExtensions.unshift(basicSetup(defaultBasicSetup));\n }\n }\n\n if (placeholderStr) {\n getExtensions.unshift(placeholder(placeholderStr));\n }\n\n switch (theme) {\n case 'light':\n getExtensions.push(defaultLightThemeOption);\n break;\n case 'dark':\n getExtensions.push(oneDark);\n break;\n default:\n getExtensions.push(theme);\n break;\n }\n\n if (editable === false) {\n getExtensions.push(EditorView.editable.of(false));\n }\n if (readOnly) {\n getExtensions.push(EditorState.readOnly.of(true));\n }\n\n if (onUpdate && typeof onUpdate === 'function') {\n getExtensions.push(EditorView.updateListener.of(onUpdate));\n }\n getExtensions = getExtensions.concat(extensions);\n\n useEffect(() => {\n if (container && !state) {\n const stateCurrent = EditorState.create({\n doc: value,\n selection,\n extensions: getExtensions,\n });\n setState(stateCurrent);\n if (!view) {\n const viewCurrent = new EditorView({\n state: stateCurrent,\n parent: container,\n root,\n });\n setView(viewCurrent);\n }\n }\n return () => {\n if (view) {\n setState(undefined);\n setView(undefined);\n }\n };\n }, [container, state]);\n\n useEffect(() => setContainer(props.container!), [props.container]);\n\n useEffect(\n () => () => {\n if (view) {\n view.destroy();\n setView(undefined);\n }\n },\n [view],\n );\n\n useEffect(() => {\n if (autoFocus && view) {\n view.focus();\n }\n }, [autoFocus, view]);\n\n useEffect(() => {\n if (view) {\n view.dispatch({ effects: StateEffect.reconfigure.of(getExtensions) });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n theme,\n extensions,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n placeholderStr,\n editable,\n readOnly,\n defaultIndentWithTab,\n defaultBasicSetup,\n onChange,\n onUpdate,\n ]);\n\n useEffect(() => {\n const currentValue = view ? view.state.doc.toString() : '';\n if (view && value !== currentValue) {\n view.dispatch({\n changes: { from: 0, to: currentValue.length, insert: value || '' },\n });\n }\n }, [value, view]);\n\n return { state, setState, view, setView, container, setContainer };\n}\n"
|
|
77
79
|
],
|
|
78
|
-
"mappings": "AAAA,SAASA,SAAT,EAAoBC,QAApB,QAAoC,OAApC;AACA,SAASC,WAAT,EAAsBC,WAAtB,QAAyC,mBAAzC;AACA,SAASC,aAAT,QAA8B,sBAA9B;AACA,SAASC,UAAT,EAAqBC,MAArB,EAAyCC,WAAzC,QAA4D,kBAA5D;AACA,SAASC,OAAT,QAAwB,4BAAxB;AACA,SAASC,UAAT,QAA2B,cAA3B;AAOA,OAAO,SAASC,aAAT,CAAuBC,KAAvB,EAA6C;EAClD,IAAM;IACJC,KADI;IAEJC,SAFI;IAGJC,QAHI;IAIJC,
|
|
80
|
+
"mappings": "AAAA,SAASA,SAAT,EAAoBC,QAApB,QAAoC,OAApC;AACA,SAASC,WAAT,EAAsBC,WAAtB,QAAyC,mBAAzC;AACA,SAASC,aAAT,QAA8B,sBAA9B;AACA,SAASC,UAAT,EAAqBC,MAArB,EAAyCC,WAAzC,QAA4D,kBAA5D;AACA,SAASC,OAAT,QAAwB,4BAAxB;AACA,SAASC,UAAT,QAA2B,cAA3B;AACA,SAASC,aAAT,QAA8B,SAA9B;AAOA,OAAO,SAASC,aAAT,CAAuBC,KAAvB,EAA6C;EAClD,IAAM;IACJC,KADI;IAEJC,SAFI;IAGJC,QAHI;IAIJC,YAJI;IAKJC,QALI;IAMJC,UAAU,GAAG,EANT;IAOJC,SAPI;IAQJC,KAAK,GAAG,OARJ;IASJC,MAAM,GAAG,EATL;IAUJC,SAAS,GAAG,EAVR;IAWJC,SAAS,GAAG,EAXR;IAYJhB,WAAW,EAAEiB,cAAc,GAAG,EAZ1B;IAaJC,KAAK,GAAG,EAbJ;IAcJC,QAAQ,GAAG,EAdP;IAeJC,QAAQ,GAAG,EAfP;IAgBJC,QAAQ,GAAG,IAhBP;IAiBJC,QAAQ,GAAG,KAjBP;IAkBJzB,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EAuBAjB,SAAS,CAAC,MAAM;IACd,IAAM+D,YAAY,GAAG5B,IAAI,GAAGA,IAAI,CAACE,KAAL,CAAWU,GAAX,CAAeC,QAAf,EAAH,GAA+B,EAAxD;;IACA,IAAIb,IAAI,IAAItB,KAAK,KAAKkD,YAAtB,EAAoC;MAClC5B,IAAI,CAACyB,QAAL,CAAc;QACZI,OAAO,EAAE;UAAEC,IAAI,EAAE,CAAR;UAAWC,EAAE,EAAEH,YAAY,CAACI,MAA5B;UAAoCC,MAAM,EAAEvD,KAAK,IAAI;QAArD;MADG,CAAd;IAGD;EACF,CAPQ,EAON,CAACA,KAAD,EAAQsB,IAAR,CAPM,CAAT;EASA,OAAO;IAAEE,KAAF;IAASC,QAAT;IAAmBH,IAAnB;IAAyBC,OAAzB;IAAkCH,SAAlC;IAA6CC;EAA7C,CAAP;AACD"
|
|
79
81
|
}
|
package/esm/utils.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EditorSelection, SelectionRange } from '@codemirror/state';
|
|
2
|
+
import { ViewUpdate } from '@codemirror/view';
|
|
3
|
+
export interface Statistics {
|
|
4
|
+
/** Get the number of lines in the editor. */
|
|
5
|
+
lineCount: number;
|
|
6
|
+
/** total length of the document */
|
|
7
|
+
length: number;
|
|
8
|
+
/** Get the proper [line-break](https://codemirror.net/docs/ref/#state.EditorState^lineSeparator) string for this state. */
|
|
9
|
+
lineBreak: string;
|
|
10
|
+
/** Returns true when the editor is [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only. */
|
|
11
|
+
readOnly: boolean;
|
|
12
|
+
/** The size (in columns) of a tab in the document, determined by the [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) facet. */
|
|
13
|
+
tabSize: number;
|
|
14
|
+
/** Cursor Position */
|
|
15
|
+
selection: EditorSelection;
|
|
16
|
+
/** Make sure the selection only has one range. */
|
|
17
|
+
selectionAsSingle: SelectionRange;
|
|
18
|
+
/** Retrieves a list of all current selections. */
|
|
19
|
+
ranges: readonly SelectionRange[];
|
|
20
|
+
/** Get the currently selected code. */
|
|
21
|
+
selectionCode: string;
|
|
22
|
+
/**
|
|
23
|
+
* The length of the given array should be the same as the number of active selections.
|
|
24
|
+
* Replaces the content of the selections with the strings in the array.
|
|
25
|
+
*/
|
|
26
|
+
selections: string[];
|
|
27
|
+
/** Return true if any text is selected. */
|
|
28
|
+
selectedText: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare const getStatistics: (view: ViewUpdate) => Statistics;
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var getStatistics = view => ({
|
|
2
|
+
lineCount: view.state.doc.lines,
|
|
3
|
+
length: view.state.doc.length,
|
|
4
|
+
lineBreak: view.state.lineBreak,
|
|
5
|
+
readOnly: view.state.readOnly,
|
|
6
|
+
tabSize: view.state.tabSize,
|
|
7
|
+
selection: view.state.selection,
|
|
8
|
+
selectionAsSingle: view.state.selection.asSingle().main,
|
|
9
|
+
ranges: view.state.selection.ranges,
|
|
10
|
+
selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to),
|
|
11
|
+
selections: view.state.selection.ranges.map(r => view.state.sliceDoc(r.from, r.to)),
|
|
12
|
+
selectedText: view.state.selection.ranges.some(r => !r.empty)
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=utils.js.map
|
package/esm/utils.js.map
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"names": [
|
|
4
|
+
"getStatistics",
|
|
5
|
+
"view",
|
|
6
|
+
"lineCount",
|
|
7
|
+
"state",
|
|
8
|
+
"doc",
|
|
9
|
+
"lines",
|
|
10
|
+
"length",
|
|
11
|
+
"lineBreak",
|
|
12
|
+
"readOnly",
|
|
13
|
+
"tabSize",
|
|
14
|
+
"selection",
|
|
15
|
+
"selectionAsSingle",
|
|
16
|
+
"asSingle",
|
|
17
|
+
"main",
|
|
18
|
+
"ranges",
|
|
19
|
+
"selectionCode",
|
|
20
|
+
"sliceDoc",
|
|
21
|
+
"from",
|
|
22
|
+
"to",
|
|
23
|
+
"selections",
|
|
24
|
+
"map",
|
|
25
|
+
"r",
|
|
26
|
+
"selectedText",
|
|
27
|
+
"some",
|
|
28
|
+
"empty"
|
|
29
|
+
],
|
|
30
|
+
"sources": [
|
|
31
|
+
"../src/utils.ts"
|
|
32
|
+
],
|
|
33
|
+
"sourcesContent": [
|
|
34
|
+
"import { EditorSelection, SelectionRange } from '@codemirror/state';\nimport { ViewUpdate } from '@codemirror/view';\n\nexport interface Statistics {\n /** Get the number of lines in the editor. */\n lineCount: number;\n /** total length of the document */\n length: number;\n /** Get the proper [line-break](https://codemirror.net/docs/ref/#state.EditorState^lineSeparator) string for this state. */\n lineBreak: string;\n /** Returns true when the editor is [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only. */\n readOnly: boolean;\n /** The size (in columns) of a tab in the document, determined by the [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) facet. */\n tabSize: number;\n /** Cursor Position */\n selection: EditorSelection;\n /** Make sure the selection only has one range. */\n selectionAsSingle: SelectionRange;\n /** Retrieves a list of all current selections. */\n ranges: readonly SelectionRange[];\n /** Get the currently selected code. */\n selectionCode: string;\n /**\n * The length of the given array should be the same as the number of active selections.\n * Replaces the content of the selections with the strings in the array.\n */\n selections: string[];\n /** Return true if any text is selected. */\n selectedText: boolean;\n}\n\nexport const getStatistics = (view: ViewUpdate): Statistics => ({\n lineCount: view.state.doc.lines,\n length: view.state.doc.length,\n lineBreak: view.state.lineBreak,\n readOnly: view.state.readOnly,\n tabSize: view.state.tabSize,\n selection: view.state.selection,\n selectionAsSingle: view.state.selection.asSingle().main,\n ranges: view.state.selection.ranges,\n selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to),\n selections: view.state.selection.ranges.map((r) => view.state.sliceDoc(r.from, r.to)),\n selectedText: view.state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10 +2,12 @@ import React, { useRef, forwardRef, useImperativeHandle } from 'react';
|
|
|
2
2
|
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
|
|
3
3
|
import { EditorView, ViewUpdate } from '@codemirror/view';
|
|
4
4
|
import { useCodeMirror } from './useCodeMirror';
|
|
5
|
+
import { Statistics } from './utils';
|
|
5
6
|
import { BasicSetupOptions } from './basicSetup';
|
|
6
7
|
|
|
7
8
|
export * from './basicSetup';
|
|
8
9
|
export * from './useCodeMirror';
|
|
10
|
+
export * from './utils';
|
|
9
11
|
|
|
10
12
|
export interface ReactCodeMirrorProps
|
|
11
13
|
extends Omit<EditorStateConfig, 'doc' | 'extensions'>,
|
|
@@ -49,6 +51,8 @@ export interface ReactCodeMirrorProps
|
|
|
49
51
|
indentWithTab?: boolean;
|
|
50
52
|
/** Fired whenever a change occurs to the document. */
|
|
51
53
|
onChange?(value: string, viewUpdate: ViewUpdate): void;
|
|
54
|
+
/** Some data on the statistics editor. */
|
|
55
|
+
onStatistics?(data: Statistics): void;
|
|
52
56
|
/** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
|
|
53
57
|
onUpdate?(viewUpdate: ViewUpdate): void;
|
|
54
58
|
/**
|
|
@@ -78,6 +82,7 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
|
|
|
78
82
|
selection,
|
|
79
83
|
extensions = [],
|
|
80
84
|
onChange,
|
|
85
|
+
onStatistics,
|
|
81
86
|
onUpdate,
|
|
82
87
|
autoFocus,
|
|
83
88
|
theme = 'light',
|
|
@@ -115,6 +120,7 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
|
|
|
115
120
|
readOnly,
|
|
116
121
|
selection,
|
|
117
122
|
onChange,
|
|
123
|
+
onStatistics,
|
|
118
124
|
onUpdate,
|
|
119
125
|
extensions,
|
|
120
126
|
});
|
package/src/useCodeMirror.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { indentWithTab } from '@codemirror/commands';
|
|
|
4
4
|
import { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';
|
|
5
5
|
import { oneDark } from '@codemirror/theme-one-dark';
|
|
6
6
|
import { basicSetup } from './basicSetup';
|
|
7
|
+
import { getStatistics } from './utils';
|
|
7
8
|
import { ReactCodeMirrorProps } from '.';
|
|
8
9
|
|
|
9
10
|
export interface UseCodeMirror extends ReactCodeMirrorProps {
|
|
@@ -15,6 +16,7 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
15
16
|
value,
|
|
16
17
|
selection,
|
|
17
18
|
onChange,
|
|
19
|
+
onStatistics,
|
|
18
20
|
onUpdate,
|
|
19
21
|
extensions = [],
|
|
20
22
|
autoFocus,
|
|
@@ -61,6 +63,7 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
61
63
|
const value = doc.toString();
|
|
62
64
|
onChange(value, vu);
|
|
63
65
|
}
|
|
66
|
+
onStatistics && onStatistics(getStatistics(vu));
|
|
64
67
|
});
|
|
65
68
|
|
|
66
69
|
let getExtensions = [updateListener, defaultThemeOption];
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
/** total length of the document */
|
|
8
|
+
length: number;
|
|
9
|
+
/** Get the proper [line-break](https://codemirror.net/docs/ref/#state.EditorState^lineSeparator) string for this state. */
|
|
10
|
+
lineBreak: string;
|
|
11
|
+
/** Returns true when the editor is [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only. */
|
|
12
|
+
readOnly: boolean;
|
|
13
|
+
/** The size (in columns) of a tab in the document, determined by the [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) facet. */
|
|
14
|
+
tabSize: number;
|
|
15
|
+
/** Cursor Position */
|
|
16
|
+
selection: EditorSelection;
|
|
17
|
+
/** Make sure the selection only has one range. */
|
|
18
|
+
selectionAsSingle: SelectionRange;
|
|
19
|
+
/** Retrieves a list of all current selections. */
|
|
20
|
+
ranges: readonly SelectionRange[];
|
|
21
|
+
/** Get the currently selected code. */
|
|
22
|
+
selectionCode: string;
|
|
23
|
+
/**
|
|
24
|
+
* The length of the given array should be the same as the number of active selections.
|
|
25
|
+
* Replaces the content of the selections with the strings in the array.
|
|
26
|
+
*/
|
|
27
|
+
selections: string[];
|
|
28
|
+
/** Return true if any text is selected. */
|
|
29
|
+
selectedText: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const getStatistics = (view: ViewUpdate): Statistics => ({
|
|
33
|
+
lineCount: view.state.doc.lines,
|
|
34
|
+
length: view.state.doc.length,
|
|
35
|
+
lineBreak: view.state.lineBreak,
|
|
36
|
+
readOnly: view.state.readOnly,
|
|
37
|
+
tabSize: view.state.tabSize,
|
|
38
|
+
selection: view.state.selection,
|
|
39
|
+
selectionAsSingle: view.state.selection.asSingle().main,
|
|
40
|
+
ranges: view.state.selection.ranges,
|
|
41
|
+
selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to),
|
|
42
|
+
selections: view.state.selection.ranges.map((r) => view.state.sliceDoc(r.from, r.to)),
|
|
43
|
+
selectedText: view.state.selection.ranges.some((r) => !r.empty),
|
|
44
|
+
});
|