-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 => <div key={lib.name} className={styles.library}> | ||
<input type="checkbox" checked={hasLibrary(lib.name)} onChange={() => toggleLibrary(lib)} /> | ||
<label className={styles.libraryName}>{lib.name}</label> | ||
<Select | ||
value={libraryVersion(lib)} | ||
onChange={value => setLibraryVersion(lib.name, value)} | ||
options={selectOptions(lib)} | ||
className={styles.librarySelect} /> | ||
</div>) | ||
|
||
return <div> | ||
<section className={styles.section}> | ||
<h3>Libraries</h3> | ||
{librariesElements} | ||
</section> | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters