UNPKG

20.7 kBJavaScriptView Raw
1'use strict';
2
3function _extends() {
4 _extends = Object.assign ? Object.assign.bind() : function(target) {
5 for (var i = 1; i < arguments.length; i++) {
6 var source = arguments[i];
7 for (var key in source) {
8 if (Object.prototype.hasOwnProperty.call(source, key)) {
9 target[key] = source[key];
10 }
11 }
12 }
13 return target;
14 };
15 return _extends.apply(this, arguments);
16}
17
18function _typeof(o) {
19 "@babel/helpers - typeof";
20 return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
21 return typeof o;
22 } : function(o) {
23 return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
24 }, _typeof(o);
25}
26var __extends = void 0 && (void 0).__extends || function() {
27 var _extendStatics = function extendStatics(d, b) {
28 _extendStatics = Object.setPrototypeOf || {
29 __proto__: []
30 }
31 instanceof Array && function(d, b) {
32 d.__proto__ = b;
33 } || function(d, b) {
34 for (var p in b)
35 if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
36 };
37 return _extendStatics(d, b);
38 };
39 return function(d, b) {
40 if (typeof b !== 'function' && b !== null) throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
41 _extendStatics(d, b);
42
43 function __() {
44 this.constructor = d;
45 }
46 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
47 };
48}();
49Object.defineProperty(exports, '__esModule', {
50 value: true
51});
52exports.UnControlled = exports.Controlled = void 0;
53var React = require('react');
54var SERVER_RENDERED = typeof navigator === 'undefined' || typeof global !== 'undefined' && global['PREVENT_CODEMIRROR_RENDER'] === true;
55var cm;
56if (!SERVER_RENDERED) {
57 cm = require('codemirror');
58}
59var Helper = function() {
60 function Helper() {}
61 Helper.equals = function(x, y) {
62 var _this = this;
63 var ok = Object.keys,
64 tx = _typeof(x),
65 ty = _typeof(y);
66 return x && y && tx === 'object' && tx === ty ? ok(x).length === ok(y).length && ok(x).every(function(key) {
67 return _this.equals(x[key], y[key]);
68 }) : x === y;
69 };
70 return Helper;
71}();
72var Shared = function() {
73 function Shared(editor, props) {
74 this.editor = editor;
75 this.props = props;
76 }
77 Shared.prototype.delegateCursor = function(position, scroll, focus) {
78 var doc = this.editor.getDoc();
79 if (focus) {
80 this.editor.focus();
81 }
82 scroll ? doc.setCursor(position) : doc.setCursor(position, null, {
83 scroll: false
84 });
85 };
86 Shared.prototype.delegateScroll = function(coordinates) {
87 this.editor.scrollTo(coordinates.x, coordinates.y);
88 };
89 Shared.prototype.delegateSelection = function(ranges, focus) {
90 var doc = this.editor.getDoc();
91 doc.setSelections(ranges);
92 if (focus) {
93 this.editor.focus();
94 }
95 };
96 Shared.prototype.apply = function(props) {
97 if (props && props.selection && props.selection.ranges) {
98 this.delegateSelection(props.selection.ranges, props.selection.focus || false);
99 }
100 if (props && props.cursor) {
101 this.delegateCursor(props.cursor, props.autoScroll || false, this.editor.getOption('autofocus') || false);
102 }
103 if (props && props.scroll) {
104 this.delegateScroll(props.scroll);
105 }
106 };
107 Shared.prototype.applyNext = function(props, next, preserved) {
108 if (props && props.selection && props.selection.ranges) {
109 if (next && next.selection && next.selection.ranges && !Helper.equals(props.selection.ranges, next.selection.ranges)) {
110 this.delegateSelection(next.selection.ranges, next.selection.focus || false);
111 }
112 }
113 if (props && props.cursor) {
114 if (next && next.cursor && !Helper.equals(props.cursor, next.cursor)) {
115 this.delegateCursor(preserved.cursor || next.cursor, next.autoScroll || false, next.autoCursor || false);
116 }
117 }
118 if (props && props.scroll) {
119 if (next && next.scroll && !Helper.equals(props.scroll, next.scroll)) {
120 this.delegateScroll(next.scroll);
121 }
122 }
123 };
124 Shared.prototype.applyUserDefined = function(props, preserved) {
125 if (preserved && preserved.cursor) {
126 this.delegateCursor(preserved.cursor, props.autoScroll || false, this.editor.getOption('autofocus') || false);
127 }
128 };
129 Shared.prototype.wire = function(props) {
130 var _this = this;
131 Object.keys(props || {}).filter(function(p) {
132 return /^on/.test(p);
133 }).forEach(function(prop) {
134 switch (prop) {
135 case 'onBlur': {
136 _this.editor.on('blur', function(cm, event) {
137 _this.props.onBlur(_this.editor, event);
138 });
139 }
140 break;
141 case 'onContextMenu': {
142 _this.editor.on('contextmenu', function(cm, event) {
143 _this.props.onContextMenu(_this.editor, event);
144 });
145 break;
146 }
147 case 'onCopy': {
148 _this.editor.on('copy', function(cm, event) {
149 _this.props.onCopy(_this.editor, event);
150 });
151 break;
152 }
153 case 'onCursor': {
154 _this.editor.on('cursorActivity', function(cm) {
155 _this.props.onCursor(_this.editor, _this.editor.getDoc().getCursor());
156 });
157 }
158 break;
159 case 'onCursorActivity': {
160 _this.editor.on('cursorActivity', function(cm) {
161 _this.props.onCursorActivity(_this.editor);
162 });
163 }
164 break;
165 case 'onCut': {
166 _this.editor.on('cut', function(cm, event) {
167 _this.props.onCut(_this.editor, event);
168 });
169 break;
170 }
171 case 'onDblClick': {
172 _this.editor.on('dblclick', function(cm, event) {
173 _this.props.onDblClick(_this.editor, event);
174 });
175 break;
176 }
177 case 'onDragEnter': {
178 _this.editor.on('dragenter', function(cm, event) {
179 _this.props.onDragEnter(_this.editor, event);
180 });
181 }
182 break;
183 case 'onDragLeave': {
184 _this.editor.on('dragleave', function(cm, event) {
185 _this.props.onDragLeave(_this.editor, event);
186 });
187 break;
188 }
189 case 'onDragOver': {
190 _this.editor.on('dragover', function(cm, event) {
191 _this.props.onDragOver(_this.editor, event);
192 });
193 }
194 break;
195 case 'onDragStart': {
196 _this.editor.on('dragstart', function(cm, event) {
197 _this.props.onDragStart(_this.editor, event);
198 });
199 break;
200 }
201 case 'onDrop': {
202 _this.editor.on('drop', function(cm, event) {
203 _this.props.onDrop(_this.editor, event);
204 });
205 }
206 break;
207 case 'onFocus': {
208 _this.editor.on('focus', function(cm, event) {
209 _this.props.onFocus(_this.editor, event);
210 });
211 }
212 break;
213 case 'onGutterClick': {
214 _this.editor.on('gutterClick', function(cm, lineNumber, gutter, event) {
215 _this.props.onGutterClick(_this.editor, lineNumber, gutter, event);
216 });
217 }
218 break;
219 case 'onInputRead': {
220 _this.editor.on('inputRead', function(cm, EditorChangeEvent) {
221 _this.props.onInputRead(_this.editor, EditorChangeEvent);
222 });
223 }
224 break;
225 case 'onKeyDown': {
226 _this.editor.on('keydown', function(cm, event) {
227 _this.props.onKeyDown(_this.editor, event);
228 });
229 }
230 break;
231 case 'onKeyHandled': {
232 _this.editor.on('keyHandled', function(cm, key, event) {
233 _this.props.onKeyHandled(_this.editor, key, event);
234 });
235 }
236 break;
237 case 'onKeyPress': {
238 _this.editor.on('keypress', function(cm, event) {
239 _this.props.onKeyPress(_this.editor, event);
240 });
241 }
242 break;
243 case 'onKeyUp': {
244 _this.editor.on('keyup', function(cm, event) {
245 _this.props.onKeyUp(_this.editor, event);
246 });
247 }
248 break;
249 case 'onMouseDown': {
250 _this.editor.on('mousedown', function(cm, event) {
251 _this.props.onMouseDown(_this.editor, event);
252 });
253 break;
254 }
255 case 'onPaste': {
256 _this.editor.on('paste', function(cm, event) {
257 _this.props.onPaste(_this.editor, event);
258 });
259 break;
260 }
261 case 'onRenderLine': {
262 _this.editor.on('renderLine', function(cm, line, element) {
263 _this.props.onRenderLine(_this.editor, line, element);
264 });
265 break;
266 }
267 case 'onScroll': {
268 _this.editor.on('scroll', function(cm) {
269 _this.props.onScroll(_this.editor, _this.editor.getScrollInfo());
270 });
271 }
272 break;
273 case 'onSelection': {
274 _this.editor.on('beforeSelectionChange', function(cm, data) {
275 _this.props.onSelection(_this.editor, data);
276 });
277 }
278 break;
279 case 'onTouchStart': {
280 _this.editor.on('touchstart', function(cm, event) {
281 _this.props.onTouchStart(_this.editor, event);
282 });
283 break;
284 }
285 case 'onUpdate': {
286 _this.editor.on('update', function(cm) {
287 _this.props.onUpdate(_this.editor);
288 });
289 }
290 break;
291 case 'onViewportChange': {
292 _this.editor.on('viewportChange', function(cm, from, to) {
293 _this.props.onViewportChange(_this.editor, from, to);
294 });
295 }
296 break;
297 }
298 });
299 };
300 return Shared;
301}();
302var Controlled = function(_super) {
303 __extends(Controlled, _super);
304
305 function Controlled(props) {
306 var _this = _super.call(this, props) || this;
307 if (SERVER_RENDERED) return _this;
308 _this.applied = false;
309 _this.appliedNext = false;
310 _this.appliedUserDefined = false;
311 _this.deferred = null;
312 _this.emulating = false;
313 _this.hydrated = false;
314 _this.initCb = function() {
315 if (_this.props.editorDidConfigure) {
316 _this.props.editorDidConfigure(_this.editor);
317 }
318 };
319 _this.mounted = false;
320 return _this;
321 }
322 Controlled.prototype.hydrate = function(props) {
323 var _this = this;
324 var _options = props && props.options ? props.options : {};
325 var userDefinedOptions = _extends({}, cm.defaults, this.editor.options, _options);
326 var optionDelta = Object.keys(userDefinedOptions).some(function(key) {
327 return _this.editor.getOption(key) !== userDefinedOptions[key];
328 });
329 if (optionDelta) {
330 Object.keys(userDefinedOptions).forEach(function(key) {
331 if (_options.hasOwnProperty(key)) {
332 if (_this.editor.getOption(key) !== userDefinedOptions[key]) {
333 _this.editor.setOption(key, userDefinedOptions[key]);
334 _this.mirror.setOption(key, userDefinedOptions[key]);
335 }
336 }
337 });
338 }
339 if (!this.hydrated) {
340 this.deferred ? this.resolveChange(props.value) : this.initChange(props.value || '');
341 }
342 this.hydrated = true;
343 };
344 Controlled.prototype.initChange = function(value) {
345 this.emulating = true;
346 var doc = this.editor.getDoc();
347 var lastLine = doc.lastLine();
348 var lastChar = doc.getLine(doc.lastLine()).length;
349 doc.replaceRange(value || '', {
350 line: 0,
351 ch: 0
352 }, {
353 line: lastLine,
354 ch: lastChar
355 });
356 this.mirror.setValue(value);
357 doc.clearHistory();
358 this.mirror.clearHistory();
359 this.emulating = false;
360 };
361 Controlled.prototype.resolveChange = function(value) {
362 this.emulating = true;
363 var doc = this.editor.getDoc();
364 if (this.deferred.origin === 'undo') {
365 doc.undo();
366 } else if (this.deferred.origin === 'redo') {
367 doc.redo();
368 } else {
369 doc.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
370 }
371 if (value && value !== doc.getValue()) {
372 var cursor = doc.getCursor();
373 doc.setValue(value);
374 doc.setCursor(cursor);
375 }
376 this.emulating = false;
377 this.deferred = null;
378 };
379 Controlled.prototype.mirrorChange = function(deferred) {
380 var doc = this.editor.getDoc();
381 if (deferred.origin === 'undo') {
382 doc.setHistory(this.mirror.getHistory());
383 this.mirror.undo();
384 } else if (deferred.origin === 'redo') {
385 doc.setHistory(this.mirror.getHistory());
386 this.mirror.redo();
387 } else {
388 this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
389 }
390 return this.mirror.getValue();
391 };
392 Controlled.prototype.componentDidMount = function() {
393 var _this = this;
394 if (SERVER_RENDERED) return;
395 if (this.props.defineMode) {
396 if (this.props.defineMode.name && this.props.defineMode.fn) {
397 cm.defineMode(this.props.defineMode.name, this.props.defineMode.fn);
398 }
399 }
400 this.editor = cm(this.ref, this.props.options);
401 this.shared = new Shared(this.editor, this.props);
402 this.mirror = cm(function() {}, this.props.options);
403 this.editor.on('electricInput', function() {
404 _this.mirror.setHistory(_this.editor.getDoc().getHistory());
405 });
406 this.editor.on('cursorActivity', function() {
407 _this.mirror.setCursor(_this.editor.getDoc().getCursor());
408 });
409 this.editor.on('beforeChange', function(cm, data) {
410 if (_this.emulating) {
411 return;
412 }
413 data.cancel();
414 _this.deferred = data;
415 var phantomChange = _this.mirrorChange(_this.deferred);
416 if (_this.props.onBeforeChange) _this.props.onBeforeChange(_this.editor, _this.deferred, phantomChange);
417 });
418 this.editor.on('change', function(cm, data) {
419 if (!_this.mounted) {
420 return;
421 }
422 if (_this.props.onChange) {
423 _this.props.onChange(_this.editor, data, _this.editor.getValue());
424 }
425 });
426 this.hydrate(this.props);
427 this.shared.apply(this.props);
428 this.applied = true;
429 this.mounted = true;
430 this.shared.wire(this.props);
431 if (this.editor.getOption('autofocus')) {
432 this.editor.focus();
433 }
434 if (this.props.editorDidMount) {
435 this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
436 }
437 };
438 Controlled.prototype.componentDidUpdate = function(prevProps) {
439 if (SERVER_RENDERED) return;
440 var preserved = {
441 cursor: null
442 };
443 if (this.props.value !== prevProps.value) {
444 this.hydrated = false;
445 }
446 if (!this.props.autoCursor && this.props.autoCursor !== undefined) {
447 preserved.cursor = this.editor.getDoc().getCursor();
448 }
449 this.hydrate(this.props);
450 if (!this.appliedNext) {
451 this.shared.applyNext(prevProps, this.props, preserved);
452 this.appliedNext = true;
453 }
454 this.shared.applyUserDefined(prevProps, preserved);
455 this.appliedUserDefined = true;
456 };
457 Controlled.prototype.componentWillUnmount = function() {
458 if (SERVER_RENDERED) return;
459 if (this.props.editorWillUnmount) {
460 this.props.editorWillUnmount(cm);
461 }
462 };
463 Controlled.prototype.shouldComponentUpdate = function(nextProps, nextState) {
464 return !SERVER_RENDERED;
465 };
466 Controlled.prototype.render = function() {
467 var _this = this;
468 if (SERVER_RENDERED) return null;
469 var className = this.props.className ? 'react-codemirror2 '.concat(this.props.className) : 'react-codemirror2';
470 return React.createElement('div', {
471 className: className,
472 ref: function ref(self) {
473 return _this.ref = self;
474 }
475 });
476 };
477 return Controlled;
478}(React.Component);
479exports.Controlled = Controlled;
480var UnControlled = function(_super) {
481 __extends(UnControlled, _super);
482
483 function UnControlled(props) {
484 var _this = _super.call(this, props) || this;
485 if (SERVER_RENDERED) return _this;
486 _this.applied = false;
487 _this.appliedUserDefined = false;
488 _this.continueChange = false;
489 _this.detached = false;
490 _this.hydrated = false;
491 _this.initCb = function() {
492 if (_this.props.editorDidConfigure) {
493 _this.props.editorDidConfigure(_this.editor);
494 }
495 };
496 _this.mounted = false;
497 _this.onBeforeChangeCb = function() {
498 _this.continueChange = true;
499 };
500 return _this;
501 }
502 UnControlled.prototype.hydrate = function(props) {
503 var _this = this;
504 var _options = props && props.options ? props.options : {};
505 var userDefinedOptions = _extends({}, cm.defaults, this.editor.options, _options);
506 var optionDelta = Object.keys(userDefinedOptions).some(function(key) {
507 return _this.editor.getOption(key) !== userDefinedOptions[key];
508 });
509 if (optionDelta) {
510 Object.keys(userDefinedOptions).forEach(function(key) {
511 if (_options.hasOwnProperty(key)) {
512 if (_this.editor.getOption(key) !== userDefinedOptions[key]) {
513 _this.editor.setOption(key, userDefinedOptions[key]);
514 }
515 }
516 });
517 }
518 if (!this.hydrated) {
519 var doc = this.editor.getDoc();
520 var lastLine = doc.lastLine();
521 var lastChar = doc.getLine(doc.lastLine()).length;
522 doc.replaceRange(props.value || '', {
523 line: 0,
524 ch: 0
525 }, {
526 line: lastLine,
527 ch: lastChar
528 });
529 }
530 this.hydrated = true;
531 };
532 UnControlled.prototype.componentDidMount = function() {
533 var _this = this;
534 if (SERVER_RENDERED) return;
535 this.detached = this.props.detach === true;
536 if (this.props.defineMode) {
537 if (this.props.defineMode.name && this.props.defineMode.fn) {
538 cm.defineMode(this.props.defineMode.name, this.props.defineMode.fn);
539 }
540 }
541 this.editor = cm(this.ref, this.props.options);
542 this.shared = new Shared(this.editor, this.props);
543 this.editor.on('beforeChange', function(cm, data) {
544 if (_this.props.onBeforeChange) {
545 _this.props.onBeforeChange(_this.editor, data, _this.editor.getValue(), _this.onBeforeChangeCb);
546 }
547 });
548 this.editor.on('change', function(cm, data) {
549 if (!_this.mounted || !_this.props.onChange) {
550 return;
551 }
552 if (_this.props.onBeforeChange) {
553 if (_this.continueChange) {
554 _this.props.onChange(_this.editor, data, _this.editor.getValue());
555 }
556 } else {
557 _this.props.onChange(_this.editor, data, _this.editor.getValue());
558 }
559 });
560 this.hydrate(this.props);
561 this.shared.apply(this.props);
562 this.applied = true;
563 this.mounted = true;
564 this.shared.wire(this.props);
565 this.editor.getDoc().clearHistory();
566 if (this.props.editorDidMount) {
567 this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
568 }
569 };
570 UnControlled.prototype.componentDidUpdate = function(prevProps) {
571 if (this.detached && this.props.detach === false) {
572 this.detached = false;
573 if (prevProps.editorDidAttach) {
574 prevProps.editorDidAttach(this.editor);
575 }
576 }
577 if (!this.detached && this.props.detach === true) {
578 this.detached = true;
579 if (prevProps.editorDidDetach) {
580 prevProps.editorDidDetach(this.editor);
581 }
582 }
583 if (SERVER_RENDERED || this.detached) return;
584 var preserved = {
585 cursor: null
586 };
587 if (this.props.value !== prevProps.value) {
588 this.hydrated = false;
589 this.applied = false;
590 this.appliedUserDefined = false;
591 }
592 if (!prevProps.autoCursor && prevProps.autoCursor !== undefined) {
593 preserved.cursor = this.editor.getDoc().getCursor();
594 }
595 this.hydrate(this.props);
596 if (!this.applied) {
597 this.shared.apply(prevProps);
598 this.applied = true;
599 }
600 if (!this.appliedUserDefined) {
601 this.shared.applyUserDefined(prevProps, preserved);
602 this.appliedUserDefined = true;
603 }
604 };
605 UnControlled.prototype.componentWillUnmount = function() {
606 if (SERVER_RENDERED) return;
607 if (this.props.editorWillUnmount) {
608 this.props.editorWillUnmount(cm);
609 }
610 };
611 UnControlled.prototype.shouldComponentUpdate = function(nextProps, nextState) {
612 var update = true;
613 if (SERVER_RENDERED) update = false;
614 if (this.detached && nextProps.detach) update = false;
615 return update;
616 };
617 UnControlled.prototype.render = function() {
618 var _this = this;
619 if (SERVER_RENDERED) return null;
620 var className = this.props.className ? 'react-codemirror2 '.concat(this.props.className) : 'react-codemirror2';
621 return React.createElement('div', {
622 className: className,
623 ref: function ref(self) {
624 return _this.ref = self;
625 }
626 });
627 };
628 return UnControlled;
629}(React.Component);
630exports.UnControlled = UnControlled;
\No newline at end of file