Skip to content
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

💻 Automatically add curly braces for individual keywords #5285

Merged
merged 30 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6bcbfe9
moving function to utils
jpelay Mar 20, 2024
bf983e3
checking for wrods associated to several keywords
jpelay Mar 20, 2024
9f275ef
refactoring function
jpelay Mar 20, 2024
5c7e8ac
translations
jpelay Mar 20, 2024
d12f1b7
adding template to customize-adventure html
jpelay Mar 20, 2024
1617b3c
Merge branch 'main' into suggestion_keywords
jpelay Mar 20, 2024
d30a921
ading curly braces to individual keywords
jpelay Mar 20, 2024
cd15f7e
deleting curly braces from messages.po
jpelay Mar 20, 2024
e1cd80a
test for alert message
jpelay Mar 21, 2024
333966e
fixing duplicated msgid's
jpelay Mar 21, 2024
7902b50
🤖 Automatically update generated files
jpelay Mar 21, 2024
927a550
add curly braces around keywords keys
jpelay Mar 27, 2024
e398bd9
Merge branch 'main' into suggestion_keywords
jpelay Mar 27, 2024
772c80a
message translation file
jpelay Mar 27, 2024
c94d08e
🤖 Automatically update generated files
jpelay Mar 27, 2024
1ec3c67
checking for keyword on initialize
jpelay Apr 1, 2024
52382c5
Merge branch 'main' into suggestion_keywords
jpelay Apr 1, 2024
5970a76
🤖 Automatically update generated files
jpelay Apr 1, 2024
3f5706b
Merge branch 'main' into suggestion_keywords
jpelay Apr 8, 2024
85ac847
stop suggesting to use curly braces
jpelay Apr 8, 2024
f677195
fix test
jpelay Apr 9, 2024
33202c9
Merge branch 'main' into suggestion_keywords
jpelay Apr 9, 2024
b6b8045
🤖 Automatically update generated files
jpelay Apr 9, 2024
73bd051
update tests
jpelay Apr 11, 2024
d48c2d0
Merge branch 'main' into suggestion_keywords
jpelay Apr 11, 2024
d8161d6
🤖 Automatically update generated files
jpelay Apr 11, 2024
dcae3da
Merge branch 'main' into suggestion_keywords
Felienne Apr 16, 2024
efc1116
Merge branch 'main' into suggestion_keywords
Felienne Apr 16, 2024
dbdd82c
🤖 Automatically update generated files
Felienne Apr 16, 2024
9f8d909
Merge branch 'main' into suggestion_keywords
mergify[bot] Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build-tools/heroku/generate-client-messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
'customization_deleted',
'directly_available',
'disabled',
'adventures_restored'
'adventures_restored',
'multiple_keywords_warning'
]


Expand Down
1 change: 1 addition & 0 deletions content/client-messages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gettext('customization_deleted')
gettext('directly_available')
gettext('disabled')
gettext('adventures_restored')
gettext('multiple_keywords_warning')
2 changes: 1 addition & 1 deletion messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ msgstr ""
msgid "more_options"
msgstr ""

msgid "multiple_levels_warning"
msgid "multiple_keywords_warning"
msgstr ""

msgid "my_account"
Expand Down
473 changes: 113 additions & 360 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"chai-colors": "^1.0.1",
"chart.js": "^3.6.2",
"codemirror": "^6.0.1",
"cypress-real-events": "^1.12.0",
"dompurify": "^2.3.5",
"istanbul-lib-coverage": "^3.2.0",
"jquery-ui-dist": "^1.13.1",
Expand Down
87 changes: 85 additions & 2 deletions static/js/adventure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { CustomWindow } from './custom-window';
import { languagePerLevel, keywords } from "./lezer-parsers/language-packages";
import { SyntaxNode } from "@lezer/common";
import { initializeTranslation } from "./lezer-parsers/tokens";
import DOMPurify from "dompurify";
import TRADUCTION_IMPORT from '../../highlighting/highlighting-trad.json';
import { convert } from "./utils";
import { ClientMessages } from "./client-messages";
import { autoSave } from "./autosave";

declare let window: CustomWindow;
Expand All @@ -12,15 +16,29 @@ export interface InitializeCustomizeAdventurePage {
}

let $editor: ClassicEditor;
let keywordHasAlert: Map<string, boolean> = new Map()

export async function initializeCustomAdventurePage(_options: InitializeCustomizeAdventurePage) {
const editorContainer = document.querySelector('#adventure-editor') as HTMLElement;
const lang = document.querySelector('html')?.getAttribute('lang') || 'en';
// Initialize the editor with the default language
let lang = $('#language').val() as string || 'en'
const TRADUCTIONS = convert(TRADUCTION_IMPORT) as Map<string, Map<string,string>>;
if (!TRADUCTIONS.has(lang)) { lang = 'en'; }
let TRADUCTION = TRADUCTIONS.get(lang) as Map<string,string>;

if (editorContainer) {
initializeEditor(lang, editorContainer);
await initializeEditor(lang, editorContainer);
showWarningIfMultipleLevels(TRADUCTION)
$editor.model.document.on('change:data', () => {
showWarningIfMultipleLevels(TRADUCTION)
})
}

$('#language').on('change', () => {
let lang = $('#language').val() as string || 'en'
if (!TRADUCTIONS.has(lang)) { lang = 'en'; }
TRADUCTION = TRADUCTIONS.get(lang) as Map<string,string>;
})
// Autosave customize adventure page
autoSave("customize_adventure")

Expand All @@ -42,6 +60,56 @@ export async function initializeCustomAdventurePage(_options: InitializeCustomiz
}, 100);
})
})

}

function showWarningIfMultipleLevels(TRADUCTION: Map<string, string>) {
const content = DOMPurify.sanitize($editor.getData())
const parser = new DOMParser();
const html = parser.parseFromString(content, 'text/html');

for (const tag of html.getElementsByTagName('code')) {
if (tag.className !== "language-python") {
const coincidences = findCoincidences(tag.innerText, TRADUCTION);
if (coincidences.length > 1 && !keywordHasAlert.get(tag.innerText)) {
keywordHasAlert.set(tag.innerText, true);
// We create the alert box dynamically using the template element in the HTML object
const template = document.querySelector('#warning_template') as HTMLTemplateElement
const clone = template.content.cloneNode(true) as HTMLElement
let close = clone.querySelector('.close-dialog');
close?.addEventListener('click', () => {
keywordHasAlert.set(tag.innerText, false);
close?.parentElement?.remove()
})
let p = clone.querySelector('p[class^="details"]')!
let message = ClientMessages['multiple_keywords_warning']
message = message.replace("{orig_keyword}", formatKeyword(tag.innerText))
let keywordList = ''
for (const keyword of coincidences) {
keywordList = keywordList === '' ? formatKeyword(`${keyword}`) : keywordList + `, ${formatKeyword(`${keyword}`)}`
}
message = message.replace("{keyword_list}", keywordList)
p.innerHTML = message
// Once the warning has been created we append it to the container
const warningContainer = document.getElementById('warnings_container')!
warningContainer.appendChild(clone)
}
}
}
}

function formatKeyword(name: string) {
return `<span class='command-highlighted'>${name}</span>`
}

function findCoincidences(name: string, TRADUCTION: Map<string, string>) {
let coincidences = [];
for (const [key, regexString] of TRADUCTION) {
if (new RegExp(`^(${regexString})$`, 'gu').test(name)) {
coincidences.push(key)
}
}
return coincidences;
}

function initializeEditor(language: string, editorContainer: HTMLElement): Promise<void> {
Expand Down Expand Up @@ -141,6 +209,21 @@ export function addCurlyBracesToCode(code: string, level: number, language: stri
return formattedCode;
}

export function addCurlyBracesToKeyword(name: string) {
let lang = $('#language').val() as string || 'en'
const TRADUCTIONS = convert(TRADUCTION_IMPORT) as Map<string, Map<string,string>>;
if (!TRADUCTIONS.has(lang)) { lang = 'en'; }
let TRADUCTION = TRADUCTIONS.get(lang) as Map<string,string>;

for (const [key, regexString] of TRADUCTION) {
if ((new RegExp(`^(${regexString})$`, 'gu').test(name)) || name === key) {
return `{${key}}`
}
}

return name;
}

function waitForElm(selector: string): Promise<NodeListOf<Element>> {
return new Promise(resolve => {
if (document.querySelector(selector)) {
Expand Down
Loading
Loading