ext-spellcheck.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if (newVal == value)
  28. return '';
  29. if (newVal.lastIndexOf(value, 0) === 0)
  30. return newVal.slice(value.length);
  31. if (newVal.substr(text.selectionEnd) == value)
  32. return newVal.slice(0, -value.length);
  33. if (newVal.slice(-2) == PLACEHOLDER) {
  34. var val = newVal.slice(0, -2);
  35. if (val.slice(-1) == " ") {
  36. if (afterKeydown)
  37. return val.substring(0, text.selectionEnd);
  38. val = val.slice(0, -1);
  39. host.session.replace(r, val);
  40. return "";
  41. }
  42. }
  43. return newVal;
  44. });
  45. };
  46. var Editor = require("../editor").Editor;
  47. require("../config").defineOptions(Editor.prototype, "editor", {
  48. spellcheck: {
  49. set: function(val) {
  50. var text = this.textInput.getElement();
  51. text.spellcheck = !!val;
  52. if (!val)
  53. this.removeListener("nativecontextmenu", exports.contextMenuHandler);
  54. else
  55. this.on("nativecontextmenu", exports.contextMenuHandler);
  56. },
  57. value: true
  58. }
  59. });
  60. }); (function() {
  61. ace.require(["ace/ext/spellcheck"], function(m) {
  62. if (typeof module == "object" && typeof exports == "object" && module) {
  63. module.exports = m;
  64. }
  65. });
  66. })();