ace.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. var ace_builds_1 = require("ace-builds");
  30. var PropTypes = require("prop-types");
  31. var React = require("react");
  32. var isEqual = require("lodash.isequal");
  33. var editorOptions_1 = require("./editorOptions");
  34. var ace = editorOptions_1.getAceInstance();
  35. var ReactAce = /** @class */ (function (_super) {
  36. __extends(ReactAce, _super);
  37. function ReactAce(props) {
  38. var _this = _super.call(this, props) || this;
  39. editorOptions_1.editorEvents.forEach(function (method) {
  40. _this[method] = _this[method].bind(_this);
  41. });
  42. _this.debounce = editorOptions_1.debounce;
  43. return _this;
  44. }
  45. ReactAce.prototype.isInShadow = function (node) {
  46. var parent = node && node.parentNode;
  47. while (parent) {
  48. if (parent.toString() === "[object ShadowRoot]") {
  49. return true;
  50. }
  51. parent = parent.parentNode;
  52. }
  53. return false;
  54. };
  55. ReactAce.prototype.componentDidMount = function () {
  56. var _this = this;
  57. var _a = this.props, className = _a.className, onBeforeLoad = _a.onBeforeLoad, onValidate = _a.onValidate, mode = _a.mode, focus = _a.focus, theme = _a.theme, fontSize = _a.fontSize, value = _a.value, defaultValue = _a.defaultValue, showGutter = _a.showGutter, wrapEnabled = _a.wrapEnabled, showPrintMargin = _a.showPrintMargin, _b = _a.scrollMargin, scrollMargin = _b === void 0 ? [0, 0, 0, 0] : _b, keyboardHandler = _a.keyboardHandler, onLoad = _a.onLoad, commands = _a.commands, annotations = _a.annotations, markers = _a.markers, placeholder = _a.placeholder;
  58. this.editor = ace.edit(this.refEditor);
  59. if (onBeforeLoad) {
  60. onBeforeLoad(ace);
  61. }
  62. var editorProps = Object.keys(this.props.editorProps);
  63. for (var i = 0; i < editorProps.length; i++) {
  64. this.editor[editorProps[i]] = this.props.editorProps[editorProps[i]];
  65. }
  66. if (this.props.debounceChangePeriod) {
  67. this.onChange = this.debounce(this.onChange, this.props.debounceChangePeriod);
  68. }
  69. this.editor.renderer.setScrollMargin(scrollMargin[0], scrollMargin[1], scrollMargin[2], scrollMargin[3]);
  70. if (this.isInShadow(this.refEditor)) {
  71. this.editor.renderer.attachToShadowRoot();
  72. }
  73. this.editor
  74. .getSession()
  75. .setMode(typeof mode === "string" ? "ace/mode/" + mode : mode);
  76. if (theme && theme !== "")
  77. this.editor.setTheme("ace/theme/" + theme);
  78. this.editor.setFontSize(typeof fontSize === "number" ? fontSize + "px" : fontSize);
  79. this.editor
  80. .getSession()
  81. .setValue(!defaultValue ? value || "" : defaultValue);
  82. if (this.props.navigateToFileEnd) {
  83. this.editor.navigateFileEnd();
  84. }
  85. this.editor.renderer.setShowGutter(showGutter);
  86. this.editor.getSession().setUseWrapMode(wrapEnabled);
  87. this.editor.setShowPrintMargin(showPrintMargin);
  88. this.editor.on("focus", this.onFocus);
  89. this.editor.on("blur", this.onBlur);
  90. this.editor.on("copy", this.onCopy);
  91. this.editor.on("paste", this.onPaste);
  92. this.editor.on("change", this.onChange);
  93. this.editor.on("input", this.onInput);
  94. if (placeholder) {
  95. this.updatePlaceholder();
  96. }
  97. this.editor
  98. .getSession()
  99. .selection.on("changeSelection", this.onSelectionChange);
  100. this.editor.getSession().selection.on("changeCursor", this.onCursorChange);
  101. if (onValidate) {
  102. // @ts-ignore types don't include
  103. this.editor.getSession().on("changeAnnotation", function () {
  104. // tslint:disable-next-line:no-shadowed-variable
  105. var annotations = _this.editor.getSession().getAnnotations();
  106. _this.props.onValidate(annotations);
  107. });
  108. }
  109. this.editor.session.on("changeScrollTop", this.onScroll);
  110. this.editor.getSession().setAnnotations(annotations || []);
  111. if (markers && markers.length > 0) {
  112. this.handleMarkers(markers);
  113. }
  114. // get a list of possible options to avoid 'misspelled option errors'
  115. var availableOptions = this.editor.$options;
  116. editorOptions_1.editorOptions.forEach(function (option) {
  117. if (availableOptions.hasOwnProperty(option)) {
  118. // @ts-ignore
  119. _this.editor.setOption(option, _this.props[option]);
  120. }
  121. else if (_this.props[option]) {
  122. console.warn("ReactAce: editor option " + option + " was activated but not found. Did you need to import a related tool or did you possibly mispell the option?");
  123. }
  124. });
  125. this.handleOptions(this.props);
  126. if (Array.isArray(commands)) {
  127. commands.forEach(function (command) {
  128. if (typeof command.exec === "string") {
  129. _this.editor.commands.bindKey(command.bindKey, command.exec);
  130. }
  131. else {
  132. _this.editor.commands.addCommand(command);
  133. }
  134. });
  135. }
  136. if (keyboardHandler) {
  137. this.editor.setKeyboardHandler("ace/keyboard/" + keyboardHandler);
  138. }
  139. if (className) {
  140. this.refEditor.className += " " + className;
  141. }
  142. if (onLoad) {
  143. onLoad(this.editor);
  144. }
  145. this.editor.resize();
  146. if (focus) {
  147. this.editor.focus();
  148. }
  149. };
  150. ReactAce.prototype.componentDidUpdate = function (prevProps) {
  151. var oldProps = prevProps;
  152. var nextProps = this.props;
  153. for (var i = 0; i < editorOptions_1.editorOptions.length; i++) {
  154. var option = editorOptions_1.editorOptions[i];
  155. if (nextProps[option] !== oldProps[option]) {
  156. // @ts-ignore
  157. this.editor.setOption(option, nextProps[option]);
  158. }
  159. }
  160. if (nextProps.className !== oldProps.className) {
  161. var appliedClasses = this.refEditor.className;
  162. var appliedClassesArray_1 = appliedClasses.trim().split(" ");
  163. var oldClassesArray = oldProps.className.trim().split(" ");
  164. oldClassesArray.forEach(function (oldClass) {
  165. var index = appliedClassesArray_1.indexOf(oldClass);
  166. appliedClassesArray_1.splice(index, 1);
  167. });
  168. this.refEditor.className =
  169. " " + nextProps.className + " " + appliedClassesArray_1.join(" ");
  170. }
  171. // First process editor value, as it may create a new session (see issue #300)
  172. if (this.editor &&
  173. nextProps.value != null &&
  174. this.editor.getValue() !== nextProps.value) {
  175. // editor.setValue is a synchronous function call, change event is emitted before setValue return.
  176. this.silent = true;
  177. var pos = this.editor.session.selection.toJSON();
  178. this.editor.setValue(nextProps.value, nextProps.cursorStart);
  179. this.editor.session.selection.fromJSON(pos);
  180. this.silent = false;
  181. }
  182. if (nextProps.placeholder !== oldProps.placeholder) {
  183. this.updatePlaceholder();
  184. }
  185. if (nextProps.mode !== oldProps.mode) {
  186. this.editor
  187. .getSession()
  188. .setMode(typeof nextProps.mode === "string"
  189. ? "ace/mode/" + nextProps.mode
  190. : nextProps.mode);
  191. }
  192. if (nextProps.theme !== oldProps.theme) {
  193. this.editor.setTheme("ace/theme/" + nextProps.theme);
  194. }
  195. if (nextProps.keyboardHandler !== oldProps.keyboardHandler) {
  196. if (nextProps.keyboardHandler) {
  197. this.editor.setKeyboardHandler("ace/keyboard/" + nextProps.keyboardHandler);
  198. }
  199. else {
  200. this.editor.setKeyboardHandler(null);
  201. }
  202. }
  203. if (nextProps.fontSize !== oldProps.fontSize) {
  204. this.editor.setFontSize(typeof nextProps.fontSize === "number"
  205. ? nextProps.fontSize + "px"
  206. : nextProps.fontSize);
  207. }
  208. if (nextProps.wrapEnabled !== oldProps.wrapEnabled) {
  209. this.editor.getSession().setUseWrapMode(nextProps.wrapEnabled);
  210. }
  211. if (nextProps.showPrintMargin !== oldProps.showPrintMargin) {
  212. this.editor.setShowPrintMargin(nextProps.showPrintMargin);
  213. }
  214. if (nextProps.showGutter !== oldProps.showGutter) {
  215. this.editor.renderer.setShowGutter(nextProps.showGutter);
  216. }
  217. if (!isEqual(nextProps.setOptions, oldProps.setOptions)) {
  218. this.handleOptions(nextProps);
  219. }
  220. if (!isEqual(nextProps.annotations, oldProps.annotations)) {
  221. this.editor.getSession().setAnnotations(nextProps.annotations || []);
  222. }
  223. if (!isEqual(nextProps.markers, oldProps.markers) &&
  224. Array.isArray(nextProps.markers)) {
  225. this.handleMarkers(nextProps.markers);
  226. }
  227. // this doesn't look like it works at all....
  228. if (!isEqual(nextProps.scrollMargin, oldProps.scrollMargin)) {
  229. this.handleScrollMargins(nextProps.scrollMargin);
  230. }
  231. if (prevProps.height !== this.props.height ||
  232. prevProps.width !== this.props.width) {
  233. this.editor.resize();
  234. }
  235. if (this.props.focus && !prevProps.focus) {
  236. this.editor.focus();
  237. }
  238. };
  239. ReactAce.prototype.handleScrollMargins = function (margins) {
  240. if (margins === void 0) { margins = [0, 0, 0, 0]; }
  241. this.editor.renderer.setScrollMargin(margins[0], margins[1], margins[2], margins[3]);
  242. };
  243. ReactAce.prototype.componentWillUnmount = function () {
  244. this.editor.destroy();
  245. this.editor = null;
  246. };
  247. ReactAce.prototype.onChange = function (event) {
  248. if (this.props.onChange && !this.silent) {
  249. var value = this.editor.getValue();
  250. this.props.onChange(value, event);
  251. }
  252. };
  253. ReactAce.prototype.onSelectionChange = function (event) {
  254. if (this.props.onSelectionChange) {
  255. var value = this.editor.getSelection();
  256. this.props.onSelectionChange(value, event);
  257. }
  258. };
  259. ReactAce.prototype.onCursorChange = function (event) {
  260. if (this.props.onCursorChange) {
  261. var value = this.editor.getSelection();
  262. this.props.onCursorChange(value, event);
  263. }
  264. };
  265. ReactAce.prototype.onInput = function (event) {
  266. if (this.props.onInput) {
  267. this.props.onInput(event);
  268. }
  269. if (this.props.placeholder) {
  270. this.updatePlaceholder();
  271. }
  272. };
  273. ReactAce.prototype.onFocus = function (event) {
  274. if (this.props.onFocus) {
  275. this.props.onFocus(event, this.editor);
  276. }
  277. };
  278. ReactAce.prototype.onBlur = function (event) {
  279. if (this.props.onBlur) {
  280. this.props.onBlur(event, this.editor);
  281. }
  282. };
  283. ReactAce.prototype.onCopy = function (_a) {
  284. var text = _a.text;
  285. if (this.props.onCopy) {
  286. this.props.onCopy(text);
  287. }
  288. };
  289. ReactAce.prototype.onPaste = function (_a) {
  290. var text = _a.text;
  291. if (this.props.onPaste) {
  292. this.props.onPaste(text);
  293. }
  294. };
  295. ReactAce.prototype.onScroll = function () {
  296. if (this.props.onScroll) {
  297. this.props.onScroll(this.editor);
  298. }
  299. };
  300. ReactAce.prototype.handleOptions = function (props) {
  301. var setOptions = Object.keys(props.setOptions);
  302. for (var y = 0; y < setOptions.length; y++) {
  303. // @ts-ignore
  304. this.editor.setOption(setOptions[y], props.setOptions[setOptions[y]]);
  305. }
  306. };
  307. ReactAce.prototype.handleMarkers = function (markers) {
  308. var _this = this;
  309. // remove foreground markers
  310. var currentMarkers = this.editor.getSession().getMarkers(true);
  311. for (var i in currentMarkers) {
  312. if (currentMarkers.hasOwnProperty(i)) {
  313. this.editor.getSession().removeMarker(currentMarkers[i].id);
  314. }
  315. }
  316. // remove background markers except active line marker and selected word marker
  317. currentMarkers = this.editor.getSession().getMarkers(false);
  318. for (var i in currentMarkers) {
  319. if (currentMarkers.hasOwnProperty(i) &&
  320. currentMarkers[i].clazz !== "ace_active-line" &&
  321. currentMarkers[i].clazz !== "ace_selected-word") {
  322. this.editor.getSession().removeMarker(currentMarkers[i].id);
  323. }
  324. }
  325. // add new markers
  326. markers.forEach(function (_a) {
  327. var startRow = _a.startRow, startCol = _a.startCol, endRow = _a.endRow, endCol = _a.endCol, className = _a.className, type = _a.type, _b = _a.inFront, inFront = _b === void 0 ? false : _b;
  328. var range = new ace_builds_1.Range(startRow, startCol, endRow, endCol);
  329. _this.editor.getSession().addMarker(range, className, type, inFront);
  330. });
  331. };
  332. ReactAce.prototype.updatePlaceholder = function () {
  333. // Adapted from https://stackoverflow.com/questions/26695708/how-can-i-add-placeholder-text-when-the-editor-is-empty
  334. var editor = this.editor;
  335. var placeholder = this.props.placeholder;
  336. var showPlaceholder = !editor.session.getValue().length;
  337. var node = editor.renderer.placeholderNode;
  338. if (!showPlaceholder && node) {
  339. editor.renderer.scroller.removeChild(editor.renderer.placeholderNode);
  340. editor.renderer.placeholderNode = null;
  341. }
  342. else if (showPlaceholder && !node) {
  343. node = editor.renderer.placeholderNode = document.createElement("div");
  344. node.textContent = placeholder || "";
  345. node.className = "ace_comment ace_placeholder";
  346. node.style.padding = "0 9px";
  347. node.style.position = "absolute";
  348. node.style.zIndex = "3";
  349. editor.renderer.scroller.appendChild(node);
  350. }
  351. else if (showPlaceholder && node) {
  352. node.textContent = placeholder;
  353. }
  354. };
  355. ReactAce.prototype.updateRef = function (item) {
  356. this.refEditor = item;
  357. };
  358. ReactAce.prototype.render = function () {
  359. var _a = this.props, name = _a.name, width = _a.width, height = _a.height, style = _a.style;
  360. var divStyle = __assign({ width: width, height: height }, style);
  361. return React.createElement("div", { ref: this.updateRef, id: name, style: divStyle });
  362. };
  363. ReactAce.propTypes = {
  364. mode: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
  365. focus: PropTypes.bool,
  366. theme: PropTypes.string,
  367. name: PropTypes.string,
  368. className: PropTypes.string,
  369. height: PropTypes.string,
  370. width: PropTypes.string,
  371. fontSize: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  372. showGutter: PropTypes.bool,
  373. onChange: PropTypes.func,
  374. onCopy: PropTypes.func,
  375. onPaste: PropTypes.func,
  376. onFocus: PropTypes.func,
  377. onInput: PropTypes.func,
  378. onBlur: PropTypes.func,
  379. onScroll: PropTypes.func,
  380. value: PropTypes.string,
  381. defaultValue: PropTypes.string,
  382. onLoad: PropTypes.func,
  383. onSelectionChange: PropTypes.func,
  384. onCursorChange: PropTypes.func,
  385. onBeforeLoad: PropTypes.func,
  386. onValidate: PropTypes.func,
  387. minLines: PropTypes.number,
  388. maxLines: PropTypes.number,
  389. readOnly: PropTypes.bool,
  390. highlightActiveLine: PropTypes.bool,
  391. tabSize: PropTypes.number,
  392. showPrintMargin: PropTypes.bool,
  393. cursorStart: PropTypes.number,
  394. debounceChangePeriod: PropTypes.number,
  395. editorProps: PropTypes.object,
  396. setOptions: PropTypes.object,
  397. style: PropTypes.object,
  398. scrollMargin: PropTypes.array,
  399. annotations: PropTypes.array,
  400. markers: PropTypes.array,
  401. keyboardHandler: PropTypes.string,
  402. wrapEnabled: PropTypes.bool,
  403. enableSnippets: PropTypes.bool,
  404. enableBasicAutocompletion: PropTypes.oneOfType([
  405. PropTypes.bool,
  406. PropTypes.array
  407. ]),
  408. enableLiveAutocompletion: PropTypes.oneOfType([
  409. PropTypes.bool,
  410. PropTypes.array
  411. ]),
  412. navigateToFileEnd: PropTypes.bool,
  413. commands: PropTypes.array,
  414. placeholder: PropTypes.string
  415. };
  416. ReactAce.defaultProps = {
  417. name: "ace-editor",
  418. focus: false,
  419. mode: "",
  420. theme: "",
  421. height: "500px",
  422. width: "500px",
  423. fontSize: 12,
  424. enableSnippets: false,
  425. showGutter: true,
  426. onChange: null,
  427. onPaste: null,
  428. onLoad: null,
  429. onScroll: null,
  430. minLines: null,
  431. maxLines: null,
  432. readOnly: false,
  433. highlightActiveLine: true,
  434. showPrintMargin: true,
  435. tabSize: 4,
  436. cursorStart: 1,
  437. editorProps: {},
  438. style: {},
  439. scrollMargin: [0, 0, 0, 0],
  440. setOptions: {},
  441. wrapEnabled: false,
  442. enableBasicAutocompletion: false,
  443. enableLiveAutocompletion: false,
  444. placeholder: null,
  445. navigateToFileEnd: true
  446. };
  447. return ReactAce;
  448. }(React.Component));
  449. exports.default = ReactAce;
  450. //# sourceMappingURL=ace.js.map