mark-selection.js 1006 B

12345678910111213141516171819202122232425262728293031323334
  1. // Because sometimes you need to mark the selected *text*.
  2. //
  3. // Adds an option 'styleSelectedText' which, when enabled, gives
  4. // selected text the CSS class "CodeMirror-selectedtext".
  5. (function() {
  6. "use strict";
  7. CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {
  8. var prev = old && old != CodeMirror.Init;
  9. if (val && !prev) {
  10. updateSelectedText(cm);
  11. cm.on("cursorActivity", updateSelectedText);
  12. } else if (!val && prev) {
  13. cm.off("cursorActivity", updateSelectedText);
  14. clearSelectedText(cm);
  15. delete cm._selectionMark;
  16. }
  17. });
  18. function clearSelectedText(cm) {
  19. if (cm._selectionMark) cm._selectionMark.clear();
  20. }
  21. function updateSelectedText(cm) {
  22. clearSelectedText(cm);
  23. if (cm.somethingSelected())
  24. cm._selectionMark = cm.markText(cm.getCursor("start"), cm.getCursor("end"),
  25. {className: "CodeMirror-selectedtext"});
  26. else
  27. cm._selectionMark = null;
  28. }
  29. })();