-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from appwrite/feat-code-example
feat: code example and json view
- Loading branch information
Showing
6 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<script lang="ts"> | ||
import { Pill } from '$lib/elements'; | ||
import Prism from 'prismjs'; | ||
import 'prismjs/components/prism-dart'; | ||
import 'prismjs/components/prism-kotlin'; | ||
import 'prismjs/components/prism-json'; | ||
import 'prismjs/plugins/autoloader/prism-autoloader'; | ||
import 'prismjs/plugins/line-numbers/prism-line-numbers'; | ||
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'; | ||
import { afterUpdate } from 'svelte'; | ||
import { Copy } from '.'; | ||
export let label: string = null; | ||
export let code: string; | ||
export let language: 'js' | 'html' | 'dart' | 'kotlin' | 'json'; | ||
export let showLineNumbers = false; | ||
export let showCopy = false; | ||
afterUpdate(async () => { | ||
Prism.highlightAll(); | ||
}); | ||
</script> | ||
|
||
<div class="code"> | ||
<div class="controls"> | ||
{#if label} | ||
<Pill>{label}</Pill> | ||
{/if} | ||
{#if showCopy} | ||
<Copy value={code}> | ||
<span class="icon-duplicate" aria-hidden="true" style="cursor: pointer;" /> | ||
</Copy> | ||
{/if} | ||
</div> | ||
<pre class={`language-${language}`} class:line-numbers={showLineNumbers}><code | ||
>{code}</code></pre> | ||
</div> | ||
|
||
<style lang="scss" global> | ||
@import 'prismjs/themes/prism.css'; | ||
div.code { | ||
position: relative; | ||
div.controls { | ||
position: absolute; | ||
right: 0.5rem; | ||
top: 0.5rem; | ||
z-index: 1; | ||
} | ||
} | ||
code, | ||
pre { | ||
&[class*='language-'] { | ||
color: #fcfcff; | ||
text-shadow: none; | ||
font-family: 'Source Code Pro'; | ||
body.theme-light & { | ||
color: #373b4d; | ||
} | ||
} | ||
::selection, | ||
&::selection { | ||
text-shadow: none; | ||
background: #b3d4fc; | ||
} | ||
&.line-numbers .line-numbers-rows { | ||
border: none; | ||
> span::before { | ||
color: #868ea3; | ||
} | ||
} | ||
} | ||
:not(pre) > code[class*='language-'], | ||
pre[class*='language-'] { | ||
background: #1b1b28; | ||
body.theme-light & { | ||
background: #fcfcff; | ||
} | ||
} | ||
.token { | ||
&.comment, | ||
&.prolog, | ||
&.doctype, | ||
&.cdata { | ||
color: #868ea3; | ||
} | ||
&.punctuation { | ||
color: #fcfcff; | ||
body.theme-light & { | ||
color: #373b4d; | ||
} | ||
} | ||
&.property, | ||
&.tag, | ||
&.boolean, | ||
&.number, | ||
&.constant, | ||
&.symbol, | ||
&.deleted, | ||
&.selector, | ||
&.attr-name, | ||
&.string, | ||
&.char, | ||
&.builtin, | ||
&.inserted { | ||
color: #fdc584; | ||
body.theme-light & { | ||
color: #e49545; | ||
} | ||
} | ||
&.operator, | ||
&.entity, | ||
&.url, | ||
.language-css &.string, | ||
.style &.string { | ||
color: #fcfcff; | ||
background: none; | ||
body.theme-light & { | ||
color: #373b4d; | ||
} | ||
} | ||
&.atrule, | ||
&.attr-value, | ||
&.keyword { | ||
color: #cbb1fc; | ||
body.theme-light & { | ||
color: #6a6af7; | ||
} | ||
} | ||
&.function { | ||
color: #ffa1ce; | ||
body.theme-light & { | ||
color: #f02e7f; | ||
} | ||
} | ||
&.class-name { | ||
color: #a1c4ff; | ||
body.theme-light & { | ||
color: #62aed2; | ||
} | ||
} | ||
&.regex, | ||
&.important, | ||
&.variable { | ||
color: #a1c4ff; | ||
body.theme-light & { | ||
color: #62aed2; | ||
} | ||
} | ||
} | ||
</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
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,7 @@ | ||
<script lang="ts"> | ||
import Code from './code.svelte'; | ||
export let code: string; | ||
</script> | ||
|
||
<Code showLineNumbers showCopy language="json" {code} /> |
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,77 @@ | ||
import '@testing-library/jest-dom'; | ||
import { fireEvent, render } from '@testing-library/svelte'; | ||
import { vi } from 'vitest'; | ||
import { Code } from '../../../src/lib/components'; | ||
|
||
test('default props', async () => { | ||
const { container } = render(Code, { | ||
code: 'console.log("test");', | ||
language: 'js' | ||
}); | ||
|
||
expect(container.querySelector('.controls').children.length).toEqual(0); | ||
}); | ||
|
||
test('shows label', async () => { | ||
const { container } = render(Code, { | ||
code: 'console.log("test");', | ||
language: 'js', | ||
label: 'My Label' | ||
}); | ||
|
||
expect(container).toContainHTML('My Label'); | ||
}); | ||
|
||
test('shows line numbers', async () => { | ||
const { container } = render(Code, { | ||
code: 'a\nb;\nc;', | ||
language: 'js', | ||
showLineNumbers: true | ||
}); | ||
|
||
expect(container.querySelectorAll('.line-numbers').length).toEqual(1); | ||
expect(container.querySelectorAll('.line-numbers-rows').length).toEqual(1); | ||
}); | ||
|
||
test('shows code highlighted javascript', async () => { | ||
const { container } = render(Code, { | ||
code: 'console.log("test");', | ||
language: 'js' | ||
}); | ||
|
||
expect(container.querySelectorAll('.token.function').length).toEqual(1); | ||
expect(container.querySelectorAll('.token.string').length).toEqual(1); | ||
expect(container.querySelectorAll('.token.punctuation').length).toEqual(4); | ||
}); | ||
|
||
test('shows code highlighted json', async () => { | ||
const { container } = render(Code, { | ||
code: JSON.stringify({ key: 'value' }), | ||
language: 'json' | ||
}); | ||
|
||
expect(container.querySelectorAll('.token.property').length).toEqual(1); | ||
expect(container.querySelectorAll('.token.operator').length).toEqual(1); | ||
expect(container.querySelectorAll('.token.string').length).toEqual(1); | ||
expect(container.querySelectorAll('.token.punctuation').length).toEqual(2); | ||
}); | ||
|
||
test('copy to clipboard function called on click', async () => { | ||
const { container } = render(Code, { | ||
code: 'console.log("test");', | ||
language: 'js', | ||
showCopy: true | ||
}); | ||
|
||
Object.assign(window.navigator, { | ||
clipboard: { | ||
writeText: vi.fn().mockImplementation(() => Promise.resolve()) | ||
} | ||
}); | ||
|
||
const button = container.querySelector('.icon-duplicate'); | ||
|
||
await fireEvent.click(button); | ||
|
||
expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith('console.log("test");'); | ||
}); |
c7f4d2b
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.
Successfully deployed to the following URLs:
console – ./
brand-new-console.torsten.appwrite.org
console-git-main-appwrite.vercel.app
console-appwrite.vercel.app
svelte-console.vercel.app