Skip to content

Commit bdc3ece

Browse files
committed
added examples to the showcase
1 parent f264690 commit bdc3ece

File tree

4 files changed

+91
-38
lines changed

4 files changed

+91
-38
lines changed

packages/components/src/modifiers/hds-code-editor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import hdsDarkHighlightStyle from './hds-code-editor/highlight-styles/hds-dark-h
3333
import type { PositionalArgs, NamedArgs } from 'ember-modifier';
3434
import type { HdsCodeEditorLanguages } from 'src/types/hds-code-editor.types';
3535

36-
const LANGUAGE_MAP: Record<HdsCodeEditorLanguages, Extension> = {
36+
export const LANGUAGE_MAP: Record<HdsCodeEditorLanguages, Extension> = {
3737
go: go(),
3838
javascript: javascript(),
3939
json: json(),

showcase/app/controllers/components/code-editor.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,44 @@ import Controller from '@ember/controller';
77
import { tracked } from '@glimmer/tracking';
88

99
export default class CodeEditorController extends Controller {
10-
@tracked demoCode = `function greetUser(username) {
11-
// Get the current hour of the day
12-
const currentHour = new Date().getHours();
13-
let greeting;
10+
languages = [
11+
{
12+
value: 'go',
13+
label: 'Go',
14+
code: `package main
1415
15-
// Determine the greeting based on the time of day
16-
if (currentHour < 12) {
17-
greeting = "Good morning";
18-
} else if (currentHour < 18) {
19-
greeting = "Good afternoon";
20-
} else {
21-
greeting = "Good evening";
22-
}
16+
import "fmt"
2317
24-
// Create a personalized message
25-
const message = \`\${greeting}, \${username}! Welcome to our site.\`;
18+
func main() {
19+
fmt.Println("Hello, world!")
20+
fmt.Println("Welcome to Go!")
21+
}`
22+
},
23+
{
24+
value: 'javascript',
25+
label: 'JavaScript',
26+
code: `function greet() {
27+
console.log('Hello, world!');
28+
}
2629
27-
// Create a new HTML element to display the message
28-
const greetingElement = document.createElement("div");
29-
greetingElement.textContent = message;
30-
greetingElement.style.fontSize = "20px";
31-
greetingElement.style.fontWeight = "bold";
32-
greetingElement.style.marginTop = "20px";
30+
greet();`
31+
},
32+
{
33+
value: 'json',
34+
label: 'JSON',
35+
code: `{
36+
"message": "Hello, world!",
37+
"status": "success",
38+
"data": null
39+
}`
40+
},
41+
{
42+
value: 'sql',
43+
label: 'SQL',
44+
code: `SELECT 'Hello, world!';
45+
SELECT 'Welcome to SQL!';
46+
SELECT 'Enjoy coding!';`
47+
}
48+
];
3349

34-
// Add the greeting element to the document body
35-
document.body.appendChild(greetingElement);
36-
}`;
3750
}

showcase/app/templates/components/code-editor.hbs

+51-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,55 @@
88
<Shw::Text::H1>CodeEditor</Shw::Text::H1>
99

1010
<section data-test-percy>
11-
<Hds::CodeEditor
12-
@canCopy={{true}}
13-
@canExpand={{true}}
14-
@title="Lorem Ipsum"
15-
@description="lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
16-
@value={{this.demoCode}}
17-
@language="json"
18-
@onInput={{fn (mut this.demoCode)}}
19-
/>
11+
<Shw::Text::H2>Content</Shw::Text::H2>
12+
<Shw::Flex @direction="column" as |SF|>
13+
<SF.Item @label="No content">
14+
<Hds::CodeEditor />
15+
</SF.Item>
16+
<SF.Item @label="With initial content">
17+
<Hds::CodeEditor @value={{this.demoJavascript}} />
18+
</SF.Item>
19+
</Shw::Flex>
20+
21+
<Shw::Text::H2>Header</Shw::Text::H2>
22+
<Shw::Flex @direction="column" as |SF|>
23+
<SF.Item @label="With title">
24+
<Hds::CodeEditor @title="Code editor with title" />
25+
</SF.Item>
26+
<SF.Item @label="With description">
27+
<Hds::CodeEditor @description="Code editor with description" />
28+
</SF.Item>
29+
<SF.Item @label="Expandable editor">
30+
<Hds::CodeEditor @canExpand={{true}} />
31+
</SF.Item>
32+
<SF.Item @label="With copyable content">
33+
<Hds::CodeEditor @canCopy={{true}} @value={{this.demoJavascript}} />
34+
</SF.Item>
35+
<SF.Item @label="With custom header content">
36+
<Hds::CodeEditor as |CE|>
37+
<CE.ToolbarButton @text="Custom button" />
38+
</Hds::CodeEditor>
39+
</SF.Item>
40+
<SF.Item @label="Complex header">
41+
<Hds::CodeEditor
42+
@title="Complex header"
43+
@description="This is a complex example"
44+
@canCopy={{true}}
45+
@canExpand={{true}}
46+
@value={{this.demoJavascript}}
47+
as |CE|
48+
>
49+
<CE.ToolbarButton @text="Custom button" />
50+
</Hds::CodeEditor>
51+
</SF.Item>
52+
</Shw::Flex>
53+
54+
<Shw::Text::H2>Syntax highlighting</Shw::Text::H2>
55+
<Shw::Grid @columns={{2}} @gap="2rem" as |SG|>
56+
{{#each this.languages as |lang|}}
57+
<SG.Item @label={{lang.label}}>
58+
<Hds::CodeEditor @language={{lang.value}} @value={{lang.code}} />
59+
</SG.Item>
60+
{{/each}}
61+
</Shw::Grid>
2062
</section>

showcase/tests/integration/modifiers/hds-code-editor-test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { hbs } from 'ember-cli-htmlbars';
66
module('Integration | Modifier | hds-code-editor', function (hooks) {
77
setupRenderingTest(hooks);
88

9-
// Replace this with your real tests.
10-
test('it renders', async function (assert) {
11-
await render(hbs`<div {{hds-code-editor}}></div>`);
12-
13-
assert.ok(true);
9+
test('it converts the element it is applied to into a CodeMirror editor', async function (assert) {
10+
await render(hbs`<div id="code-editor-wrapper" {{hds-code-editor}}></div>`);
11+
assert.dom('#code-editor-wrapper .cm-editor').exists('code editor is rendered');
1412
});
1513
});

0 commit comments

Comments
 (0)