@workbench-kit/monaco 0.0.2-prototype.0.2.13 → 0.0.2-prototype.0.2.14
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/package.json +1 -1
- package/src/WorkbenchMonacoDiffEditor.tsx +128 -0
- package/src/index.ts +6 -2
- package/src/monaco-loader.ts +2 -2
- package/src/testing/vitestMock.tsx +43 -0
package/package.json
CHANGED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, type ReactNode } from 'react';
|
|
2
|
+
import type { DiffOnMount } from '@monaco-editor/react';
|
|
3
|
+
import type * as Monaco from 'monaco-editor';
|
|
4
|
+
|
|
5
|
+
import { DiffEditor } from './monaco-loader.js';
|
|
6
|
+
import type { WorkbenchMonaco } from './monaco-loader.js';
|
|
7
|
+
import { monacoThemeForWorkspaceTheme } from './monacoWorkbenchTheme.js';
|
|
8
|
+
import {
|
|
9
|
+
prepareMonacoWorkbenchEditor,
|
|
10
|
+
type WorkbenchMonacoEditorTheme,
|
|
11
|
+
} from './WorkbenchMonacoEditor.js';
|
|
12
|
+
|
|
13
|
+
export interface WorkbenchMonacoDiffEditorProps {
|
|
14
|
+
beforeMount?: ((monacoInstance: WorkbenchMonaco) => void) | undefined;
|
|
15
|
+
className?: string | undefined;
|
|
16
|
+
height?: number | string | undefined;
|
|
17
|
+
language?: string | undefined;
|
|
18
|
+
loading?: ReactNode | undefined;
|
|
19
|
+
modified: string;
|
|
20
|
+
modifiedModelPath?: string | undefined;
|
|
21
|
+
onModifiedChange?: ((value: string) => void) | undefined;
|
|
22
|
+
onMount?: DiffOnMount | undefined;
|
|
23
|
+
options?: Monaco.editor.IDiffEditorConstructionOptions | undefined;
|
|
24
|
+
original: string;
|
|
25
|
+
originalModelPath?: string | undefined;
|
|
26
|
+
readOnly?: boolean | undefined;
|
|
27
|
+
theme?: WorkbenchMonacoEditorTheme | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultDiffEditorOptions: Monaco.editor.IDiffEditorConstructionOptions = {
|
|
31
|
+
automaticLayout: true,
|
|
32
|
+
contextmenu: true,
|
|
33
|
+
fixedOverflowWidgets: true,
|
|
34
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
|
|
35
|
+
fontSize: 13,
|
|
36
|
+
lineHeight: 20,
|
|
37
|
+
glyphMargin: false,
|
|
38
|
+
minimap: { enabled: false },
|
|
39
|
+
overviewRulerBorder: false,
|
|
40
|
+
overviewRulerLanes: 0,
|
|
41
|
+
padding: { bottom: 12, top: 12 },
|
|
42
|
+
renderLineHighlight: 'line',
|
|
43
|
+
scrollBeyondLastLine: false,
|
|
44
|
+
scrollbar: {
|
|
45
|
+
alwaysConsumeMouseWheel: false,
|
|
46
|
+
horizontalScrollbarSize: 10,
|
|
47
|
+
verticalScrollbarSize: 10,
|
|
48
|
+
},
|
|
49
|
+
wordWrap: 'on',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function WorkbenchMonacoDiffEditor({
|
|
53
|
+
beforeMount,
|
|
54
|
+
className,
|
|
55
|
+
height = '100%',
|
|
56
|
+
language = 'plaintext',
|
|
57
|
+
loading = (
|
|
58
|
+
<div className="ui-panel-loading ui-panel-centered-state" role="status" aria-live="polite">
|
|
59
|
+
<i aria-hidden className="codicon codicon-loading codicon-modifier-spin" />
|
|
60
|
+
<span>Loading diff editor...</span>
|
|
61
|
+
</div>
|
|
62
|
+
),
|
|
63
|
+
modified,
|
|
64
|
+
modifiedModelPath,
|
|
65
|
+
onModifiedChange,
|
|
66
|
+
onMount,
|
|
67
|
+
options,
|
|
68
|
+
original,
|
|
69
|
+
originalModelPath,
|
|
70
|
+
readOnly = false,
|
|
71
|
+
theme = 'dark',
|
|
72
|
+
}: WorkbenchMonacoDiffEditorProps) {
|
|
73
|
+
const modifiedChangeDisposableRef = useRef<Monaco.IDisposable | null>(null);
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
return () => {
|
|
77
|
+
modifiedChangeDisposableRef.current?.dispose();
|
|
78
|
+
modifiedChangeDisposableRef.current = null;
|
|
79
|
+
};
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
const handleBeforeMount = useCallback(
|
|
83
|
+
(monacoInstance: WorkbenchMonaco) => {
|
|
84
|
+
prepareMonacoWorkbenchEditor(monacoInstance, theme);
|
|
85
|
+
beforeMount?.(monacoInstance);
|
|
86
|
+
},
|
|
87
|
+
[beforeMount, theme],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const handleMount = useCallback<DiffOnMount>(
|
|
91
|
+
(diffEditor, monacoInstance) => {
|
|
92
|
+
modifiedChangeDisposableRef.current?.dispose();
|
|
93
|
+
modifiedChangeDisposableRef.current = null;
|
|
94
|
+
|
|
95
|
+
if (onModifiedChange && !readOnly) {
|
|
96
|
+
const modifiedEditor = diffEditor.getModifiedEditor();
|
|
97
|
+
modifiedChangeDisposableRef.current = modifiedEditor.onDidChangeModelContent(() => {
|
|
98
|
+
onModifiedChange(modifiedEditor.getValue());
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
onMount?.(diffEditor, monacoInstance);
|
|
103
|
+
},
|
|
104
|
+
[onModifiedChange, onMount, readOnly],
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<DiffEditor
|
|
109
|
+
className={className}
|
|
110
|
+
beforeMount={handleBeforeMount}
|
|
111
|
+
height={height}
|
|
112
|
+
language={language}
|
|
113
|
+
loading={loading}
|
|
114
|
+
modified={modified}
|
|
115
|
+
modifiedModelPath={modifiedModelPath}
|
|
116
|
+
options={{
|
|
117
|
+
...defaultDiffEditorOptions,
|
|
118
|
+
...options,
|
|
119
|
+
originalEditable: options?.originalEditable ?? false,
|
|
120
|
+
readOnly,
|
|
121
|
+
}}
|
|
122
|
+
original={original}
|
|
123
|
+
originalModelPath={originalModelPath}
|
|
124
|
+
theme={monacoThemeForWorkspaceTheme(theme)}
|
|
125
|
+
onMount={handleMount}
|
|
126
|
+
/>
|
|
127
|
+
);
|
|
128
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export type { OnMount } from '@monaco-editor/react';
|
|
1
|
+
export type { DiffOnMount, OnMount } from '@monaco-editor/react';
|
|
2
2
|
export type { IDisposable, editor } from 'monaco-editor';
|
|
3
3
|
export type { WorkbenchMonaco } from './monaco-loader.js';
|
|
4
4
|
|
|
5
|
-
export { Editor, loader, monaco } from './monaco-loader.js';
|
|
5
|
+
export { DiffEditor, Editor, loader, monaco } from './monaco-loader.js';
|
|
6
6
|
export {
|
|
7
7
|
createMonacoWorker,
|
|
8
8
|
installMonacoEnvironment,
|
|
@@ -45,3 +45,7 @@ export {
|
|
|
45
45
|
type WorkbenchMonacoEditorProps,
|
|
46
46
|
type WorkbenchMonacoEditorTheme,
|
|
47
47
|
} from './WorkbenchMonacoEditor.js';
|
|
48
|
+
export {
|
|
49
|
+
WorkbenchMonacoDiffEditor,
|
|
50
|
+
type WorkbenchMonacoDiffEditorProps,
|
|
51
|
+
} from './WorkbenchMonacoDiffEditor.js';
|
package/src/monaco-loader.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import Editor, { loader } from '@monaco-editor/react';
|
|
1
|
+
import Editor, { DiffEditor, loader } from '@monaco-editor/react';
|
|
2
2
|
import * as monaco from 'monaco-editor';
|
|
3
3
|
|
|
4
4
|
loader.config({ monaco });
|
|
5
5
|
|
|
6
6
|
export type WorkbenchMonaco = typeof monaco;
|
|
7
7
|
|
|
8
|
-
export { Editor, loader, monaco };
|
|
8
|
+
export { DiffEditor, Editor, loader, monaco };
|
|
@@ -8,6 +8,15 @@ export interface MockWorkbenchMonacoEditorProps {
|
|
|
8
8
|
value?: string;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export interface MockWorkbenchMonacoDiffEditorProps {
|
|
12
|
+
language?: string;
|
|
13
|
+
modified?: string;
|
|
14
|
+
onModifiedChange?: (value: string) => void;
|
|
15
|
+
original?: string;
|
|
16
|
+
readOnly?: boolean;
|
|
17
|
+
theme?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
11
20
|
export function WorkbenchMonacoEditor({
|
|
12
21
|
language,
|
|
13
22
|
onChange,
|
|
@@ -25,6 +34,37 @@ export function WorkbenchMonacoEditor({
|
|
|
25
34
|
});
|
|
26
35
|
}
|
|
27
36
|
|
|
37
|
+
export function WorkbenchMonacoDiffEditor({
|
|
38
|
+
language,
|
|
39
|
+
modified,
|
|
40
|
+
onModifiedChange,
|
|
41
|
+
original,
|
|
42
|
+
readOnly = false,
|
|
43
|
+
theme = 'dark',
|
|
44
|
+
}: MockWorkbenchMonacoDiffEditorProps) {
|
|
45
|
+
return createElement(
|
|
46
|
+
'div',
|
|
47
|
+
{
|
|
48
|
+
'data-language': language,
|
|
49
|
+
'data-readonly': readOnly ? 'true' : 'false',
|
|
50
|
+
'data-theme': monacoThemeForWorkspaceTheme(theme),
|
|
51
|
+
'data-testid': 'monaco-diff-editor',
|
|
52
|
+
},
|
|
53
|
+
createElement('textarea', {
|
|
54
|
+
'data-side': 'original',
|
|
55
|
+
readOnly: true,
|
|
56
|
+
value: original ?? '',
|
|
57
|
+
}),
|
|
58
|
+
createElement('textarea', {
|
|
59
|
+
'data-side': 'modified',
|
|
60
|
+
readOnly,
|
|
61
|
+
value: modified ?? '',
|
|
62
|
+
onChange: (event: ChangeEvent<HTMLTextAreaElement>) =>
|
|
63
|
+
onModifiedChange?.(event.currentTarget.value),
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
28
68
|
export const useMonacoWorkbenchThemeSync = () => undefined;
|
|
29
69
|
export const prepareMonacoWorkbenchEditor = () => undefined;
|
|
30
70
|
export const defineMonacoWorkbenchTheme = () => undefined;
|
|
@@ -58,6 +98,7 @@ export const monaco = {
|
|
|
58
98
|
};
|
|
59
99
|
|
|
60
100
|
export const Editor = WorkbenchMonacoEditor;
|
|
101
|
+
export const DiffEditor = WorkbenchMonacoDiffEditor;
|
|
61
102
|
export const loader = { config: () => undefined };
|
|
62
103
|
|
|
63
104
|
export function createWorkbenchMonacoMockModule(
|
|
@@ -68,6 +109,8 @@ export function createWorkbenchMonacoMockModule(
|
|
|
68
109
|
|
|
69
110
|
return {
|
|
70
111
|
WorkbenchMonacoEditor: renderEditor ?? defaultRender,
|
|
112
|
+
WorkbenchMonacoDiffEditor,
|
|
113
|
+
DiffEditor: WorkbenchMonacoDiffEditor,
|
|
71
114
|
useMonacoWorkbenchThemeSync: () => undefined,
|
|
72
115
|
prepareMonacoWorkbenchEditor: () => undefined,
|
|
73
116
|
monacoThemeForWorkspaceTheme: (theme: string) => theme,
|