diff --git a/frontend/src/components/Scratch/LibraryPanel.module.css b/frontend/src/components/Scratch/LibraryPanel.module.css new file mode 100644 index 00000000..a2629b87 --- /dev/null +++ b/frontend/src/components/Scratch/LibraryPanel.module.css @@ -0,0 +1,42 @@ +.section { + padding: 1.5em; + border-radius: 0; + background: transparent; +} + +.section > h3 { + font-size: 1.1em; + font-weight: 500; + + padding: 0.5em; + padding-top: 0; + + color: var(--g1200); +} + +.section:not(:last-child) { + border-bottom: 1px solid var(--a100); +} + +.library { + display: inline-flex; + flex-direction: row; + + width: 100%; + border-radius: 0.5em; +} + +.libraryName { + cursor: default; + font-size: 0.8rem; + + font-size: 1.0em; + font-weight: 500; + + padding: .5em; + +} + +.librarySelect { + flex-grow: 1; +} diff --git a/frontend/src/components/Scratch/LibraryPanel.tsx b/frontend/src/components/Scratch/LibraryPanel.tsx new file mode 100644 index 00000000..f6937e37 --- /dev/null +++ b/frontend/src/components/Scratch/LibraryPanel.tsx @@ -0,0 +1,85 @@ +import { useLibraries } from "@/lib/api" +import { Library, TerseScratch } from "@/lib/api/types" + +import Select from "../Select2" + +import styles from "./LibraryPanel.module.css" + +type LibrariesT = { + libraries: Library[] +} + +type Props = { + scratch: TerseScratch + onChange: (value: LibrariesT) => void +} + +export default function LibraryPanel({ scratch, onChange }: Props) { + const libraries = useLibraries() + + const hasLibrary = libName => scratch.libraries.some(lib => lib.name == libName) + const libraryVersion = lib => { + const scratchlib = scratch.libraries.find(scratchlib => scratchlib.name == lib.name) + if (scratchlib != null) { + return scratchlib.version + } else { + return "___NULL_VERSION___" + } + } + + const setLibraryVersion = (libName, ver) => { + if (ver == "___NULL_VERSION___") { + return unsetLibrary(libName) + } + + // clone the libraries + const libs = JSON.parse(JSON.stringify(scratch.libraries)) + // Check if the library is already enabled, if so return it + const scratchlib = scratch.libraries.find(scratchlib => scratchlib.name == libName) + if (scratchlib != null) { + // If it is, set the version + scratchlib.version = ver + } else { + // If it isn't, add the library to the list + libs.push({ name: libName, version: ver }) + } + onChange({ + libraries: libs, + }) + } + const unsetLibrary = libName => { + // clone the libraries + let libs = JSON.parse(JSON.stringify(scratch.libraries)) + // Only keep the libs whose name are not libName + libs = libs.filter(lib => lib.name != libName) + onChange({ + libraries: libs, + }) + } + const toggleLibrary = lib => { + if (hasLibrary(lib.name)) { + unsetLibrary(lib.name) + } else { + setLibraryVersion(lib.name, lib.supported_versions[0]) + } + } + + const selectOptions = lib => Object.fromEntries([["___NULL_VERSION___", "Disabled"], ...lib.supported_versions.map(ver => [ver, ver])]) + + const librariesElements = libraries.map(lib =>
+ toggleLibrary(lib)} /> + +