Skip to content

Commit 38780b4

Browse files
committed
updated codemirror setup
1 parent 990981b commit 38780b4

File tree

8 files changed

+85
-13
lines changed

8 files changed

+85
-13
lines changed

packages/components/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,11 @@
397397
"types": "./declarations/*.d.ts",
398398
"default": "./dist/*"
399399
},
400-
"./addon-main.js": "./addon-main.cjs"
400+
"./addon-main.js": "./addon-main.cjs",
401+
"./codemirror": {
402+
"types": "./declarations/codemirror.d.ts",
403+
"default": "./dist/codemirror.js"
404+
}
401405
},
402406
"typesVersions": {
403407
"*": {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from '@codemirror/state';
2+
export * from '@codemirror/view';
3+
export * from '@codemirror/commands';
4+
export * from '@codemirror/language';
5+
export * from '@codemirror/lint';

packages/components/src/components/hds/code-editor/index.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
ariaDescribedBy=this.ariaDescribedBy
5656
ariaLabel=@ariaLabel
5757
ariaLabelledBy=this.ariaLabelledBy
58+
customExtensions=@customExtensions
5859
cspNonce=@cspNonce
5960
extraKeys=@extraKeys
6061
hasLineWrapping=@hasLineWrapping

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ type HdsCodeEditorBlurHandler = (
4242
) => void;
4343

4444
interface HdsCodeEditorExtraKeys {
45-
[key: string]: () => void;
46-
}
45+
[key: string]: (view: EditorViewType) => boolean;
46+
};
4747

4848
export interface HdsCodeEditorSignature {
4949
Args: {
@@ -53,6 +53,7 @@ export interface HdsCodeEditorSignature {
5353
ariaLabelledBy?: string;
5454
cspNonce?: string;
5555
extraKeys?: HdsCodeEditorExtraKeys;
56+
customExtensions?: Extension[];
5657
hasLineWrapping?: boolean;
5758
isLintingEnabled?: boolean;
5859
language?: HdsCodeEditorLanguages;
@@ -397,8 +398,18 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
397398
language,
398399
hasLineWrapping,
399400
isLintingEnabled,
401+
customExtensions,
400402
onLint,
401-
}) => {
403+
}: Pick<
404+
HdsCodeEditorSignature['Args']['Named'],
405+
| 'cspNonce'
406+
| 'extraKeys'
407+
| 'language'
408+
| 'hasLineWrapping'
409+
| 'isLintingEnabled'
410+
| 'customExtensions'
411+
| 'onLint'
412+
>) => {
402413
const [
403414
{
404415
keymap,
@@ -417,11 +428,8 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
417428

418429
const languageExtensions = await this._loadLanguageExtensionsTask.perform(
419430
{
420-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
421431
language,
422-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
423432
isLintingEnabled,
424-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
425433
onLint,
426434
}
427435
);
@@ -448,7 +456,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
448456
hasLineWrapping ? EditorView.lineWrapping : []
449457
);
450458

451-
let extensions = [
459+
let extensions: Extension[] = [
452460
lineWrappingExtension,
453461
bracketMatching(),
454462
highlightActiveLine(),
@@ -458,13 +466,14 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
458466
keymap.of([...defaultKeymap, ...historyKeymap]),
459467
// custom extensions
460468
handleUpdateExtension,
469+
// user-provided extensions
470+
...(customExtensions ?? []),
461471
// hds dark theme
462472
hdsDarkTheme,
463473
syntaxHighlighting(hdsDarkHighlightStyle),
464474
];
465475

466476
if (extraKeys !== undefined) {
467-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
468477
const customKeyMap = Object.entries(extraKeys).map(([key, value]) => ({
469478
key: key,
470479
run: value,
@@ -478,11 +487,9 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
478487
}
479488

480489
// add nonce to the editor view if it exists
481-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
482490
const nonce = cspNonce ?? getCSPNonceFromMeta();
483491

484492
if (nonce !== undefined) {
485-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
486493
extensions = [...extensions, EditorView.cspNonce.of(nonce)];
487494
}
488495

@@ -504,6 +511,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
504511
value,
505512
hasLineWrapping,
506513
isLintingEnabled,
514+
customExtensions,
507515
onLint,
508516
}: Pick<
509517
HdsCodeEditorSignature['Args']['Named'],
@@ -513,6 +521,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
513521
| 'value'
514522
| 'hasLineWrapping'
515523
| 'isLintingEnabled'
524+
| 'customExtensions'
516525
| 'onLint'
517526
>
518527
) => {
@@ -525,6 +534,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
525534
language,
526535
hasLineWrapping: hasLineWrapping ?? false,
527536
isLintingEnabled,
537+
customExtensions,
528538
onLint,
529539
});
530540

@@ -592,6 +602,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
592602
isLintingEnabled,
593603
language,
594604
value,
605+
customExtensions,
595606
} = named;
596607

597608
this.onInput = onInput;
@@ -610,6 +621,7 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
610621
extraKeys,
611622
language,
612623
value,
624+
customExtensions,
613625
});
614626

615627
if (editor === undefined) {

showcase/app/components/page-components/code-editor/index.gts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SubSectionStandalone from 'showcase/components/page-components/code-edito
1212
import SubSectionHeader from 'showcase/components/page-components/code-editor/sub-sections/header';
1313
import SubSectionSyntaxHighlighting from 'showcase/components/page-components/code-editor/sub-sections/syntax-highlighting';
1414
import SubSectionLinting from 'showcase/components/page-components/code-editor/sub-sections/linting';
15+
import SubSectionCustomExtension from 'showcase/components/page-components/code-editor/sub-sections/custom-extension';
1516
import SubSectionStandaloneModifier from 'showcase/components/page-components/code-editor/sub-sections/standalone-modifier';
1617

1718
const CodeEditorIndex: TemplateOnlyComponent = <template>
@@ -25,6 +26,7 @@ const CodeEditorIndex: TemplateOnlyComponent = <template>
2526
<SubSectionHeader />
2627
<SubSectionSyntaxHighlighting />
2728
<SubSectionLinting />
29+
<SubSectionCustomExtension />
2830
<SubSectionStandaloneModifier />
2931
</section>
3032
</template>;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
import Component from '@glimmer/component';
6+
import { keymap } from '@hashicorp/design-system-components/codemirror';
7+
8+
import ShwTextH2 from 'showcase/components/shw/text/h2';
9+
10+
import { HdsCodeEditor } from '@hashicorp/design-system-components/components';
11+
12+
import type { EditorView } from '@hashicorp/design-system-components/codemirror';
13+
14+
const insertCodeSnippet = (view: EditorView) => {
15+
view.dispatch({
16+
changes: {
17+
from: view.state.selection.main.head,
18+
insert: '// This is a custom inserted snippet\n',
19+
},
20+
});
21+
22+
return true;
23+
};
24+
25+
const codeSnippetKeymap = keymap.of([
26+
{
27+
key: 'Ctrl-Shift-h',
28+
run: insertCodeSnippet,
29+
},
30+
]);
31+
32+
export default class SubSectionCustomExtension extends Component {
33+
customExtensions = [codeSnippetKeymap];
34+
35+
<template>
36+
<ShwTextH2>Custom Extension</ShwTextH2>
37+
38+
<HdsCodeEditor @customExtensions={{this.customExtensions}} as |CE|>
39+
<CE.Title>Code editor with custom extension</CE.Title>
40+
<CE.Description>
41+
This extension will listen for a specific key combination (<code
42+
>Ctrl-Shift-h</code>
43+
) and insert a predefined snippet of code at the current cursor
44+
position.
45+
</CE.Description>
46+
</HdsCodeEditor>
47+
</template>
48+
}

showcase/app/components/page-components/code-editor/sub-sections/linting.gts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: MPL-2.0
44
*/
55
import Component from '@glimmer/component';
6-
import type { Diagnostic as DiagnosticType } from '@codemirror/lint';
6+
import type { Diagnostic as DiagnosticType } from '@hashicorp/design-system-components/codemirror';
77

88
import ShwTextH2 from 'showcase/components/shw/text/h2';
99
import ShwFlex from 'showcase/components/shw/flex';

showcase/tests/unit/modifiers/hds-code-editor/linters/json-linter-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { module, test } from 'qunit';
7-
import { Text } from '@codemirror/state';
7+
import { Text } from '@hashicorp/design-system-components/codemirror';
88
import {
99
findNextToken,
1010
determineErrorMessage,

0 commit comments

Comments
 (0)