From dc09fd88451cb881850cd4fe1f85d7e21f9cae29 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 28 Oct 2021 21:05:35 +0100 Subject: [PATCH] Improve language selection --- src/components/EditorControls.js | 4 +-- src/components/Menu.js | 61 ++++++++++++++++++++++++++------ src/util/highlighting.js | 61 ++++++++++++++++---------------- 3 files changed, 82 insertions(+), 44 deletions(-) diff --git a/src/components/EditorControls.js b/src/components/EditorControls.js index 9cda168..ad4bb20 100644 --- a/src/components/EditorControls.js +++ b/src/components/EditorControls.js @@ -5,7 +5,7 @@ import history from 'history/browser'; import copy from 'copy-to-clipboard'; import { MenuButton, Button } from './Menu'; -import { languageIds } from '../util/highlighting'; +import { languages } from '../util/highlighting'; import themes from '../style/themes'; import { postUrl } from '../util/constants'; @@ -81,7 +81,7 @@ export default function EditorControls({ label="language" value={language} setValue={setLanguage} - ids={languageIds} + ids={languages} />
diff --git a/src/components/Menu.js b/src/components/Menu.js index e8aa365..55c6dc5 100644 --- a/src/components/Menu.js +++ b/src/components/Menu.js @@ -31,8 +31,29 @@ const Menu = styled.ul` max-height: calc(100vh - 2em); overflow: auto; + > li.title { + text-align: center; + padding: 0.7em 0 0.3em 0; + cursor: initial; + font-weight: bold; + } + + > li.title:hover { + background-color: inherit; + } + + > li.selected { + ::before { + content: '*'; + font-weight: bold; + } + + padding-left: calc(1em - 1ch); + font-weight: bold; + } + > li { - padding: 0.15em 0.5em; + padding: 0em 1em 0.05em 1em; } > li:hover { @@ -63,19 +84,37 @@ export const MenuButton = ({ label, ids, value, setValue }) => { setValue(id); } + function make(ids) { + return ids.map(id => ( +
  • select(e, id)} + > + {id} +
  • + )); + } + + let items; + if (Array.isArray(ids)) { + items = make(ids); + } else { + items = Object.entries(ids).map(([title, values]) => + [ +
  • e.stopPropagation()}> + [{title}] +
  • , + ] + .concat(make(values)) + .flat() + ); + } + return ( ); }; diff --git a/src/util/highlighting.js b/src/util/highlighting.js index 53d1618..8862fe6 100644 --- a/src/util/highlighting.js +++ b/src/util/highlighting.js @@ -1,4 +1,7 @@ -import { highlight, languages } from 'prismjs/components/prism-core'; +import { + highlight, + languages as prismLanguages, +} from 'prismjs/components/prism-core'; import 'prismjs/components/prism-markup'; import 'prismjs/components/prism-css'; @@ -21,47 +24,43 @@ import 'prismjs/components/prism-log'; import 'prismjs/components/prism-markdown'; import 'prismjs/components/prism-php'; import 'prismjs/components/prism-markup-templating'; +import 'prismjs/components/prism-properties'; import 'prismjs/components/prism-protobuf'; import 'prismjs/components/prism-python'; import 'prismjs/components/prism-jsx'; import 'prismjs/components/prism-typescript'; +import 'prismjs/components/prism-tsx'; import 'prismjs/components/prism-ruby'; import 'prismjs/components/prism-rust'; import 'prismjs/components/prism-sql'; import 'prismjs/components/prism-toml'; import 'prismjs/components/prism-yaml'; -export const languageIds = [ - 'plain', - 'markup', - 'css', - 'clike', - 'bash', - 'diff', - 'docker', - 'go', - 'groovy', - 'haskell', - 'java', - 'javastacktrace', - 'javascript', - 'json', - 'kotlin', - 'log', - 'markdown', - 'php', - 'protobuf', - 'python', - 'jsx', - 'typescript', - 'ruby', - 'rust', - 'sql', - 'toml', - 'yaml', -]; +export const languages = { + config: ['yaml', 'json', 'toml', 'properties'], + logs: ['log', 'javastacktrace'], + code: [ + 'java', + 'javascript', + 'typescript', + 'python', + 'kotlin', + 'clike', + 'bash', + 'ruby', + 'rust', + 'sql', + 'go', + 'groovy', + 'haskell', + ], + web: ['markup', 'css', 'php', 'jsx', 'tsx'], + misc: ['plain', 'docker', 'diff', 'markdown', 'protobuf'], +}; + +export const languageIds = Object.values(languages).flat(1); export function getHighlighter(language) { - const grammar = languages[language] || {}; + const grammar = prismLanguages[language] || {}; return input => highlight(input, grammar); }