obsidian-rtl icon indicating copy to clipboard operation
obsidian-rtl copied to clipboard

Support text direction per line/paragraph

Open Mohamed-Hamza opened this issue 5 years ago • 22 comments

Hey good man, Will u make it go RTL for only the scope (or the line) in which I make the RTL/LTR switch , not the whole note? Mixing directions in the same note is needed please!!

image

Mohamed-Hamza avatar Jul 03 '21 16:07 Mohamed-Hamza

and thanks man :)

Mohamed-Hamza avatar Jul 03 '21 16:07 Mohamed-Hamza

I wish I could do that, but the underlying text editing engine (CodeMirror 5) only supports direction for the entire text at once. Hopefully this will change in the near future with the next major version, CodeMirror 6, being more and more ready for prime time.

esm7 avatar Jul 04 '21 03:07 esm7

Now that https://github.com/codemirror/codemirror.next/issues/782 is closed I think we are closer to having per block direction.

zoli avatar Apr 20 '22 14:04 zoli

That's amazing news, can't wait to try it out when Obsidian upgrades to this version!

esm7 avatar Apr 20 '22 14:04 esm7

I think per line RTL/LTR is now possible regarding that obsidian api has upgraded @codemirror/view v6.0.0.

https://github.com/obsidianmd/obsidian-api/issues/64#issuecomment-1196431428=

zoli avatar Jul 27 '22 08:07 zoli

Maybe now you can reopen this issue?

zoli avatar Jul 27 '22 08:07 zoli

It is indeed possible! I even did a quick dirty test and perLineTextDirection indeed works! The thing is that this feature is not the same as 'auto' HTML direction. There is no automatic setting of the direction of any kind -- just the ability to specify different directions within the same document. So if I understand it correctly, to use this feature properly I'll need to add logic that decides on each line's direction and track it when the document changes. Reopening the issue and will work on it at the closest opportunity.

esm7 avatar Jul 28 '22 10:07 esm7

I'll need to add logic that decides on each line's direction and track it when the document changes.

You are right. I was thinking of STH like auto language detection of the line and using a map of languages which are RTL or LTR. Also a context menu for setting the direction manually would be nice.

zoli avatar Jul 28 '22 11:07 zoli

Keeping metadata outside the note (e.g. manual direction) is a tricky business; This plugin does it but it works on a complete-note basis which is somehow still reasonable. But doing it on a per-line basis is probably very error prone, in the sense that if a file was modified outside of Obsidian (even due to syncing with a different device), the line numbers will mismatch and the wrong lines will be aligned. I prefer a language detection approach similar to how 'auto' works for HTML, plus the ability to specify the default direction of a complete note.

esm7 avatar Jul 28 '22 11:07 esm7

Is there any way i cant do Mixed RTL&LTR same note

oranmizrahi avatar Sep 08 '22 15:09 oranmizrahi

@OranMizrahi see the correspondence above. The basic ability was recently added to CodeMirror (the underlying editor that Obsidian uses), what's missing now is to write some logic around it in Obsidian.

esm7 avatar Sep 08 '22 16:09 esm7

Just wanted to note I'm working to implement this. Here is the branch.

Most of live preview is done, But for the render part I should add a markdown post processor which I haven't started on that yet. I will make a PR after completing.

zoli avatar Sep 13 '22 12:09 zoli

That's awesome and the choice of franc to power the logic is spot-on! I only skimmed through the code and didn't fully review the flow, but one question that popped up for me is which events trigger franc on what input. We naturally wouldn't want to run it on the whole document for every key press. That's probably not the case right now, but I just wanted to make sure ahead of time :-)

esm7 avatar Sep 13 '22 13:09 esm7

Thanks.

one question that popped up for me is which events trigger franc on what input.

Currently, The Codemirror extension is a View plugin so yes the trigger is on each key press or anything related to the view port change but it only runs franc on visible lines (the viewport). It wont run on the whole document and this helps with the performance, But still I am thinking of maybe using some sort of caching so it wont check the lines that have not changed.

zoli avatar Sep 13 '22 14:09 zoli

I think that even if it gets to a point that it runs only on the current line, synchronously running the franc guesswork for every single key press is going to introduce noticeable latency, which will be disturbing for many users. (I personally suffer from visual latency introduced even by plugins that run regular expressions for triggering suggestion popups :( ) How about looking into the option of triggering it asynchronously, and only for the active line or paragraph?

esm7 avatar Sep 13 '22 14:09 esm7

How about looking into the option of triggering it asynchronously, and only for the active line or paragraph?

Good idea I will look into it.

zoli avatar Sep 13 '22 14:09 zoli

Good luck guys i wish you solve it. Im waiting for any new info 😄

oranmizrahi avatar Sep 13 '22 14:09 oranmizrahi

Actually, it may be worth suggesting another approach here.

I believe that what most users expect of an editor with an auto text direction, is to behave similarly to auto text boxes in Windows, Mac, Linux, or web apps. These use a much simpler algorithm than what franc does, of simply searching for the first character in the line that possesses "strong directionality". Maybe we should implement the same behavior, because:

  1. It's much more efficient,
  2. More importantly, this is what you'd get if you copy the same text to another editor with automatic text direction, or even render the markdown as an HTML with dir=auto.

For example, a text that starts from an English letter with some Arabic words to follow, will be treated by all common apps as an English text, but franc will detect it as Arabic. franc is more precise but the commonly-used algorithm is more deterministic and this is what users learned to expect.

This simple directionality detection algorithm is used in so many places, there are countless implementations of it, here's one regex for example. (Group 1 is LTR, group 2 is RTL.)

What do you think?

esm7 avatar Sep 13 '22 14:09 esm7

I was going to do a similar thing (checking the language of first word not the whole line) but then I saw that its possible to check the whole line without much problem and I remembered my problem with Gmail which was whenever I wanted to write Persian which is RTL but the first word was english I always had to change the sentence so the first word be Persian. That's a problem which I as a Persian writer have with that type of implementation, Although it doesn't come up often.

The consistency with other environments that you mentioned is compelling enough for me to change the implementation to what you mentioned.

Also even for franc I should've made an extra weight for starting word so it would took STH like تست Test that is near half english half persian as persian (it starts with persian word).

So I will change it to checking the first alphabetic character and decide the direction based on that.

zoli avatar Sep 13 '22 15:09 zoli

@esm7 I switched to checking first meaningful character and there is no need to franc anymore. Also while editing a file will only look into changing the view port event and changing line , Still there is some shifting in the ranges because changing a line will change all next lines after range but no regex running on all next lines. I've implemented the markdown post processor to, So I think there isn't much left. Will test it for couple of days and then make the pull request.

zoli avatar Nov 23 '22 19:11 zoli