@@ -5,17 +5,21 @@ import {
55} from '@monaco-editor/react' ;
66import { ReactSlot } from '@svelte-preprocess-react/react-slot' ;
77import { sveltify } from '@svelte-preprocess-react/sveltify' ;
8- import React , { useEffect } from 'react' ;
8+ import React , { useEffect , useMemo , useRef } from 'react' ;
99import { useFunction } from '@utils/hooks/useFunction' ;
1010import { Spin } from 'antd' ;
11+ import { isNumber } from 'lodash-es' ;
1112import type { editor , IDisposable } from 'monaco-editor' ;
1213
14+ import { useValueChange } from '../useValueChange' ;
15+
1316import '../monaco-editor.less' ;
1417
1518export interface MonacoDiffEditorProps extends DiffEditorProps {
1619 themeMode ?: string ;
1720 height ?: string | number ;
1821 children ?: React . ReactNode ;
22+ readOnly ?: boolean ;
1923 value ?: string ;
2024 onValueChange : ( value : string | undefined ) => void ;
2125 onChange ?: (
@@ -24,6 +28,7 @@ export interface MonacoDiffEditorProps extends DiffEditorProps {
2428 ) => void ;
2529 onValidate ?: OnValidate ;
2630 afterMount ?: DiffEditorProps [ 'onMount' ] ;
31+ line ?: number ;
2732}
2833
2934export const MonacoDiffEditor = sveltify < MonacoDiffEditorProps , [ 'loading' ] > (
@@ -40,22 +45,35 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
4045 onChange,
4146 onValueChange,
4247 onValidate,
43- value,
48+ value : valueProp ,
4449 modified,
50+ options,
51+ readOnly,
52+ line,
4553 ...props
4654 } ) => {
4755 const beforeMountFunction = useFunction ( beforeMount ) ;
4856 const afterMountFunction = useFunction ( afterMount ) ;
49- const disposablesRef = React . useRef < IDisposable [ ] > ( [ ] ) ;
50-
57+ const disposablesRef = useRef < IDisposable [ ] > ( [ ] ) ;
58+ const editorRef = useRef < editor . IStandaloneDiffEditor | null > ( null ) ;
59+ const [ isEditorReady , setIsEditorReady ] = React . useState ( false ) ;
60+ const [ value , setValue ] = useValueChange ( {
61+ onValueChange,
62+ value : valueProp ,
63+ } ) ;
5164 const handleEditorMount : MonacoDiffEditorProps [ 'onMount' ] = (
5265 editor ,
5366 monaco
5467 ) => {
68+ editorRef . current = editor ;
69+ if ( isNumber ( line ) ) {
70+ editor . revealLine ( line ) ;
71+ }
72+ setIsEditorReady ( true ) ;
5573 const modifiedEditor = editor . getModifiedEditor ( ) ;
5674 const mountDisposable = modifiedEditor . onDidChangeModelContent ( ( e ) => {
5775 const newValue = modifiedEditor . getValue ( ) ;
58- onValueChange ( newValue ) ;
76+ setValue ( newValue ) ;
5977 onChange ?.( newValue , e ) ;
6078 } ) ;
6179
@@ -84,6 +102,13 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
84102 } ) ;
85103 } ;
86104 } , [ ] ) ;
105+
106+ useEffect ( ( ) => {
107+ if ( isEditorReady && isNumber ( line ) ) {
108+ editorRef . current ?. revealLine ( line ) ;
109+ }
110+ } , [ line , isEditorReady ] ) ;
111+
87112 return (
88113 < >
89114 < div style = { { display : 'none' } } > { children } </ div >
@@ -96,6 +121,13 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
96121 >
97122 < DiffEditor
98123 { ...props }
124+ options = { useMemo (
125+ ( ) => ( {
126+ readOnly,
127+ ...( options || { } ) ,
128+ } ) ,
129+ [ options , readOnly ]
130+ ) }
99131 modified = { value || modified }
100132 beforeMount = { beforeMountFunction }
101133 onMount = { ( editor , monaco ) => {
@@ -107,14 +139,12 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
107139 slots . loading ? (
108140 < ReactSlot slot = { slots . loading } />
109141 ) : (
110- props . loading || (
111- < Spin
112- tip = "Editor is Loading..."
113- wrapperClassName = "ms-gr-pro-monaco-editor-spin"
114- >
115- < div />
116- </ Spin >
117- )
142+ < Spin
143+ tip = { props . loading }
144+ wrapperClassName = "ms-gr-pro-monaco-editor-spin"
145+ >
146+ < div />
147+ </ Spin >
118148 )
119149 }
120150 theme = { themeMode === 'dark' ? 'vs-dark' : 'light' }
0 commit comments