Skip to content

Commit ce026f0

Browse files
committed
commit changes new control MonacoEditor
1 parent 5b3c3ce commit ce026f0

File tree

9 files changed

+76
-89
lines changed

9 files changed

+76
-89
lines changed
2.19 MB
Loading
2.19 MB
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Monaco Editor control
2+
3+
This control is a implementatiopn of Monaco Editor.
4+
5+
Here is an example of the control:
6+
7+
![monacoeditor](../assets/MonacoEditor1.png)
8+
9+
`MonacoEditor` dark theme:
10+
11+
![monacoeditor](../assets/MonacoEditor2.png)
12+
13+
14+
## How to use this control in your solutions
15+
16+
- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../../#getting-started) page for more information about installing the dependency.
17+
- Import the control into your component:
18+
19+
```TypeScript
20+
import { MonacoEditor } from "@pnp/spfx-controls-react/lib/MonacoEditor";
21+
```
22+
23+
- Use the `MonacoEditor` control in your code as follows:
24+
25+
```TypeScript
26+
<MonacoEditor value={defaultValue}
27+
showMiniMap={true}
28+
onValueChange={onValueChange}
29+
language={"javascript"}/>
30+
```
31+
32+
- The `onValueChange` change event returns the upadated code and array with messages of errors on validation and can be implemented as follows:
33+
**this validation is done only to JSON language
34+
35+
```TypeScript
36+
const onValueChange = React.useCallback((newValue: string, validationErrors: string[]): void => {console.log(newValue);} , []);
37+
```
38+
39+
## Implementation
40+
41+
The `MonacoEditor` control can be configured with the following properties:
42+
43+
| Property | Type | Required | Description |
44+
| ---- | ---- | ---- | ---- |
45+
| value | string | yes | default content for editor |
46+
| theme | string | no | theme used by editor , two themes are supported 'vs' and 'vs-dark', default 'vs' |
47+
| readOnly | boolean | no | indecate if editor is in read only mode |
48+
| showLineNumbers | boolean | no | editor show linenumber or not, default : yes|
49+
| onValueChange |(newValue:string, validationErrors:string[]) => void | no | function to get code changes, return an array with validation error in case of language is 'JSON' |
50+
| language |string | yes | editor code language, please see https://microsoft.github.io/monaco-editor/index.html for supported languages|
51+
| jsonDiagnosticsOptions |monaco.languages.json.DiagnosticsOptions | no | define options to JSON validation, please see https://microsoft.github.io/monaco-editor/index.html for more details |
52+
| jscriptDiagnosticsOptions | monaco.languages.typescript.DiagnosticsOptions | no | define options to javascript or typescript validation, please see https://microsoft.github.io/monaco-editor/index.html for more details |
53+
54+

src/controls/monacoEditor/IJscriptDiagnosticsOptions.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/controls/monacoEditor/IJsonDiagnosticsOptions.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/controls/monacoEditor/IMonacoEditorProps.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { IJscriptDiagnosticsOptions } from "./IJscriptDiagnosticsOptions";
2-
import { IJsonDiagnosticsOptions } from "./IJsonDiagnosticsOptions";
1+
32
import * as monaco from "monaco-editor";
43
export enum Elanguages {
54
typescript = 'typescript',
@@ -17,12 +16,12 @@ export enum Elanguages {
1716
}
1817
export interface IMonacoEditorProps {
1918
value: string;
20-
theme: string;
21-
readOnly: boolean;
22-
showLineNumbers: boolean;
23-
showMiniMap: boolean;
24-
onValueChange: (newValue:string, validationErrors:string[]) => void;
25-
language: Elanguages;
19+
theme?: string;
20+
readOnly?: boolean;
21+
showLineNumbers?: boolean;
22+
showMiniMap?: boolean;
23+
onValueChange?: (newValue:string, validationErrors:string[]) => void;
24+
language: string | Elanguages;
2625
jsonDiagnosticsOptions?: monaco.languages.json.DiagnosticsOptions;
2726
jscriptDiagnosticsOptions?: monaco.languages.typescript.DiagnosticsOptions;
2827

src/controls/monacoEditor/MonacoEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ export const MonacoEditor: React.FunctionComponent<IMonacoEditorProps> = (
2222
jsonDiagnosticsOptions,
2323
jscriptDiagnosticsOptions,
2424
} = props || ({} as IMonacoEditorProps);
25+
2526
const containerRef = React.useRef<HTMLDivElement>(null);
2627
const editorRef = React.useRef<any>(null);
2728
const { controlClasses } = useMonacoEditorStyles();
2829
const { monaco, status, error } = useMonaco();
30+
2931
const onDidChangeModelContent = React.useCallback(
3032
(e: any): void => {
3133
if (editorRef.current) {

src/controls/monacoEditor/useMonaco.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const useMonaco = () => {
1212
const [monaco, setMonaco] = useState<Monaco>(undefined);
1313
const [status, setStatus] = useState<EStatus>(EStatus.LOADING);
1414
const [error, setError] = useState<Error>(undefined);
15-
// you can change the source of the monaco files
1615

1716
useEffect(() => {
1817
(async () => {

src/webparts/controlsTest/components/TestControl.tsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1+
import { reactionSlotClassNames } from "@fluentui/react-northstar";
12
import { WebPartContext } from "@microsoft/sp-webpart-base";
2-
import { IBasePickerStyles } from "office-ui-fabric-react/lib/Pickers";
33
import { Stack } from "office-ui-fabric-react/lib/Stack";
44
import * as React from "react";
5-
import { ListItemComments } from "../../../controls/listItemComments";
6-
import { Elanguages, MonacoEditor } from "../../../controls/monacoEditor";
7-
import { PeoplePicker, PrincipalType } from "../../../PeoplePicker";
5+
import { MonacoEditor } from "../../../controls/monacoEditor";
86
export interface ITestControlProps {
97
context: WebPartContext;
108
}
119

1210
export const TestControl: React.FunctionComponent<ITestControlProps> = (
1311
props: React.PropsWithChildren<ITestControlProps>
1412
) => {
13+
const defaultValue = React.useMemo(() => {
14+
return (['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'));
15+
} , []);
16+
17+
const onValueChange = React.useCallback( (newValue: string, validationErrors: string[]): void => {
18+
console.log(newValue);
19+
} , []);
20+
1521
return (
1622
<>
1723
<Stack>
1824
<MonacoEditor
19-
value={""}
20-
theme={"vs"}
21-
readOnly={false}
22-
showLineNumbers={true}
25+
value={defaultValue}
2326
showMiniMap={true}
24-
25-
onValueChange={function (newValue: string, validationErrors: string[]): void {
26-
console.log(newValue);
27-
28-
}}
29-
language={
30-
Elanguages.json
31-
}
27+
onValueChange={onValueChange}
28+
language={"javascript"}
3229
/>
3330
</Stack>
3431
</>

0 commit comments

Comments
 (0)