ext-spellcheck.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ace.define("ace/ext/spellcheck",[], 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(m) {
  64. if (typeof module == "object" && typeof exports == "object" && module) {
  65. module.exports = m;
  66. }
  67. });
  68. })();