-
Notifications
You must be signed in to change notification settings - Fork 302
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
🪲 Set highlighter language and level #6126
Conversation
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
This pull request has been removed from the queue for the following reason: Pull request #6126 has been dequeued. The pull request rule doesn't match anymore. The following conditions don't match anymore:
You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. If you want to requeue this pull request, you need to post a comment with the text: |
static/js/cm-editor.ts
Outdated
@@ -210,8 +210,8 @@ export class HedyCodeMirrorEditor implements HedyEditor { | |||
state: state | |||
}); | |||
|
|||
const levelStr = $(element).closest('[data-level]').attr('data-level'); | |||
const lang = $(element).closest('[data-kwlang]').attr('data-kwlang') ?? 'en'; | |||
const levelStr = $('[data-level]').attr('data-level'); |
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.
The old code tried to find the closest enclosing element that has a data-level
attribute.
The new code finds any data-level
attribute anywhere on the page, whether it has a relation to the editor or not.
Why did the old code not work?
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.
There is no data-level or data-kwlang on any element. This is why it did not work. I added the attributes to the editor but then ofc closest
does not work. We could either put the attributes on an enclosing element indeed which is a bit weird or keep them in the editor and fetch them directly from the editor $('#editor').attr('data-*')
. What was your intention?
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.
Putting the level and kwlang directly on the editor works for the main editor because we control the HTML for it, but doesn't work very well for example for all the syntax highlighted preview editors in running text, because those are <pre>
tags that are generated from Markdown. So I wanted a place to put it where it's still in context, but not the editor itself, so it become "some enclosing <div>
".
I'd like to keep the logic consistent between the read/write editor and the preview-only editors. That allows us to de-dupe this information wherever it makes sense (*).
I for sure though x.closest()
would return x
if it matched the selector, but perhaps it doesn't. The document seems to bear that out though, I recall looking this up:
![image](https://private-user-images.githubusercontent.com/524162/406030985-3ade8725-9306-4237-8cf8-efa35e6ff757.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyMjM0NjUsIm5iZiI6MTczOTIyMzE2NSwicGF0aCI6Ii81MjQxNjIvNDA2MDMwOTg1LTNhZGU4NzI1LTkzMDYtNDIzNy04Y2Y4LWVmYTM1ZTZmZjc1Ny5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQyMTMyNDVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03YjgyMjRjZDJlNmEyYTdjODk4YTM3YmI2YzkxYzBmZTQ0YjFiNTE5YWVhMDBkZjg1ZTcyNTQ5OGI2Zjc1ZmU2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.Q5wIlLjfMrpPBXr-mmI1DO-Sc6jPticv0mPqfIUn1Jw)
https://api.jquery.com/closest/
(*) In general, I'd like to move to a future in which more data is stored in the HTML, via
data-*
attributes in strategic locations, than passed via JavaScript toinitializeXxx()
functions. The latter requires a very rigid initialization order that doesn't work very well with HTMX.
@@ -15,7 +15,8 @@ | |||
<!-- The actual editor --> | |||
<div id="editor" data-cy="editor" style="background: #272822; font-size:0.95em; color: white; direction: {{ g.dir }};" lang="en" blurred='false' | |||
{% if editor_readonly %}data-readonly="true"{% endif %} | |||
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"></div> | |||
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height" | |||
data-kwlang="{{ current_language().lang }}" data-level="{{ level }}"></div> |
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.
An enclosing scope should be good enough, and I think it should be on there already, no?
@@ -25,7 +25,8 @@ | |||
<!-- The actual editor --> | |||
<div id="editor" style="background: #272822; font-size:0.95em; color: white; direction: {{ g.dir }};" lang="en" blurred='false' | |||
{% if editor_readonly %}data-readonly="true"{% endif %} | |||
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"></div> | |||
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height" | |||
data-kwlang="{{ current_language().lang }}" data-level="{{ level }}"></div> |
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.
Same here, if we can put this attribute on a <div>
that contains all editors, it should work already?
6a32e44
to
2eee2db
Compare
templates/layout.html
Outdated
@@ -48,7 +48,8 @@ | |||
<link rel="icon" type="image/png" href="{{static('/images/Hedy-logo-96x96-laptop.png')}}"/> | |||
<link rel="stylesheet" href="{{static('/fontawesome/css/all.min.css')}}"/> | |||
</head> | |||
<body id="main_container" dir="{{ g.dir }}" class="{% if not raw %}bg-gray-100{% endif %}" hx-ext="disable-element,morph" data-logged-in="{{ 1 if username else 0 }}"> | |||
<body id="main_container" dir="{{ g.dir }}" class="{% if not raw %}bg-gray-100{% endif %}" hx-ext="disable-element,morph" | |||
data-logged-in="{{ 1 if username else 0 }}" data-kwlang="{{ current_language().lang }}" data-level="{{ level }}"> |
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.
Is the kwlang
necessarily the same as the current display language?
And what about pages that don't have a level
parameter? (Like, teacher's interface documentation? Or the explore page? Or the public adventures search page) It seems safer to scope this more strictly to the code page bits?
But also -- I'm not sure, I'm asking.
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.
If the user is logged in and their profile language is set to Turkish but their keyword language is set to English, should we highlight Turkish keywords if they are used? I think we should, so the language there should not be the g.keyword_lang
, it should be the session language.
I am not sure how to resolve this with given the constraints. I put the data attributes in the main container because this is literally the common div that encompasses all usages of editor and editor-like elements. However, as you pointed out it does not make sense to use a level there, which is what I meant by weird in my previous comment.
I placed it now on the editor_area which is your initial idea probably. However, this does not work as expected. I get intermittent failures to load the highlighting correctly which look like this:
Converted this to a draft, so that it does not get merged until this is solved.
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
…ll relevant elements
I ended up putting them in I did change the source back to
Well... if not for that, then what is It may be that there is no useful distinction between keyword language and interface language; but if that's the case, I think I'd rather do that by setting There also seems to be a bunch of swizzling of HTML attributes in JavaScript, where we set (I don't want to put in the effort to validate which bits can be removed though...) |
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
Do we have a list of places where common UI elements are used? I feel like this "not catching of all cases" comes up all the time... |
I know :( I traced that the editor-and-output.html is used in the following places:
But I noticed we also have an embedded-editor.html which is returned by Btw I think the keyword language is meant to define in which language to display the keywords in the program at hand. The highlighting, however, should work for the current language and English. I base this assumption on the way the grammars of the language (the lark files, not the highlighting grammars) are created. |
Thanks, that list is extremely helpful!
I think highlighting will automatically work for But that brings up a good point: let's say keyword language is set to German and display language is set to Dutch (for whatever reason). Keywords should be rendered in German if we do that, right? Shouldn't we then highlight "German + English" (i.e., the keyword language) rather than "Dutch + English" (the UI language) ? |
In your profile, if you set a language other than English, you can only choose your keyword language to be either that same language or English. So, if my language is Turkish, I can pick to have as a keyword language either English or Turkish. I think this was done to simplify the problem of grammar generation. |
However, I have to point out that we did explore the idea of having multiple languages at the same time, e.g. Dutch, Frisian and English, or Dutch, French and English. This would work for countries with multiple official languages. It sounds like a very nice feature but it got de-prioritized. |
So you are saying that the highlighting grammar based on the display language is going to be a union of all possible keyword languages for that display language? That's good to know... but for clarity's sake I think I would still prefer a different keyword then. How about:
? Or something to that effect? (Because effectively we are talking about them being different) |
I added the attributes in other missing places. It's not elegant, but it'll do... ? We can start paying more attention in the future. |
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
PR #6085 makes uses of
data-level
anddata-kwlang
but these are never set. As a result, the language of the highlighter always defaults to English regardless of the current language. This PR makes some minimal changes to address the issue.Fixes #6112
How to test
As an anonymous user go to /hedy and change the language to Turkish (or any other language). In the editor paste
yazdır test
and check that the first word is highlighted as a command. Do the same check for the /tryit page and a logged in user.