1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = void 0;
|
7 |
|
8 | var monaco = _interopRequireWildcard(require("monaco-editor/esm/vs/editor/editor.api"));
|
9 |
|
10 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
11 |
|
12 | var _react = _interopRequireDefault(require("react"));
|
13 |
|
14 | var _utils = require("./utils");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
17 |
|
18 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
19 |
|
20 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
21 |
|
22 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
23 |
|
24 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
25 |
|
26 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
27 |
|
28 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
29 |
|
30 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
31 |
|
32 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
33 |
|
34 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
35 |
|
36 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
37 |
|
38 | function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
39 |
|
40 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
41 |
|
42 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
43 |
|
44 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
45 |
|
46 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
47 |
|
48 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
49 |
|
50 | var MonacoEditor = function (_React$Component) {
|
51 | _inherits(MonacoEditor, _React$Component);
|
52 |
|
53 | var _super = _createSuper(MonacoEditor);
|
54 |
|
55 | function MonacoEditor(props) {
|
56 | var _this;
|
57 |
|
58 | _classCallCheck(this, MonacoEditor);
|
59 |
|
60 | _this = _super.call(this, props);
|
61 |
|
62 | _defineProperty(_assertThisInitialized(_this), "assignRef", function (component) {
|
63 | _this.containerElement = component;
|
64 | });
|
65 |
|
66 | _this.containerElement = undefined;
|
67 | return _this;
|
68 | }
|
69 |
|
70 | _createClass(MonacoEditor, [{
|
71 | key: "componentDidMount",
|
72 | value: function componentDidMount() {
|
73 | this.initMonaco();
|
74 | }
|
75 | }, {
|
76 | key: "componentDidUpdate",
|
77 | value: function componentDidUpdate(prevProps) {
|
78 | var _this$props = this.props,
|
79 | value = _this$props.value,
|
80 | language = _this$props.language,
|
81 | theme = _this$props.theme,
|
82 | height = _this$props.height,
|
83 | options = _this$props.options,
|
84 | width = _this$props.width;
|
85 | var editor = this.editor;
|
86 | var model = editor.getModel();
|
87 |
|
88 | if (this.props.value != null && this.props.value !== model.getValue()) {
|
89 | this.__prevent_trigger_change_event = true;
|
90 | this.editor.pushUndoStop();
|
91 | model.pushEditOperations([], [{
|
92 | range: model.getFullModelRange(),
|
93 | text: value
|
94 | }]);
|
95 | this.editor.pushUndoStop();
|
96 | this.__prevent_trigger_change_event = false;
|
97 | }
|
98 |
|
99 | if (prevProps.language !== language) {
|
100 | monaco.editor.setModelLanguage(model, language);
|
101 | }
|
102 |
|
103 | if (prevProps.theme !== theme) {
|
104 | monaco.editor.setTheme(theme);
|
105 | }
|
106 |
|
107 | if (editor && (width !== prevProps.width || height !== prevProps.height)) {
|
108 | editor.layout();
|
109 | }
|
110 |
|
111 | if (prevProps.options !== options) {
|
112 | editor.updateOptions(options);
|
113 | }
|
114 | }
|
115 | }, {
|
116 | key: "componentWillUnmount",
|
117 | value: function componentWillUnmount() {
|
118 | this.destroyMonaco();
|
119 | }
|
120 | }, {
|
121 | key: "destroyMonaco",
|
122 | value: function destroyMonaco() {
|
123 | if (this.editor) {
|
124 | this.editor.dispose();
|
125 | var model = this.editor.getModel();
|
126 |
|
127 | if (model) {
|
128 | model.dispose();
|
129 | }
|
130 | }
|
131 |
|
132 | if (this._subscription) {
|
133 | this._subscription.dispose();
|
134 | }
|
135 | }
|
136 | }, {
|
137 | key: "initMonaco",
|
138 | value: function initMonaco() {
|
139 | var value = this.props.value != null ? this.props.value : this.props.defaultValue;
|
140 | var _this$props2 = this.props,
|
141 | language = _this$props2.language,
|
142 | theme = _this$props2.theme,
|
143 | options = _this$props2.options,
|
144 | overrideServices = _this$props2.overrideServices;
|
145 |
|
146 | if (this.containerElement) {
|
147 |
|
148 | Object.assign(options, this.editorWillMount());
|
149 | this.editor = monaco.editor.create(this.containerElement, _objectSpread({
|
150 | value: value,
|
151 | language: language
|
152 | }, options, {}, theme ? {
|
153 | theme: theme
|
154 | } : {}), overrideServices);
|
155 |
|
156 | this.editorDidMount(this.editor);
|
157 | }
|
158 | }
|
159 | }, {
|
160 | key: "editorWillMount",
|
161 | value: function editorWillMount() {
|
162 | var editorWillMount = this.props.editorWillMount;
|
163 | var options = editorWillMount(monaco);
|
164 | return options || {};
|
165 | }
|
166 | }, {
|
167 | key: "editorDidMount",
|
168 | value: function editorDidMount(editor) {
|
169 | var _this2 = this;
|
170 |
|
171 | this.props.editorDidMount(editor, monaco);
|
172 | this._subscription = editor.onDidChangeModelContent(function (event) {
|
173 | if (!_this2.__prevent_trigger_change_event) {
|
174 | _this2.props.onChange(editor.getValue(), event);
|
175 | }
|
176 | });
|
177 | }
|
178 | }, {
|
179 | key: "render",
|
180 | value: function render() {
|
181 | var _this$props3 = this.props,
|
182 | width = _this$props3.width,
|
183 | height = _this$props3.height;
|
184 | var fixedWidth = (0, _utils.processSize)(width);
|
185 | var fixedHeight = (0, _utils.processSize)(height);
|
186 | var style = {
|
187 | width: fixedWidth,
|
188 | height: fixedHeight
|
189 | };
|
190 | return _react["default"].createElement("div", {
|
191 | ref: this.assignRef,
|
192 | style: style,
|
193 | className: "react-monaco-editor-container"
|
194 | });
|
195 | }
|
196 | }]);
|
197 |
|
198 | return MonacoEditor;
|
199 | }(_react["default"].Component);
|
200 |
|
201 | MonacoEditor.propTypes = {
|
202 | width: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
|
203 | height: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
|
204 | value: _propTypes["default"].string,
|
205 | defaultValue: _propTypes["default"].string,
|
206 | language: _propTypes["default"].string,
|
207 | theme: _propTypes["default"].string,
|
208 | options: _propTypes["default"].object,
|
209 | overrideServices: _propTypes["default"].object,
|
210 | editorDidMount: _propTypes["default"].func,
|
211 | editorWillMount: _propTypes["default"].func,
|
212 | onChange: _propTypes["default"].func
|
213 | };
|
214 | MonacoEditor.defaultProps = {
|
215 | width: "100%",
|
216 | height: "100%",
|
217 | value: null,
|
218 | defaultValue: "",
|
219 | language: "javascript",
|
220 | theme: null,
|
221 | options: {},
|
222 | overrideServices: {},
|
223 | editorDidMount: _utils.noop,
|
224 | editorWillMount: _utils.noop,
|
225 | onChange: _utils.noop
|
226 | };
|
227 | var _default = MonacoEditor;
|
228 | exports["default"] = _default; |
\ | No newline at end of file |