Cursor.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package hide.comp.cdb;
  2. class Cursor {
  3. var editor : Editor;
  4. public var table : Table;
  5. public var x : Int;
  6. public var y : Int;
  7. public var select : Null<{ x : Int, y : Int }>;
  8. public var onchange : Void -> Void;
  9. public function new(editor) {
  10. this.editor = editor;
  11. set();
  12. }
  13. public function set( ?t:Table, ?x=0, ?y=0, ?sel, update = true ) {
  14. if( t != null ) {
  15. for( t2 in editor.tables )
  16. if( t.sheet.name == t2.sheet.name ) {
  17. t = t2;
  18. break;
  19. }
  20. }
  21. this.table = t;
  22. this.x = x;
  23. this.y = y;
  24. this.select = sel;
  25. var ch = onchange;
  26. if( ch != null ) {
  27. onchange = null;
  28. ch();
  29. }
  30. if( update ) this.update();
  31. }
  32. public function setDefault(line, column) {
  33. set(editor.tables[0], column, line);
  34. }
  35. public function getLine() {
  36. if( table == null ) return null;
  37. return table.lines[y];
  38. }
  39. public function getCell() {
  40. var line = getLine();
  41. if( line == null ) return null;
  42. return line.cells[x];
  43. }
  44. public function save() {
  45. return { sheet : table.sheet, x : x, y : y, select : select == null ? null : { x : select.x, y : select.y} };
  46. }
  47. public function load( s ) {
  48. var table = null;
  49. for( t in editor.tables )
  50. if( t.sheet == s.sheet ) {
  51. table = t;
  52. break;
  53. }
  54. if( table == null )
  55. return false;
  56. set(table, s.x, s.y, s.select);
  57. return true;
  58. }
  59. public function move( dx : Int, dy : Int, shift : Bool, ctrl : Bool ) {
  60. if( table == null )
  61. table = editor.tables[0];
  62. if( x == -1 && ctrl ) {
  63. if( dy != 0 )
  64. editor.moveLine(getLine(), dy);
  65. update();
  66. return;
  67. }
  68. if( !shift )
  69. select = null;
  70. else if( select == null )
  71. select = { x : x, y : y };
  72. if( dx < 0 && (table.displayMode == Table ? x >= 0 : x > 0) )
  73. x--;
  74. if( dy < 0 && y > 0 )
  75. y--;
  76. if( dx > 0 && x < table.sheet.columns.length - 1 )
  77. x++;
  78. if( dy > 0 && y < table.lines.length - 1 )
  79. y++;
  80. update();
  81. }
  82. public function hide() {
  83. var elt = editor.element;
  84. elt.find(".selected").removeClass("selected");
  85. elt.find(".cursorView").removeClass("cursorView");
  86. elt.find(".cursorLine").removeClass("cursorLine");
  87. }
  88. public function update() {
  89. var elt = editor.element;
  90. hide();
  91. if( table == null )
  92. return;
  93. if( y < 0 ) {
  94. y = 0;
  95. select = null;
  96. }
  97. if( y >= table.lines.length ) {
  98. y = table.lines.length - 1;
  99. select = null;
  100. }
  101. var max = table.sheet.props.isProps ? 1 : table.sheet.columns.length;
  102. if( x >= max ) {
  103. x = max - 1;
  104. select = null;
  105. }
  106. var line = getLine();
  107. if( line == null )
  108. return;
  109. if( x < 0 ) {
  110. line.element.addClass("selected");
  111. if( select != null ) {
  112. var cy = y;
  113. while( select.y != cy ) {
  114. if( select.y > cy ) cy++ else cy--;
  115. table.lines[cy].element.addClass("selected");
  116. }
  117. }
  118. } else {
  119. var c = line.cells[x];
  120. if( c != null )
  121. c.element.addClass("cursorView").closest("tr").addClass("cursorLine");
  122. if( select != null ) {
  123. var s = getSelection();
  124. for( y in s.y1...s.y2 + 1 ) {
  125. var l = table.lines[y];
  126. for( x in s.x1...s.x2+1)
  127. l.cells[x].element.addClass("selected");
  128. }
  129. }
  130. }
  131. var e = line.element[0];
  132. if( e != null ) untyped e.scrollIntoViewIfNeeded();
  133. }
  134. public function getSelection() {
  135. if( table == null )
  136. return null;
  137. var x1 = if( x < 0 ) 0 else x;
  138. var x2 = if( x < 0 ) table.sheet.columns.length-1 else if( select != null ) select.x else x1;
  139. var y1 = y;
  140. var y2 = if( select != null ) select.y else y1;
  141. if( x2 < x1 ) {
  142. var tmp = x2;
  143. x2 = x1;
  144. x1 = tmp;
  145. }
  146. if( y2 < y1 ) {
  147. var tmp = y2;
  148. y2 = y1;
  149. y1 = tmp;
  150. }
  151. return { x1 : x1, x2 : x2, y1 : y1, y2 : y2 };
  152. }
  153. public function clickLine( line : Line, shiftKey = false ) {
  154. var sheet = line.table.sheet;
  155. if( shiftKey && this.table == line.table && x < 0 ) {
  156. select = { x : -1, y : line.index };
  157. update();
  158. } else
  159. set(line.table, -1, line.index);
  160. }
  161. public function clickCell( cell : Cell, shiftKey = false ) {
  162. var xIndex = cell.table.displayMode == Table ? cell.columnIndex : 0;
  163. if( shiftKey && table == cell.table ) {
  164. select = { x : xIndex, y : cell.line.index };
  165. update();
  166. } else
  167. set(cell.table, xIndex, cell.line.index);
  168. }
  169. }