1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = function (d, b) {
|
3 | extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6 | return extendStatics(d, b);
|
7 | };
|
8 | return function (d, b) {
|
9 | if (typeof b !== "function" && b !== null)
|
10 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11 | extendStatics(d, b);
|
12 | function __() { this.constructor = d; }
|
13 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14 | };
|
15 | })();
|
16 | var __assign = (this && this.__assign) || function () {
|
17 | __assign = Object.assign || function(t) {
|
18 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
19 | s = arguments[i];
|
20 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
21 | t[p] = s[p];
|
22 | }
|
23 | return t;
|
24 | };
|
25 | return __assign.apply(this, arguments);
|
26 | };
|
27 | var __rest = (this && this.__rest) || function (s, e) {
|
28 | var t = {};
|
29 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
30 | t[p] = s[p];
|
31 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
32 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
33 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
34 | t[p[i]] = s[p[i]];
|
35 | }
|
36 | return t;
|
37 | };
|
38 | import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
|
39 | import * as PropTypes from "prop-types";
|
40 | import * as React from "react";
|
41 | import { noop, processSize } from "./utils";
|
42 | var MonacoEditor = (function (_super) {
|
43 | __extends(MonacoEditor, _super);
|
44 | function MonacoEditor(props) {
|
45 | var _this = _super.call(this, props) || this;
|
46 | _this.assignRef = function (component) {
|
47 | _this.containerElement = component;
|
48 | };
|
49 | _this.containerElement = undefined;
|
50 | return _this;
|
51 | }
|
52 | MonacoEditor.prototype.componentDidMount = function () {
|
53 | this.initMonaco();
|
54 | };
|
55 | MonacoEditor.prototype.componentDidUpdate = function (prevProps) {
|
56 | var _a = this.props, value = _a.value, language = _a.language, theme = _a.theme, height = _a.height, options = _a.options, width = _a.width, className = _a.className;
|
57 | var editor = this.editor;
|
58 | var model = editor.getModel();
|
59 | if (this.props.value != null && this.props.value !== model.getValue()) {
|
60 | this.__prevent_trigger_change_event = true;
|
61 | this.editor.pushUndoStop();
|
62 |
|
63 |
|
64 | model.pushEditOperations([], [
|
65 | {
|
66 | range: model.getFullModelRange(),
|
67 | text: value,
|
68 | },
|
69 | ]);
|
70 | this.editor.pushUndoStop();
|
71 | this.__prevent_trigger_change_event = false;
|
72 | }
|
73 | if (prevProps.language !== language) {
|
74 | monaco.editor.setModelLanguage(model, language);
|
75 | }
|
76 | if (prevProps.theme !== theme) {
|
77 | monaco.editor.setTheme(theme);
|
78 | }
|
79 | if (editor && (width !== prevProps.width || height !== prevProps.height)) {
|
80 | editor.layout();
|
81 | }
|
82 | if (prevProps.options !== options) {
|
83 |
|
84 |
|
85 | var _model = options.model, optionsWithoutModel = __rest(options, ["model"]);
|
86 | editor.updateOptions(__assign(__assign({}, (className ? { extraEditorClassName: className } : {})), optionsWithoutModel));
|
87 | }
|
88 | };
|
89 | MonacoEditor.prototype.componentWillUnmount = function () {
|
90 | this.destroyMonaco();
|
91 | };
|
92 | MonacoEditor.prototype.destroyMonaco = function () {
|
93 | if (this.editor) {
|
94 | this.editorWillUnmount(this.editor);
|
95 | this.editor.dispose();
|
96 | var model = this.editor.getModel();
|
97 | if (model) {
|
98 | model.dispose();
|
99 | }
|
100 | }
|
101 | if (this._subscription) {
|
102 | this._subscription.dispose();
|
103 | }
|
104 | };
|
105 | MonacoEditor.prototype.initMonaco = function () {
|
106 | var value = this.props.value != null ? this.props.value : this.props.defaultValue;
|
107 | var _a = this.props, language = _a.language, theme = _a.theme, overrideServices = _a.overrideServices, className = _a.className;
|
108 | if (this.containerElement) {
|
109 |
|
110 | var options = __assign(__assign({}, this.props.options), this.editorWillMount());
|
111 | this.editor = monaco.editor.create(this.containerElement, __assign(__assign(__assign({ value: value, language: language }, (className ? { extraEditorClassName: className } : {})), options), (theme ? { theme: theme } : {})), overrideServices);
|
112 |
|
113 | this.editorDidMount(this.editor);
|
114 | }
|
115 | };
|
116 | MonacoEditor.prototype.editorWillMount = function () {
|
117 | var editorWillMount = this.props.editorWillMount;
|
118 | var options = editorWillMount(monaco);
|
119 | return options || {};
|
120 | };
|
121 | MonacoEditor.prototype.editorDidMount = function (editor) {
|
122 | var _this = this;
|
123 | this.props.editorDidMount(editor, monaco);
|
124 | this._subscription = editor.onDidChangeModelContent(function (event) {
|
125 | if (!_this.__prevent_trigger_change_event) {
|
126 | _this.props.onChange(editor.getValue(), event);
|
127 | }
|
128 | });
|
129 | };
|
130 | MonacoEditor.prototype.editorWillUnmount = function (editor) {
|
131 | var editorWillUnmount = this.props.editorWillUnmount;
|
132 | editorWillUnmount(editor, monaco);
|
133 | };
|
134 | MonacoEditor.prototype.render = function () {
|
135 | var _a = this.props, width = _a.width, height = _a.height;
|
136 | var fixedWidth = processSize(width);
|
137 | var fixedHeight = processSize(height);
|
138 | var style = {
|
139 | width: fixedWidth,
|
140 | height: fixedHeight,
|
141 | };
|
142 | return (React.createElement("div", { ref: this.assignRef, style: style, className: "react-monaco-editor-container" }));
|
143 | };
|
144 | MonacoEditor.propTypes = {
|
145 | width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
146 | height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
147 | value: PropTypes.string,
|
148 | defaultValue: PropTypes.string,
|
149 | language: PropTypes.string,
|
150 | theme: PropTypes.string,
|
151 | options: PropTypes.object,
|
152 | overrideServices: PropTypes.object,
|
153 | editorWillMount: PropTypes.func,
|
154 | editorDidMount: PropTypes.func,
|
155 | editorWillUnmount: PropTypes.func,
|
156 | onChange: PropTypes.func,
|
157 | className: PropTypes.string,
|
158 | };
|
159 | MonacoEditor.defaultProps = {
|
160 | width: "100%",
|
161 | height: "100%",
|
162 | value: null,
|
163 | defaultValue: "",
|
164 | language: "javascript",
|
165 | theme: null,
|
166 | options: {},
|
167 | overrideServices: {},
|
168 | editorWillMount: noop,
|
169 | editorDidMount: noop,
|
170 | editorWillUnmount: noop,
|
171 | onChange: noop,
|
172 | className: null,
|
173 | };
|
174 | return MonacoEditor;
|
175 | }(React.Component));
|
176 | export default MonacoEditor;
|
177 |
|
\ | No newline at end of file |