-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import/export JSON for models and diagrams #332
+476
−182
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4256747
first commit, output working and input up to the console
KevinDCarlson 7815443
importing json
KevinDCarlson 309af97
adding import and export files
KevinDCarlson 5f8720d
added auto-close prop back to Open element
KevinDCarlson bd5b1d8
improving file structure
KevinDCarlson ce5544e
change Open component to Import
KevinDCarlson 277382c
Add diagram import-export
KevinDCarlson c86f6f4
Added styling for import menu
KevinDCarlson 1cb2f0f
rearrange menu
KevinDCarlson c65821c
Little improvements
KevinDCarlson 9ef59da
Address code quality nits from PR review
jmoggr 676338f
Merge menu components for the model and diagram editor (#407)
jmoggr 7113f08
Fix Evan nits, rebase
KevinDCarlson 77c3bdb
Wrap JsonImport in error boundary
jmoggr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
.json_import { | ||
min-width: 20em; | ||
max-width: 80vw; | ||
margin: 0 auto; | ||
padding: 1rem; | ||
border: 1px solid #ccc; | ||
border-radius: 8px; | ||
background-color: #f9f9f9; | ||
width: auto; | ||
} | ||
|
||
.json_import .flex { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
} | ||
|
||
.json_import label { | ||
font-weight: 500; | ||
} | ||
|
||
.json_import input[type="file"] { | ||
border: 1px solid #ccc; | ||
padding: 0.5rem; | ||
border-radius: 4px; | ||
} | ||
|
||
.json_import textarea { | ||
border: 1px solid #ccc; | ||
padding: 0.5rem; | ||
border-radius: 4px; | ||
font-family: monospace; | ||
resize: vertical; | ||
min-height: 10rem; | ||
max-height: 80vh; | ||
} | ||
|
||
.json_import button { | ||
padding: 0.5rem 1rem; | ||
background-color: #007bff; | ||
color: white; | ||
border: none; | ||
border-radius: 4px; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
} | ||
|
||
.json_import button:hover { | ||
background-color: #0056b3; | ||
} | ||
|
||
.json_import .error { | ||
color: red; | ||
margin-top: 1rem; | ||
} |
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,120 @@ | ||
import { ErrorBoundary, createSignal } from "solid-js"; | ||
import type { JSX } from "solid-js"; | ||
import type { Document } from "../api"; | ||
import "./json_import.css"; | ||
import { ErrorBoundaryDialog } from "../util/errors"; | ||
|
||
interface JsonImportProps<T extends string> { | ||
onImport: (data: Document<T>) => void; | ||
validate?: (data: Document<T>) => boolean | string; | ||
} | ||
|
||
/** | ||
* Component for importing JSON data. | ||
* Handles file upload and direct clipboard paste. | ||
* File size is currently limited to 5MB. | ||
* | ||
*/ | ||
export const JsonImport = <T extends string>({ onImport, validate }: JsonImportProps<T>) => { | ||
KevinDCarlson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [error, setError] = createSignal<string | null>(null); | ||
KevinDCarlson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [importValue, setImportValue] = createSignal(""); | ||
|
||
const handleError = (e: unknown) => { | ||
setError(e instanceof Error ? e.message : "Unknown error occurred"); | ||
}; | ||
|
||
const validateAndImport = (jsonString: string) => { | ||
try { | ||
const data = JSON.parse(jsonString); | ||
|
||
// Run custom validation if provided | ||
if (validate) { | ||
const validationResult = validate(data); | ||
if (typeof validationResult === "string") { | ||
setError(validationResult); | ||
return; | ||
} | ||
} | ||
|
||
// Clear any previous errors and import | ||
setError(null); | ||
onImport(data); | ||
setImportValue(""); // Clear paste area after successful import | ||
} catch (e) { | ||
epatters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
handleError(e); | ||
} | ||
}; | ||
|
||
// Handle file upload | ||
const handleFileUpload: JSX.EventHandler<HTMLInputElement, Event> = async (event) => { | ||
const input = event.currentTarget; | ||
|
||
const file = input.files?.[0]; | ||
if (!file) return; | ||
|
||
// Validate file type | ||
if (file.type !== "application/json" && !file.name.endsWith(".json")) { | ||
setError("Please upload a JSON file"); | ||
return; | ||
} | ||
|
||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB | ||
if (file.size > MAX_FILE_SIZE) { | ||
setError("File size exceeds 5MB limit"); | ||
return; | ||
} | ||
|
||
const text = await file.text(); | ||
validateAndImport(text); | ||
|
||
// Reset file input | ||
input.value = ""; | ||
}; | ||
|
||
// Handle paste | ||
const handleTextareaSubmit = () => { | ||
if (!importValue().trim()) { | ||
setError("Please enter some JSON"); | ||
return; | ||
} | ||
validateAndImport(importValue()); | ||
}; | ||
|
||
const handleInput: JSX.EventHandler<HTMLTextAreaElement, Event> = (event) => { | ||
const textarea = event.currentTarget; | ||
setImportValue(textarea.value); | ||
}; | ||
|
||
return ( | ||
<div class="json_import"> | ||
<ErrorBoundary fallback={(err) => <ErrorBoundaryDialog error={err} />}> | ||
{/* File upload */} | ||
<div class="flex"> | ||
<label>Import from file:</label> | ||
<input | ||
type="file" | ||
accept=".json,application/json" | ||
onChange={handleFileUpload} | ||
/> | ||
</div> | ||
|
||
{/* JSON paste */} | ||
<div class="flex"> | ||
<label>Or paste JSON:</label> | ||
<textarea | ||
value={importValue()} | ||
onInput={handleInput} | ||
onPaste={handleInput} | ||
placeholder="Paste your JSON here..." | ||
/> | ||
<button onClick={handleTextareaSubmit} aria-label="Import JSON"> | ||
Import Pasted JSON | ||
</button> | ||
</div> | ||
|
||
{/* Error display */} | ||
{error() && <div class="error">{error()}</div>} | ||
</ErrorBoundary> | ||
</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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've put this in
components
and export inutil
because export doesn't actually build a component but it needs to be used bymodel_menu
,diagram_menu
, andanalysis_menu
. Not sure if that's the right file structure.