ext-rtl.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. define("ace/ext/rtl",["ace_require","exports","module","ace/editor","ace/config"], function(ace_require, exports, module) {
  2. "use strict";
  3. var commands = [{
  4. name: "leftToRight",
  5. bindKey: { win: "Ctrl-Alt-Shift-L", mac: "Command-Alt-Shift-L" },
  6. exec: function(editor) {
  7. editor.session.$bidiHandler.setRtlDirection(editor, false);
  8. },
  9. readOnly: true
  10. }, {
  11. name: "rightToLeft",
  12. bindKey: { win: "Ctrl-Alt-Shift-R", mac: "Command-Alt-Shift-R" },
  13. exec: function(editor) {
  14. editor.session.$bidiHandler.setRtlDirection(editor, true);
  15. },
  16. readOnly: true
  17. }];
  18. var Editor = ace_require("../editor").Editor;
  19. ace_require("../config").defineOptions(Editor.prototype, "editor", {
  20. rtlText: {
  21. set: function(val) {
  22. if (val) {
  23. this.on("change", onChange);
  24. this.on("changeSelection", onChangeSelection);
  25. this.renderer.on("afterRender", updateLineDirection);
  26. this.commands.on("exec", onCommandEmitted);
  27. this.commands.addCommands(commands);
  28. } else {
  29. this.off("change", onChange);
  30. this.off("changeSelection", onChangeSelection);
  31. this.renderer.off("afterRender", updateLineDirection);
  32. this.commands.off("exec", onCommandEmitted);
  33. this.commands.removeCommands(commands);
  34. clearTextLayer(this.renderer);
  35. }
  36. this.renderer.updateFull();
  37. }
  38. },
  39. rtl: {
  40. set: function(val) {
  41. this.session.$bidiHandler.$isRtl = val;
  42. if (val) {
  43. this.setOption("rtlText", false);
  44. this.renderer.on("afterRender", updateLineDirection);
  45. this.session.$bidiHandler.seenBidi = true;
  46. } else {
  47. this.renderer.off("afterRender", updateLineDirection);
  48. clearTextLayer(this.renderer);
  49. }
  50. this.renderer.updateFull();
  51. }
  52. }
  53. });
  54. function onChangeSelection(e, editor) {
  55. var lead = editor.getSelection().lead;
  56. if (editor.session.$bidiHandler.isRtlLine(lead.row)) {
  57. if (lead.column === 0) {
  58. if (editor.session.$bidiHandler.isMoveLeftOperation && lead.row > 0) {
  59. editor.getSelection().moveCursorTo(lead.row - 1, editor.session.getLine(lead.row - 1).length);
  60. } else {
  61. if (editor.getSelection().isEmpty())
  62. lead.column += 1;
  63. else
  64. lead.setPosition(lead.row, lead.column + 1);
  65. }
  66. }
  67. }
  68. }
  69. function onCommandEmitted(commadEvent) {
  70. commadEvent.editor.session.$bidiHandler.isMoveLeftOperation = /gotoleft|selectleft|backspace|removewordleft/.test(commadEvent.command.name);
  71. }
  72. function onChange(delta, editor) {
  73. var session = editor.session;
  74. session.$bidiHandler.currentRow = null;
  75. if (session.$bidiHandler.isRtlLine(delta.start.row) && delta.action === 'insert' && delta.lines.length > 1) {
  76. for (var row = delta.start.row; row < delta.end.row; row++) {
  77. if (session.getLine(row + 1).charAt(0) !== session.$bidiHandler.RLE)
  78. session.doc.$lines[row + 1] = session.$bidiHandler.RLE + session.getLine(row + 1);
  79. }
  80. }
  81. }
  82. function updateLineDirection(e, renderer) {
  83. var session = renderer.session;
  84. var $bidiHandler = session.$bidiHandler;
  85. var cells = renderer.$textLayer.$lines.cells;
  86. var width = renderer.layerConfig.width - renderer.layerConfig.padding + "px";
  87. cells.forEach(function(cell) {
  88. var style = cell.element.style;
  89. if ($bidiHandler && $bidiHandler.isRtlLine(cell.row)) {
  90. style.direction = "rtl";
  91. style.textAlign = "right";
  92. style.width = width;
  93. } else {
  94. style.direction = "";
  95. style.textAlign = "";
  96. style.width = "";
  97. }
  98. });
  99. }
  100. function clearTextLayer(renderer) {
  101. var lines = renderer.$textLayer.$lines;
  102. lines.cells.forEach(clear);
  103. lines.cellCache.forEach(clear);
  104. function clear(cell) {
  105. var style = cell.element.style;
  106. style.direction = style.textAlign = style.width = "";
  107. }
  108. }
  109. }); (function() {
  110. window.ace_require(["ace/ext/rtl"], function(m) {
  111. if (typeof module == "object" && typeof exports == "object" && module) {
  112. module.exports = m;
  113. }
  114. });
  115. })();