From be428859b7938386a9388c22e0571b8073066de2 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Mon, 13 Apr 2020 22:08:15 +0200 Subject: [PATCH] Add basic language selector --- assets/sass/_demo.scss | 14 +++++++ assets/ts/app.ts | 2 +- assets/ts/{try-form.tsx => demo.tsx} | 34 +++++++++++++++- config/packages/twig.yaml | 1 - config/services.yaml | 1 - package.json | 4 +- src/Controller/LanguagesAction.php | 59 ++++++++++++++++++++++++++++ src/Twig/KeyLighterTwigAccess.php | 53 ------------------------- templates/keylighter.html.twig | 3 -- yarn.lock | 5 +++ 10 files changed, 114 insertions(+), 62 deletions(-) rename assets/ts/{try-form.tsx => demo.tsx} (83%) create mode 100644 src/Controller/LanguagesAction.php delete mode 100755 src/Twig/KeyLighterTwigAccess.php diff --git a/assets/sass/_demo.scss b/assets/sass/_demo.scss index 050d421..acb9f5e 100644 --- a/assets/sass/_demo.scss +++ b/assets/sass/_demo.scss @@ -43,3 +43,17 @@ .demo__time-split--formatting { background: darken($color-accent, 20%); } + +.demo__languages { + display: flex; + padding: 0; + flex-wrap: wrap; + margin-bottom: -.5rem; + margin-right: -.5rem; + + li { + list-style: none; + margin-right: .5rem; + margin-bottom: .5rem; + } +} diff --git a/assets/ts/app.ts b/assets/ts/app.ts index 7028ea7..279bd53 100644 --- a/assets/ts/app.ts +++ b/assets/ts/app.ts @@ -4,7 +4,7 @@ import 'bootstrap' import '@fortawesome/fontawesome-pro/css/all.min.css' import 'typeface-raleway' import '../sass/style.scss' -import './try-form' +import './demo' $('[data-toggle="tooltip"]').tooltip(); diff --git a/assets/ts/try-form.tsx b/assets/ts/demo.tsx similarity index 83% rename from assets/ts/try-form.tsx rename to assets/ts/demo.tsx index 9308fd9..da54692 100644 --- a/assets/ts/try-form.tsx +++ b/assets/ts/demo.tsx @@ -1,9 +1,38 @@ import * as React from "react"; import { useState } from "react"; import * as ReactDOM from "react-dom" +import { useFetch } from "react-async"; const formatter = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }); +type Language = { + name: string; + class: string; + embeddable: boolean; + standalone: boolean; +} + +type LanguageSelectorProps = { + language: string; + onSelect(language: Language): void; +} + +const LanguageSelector = ({ language, onSelect }: LanguageSelectorProps) => { + const { data: languages, isPending } = useFetch("/languages", { headers: { "Accept": "application/json" }}); + + const regex = language && new RegExp(`${language}`); + const matching = languages && languages.filter(current => !language || current.name.match(regex)); + const isValidLanguage = languages && typeof languages.find(current => current.name === language) !== "undefined"; + + return isPending ? null : +}; + type HighlightFormProps = { onSubmit(code: string, language: string); } @@ -19,6 +48,8 @@ const HighlightForm = ({ onSubmit }: HighlightFormProps) => { const handleSubmit = () => onSubmit && onSubmit(code, language); + const handleLanguageSelection = language => setLanguage(language.name); + return (
@@ -42,8 +73,7 @@ const HighlightForm = ({ onSubmit }: HighlightFormProps) => {
-
-
+