-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for right-to-left text direction. #3400
Conversation
Tests are failing because some of the changed files are indented with tabs, please use 4 spaces to get tests pass. I have several questions about this pr:
|
@nightwing would greatly appreciate your review / comments. Just a couple of words on the motivation for the need to change direction between LTR and RTL. #3137 resolved problem of text flow (also called basic reordering) on the text segments level (continuous segment which is associated with the same language flows in the same direction. For example, "hello world" appears as flowing from left to right, while "HELLO WORLD" should appear as flowing from right to left: "DLROW OLLEH". This PR resolves the problem on the paragraph (line) level. Base text direction mentioned by @ashensis above affects relative position of segments of text (also called directional runs. for example "hello world" is a directional run with left to right direction, while "DLROW OLLEH" is a directional run with right to left direction) in a larger context (i.e. paragraph). For example:
Why paragraph / line level is important ? Mainly for following reasons:
|
@tomerm thanks for the clarification, could you also explain why is this needed in a code editor, it seems like this is going to be much more complicated for modes with highlighting, where we'd probably want to preserve direction of some code spans based on their syntactic meaning. |
@nightwing relating to several general questions (leaving it to @ashensis to reply to code related question).
This is part of standard UBA (Unicode Bidi Algorithm). It is implemented by all editors. The difference is only:
Several examples for some of popular editors:
Text direction is very important for text formulated in natural language. Thus it is natural for plain text mode. Obviously it can be applicable for comments. In other words editing of Java (or similar) code may require enforcing text direction of English comment to LTR while Arabic / Hebrew comments to RTL.
Indeed. This is the main usage of editor at the moment. Addressing Bidi requirements for other modes (i.e. Java / C / C++ / Jason / markdown etc. ) will require additional efforts. We need to assure that syntax of specific programming language is not violated on display by presence of Bidi text in constants / comments (more rarely in variables / function names). This specific requirement is addressed on display only (similar to coloring scheme : comments appear in green, variable in blue etc.). Color information is not serialized with edited source code. Similarly enforcement of code syntax happens on display only without affecting storage in any way. |
Indeed. If we wish to relate to text direction in the code editor we need to take care:
|
Interesting.
|
No advantage. However if you have in your product contexts in which you edit some type of source codes and also contexts in which you edit plain / rich text, sometimes it is easier to just leverage the same editor for all contexts instead of having embedded two different editors. It is really a choice of software product owners. We are not planning to turn Ace into full blown rich text editor (i.e. like CKEditor). We are adding some very basic capabilities only. They are really essential for bidi users.
Clear advantage is to have full control instead of letting somebody else (UBA heuristic or web browser etc.) to make a decision for you. While UBA heuristic behind dir=auto is not bad it is still a heuristic and it fails to address a general case. For example Arabic / Hebrew sentences starting from Latin letters (i.e. ace IS A GREAT EDITOR) it will never display with RTL direction. |
The majority of questions already had been answered by tomerm. 'where is the code handling CTRL+ALT(left) and CTRL+ALT(right)? how would it interact with multiselect' 'it is possible to select ltr/rtl mark in the line and delete it, is it the desired behavior?' |
lib/ace/edit_session.js
Outdated
@@ -1135,6 +1135,11 @@ EditSession.$uid = 0; | |||
* | |||
**/ | |||
this.insert = function(position, text) { | |||
if (text == this.$bidiHandler.LRE || text == this.$bidiHandler.RLE || this.$bidiHandler.isRtlLine()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is better to use eventListener, instead of modifying session insert and remove, either the change event, or add a new event to https://github.com/ajaxorg/ace/blob/master/lib/ace/editor.js#L966.
lib/ace/keyboard/textinput.js
Outdated
@@ -201,6 +201,15 @@ var TextInput = function(parentNode, host) { | |||
if (afterContextMenu) | |||
afterContextMenu = false; | |||
}; | |||
|
|||
var onKeyDown = function(e) { | |||
if(!inComposition && e.shiftKey && e.ctrlKey && host.session.$bidiHandler.hasRtlSupport() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be done on keyup instead, to not interfere with ctrl-shift-
shortcuts?
Also note that some laptops (e.g. macbook) do not have right ctrl key, so this needs to be customizable.
lib/ace/layer/text.js
Outdated
@@ -204,6 +204,8 @@ var Text = function(parentEl) { | |||
); | |||
lineElement.style.height = config.lineHeight * this.session.getRowLength(row) + "px"; | |||
lineElement.innerHTML = html.join(""); | |||
if (this.session.$bidiHandler.hasRtlSupport()) | |||
this.setRtlDir({childNodes: [lineElement], textContent: lineElement.textContent}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be better to do this without accessing textContent, because getting the same information from line is faster
Thanks for detailed explanations.
|
Thank you @nightwing for prompt review. text selection/marker drawing on wrapped lines is broken |
|
@nightwing
|
@ashensis does seenBidi break code already on master or only the new code when switching direction of a line? |
@nightwing |
@ashensis if document doesn't contain any bidi characters computing bidi map is not necessary. |
@nightwing |
Hi @nightwing and @ashensis Thanks, |
Hi, i have rebased and refactored code from this pull request in #3658. And also fixed the bug with seenBidi. |
merged as #3658 |
The must to have requirement to any text editor from the Arabic/Hebrew customer's perspective is support for base text direction.
The natural mode of typing & display for aforementioned languages is right-to-left.
[For detailed explanation of what base text direction term means please see, for instance, pages 5-9 of the following reference document.
https://docs.google.com/document/d/1dDrSwimrQbpbXybhMYDEiJXLeeqvelnY4cc8HeCP1Nk/edit?usp=sharing]
Virtually every editor (like those from MS Office or Open Office) or web based (like CKEditor, IBM Docs) text editors as well as large variety of program products and platforms including Windows system editors, web browser edit fields and areas allows the possibility to specify the text direction that impacts the Arabic/Hebrew text.
The current proposal (based on recently merged PR 'Support for Arabic/Hebrew/Persian languages' #3137) introduces the right-to-left text direction support for ACE text mode editors ('text' and 'plain text').
As in almost all editors it provides possibility to toggle between left-to-right (default) and right-to-left by pressing respectively CTRL+ALT+Shift+L(left) and CTRL+ALT+SHIFT+R(right) hot key combination.
The text direction is provided on per line basis, on switching to right-to-left paragraph text becomes right aligned.
Propagating the line by pressing enter at the 'end' position produces new right-to-left line.
It is possible to switch the few selected line between right-to-left and left-to-right simultaneously.
Both 'insert' and 'overwrite' typing modes are supported.
Paragraph level right-to-left text direction is serialize-able since it is achieved by inserting RLE UCC mark at position 0 of corresponding document text line (which can't be removed by user's accidental keyboard operations) and adding 'direction' and text-align CSS styles in order to achive right-to-left text direction and right alignment (including wrapped lines).
Tested on FF, IE, Chrome/Opera