selection-pointer.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. cm.off("cursorActivity", reset);
  18. cm.off("scroll", reset);
  19. cm.state.selectionPointer = null;
  20. cm.display.lineDiv.style.cursor = "";
  21. }
  22. if (val) {
  23. data = cm.state.selectionPointer = {
  24. value: typeof val == "string" ? val : "default",
  25. mousemove: function(event) { mousemove(cm, event); },
  26. mouseout: function(event) { mouseout(cm, event); },
  27. rects: null,
  28. mouseX: null, mouseY: null,
  29. willUpdate: false
  30. };
  31. CodeMirror.on(cm.getWrapperElement(), "mousemove", data.mousemove);
  32. CodeMirror.on(cm.getWrapperElement(), "mouseout", data.mouseout);
  33. cm.on("cursorActivity", reset);
  34. cm.on("scroll", reset);
  35. }
  36. });
  37. function mousemove(cm, event) {
  38. var data = cm.state.selectionPointer;
  39. if (event.buttons == null ? event.which : event.buttons) {
  40. data.mouseX = data.mouseY = null;
  41. } else {
  42. data.mouseX = event.clientX;
  43. data.mouseY = event.clientY;
  44. }
  45. scheduleUpdate(cm);
  46. }
  47. function mouseout(cm, event) {
  48. if (!cm.getWrapperElement().contains(event.relatedTarget)) {
  49. var data = cm.state.selectionPointer;
  50. data.mouseX = data.mouseY = null;
  51. scheduleUpdate(cm);
  52. }
  53. }
  54. function reset(cm) {
  55. cm.state.selectionPointer.rects = null;
  56. scheduleUpdate(cm);
  57. }
  58. function scheduleUpdate(cm) {
  59. if (!cm.state.selectionPointer.willUpdate) {
  60. cm.state.selectionPointer.willUpdate = true;
  61. setTimeout(function() {
  62. update(cm);
  63. cm.state.selectionPointer.willUpdate = false;
  64. }, 50);
  65. }
  66. }
  67. function update(cm) {
  68. var data = cm.state.selectionPointer;
  69. if (!data) return;
  70. if (data.rects == null && data.mouseX != null) {
  71. data.rects = [];
  72. if (cm.somethingSelected()) {
  73. for (var sel = cm.display.selectionDiv.firstChild; sel; sel = sel.nextSibling)
  74. data.rects.push(sel.getBoundingClientRect());
  75. }
  76. }
  77. var inside = false;
  78. if (data.mouseX != null) for (var i = 0; i < data.rects.length; i++) {
  79. var rect = data.rects[i];
  80. if (rect.left <= data.mouseX && rect.right >= data.mouseX &&
  81. rect.top <= data.mouseY && rect.bottom >= data.mouseY)
  82. inside = true;
  83. }
  84. var cursor = inside ? data.value : "";
  85. if (cm.display.lineDiv.style.cursor != cursor)
  86. cm.display.lineDiv.style.cursor = cursor;
  87. }
  88. });