ext-split.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ace.define("ace/split",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/lib/event_emitter","ace/editor","ace/virtual_renderer","ace/edit_session"], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("./lib/oop");
  4. var lang = require("./lib/lang");
  5. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  6. var Editor = require("./editor").Editor;
  7. var Renderer = require("./virtual_renderer").VirtualRenderer;
  8. var EditSession = require("./edit_session").EditSession;
  9. var Split = function(container, theme, splits) {
  10. this.BELOW = 1;
  11. this.BESIDE = 0;
  12. this.$container = container;
  13. this.$theme = theme;
  14. this.$splits = 0;
  15. this.$editorCSS = "";
  16. this.$editors = [];
  17. this.$orientation = this.BESIDE;
  18. this.setSplits(splits || 1);
  19. this.$cEditor = this.$editors[0];
  20. this.on("focus", function(editor) {
  21. this.$cEditor = editor;
  22. }.bind(this));
  23. };
  24. (function(){
  25. oop.implement(this, EventEmitter);
  26. this.$createEditor = function() {
  27. var el = document.createElement("div");
  28. el.className = this.$editorCSS;
  29. el.style.cssText = "position: absolute; top:0px; bottom:0px";
  30. this.$container.appendChild(el);
  31. var editor = new Editor(new Renderer(el, this.$theme));
  32. editor.on("focus", function() {
  33. this._emit("focus", editor);
  34. }.bind(this));
  35. this.$editors.push(editor);
  36. editor.setFontSize(this.$fontSize);
  37. return editor;
  38. };
  39. this.setSplits = function(splits) {
  40. var editor;
  41. if (splits < 1) {
  42. throw "The number of splits have to be > 0!";
  43. }
  44. if (splits == this.$splits) {
  45. return;
  46. } else if (splits > this.$splits) {
  47. while (this.$splits < this.$editors.length && this.$splits < splits) {
  48. editor = this.$editors[this.$splits];
  49. this.$container.appendChild(editor.container);
  50. editor.setFontSize(this.$fontSize);
  51. this.$splits ++;
  52. }
  53. while (this.$splits < splits) {
  54. this.$createEditor();
  55. this.$splits ++;
  56. }
  57. } else {
  58. while (this.$splits > splits) {
  59. editor = this.$editors[this.$splits - 1];
  60. this.$container.removeChild(editor.container);
  61. this.$splits --;
  62. }
  63. }
  64. this.resize();
  65. };
  66. this.getSplits = function() {
  67. return this.$splits;
  68. };
  69. this.getEditor = function(idx) {
  70. return this.$editors[idx];
  71. };
  72. this.getCurrentEditor = function() {
  73. return this.$cEditor;
  74. };
  75. this.focus = function() {
  76. this.$cEditor.focus();
  77. };
  78. this.blur = function() {
  79. this.$cEditor.blur();
  80. };
  81. this.setTheme = function(theme) {
  82. this.$editors.forEach(function(editor) {
  83. editor.setTheme(theme);
  84. });
  85. };
  86. this.setKeyboardHandler = function(keybinding) {
  87. this.$editors.forEach(function(editor) {
  88. editor.setKeyboardHandler(keybinding);
  89. });
  90. };
  91. this.forEach = function(callback, scope) {
  92. this.$editors.forEach(callback, scope);
  93. };
  94. this.$fontSize = "";
  95. this.setFontSize = function(size) {
  96. this.$fontSize = size;
  97. this.forEach(function(editor) {
  98. editor.setFontSize(size);
  99. });
  100. };
  101. this.$cloneSession = function(session) {
  102. var s = new EditSession(session.getDocument(), session.getMode());
  103. var undoManager = session.getUndoManager();
  104. if (undoManager) {
  105. var undoManagerProxy = new UndoManagerProxy(undoManager, s);
  106. s.setUndoManager(undoManagerProxy);
  107. }
  108. s.$informUndoManager = lang.delayedCall(function() { s.$deltas = []; });
  109. s.setTabSize(session.getTabSize());
  110. s.setUseSoftTabs(session.getUseSoftTabs());
  111. s.setOverwrite(session.getOverwrite());
  112. s.setBreakpoints(session.getBreakpoints());
  113. s.setUseWrapMode(session.getUseWrapMode());
  114. s.setUseWorker(session.getUseWorker());
  115. s.setWrapLimitRange(session.$wrapLimitRange.min,
  116. session.$wrapLimitRange.max);
  117. s.$foldData = session.$cloneFoldData();
  118. return s;
  119. };
  120. this.setSession = function(session, idx) {
  121. var editor;
  122. if (idx == null) {
  123. editor = this.$cEditor;
  124. } else {
  125. editor = this.$editors[idx];
  126. }
  127. var isUsed = this.$editors.some(function(editor) {
  128. return editor.session === session;
  129. });
  130. if (isUsed) {
  131. session = this.$cloneSession(session);
  132. }
  133. editor.setSession(session);
  134. return session;
  135. };
  136. this.getOrientation = function() {
  137. return this.$orientation;
  138. };
  139. this.setOrientation = function(orientation) {
  140. if (this.$orientation == orientation) {
  141. return;
  142. }
  143. this.$orientation = orientation;
  144. this.resize();
  145. };
  146. this.resize = function() {
  147. var width = this.$container.clientWidth;
  148. var height = this.$container.clientHeight;
  149. var editor;
  150. if (this.$orientation == this.BESIDE) {
  151. var editorWidth = width / this.$splits;
  152. for (var i = 0; i < this.$splits; i++) {
  153. editor = this.$editors[i];
  154. editor.container.style.width = editorWidth + "px";
  155. editor.container.style.top = "0px";
  156. editor.container.style.left = i * editorWidth + "px";
  157. editor.container.style.height = height + "px";
  158. editor.resize();
  159. }
  160. } else {
  161. var editorHeight = height / this.$splits;
  162. for (var i = 0; i < this.$splits; i++) {
  163. editor = this.$editors[i];
  164. editor.container.style.width = width + "px";
  165. editor.container.style.top = i * editorHeight + "px";
  166. editor.container.style.left = "0px";
  167. editor.container.style.height = editorHeight + "px";
  168. editor.resize();
  169. }
  170. }
  171. };
  172. }).call(Split.prototype);
  173. function UndoManagerProxy(undoManager, session) {
  174. this.$u = undoManager;
  175. this.$doc = session;
  176. }
  177. (function() {
  178. this.execute = function(options) {
  179. this.$u.execute(options);
  180. };
  181. this.undo = function() {
  182. var selectionRange = this.$u.undo(true);
  183. if (selectionRange) {
  184. this.$doc.selection.setSelectionRange(selectionRange);
  185. }
  186. };
  187. this.redo = function() {
  188. var selectionRange = this.$u.redo(true);
  189. if (selectionRange) {
  190. this.$doc.selection.setSelectionRange(selectionRange);
  191. }
  192. };
  193. this.reset = function() {
  194. this.$u.reset();
  195. };
  196. this.hasUndo = function() {
  197. return this.$u.hasUndo();
  198. };
  199. this.hasRedo = function() {
  200. return this.$u.hasRedo();
  201. };
  202. }).call(UndoManagerProxy.prototype);
  203. exports.Split = Split;
  204. });
  205. ace.define("ace/ext/split",["require","exports","module","ace/split"], function(require, exports, module) {
  206. "use strict";
  207. module.exports = require("../split");
  208. });
  209. (function() {
  210. ace.require(["ace/ext/split"], function() {});
  211. })();