729151a662f8055d864efe142b48ba27.js 4.5 KB

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