-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a Svelte examples section
- Loading branch information
Showing
10 changed files
with
513 additions
and
21 deletions.
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
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 @@ | ||
<script> | ||
import { JSONEditor } from '$lib' // replace this with 'svelte-jsoneditor' | ||
let json = { | ||
array: [1, 2, 3], | ||
boolean: true, | ||
color: '#82b92c', | ||
null: null, | ||
number: 123, | ||
object: { a: 'b', c: 'd' }, | ||
string: 'Hello World' | ||
} | ||
let text = undefined // used when in code mode | ||
function handleChange(content) { | ||
console.log('contents changed:', { json, text }) | ||
json = content.json | ||
text = content.text | ||
} | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Basic usage (one-way binding) | svelte-jsoneditor</title> | ||
</svelte:head> | ||
|
||
<h1>Basic usage (one-way binding)</h1> | ||
|
||
<p> | ||
Use JSONEditor with one-way binding: pass read-only properties <code>json</code> and | ||
<code>text</code>, and pass an <code>onChange</code> callback function to receive changes. | ||
</p> | ||
|
||
<div class="editor"> | ||
<JSONEditor {json} {text} onChange={handleChange} /> | ||
</div> | ||
|
||
<style> | ||
.editor { | ||
width: 700px; | ||
height: 400px; | ||
} | ||
</style> |
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,38 @@ | ||
<script> | ||
import { JSONEditor } from '$lib' // replace this with 'svelte-jsoneditor' | ||
let json = { | ||
array: [1, 2, 3], | ||
boolean: true, | ||
color: '#82b92c', | ||
null: null, | ||
number: 123, | ||
object: { a: 'b', c: 'd' }, | ||
string: 'Hello World' | ||
} | ||
let text = undefined // used when in code mode | ||
$: console.log('contents changed:', { json, text }) | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Basic usage (two-way binding) | svelte-jsoneditor</title> | ||
</svelte:head> | ||
|
||
<h1>Basic usage (two-way binding)</h1> | ||
|
||
<p> | ||
Use JSONEditor utilizing two-way binding: bind properties <code>json</code> and | ||
<code>text</code>, so they will automatically update on changes. | ||
</p> | ||
|
||
<div class="editor"> | ||
<JSONEditor bind:json bind:text /> | ||
</div> | ||
|
||
<style> | ||
.editor { | ||
width: 700px; | ||
height: 400px; | ||
} | ||
</style> |
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 @@ | ||
<script> | ||
import { JSONEditor } from '$lib' // replace this with 'svelte-jsoneditor' | ||
let json = { | ||
array: [1, 2, 3], | ||
boolean: true, | ||
color: '#82b92c', | ||
null: null, | ||
number: 123, | ||
object: { a: 'b', c: 'd' }, | ||
string: 'Hello World' | ||
} | ||
let text = undefined // used when in code mode | ||
function handleClassName(path, value) { | ||
if (JSON.stringify(path) === '["object","c"]' || JSON.stringify(path) === '["string"]') { | ||
return 'custom-class-highlight' | ||
} | ||
if (value === true || value === false) { | ||
return 'custom-class-boolean' | ||
} | ||
} | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Custom, dynamic styling | svelte-jsoneditor</title> | ||
</svelte:head> | ||
|
||
<h1>Custom, dynamic styling</h1> | ||
|
||
<p> | ||
You can apply dynamic styling depending on the document contents using <code>onClassName</code>. | ||
In this example, all boolean values get a red background, and nodes with paths | ||
<code>/string</code> and <code>/object/c</code> get a green background. | ||
</p> | ||
|
||
<div class="editor"> | ||
<JSONEditor bind:json bind:text onClassName={handleClassName} /> | ||
</div> | ||
|
||
<style> | ||
.editor { | ||
width: 700px; | ||
height: 400px; | ||
} | ||
:global(.json-node.custom-class-highlight .contents) { | ||
background: #bfff66; | ||
} | ||
:global(.json-node.custom-class-boolean .value) { | ||
background: #ffb5c2; | ||
} | ||
</style> |
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,74 @@ | ||
<script> | ||
import { JSONEditor } from '$lib' // replace this with 'svelte-jsoneditor' | ||
import { faCopy } from '@fortawesome/free-regular-svg-icons' | ||
let json = { | ||
array: [1, 2, 3], | ||
boolean: true, | ||
color: '#82b92c', | ||
null: null, | ||
number: 123, | ||
object: { a: 'b', c: 'd' }, | ||
string: 'Hello World' | ||
} | ||
let text = undefined // used when in code mode | ||
function handleCopy() { | ||
console.log('Custom copy button clicked') | ||
const contents = text !== undefined ? text : JSON.stringify(json, null, 2) | ||
navigator.clipboard.writeText(contents).catch((err) => console.error(err)) | ||
} | ||
function handleRenderMenu(mode, items) { | ||
const separator = { | ||
separator: true | ||
} | ||
const customCopyButton = { | ||
onClick: handleCopy, | ||
icon: faCopy, | ||
title: 'Copy document to clipboard', | ||
className: 'custom-copy-button' | ||
} | ||
const space = { | ||
space: true | ||
} | ||
const itemsWithoutSpace = items.slice(0, items.length - 2) | ||
return itemsWithoutSpace.concat([separator, customCopyButton, space]) | ||
} | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Custom menu buttons | svelte-jsoneditor</title> | ||
</svelte:head> | ||
|
||
<h1>Custom menu buttons</h1> | ||
|
||
<p> | ||
You can add/remove buttons from the main menu using <code>onRenderMenu</code>. In this example a | ||
button is added to quickly copy the document to the clipboard. | ||
</p> | ||
|
||
<div class="editor"> | ||
<JSONEditor bind:json bind:text onRenderMenu={handleRenderMenu} /> | ||
</div> | ||
|
||
<style> | ||
.editor { | ||
width: 700px; | ||
height: 400px; | ||
} | ||
:global(.custom-copy-button) { | ||
background: #ff3e00 !important; | ||
} | ||
:global(.custom-copy-button:hover), | ||
:global(.custom-copy-button:active) { | ||
background: #ff632f !important; | ||
} | ||
</style> |
Oops, something went wrong.