@olenbetong/synergi-react 5.7.0 → 5.8.0
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/es/components/CodeEditor/CodeDiffEditor.d.ts +40 -0
- package/es/components/CodeEditor/CodeDiffEditor.js +138 -0
- package/es/components/CodeEditor/index.d.ts +1 -0
- package/es/components/CodeEditor/index.js +1 -0
- package/package.json +2 -2
- package/src/components/CodeEditor/CodeDiffEditor.tsx +205 -0
- package/src/components/CodeEditor/index.ts +1 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as monaco from "monaco-editor";
|
|
2
|
+
import "./CodeEditor.css";
|
|
3
|
+
export type CodeDiffEditorHandle = {
|
|
4
|
+
/** The Monaco diff editor instance, once mounted. */
|
|
5
|
+
editor: monaco.editor.IStandaloneDiffEditor | null;
|
|
6
|
+
/** Focuses the modified (editable) side. */
|
|
7
|
+
focus(): void;
|
|
8
|
+
};
|
|
9
|
+
export type CodeDiffEditorProps = {
|
|
10
|
+
/** The text compared against (left / removed side). Never editable. */
|
|
11
|
+
original: string;
|
|
12
|
+
/** The current text (right / added side). Controlled: `onChange` reports every edit. */
|
|
13
|
+
value: string;
|
|
14
|
+
onChange?: (value: string) => void;
|
|
15
|
+
/** Monaco language ID for both sides, e.g. `html`, `css`, `json`, `markdown`. */
|
|
16
|
+
language?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Identifies the document. When it changes, both sides get fresh models, so undo history does
|
|
19
|
+
* not leak from one record to the next. Without it, a new `value` is applied as an edit that
|
|
20
|
+
* can be undone.
|
|
21
|
+
*/
|
|
22
|
+
docKey?: string;
|
|
23
|
+
/** Makes the modified side read-only. The original side is always read-only. */
|
|
24
|
+
readOnly?: boolean;
|
|
25
|
+
/** One column with changes inline (default), or two columns side by side. */
|
|
26
|
+
inline?: boolean;
|
|
27
|
+
/** Ctrl/Cmd+S inside the modified side. */
|
|
28
|
+
onSave?: () => void;
|
|
29
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
30
|
+
options?: monaco.editor.IDiffEditorOptions;
|
|
31
|
+
className?: string;
|
|
32
|
+
style?: React.CSSProperties;
|
|
33
|
+
"aria-label"?: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
37
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
38
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
39
|
+
*/
|
|
40
|
+
export declare const CodeDiffEditor: import("react").ForwardRefExoticComponent<CodeDiffEditorProps & import("react").RefAttributes<CodeDiffEditorHandle>>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import * as monaco from "monaco-editor";
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
function diffOptions(inline, readOnly, options) {
|
|
8
|
+
return {
|
|
9
|
+
renderSideBySide: !inline,
|
|
10
|
+
// Monaco otherwise drops to the inline view below 900px, which would ignore `inline={false}`.
|
|
11
|
+
useInlineViewWhenSpaceIsLimited: inline ? undefined : false,
|
|
12
|
+
readOnly,
|
|
13
|
+
...options,
|
|
14
|
+
originalEditable: false,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
19
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
20
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
21
|
+
*/
|
|
22
|
+
export const CodeDiffEditor = forwardRef(function CodeDiffEditor({ original, value, onChange, language = "plaintext", docKey, readOnly = false, inline = true, onSave, options, className, style, "aria-label": ariaLabel, }, ref) {
|
|
23
|
+
let container = useRef(null);
|
|
24
|
+
let editor = useRef(null);
|
|
25
|
+
// Latest callbacks without re-creating the editor.
|
|
26
|
+
let callbacks = useRef({ onChange, onSave });
|
|
27
|
+
callbacks.current = { onChange, onSave };
|
|
28
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
29
|
+
let applyingValue = useRef(false);
|
|
30
|
+
let shownDoc = useRef(undefined);
|
|
31
|
+
let colorScheme = useResolvedColorScheme();
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!container.current)
|
|
34
|
+
return;
|
|
35
|
+
let instance = monaco.editor.createDiffEditor(container.current, {
|
|
36
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
37
|
+
automaticLayout: true,
|
|
38
|
+
minimap: { enabled: false },
|
|
39
|
+
scrollBeyondLastLine: false,
|
|
40
|
+
fixedOverflowWidgets: true,
|
|
41
|
+
renderOverviewRuler: true,
|
|
42
|
+
hideUnchangedRegions: { enabled: true },
|
|
43
|
+
modifiedAriaLabel: ariaLabel,
|
|
44
|
+
...diffOptions(inline, readOnly, options),
|
|
45
|
+
});
|
|
46
|
+
instance.setModel({
|
|
47
|
+
original: monaco.editor.createModel(original, language),
|
|
48
|
+
modified: monaco.editor.createModel(value, language),
|
|
49
|
+
});
|
|
50
|
+
editor.current = instance;
|
|
51
|
+
shownDoc.current = docKey;
|
|
52
|
+
let modifiedEditor = instance.getModifiedEditor();
|
|
53
|
+
let changeListener = modifiedEditor.onDidChangeModelContent(() => {
|
|
54
|
+
if (!applyingValue.current) {
|
|
55
|
+
callbacks.current.onChange?.(modifiedEditor.getValue());
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
modifiedEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
59
|
+
return () => {
|
|
60
|
+
changeListener.dispose();
|
|
61
|
+
// The diff editor does not own its models; dispose them once nothing shows them.
|
|
62
|
+
let models = instance.getModel();
|
|
63
|
+
instance.dispose();
|
|
64
|
+
models?.original.dispose();
|
|
65
|
+
models?.modified.dispose();
|
|
66
|
+
editor.current = null;
|
|
67
|
+
};
|
|
68
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
69
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
}, []);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
let instance = editor.current;
|
|
73
|
+
if (!instance)
|
|
74
|
+
return;
|
|
75
|
+
applyingValue.current = true;
|
|
76
|
+
try {
|
|
77
|
+
if (shownDoc.current !== docKey) {
|
|
78
|
+
// New document: new models, so undo history stays with the document it belongs to.
|
|
79
|
+
let old = instance.getModel();
|
|
80
|
+
instance.setModel({
|
|
81
|
+
original: monaco.editor.createModel(original, language),
|
|
82
|
+
modified: monaco.editor.createModel(value, language),
|
|
83
|
+
});
|
|
84
|
+
old?.original.dispose();
|
|
85
|
+
old?.modified.dispose();
|
|
86
|
+
shownDoc.current = docKey;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
let models = instance.getModel();
|
|
90
|
+
if (!models)
|
|
91
|
+
return;
|
|
92
|
+
// The original side is never edited, so there is no undo history to keep.
|
|
93
|
+
if (models.original.getValue() !== original) {
|
|
94
|
+
models.original.setValue(original);
|
|
95
|
+
}
|
|
96
|
+
if (models.modified.getValue() !== value) {
|
|
97
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
98
|
+
instance
|
|
99
|
+
.getModifiedEditor()
|
|
100
|
+
.executeEdits("external", [{ range: models.modified.getFullModelRange(), text: value }]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
applyingValue.current = false;
|
|
105
|
+
}
|
|
106
|
+
}, [docKey, original, value, language]);
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
let models = editor.current?.getModel();
|
|
109
|
+
if (!models)
|
|
110
|
+
return;
|
|
111
|
+
for (let model of [models.original, models.modified]) {
|
|
112
|
+
if (model.getLanguageId() !== language) {
|
|
113
|
+
monaco.editor.setModelLanguage(model, language);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}, [language]);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
let instance = editor.current;
|
|
119
|
+
if (!instance)
|
|
120
|
+
return;
|
|
121
|
+
instance.updateOptions(diffOptions(inline, readOnly, options));
|
|
122
|
+
instance.getModifiedEditor().updateOptions({ ariaLabel });
|
|
123
|
+
}, [inline, readOnly, ariaLabel, options]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
// The theme is global to every editor on the page.
|
|
126
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
127
|
+
}, [colorScheme]);
|
|
128
|
+
useImperativeHandle(ref, () => ({
|
|
129
|
+
get editor() {
|
|
130
|
+
return editor.current;
|
|
131
|
+
},
|
|
132
|
+
focus() {
|
|
133
|
+
editor.current?.getModifiedEditor().focus();
|
|
134
|
+
},
|
|
135
|
+
}));
|
|
136
|
+
// `monaco-component`: see CodeEditor - Monaco's context menu needs the theme variables it defines on that class.
|
|
137
|
+
return _jsx("div", { ref: container, className: clsx("ObCodeEditor-root", "monaco-component", className), style: style });
|
|
138
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.8.0",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@mui/material": "9.5.0",
|
|
69
69
|
"@olenbetong/appframe-core": "^2.16.2",
|
|
70
70
|
"@olenbetong/appframe-data": "^1.10.3",
|
|
71
|
-
"@olenbetong/appframe-ds": "^1.14.
|
|
71
|
+
"@olenbetong/appframe-ds": "^1.14.2",
|
|
72
72
|
"@olenbetong/appframe-mui": "^7.0.13",
|
|
73
73
|
"@olenbetong/appframe-react": "^2.2.8",
|
|
74
74
|
"clsx": "^2.1.1",
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import clsx from "clsx";
|
|
2
|
+
import * as monaco from "monaco-editor";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* Where Monaco comes from is described in CodeEditor.tsx. The diff editor needs nothing more: the
|
|
10
|
+
* platform's `monaco-editor` entry is the full build, and the diff is computed in the editor worker
|
|
11
|
+
* its `MonacoEnvironment` already serves.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type CodeDiffEditorHandle = {
|
|
15
|
+
/** The Monaco diff editor instance, once mounted. */
|
|
16
|
+
editor: monaco.editor.IStandaloneDiffEditor | null;
|
|
17
|
+
/** Focuses the modified (editable) side. */
|
|
18
|
+
focus(): void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type CodeDiffEditorProps = {
|
|
22
|
+
/** The text compared against (left / removed side). Never editable. */
|
|
23
|
+
original: string;
|
|
24
|
+
/** The current text (right / added side). Controlled: `onChange` reports every edit. */
|
|
25
|
+
value: string;
|
|
26
|
+
onChange?: (value: string) => void;
|
|
27
|
+
/** Monaco language ID for both sides, e.g. `html`, `css`, `json`, `markdown`. */
|
|
28
|
+
language?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Identifies the document. When it changes, both sides get fresh models, so undo history does
|
|
31
|
+
* not leak from one record to the next. Without it, a new `value` is applied as an edit that
|
|
32
|
+
* can be undone.
|
|
33
|
+
*/
|
|
34
|
+
docKey?: string;
|
|
35
|
+
/** Makes the modified side read-only. The original side is always read-only. */
|
|
36
|
+
readOnly?: boolean;
|
|
37
|
+
/** One column with changes inline (default), or two columns side by side. */
|
|
38
|
+
inline?: boolean;
|
|
39
|
+
/** Ctrl/Cmd+S inside the modified side. */
|
|
40
|
+
onSave?: () => void;
|
|
41
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
42
|
+
options?: monaco.editor.IDiffEditorOptions;
|
|
43
|
+
className?: string;
|
|
44
|
+
style?: React.CSSProperties;
|
|
45
|
+
"aria-label"?: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function diffOptions(
|
|
49
|
+
inline: boolean,
|
|
50
|
+
readOnly: boolean,
|
|
51
|
+
options: monaco.editor.IDiffEditorOptions | undefined,
|
|
52
|
+
): monaco.editor.IDiffEditorOptions {
|
|
53
|
+
return {
|
|
54
|
+
renderSideBySide: !inline,
|
|
55
|
+
// Monaco otherwise drops to the inline view below 900px, which would ignore `inline={false}`.
|
|
56
|
+
useInlineViewWhenSpaceIsLimited: inline ? undefined : false,
|
|
57
|
+
readOnly,
|
|
58
|
+
...options,
|
|
59
|
+
originalEditable: false,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
65
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
66
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
67
|
+
*/
|
|
68
|
+
export const CodeDiffEditor = forwardRef<CodeDiffEditorHandle, CodeDiffEditorProps>(function CodeDiffEditor(
|
|
69
|
+
{
|
|
70
|
+
original,
|
|
71
|
+
value,
|
|
72
|
+
onChange,
|
|
73
|
+
language = "plaintext",
|
|
74
|
+
docKey,
|
|
75
|
+
readOnly = false,
|
|
76
|
+
inline = true,
|
|
77
|
+
onSave,
|
|
78
|
+
options,
|
|
79
|
+
className,
|
|
80
|
+
style,
|
|
81
|
+
"aria-label": ariaLabel,
|
|
82
|
+
},
|
|
83
|
+
ref,
|
|
84
|
+
) {
|
|
85
|
+
let container = useRef<HTMLDivElement>(null);
|
|
86
|
+
let editor = useRef<monaco.editor.IStandaloneDiffEditor | null>(null);
|
|
87
|
+
// Latest callbacks without re-creating the editor.
|
|
88
|
+
let callbacks = useRef({ onChange, onSave });
|
|
89
|
+
callbacks.current = { onChange, onSave };
|
|
90
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
91
|
+
let applyingValue = useRef(false);
|
|
92
|
+
let shownDoc = useRef<string | undefined>(undefined);
|
|
93
|
+
let colorScheme = useResolvedColorScheme();
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!container.current) return;
|
|
97
|
+
let instance = monaco.editor.createDiffEditor(container.current, {
|
|
98
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
99
|
+
automaticLayout: true,
|
|
100
|
+
minimap: { enabled: false },
|
|
101
|
+
scrollBeyondLastLine: false,
|
|
102
|
+
fixedOverflowWidgets: true,
|
|
103
|
+
renderOverviewRuler: true,
|
|
104
|
+
hideUnchangedRegions: { enabled: true },
|
|
105
|
+
modifiedAriaLabel: ariaLabel,
|
|
106
|
+
...diffOptions(inline, readOnly, options),
|
|
107
|
+
});
|
|
108
|
+
instance.setModel({
|
|
109
|
+
original: monaco.editor.createModel(original, language),
|
|
110
|
+
modified: monaco.editor.createModel(value, language),
|
|
111
|
+
});
|
|
112
|
+
editor.current = instance;
|
|
113
|
+
shownDoc.current = docKey;
|
|
114
|
+
|
|
115
|
+
let modifiedEditor = instance.getModifiedEditor();
|
|
116
|
+
let changeListener = modifiedEditor.onDidChangeModelContent(() => {
|
|
117
|
+
if (!applyingValue.current) {
|
|
118
|
+
callbacks.current.onChange?.(modifiedEditor.getValue());
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
modifiedEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
changeListener.dispose();
|
|
125
|
+
// The diff editor does not own its models; dispose them once nothing shows them.
|
|
126
|
+
let models = instance.getModel();
|
|
127
|
+
instance.dispose();
|
|
128
|
+
models?.original.dispose();
|
|
129
|
+
models?.modified.dispose();
|
|
130
|
+
editor.current = null;
|
|
131
|
+
};
|
|
132
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
133
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
134
|
+
}, []);
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
let instance = editor.current;
|
|
138
|
+
if (!instance) return;
|
|
139
|
+
|
|
140
|
+
applyingValue.current = true;
|
|
141
|
+
try {
|
|
142
|
+
if (shownDoc.current !== docKey) {
|
|
143
|
+
// New document: new models, so undo history stays with the document it belongs to.
|
|
144
|
+
let old = instance.getModel();
|
|
145
|
+
instance.setModel({
|
|
146
|
+
original: monaco.editor.createModel(original, language),
|
|
147
|
+
modified: monaco.editor.createModel(value, language),
|
|
148
|
+
});
|
|
149
|
+
old?.original.dispose();
|
|
150
|
+
old?.modified.dispose();
|
|
151
|
+
shownDoc.current = docKey;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let models = instance.getModel();
|
|
156
|
+
if (!models) return;
|
|
157
|
+
// The original side is never edited, so there is no undo history to keep.
|
|
158
|
+
if (models.original.getValue() !== original) {
|
|
159
|
+
models.original.setValue(original);
|
|
160
|
+
}
|
|
161
|
+
if (models.modified.getValue() !== value) {
|
|
162
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
163
|
+
instance
|
|
164
|
+
.getModifiedEditor()
|
|
165
|
+
.executeEdits("external", [{ range: models.modified.getFullModelRange(), text: value }]);
|
|
166
|
+
}
|
|
167
|
+
} finally {
|
|
168
|
+
applyingValue.current = false;
|
|
169
|
+
}
|
|
170
|
+
}, [docKey, original, value, language]);
|
|
171
|
+
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
let models = editor.current?.getModel();
|
|
174
|
+
if (!models) return;
|
|
175
|
+
for (let model of [models.original, models.modified]) {
|
|
176
|
+
if (model.getLanguageId() !== language) {
|
|
177
|
+
monaco.editor.setModelLanguage(model, language);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}, [language]);
|
|
181
|
+
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
let instance = editor.current;
|
|
184
|
+
if (!instance) return;
|
|
185
|
+
instance.updateOptions(diffOptions(inline, readOnly, options));
|
|
186
|
+
instance.getModifiedEditor().updateOptions({ ariaLabel });
|
|
187
|
+
}, [inline, readOnly, ariaLabel, options]);
|
|
188
|
+
|
|
189
|
+
useEffect(() => {
|
|
190
|
+
// The theme is global to every editor on the page.
|
|
191
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
192
|
+
}, [colorScheme]);
|
|
193
|
+
|
|
194
|
+
useImperativeHandle(ref, () => ({
|
|
195
|
+
get editor() {
|
|
196
|
+
return editor.current;
|
|
197
|
+
},
|
|
198
|
+
focus() {
|
|
199
|
+
editor.current?.getModifiedEditor().focus();
|
|
200
|
+
},
|
|
201
|
+
}));
|
|
202
|
+
|
|
203
|
+
// `monaco-component`: see CodeEditor - Monaco's context menu needs the theme variables it defines on that class.
|
|
204
|
+
return <div ref={container} className={clsx("ObCodeEditor-root", "monaco-component", className)} style={style} />;
|
|
205
|
+
});
|