selection-pointer.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineOption("selectionPointer", false, function(cm, val) {
  13. var data = cm.state.selectionPointer;
  14. if (data) {
  15. CodeMirror.off(cm.getWrapperElement(), "mousemove", data.mousemove);
  16. CodeMirror.off(cm.getWrapperElement(), "mouseout", data.mouseout);
  17. CodeMirror.off(window, "scroll", data.windowScroll);
  18. cm.off("cursorActivity", reset);
  19. cm.off("scroll", reset);
  20. cm.state.selectionPointer = null;
  21. cm.display.lineDiv.style.cursor = "";
  22. }
  23. if (val) {
  24. data = cm.state.selectionPointer = {
  25. value: typeof val == "string" ? val : "default",
  26. mousemove: function(event) { mousemove(cm, event); },
  27. mouseout: function(event) { mouseout(cm, event); },
  28. windowScroll: function() { reset(cm); },
  29. rects: null,
  30. mouseX: null, mouseY: null,
  31. willUpdate: false
  32. };
  33. CodeMirror.on(cm.getWrapperElement(), "mousemove", data.mousemove);
  34. CodeMirror.on(cm.getWrapperElement(), "mouseout", data.mouseout);
  35. CodeMirror.on(window, "scroll", data.windowScroll);
  36. cm.on("cursorActivity", reset);
  37. cm.on("scroll", reset);
  38. }
  39. });
  40. function mousemove(cm, event) {
  41. var data = cm.state.selectionPointer;
  42. if (event.buttons == null ? event.which : event.buttons) {
  43. data.mouseX = data.mouseY = null;
  44. } else {
  45. data.mouseX = event.clientX;
  46. data.mouseY = event.clientY;
  47. }
  48. scheduleUpdate(cm);
  49. }
  50. function mouseout(cm, event) {
  51. if (!cm.getWrapperElement().contains(event.relatedTarget)) {
  52. var data = cm.state.selectionPointer;
  53. data.mouseX = data.mouseY = null;
  54. scheduleUpdate(cm);
  55. }
  56. }
  57. function reset(cm) {
  58. cm.state.selectionPointer.rects = null;
  59. scheduleUpdate(cm);
  60. }
  61. function scheduleUpdate(cm) {
  62. if (!cm.state.selectionPointer.willUpdate) {
  63. cm.state.selectionPointer.willUpdate = true;
  64. setTimeout(function() {
  65. update(cm);
  66. cm.state.selectionPointer.willUpdate = false;
  67. }, 50);
  68. }
  69. }
  70. function update(cm) {
  71. var data = cm.state.selectionPointer;
  72. if (!data) return;
  73. if (data.rects == null && data.mouseX != null) {
  74. data.rects = [];
  75. if (cm.somethingSelected()) {
  76. for (var sel = cm.display.selectionDiv.firstChild; sel; sel = sel.nextSibling)
  77. data.rects.push(sel.getBoundingClientRect());
  78. }
  79. }
  80. var inside = false;
  81. if (data.mouseX != null) for (var i = 0; i < data.rects.length; i++) {
  82. var rect = data.rects[i];
  83. if (rect.left <= data.mouseX && rect.right >= data.mouseX &&
  84. rect.top <= data.mouseY && rect.bottom >= data.mouseY)
  85. inside = true;
  86. }
  87. var cursor = inside ? data.value : "";
  88. if (cm.display.lineDiv.style.cursor != cursor)
  89. cm.display.lineDiv.style.cursor = cursor;
  90. }
  91. });