Skip to content

Commit

Permalink
drop valid Pymatgen structures as JSON files on Structure.svelte to d…
Browse files Browse the repository at this point in the history
…isplay them

add props
dragover: boolean = false
allow_file_drop: boolean = true
add slot='formula' in bottom left
  • Loading branch information
janosh committed Jun 5, 2023
1 parent 9e1af46 commit 7127b18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
38 changes: 37 additions & 1 deletion src/lib/Structure.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { Canvas } from '@threlte/core'
import { Tooltip } from 'svelte-zoo'
import Icon from './Icon.svelte'
import StructureLegend from './StructureLegend.svelte'
Expand Down Expand Up @@ -50,6 +49,8 @@
export let reset_text: string = `Reset view`
export let color_scheme: 'Jmol' | 'Vesta' = `Vesta`
export let hovered: boolean = false
export let dragover: boolean = false
export let allow_file_drop: boolean = true
// interactivity()
$: $element_colors = element_color_schemes[color_scheme]
Expand Down Expand Up @@ -98,6 +99,21 @@
: `${alphabetical_formula(structure)}.json`
download(data, filename, `application/json`)
}
function on_file_drop(event: DragEvent) {
dragover = false
if (!allow_file_drop) return
const file = event.dataTransfer?.items[0].getAsFile()
const reader = new FileReader()
reader.onloadend = (event: ProgressEvent<FileReader>) => {
try {
structure = JSON.parse(event.target.result)
} catch (error) {
console.error(`Invalid JSON file`, error)
}
}
reader.readAsText(file)
}
</script>

<svelte:window
Expand All @@ -114,6 +130,10 @@
bind:clientHeight={height}
on:mouseenter={() => (hovered = true)}
on:mouseleave={() => (hovered = false)}
on:drop|preventDefault={on_file_drop}
on:dragover|preventDefault={() => allow_file_drop && (dragover = true)}
on:dragleave|preventDefault={() => allow_file_drop && (dragover = false)}
class:dragover
>
<section class:visible={visible_buttons}>
<!-- TODO show only when camera was moved -->
Expand Down Expand Up @@ -200,6 +220,12 @@
</Canvas>

<StructureLegend elements={get_elements(structure)} />

<slot name="formula" {structure}>
<div class="formula">
{alphabetical_formula(structure)}
</div>
</slot>
</div>
{:else if structure}
<p class="warn">No sites found in structure</p>
Expand All @@ -217,6 +243,16 @@
container-type: inline-size;
--controls-transition-duration: 0.3s;
}
.structure.dragover {
background: rgba(0, 0, 0, 0.7);
}
div.formula {
position: absolute;
bottom: 0;
left: 0;
font-size: 1.2em;
padding: 1pt 5pt;
}
section {
position: absolute;
Expand Down
5 changes: 2 additions & 3 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
PropertySelect,
Structure,
TableInset,
alphabetical_formula,
element_data,
} from '$lib'
import { property_labels } from '$lib/labels'
Expand All @@ -22,7 +21,7 @@
let heatmap_key: string | null = null
$: heatmap_values = heatmap_key ? element_data.map((el) => el[heatmap_key]) : []
$: href = `https://materialsproject.org/materials/${mp_id[0]}`
$: structure = structures.find((struct) => struct.id === mp_id[0]) || {}
$: structure = structures.find((struct) => struct.id === mp_id[0])
let mp_id = [`mp-756175`]
$: [y_label, y_unit] = property_labels[heatmap_key] ?? []
Expand Down Expand Up @@ -92,7 +91,7 @@
<ColorCustomizer collapsible={false} />
{/if}

<h2 id={mp_id[0]}><a {href}>{mp_id}</a> ({alphabetical_formula(structure)})</h2>
<h2 id={mp_id[0]}><a {href}>{mp_id}</a></h2>
<Structure {structure} />

<h2>More Demos</h2>
Expand Down

0 comments on commit 7127b18

Please sign in to comment.