UNPKG

7.61 kBJavaScriptView Raw
1var __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})();
16var __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};
27import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
28import * as PropTypes from "prop-types";
29import * as React from "react";
30import { noop, processSize } from "./utils";
31var MonacoDiffEditor = /** @class */ (function (_super) {
32 __extends(MonacoDiffEditor, _super);
33 function MonacoDiffEditor(props) {
34 var _this = _super.call(this, props) || this;
35 _this.assignRef = function (component) {
36 _this.containerElement = component;
37 };
38 _this.containerElement = undefined;
39 return _this;
40 }
41 MonacoDiffEditor.prototype.componentDidMount = function () {
42 this.initMonaco();
43 };
44 MonacoDiffEditor.prototype.componentDidUpdate = function (prevProps) {
45 var _a = this.props, language = _a.language, theme = _a.theme, height = _a.height, options = _a.options, width = _a.width, className = _a.className;
46 var _b = this.editor.getModel(), original = _b.original, modified = _b.modified;
47 if (this.props.original !== original.getValue()) {
48 original.setValue(this.props.original);
49 }
50 if (this.props.value != null && this.props.value !== modified.getValue()) {
51 this.__prevent_trigger_change_event = true;
52 // modifiedEditor is not in the public API for diff editors
53 this.editor.getModifiedEditor().pushUndoStop();
54 // pushEditOperations says it expects a cursorComputer, but doesn't seem to need one.
55 // @ts-expect-error
56 modified.pushEditOperations([], [
57 {
58 range: modified.getFullModelRange(),
59 text: this.props.value,
60 },
61 ]);
62 // modifiedEditor is not in the public API for diff editors
63 this.editor.getModifiedEditor().pushUndoStop();
64 this.__prevent_trigger_change_event = false;
65 }
66 if (prevProps.language !== language) {
67 monaco.editor.setModelLanguage(original, language);
68 monaco.editor.setModelLanguage(modified, language);
69 }
70 if (prevProps.theme !== theme) {
71 monaco.editor.setTheme(theme);
72 }
73 if (this.editor &&
74 (width !== prevProps.width || height !== prevProps.height)) {
75 this.editor.layout();
76 }
77 if (prevProps.options !== options) {
78 this.editor.updateOptions(__assign(__assign({}, (className ? { extraEditorClassName: className } : {})), options));
79 }
80 };
81 MonacoDiffEditor.prototype.componentWillUnmount = function () {
82 this.destroyMonaco();
83 };
84 MonacoDiffEditor.prototype.editorWillMount = function () {
85 var editorWillMount = this.props.editorWillMount;
86 var options = editorWillMount(monaco);
87 return options || {};
88 };
89 MonacoDiffEditor.prototype.editorDidMount = function (editor) {
90 var _this = this;
91 this.props.editorDidMount(editor, monaco);
92 var modified = editor.getModel().modified;
93 this._subscription = modified.onDidChangeContent(function (event) {
94 if (!_this.__prevent_trigger_change_event) {
95 _this.props.onChange(modified.getValue(), event);
96 }
97 });
98 };
99 MonacoDiffEditor.prototype.initModels = function (value, original) {
100 var language = this.props.language;
101 var originalModel = monaco.editor.createModel(original, language);
102 var modifiedModel = monaco.editor.createModel(value, language);
103 this.editor.setModel({
104 original: originalModel,
105 modified: modifiedModel,
106 });
107 };
108 MonacoDiffEditor.prototype.initMonaco = function () {
109 var value = this.props.value != null ? this.props.value : this.props.defaultValue;
110 var _a = this.props, original = _a.original, theme = _a.theme, options = _a.options, overrideServices = _a.overrideServices, className = _a.className;
111 if (this.containerElement) {
112 // Before initializing monaco editor
113 this.editorWillMount();
114 this.editor = monaco.editor.createDiffEditor(this.containerElement, __assign(__assign(__assign({}, (className ? { extraEditorClassName: className } : {})), options), (theme ? { theme: theme } : {})), overrideServices);
115 // After initializing monaco editor
116 this.initModels(value, original);
117 this.editorDidMount(this.editor);
118 }
119 };
120 MonacoDiffEditor.prototype.destroyMonaco = function () {
121 if (this.editor) {
122 this.editor.dispose();
123 var _a = this.editor.getModel(), original = _a.original, modified = _a.modified;
124 if (original) {
125 original.dispose();
126 }
127 if (modified) {
128 modified.dispose();
129 }
130 }
131 if (this._subscription) {
132 this._subscription.dispose();
133 }
134 };
135 MonacoDiffEditor.prototype.render = function () {
136 var _a = this.props, width = _a.width, height = _a.height;
137 var fixedWidth = processSize(width);
138 var fixedHeight = processSize(height);
139 var style = {
140 width: fixedWidth,
141 height: fixedHeight,
142 };
143 return (React.createElement("div", { ref: this.assignRef, style: style, className: "react-monaco-editor-container" }));
144 };
145 MonacoDiffEditor.propTypes = {
146 width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
147 height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
148 original: PropTypes.string,
149 value: PropTypes.string,
150 defaultValue: PropTypes.string,
151 language: PropTypes.string,
152 theme: PropTypes.string,
153 options: PropTypes.object,
154 overrideServices: PropTypes.object,
155 editorDidMount: PropTypes.func,
156 editorWillMount: PropTypes.func,
157 onChange: PropTypes.func,
158 className: PropTypes.string,
159 };
160 MonacoDiffEditor.defaultProps = {
161 width: "100%",
162 height: "100%",
163 original: null,
164 value: null,
165 defaultValue: "",
166 language: "javascript",
167 theme: null,
168 options: {},
169 overrideServices: {},
170 editorDidMount: noop,
171 editorWillMount: noop,
172 onChange: noop,
173 className: null,
174 };
175 return MonacoDiffEditor;
176}(React.Component));
177export default MonacoDiffEditor;
178//# sourceMappingURL=diff.js.map
\No newline at end of file