ext-spellcheck.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ace.define("ace/ext/spellcheck",["require","exports","module","ace/lib/event","ace/editor","ace/config"], function(require, exports, module) {
  2. "use strict";
  3. var event = require("../lib/event");
  4. exports.contextMenuHandler = function(e){
  5. var host = e.target;
  6. var text = host.textInput.getElement();
  7. if (!host.selection.isEmpty())
  8. return;
  9. var c = host.getCursorPosition();
  10. var r = host.session.getWordRange(c.row, c.column);
  11. var w = host.session.getTextRange(r);
  12. host.session.tokenRe.lastIndex = 0;
  13. if (!host.session.tokenRe.test(w))
  14. return;
  15. var PLACEHOLDER = "\x01\x01";
  16. var value = w + " " + PLACEHOLDER;
  17. text.value = value;
  18. text.setSelectionRange(w.length, w.length + 1);
  19. text.setSelectionRange(0, 0);
  20. text.setSelectionRange(0, w.length);
  21. var afterKeydown = false;
  22. event.addListener(text, "keydown", function onKeydown() {
  23. event.removeListener(text, "keydown", onKeydown);
  24. afterKeydown = true;
  25. });
  26. host.textInput.setInputHandler(function(newVal) {
  27. console.log(newVal , value, text.selectionStart, text.selectionEnd)
  28. if (newVal == value)
  29. return '';
  30. if (newVal.lastIndexOf(value, 0) === 0)
  31. return newVal.slice(value.length);
  32. if (newVal.substr(text.selectionEnd) == value)
  33. return newVal.slice(0, -value.length);
  34. if (newVal.slice(-2) == PLACEHOLDER) {
  35. var val = newVal.slice(0, -2);
  36. if (val.slice(-1) == " ") {
  37. if (afterKeydown)
  38. return val.substring(0, text.selectionEnd);
  39. val = val.slice(0, -1);
  40. host.session.replace(r, val);
  41. return "";
  42. }
  43. }
  44. return newVal;
  45. });
  46. };
  47. var Editor = require("../editor").Editor;
  48. require("../config").defineOptions(Editor.prototype, "editor", {
  49. spellcheck: {
  50. set: function(val) {
  51. var text = this.textInput.getElement();
  52. text.spellcheck = !!val;
  53. if (!val)
  54. this.removeListener("nativecontextmenu", exports.contextMenuHandler);
  55. else
  56. this.on("nativecontextmenu", exports.contextMenuHandler);
  57. },
  58. value: true
  59. }
  60. });
  61. });
  62. (function() {
  63. ace.require(["ace/ext/spellcheck"], function() {});
  64. })();