-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Add json editor field input component
- Loading branch information
1 parent
152e64b
commit 4297b87
Showing
10 changed files
with
243 additions
and
22 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
4 changes: 4 additions & 0 deletions
4
...c/shared/dynamic-form-inputs/code-editor-form-input/json-editor-form-input.component.html
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,4 @@ | ||
<div #editor class="json-editor" [class.invalid]="!isValid" [style.height]="height || '300px'"></div> | ||
<div class="error-message"> | ||
<span *ngIf="errorMessage">{{ errorMessage }}</span> | ||
</div> |
44 changes: 44 additions & 0 deletions
44
...c/shared/dynamic-form-inputs/code-editor-form-input/json-editor-form-input.component.scss
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,44 @@ | ||
.json-editor { | ||
min-height: 6rem; | ||
background-color: var(--color-json-editor-background-color); | ||
color: var(--color-json-editor-text); | ||
border: 1px solid var(--color-component-border-200); | ||
border-radius: 3px; | ||
padding: 6px; | ||
tab-size: 4; | ||
font-family: 'Source Code Pro', 'Lucida Console', Monaco, monospace; | ||
font-size: 14px; | ||
font-weight: 400; | ||
height: 340px; | ||
letter-spacing: normal; | ||
line-height: 20px; | ||
|
||
&:focus { | ||
border-color: var(--color-primary-500); | ||
} | ||
|
||
&.invalid { | ||
border-color: var(--clr-forms-invalid-color); | ||
} | ||
|
||
// prettier-ignore | ||
::ng-deep { | ||
.je-string { color: var(--color-json-editor-string); } | ||
.je-number { color: var(--color-json-editor-number); } | ||
.je-boolean { color: var(--color-json-editor-boolean); } | ||
.je-null { color: var(--color-json-editor-null); } | ||
.je-key { color: var(--color-json-editor-key); } | ||
.je-error { | ||
text-decoration-line: underline; | ||
text-decoration-style: wavy; | ||
text-decoration-color: var(--color-json-editor-error); | ||
} | ||
} | ||
} | ||
|
||
.error-message { | ||
min-height: 1rem; | ||
color: var(--color-json-editor-error); | ||
} | ||
|
||
|
129 changes: 129 additions & 0 deletions
129
...src/shared/dynamic-form-inputs/code-editor-form-input/json-editor-form-input.component.ts
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,129 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
OnInit, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import { AbstractControl, FormControl, ValidationErrors, ValidatorFn } from '@angular/forms'; | ||
import { DefaultFormComponentConfig, DefaultFormComponentId } from '@vendure/common/lib/shared-types'; | ||
import { CodeJar } from 'codejar'; | ||
|
||
import { FormInputComponent } from '../../../common/component-registry-types'; | ||
|
||
export function jsonValidator(): ValidatorFn { | ||
return (control: AbstractControl): ValidationErrors | null => { | ||
const error: ValidationErrors = { jsonInvalid: true }; | ||
|
||
try { | ||
JSON.parse(control.value); | ||
} catch (e) { | ||
control.setErrors(error); | ||
return error; | ||
} | ||
|
||
control.setErrors(null); | ||
return null; | ||
}; | ||
} | ||
|
||
@Component({ | ||
selector: 'vdr-json-editor-form-input', | ||
templateUrl: './json-editor-form-input.component.html', | ||
styleUrls: ['./json-editor-form-input.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class JsonEditorFormInputComponent implements FormInputComponent, AfterViewInit, OnInit { | ||
static readonly id: DefaultFormComponentId = 'json-editor-form-input'; | ||
readonly: boolean; | ||
formControl: FormControl; | ||
config: DefaultFormComponentConfig<'json-editor-form-input'>; | ||
isValid = true; | ||
height: DefaultFormComponentConfig<'json-editor-form-input'>['height']; | ||
errorMessage: string | undefined; | ||
@ViewChild('editor') private editorElementRef: ElementRef<HTMLDivElement>; | ||
jar: CodeJar; | ||
|
||
constructor(private changeDetector: ChangeDetectorRef) {} | ||
|
||
ngOnInit() { | ||
this.formControl.addValidators(jsonValidator()); | ||
} | ||
|
||
ngAfterViewInit() { | ||
let lastVal = ''; | ||
const highlight = (editor: HTMLElement) => { | ||
const code = editor.textContent ?? ''; | ||
if (code === lastVal) { | ||
return; | ||
} | ||
lastVal = code; | ||
this.errorMessage = this.getJsonError(code); | ||
this.changeDetector.markForCheck(); | ||
editor.innerHTML = this.syntaxHighlight(code, this.getErrorPos(this.errorMessage)); | ||
}; | ||
this.jar = CodeJar(this.editorElementRef.nativeElement, highlight); | ||
this.jar.onUpdate(value => { | ||
this.formControl.setValue(value); | ||
this.formControl.markAsDirty(); | ||
this.isValid = this.formControl.valid; | ||
}); | ||
this.jar.updateCode(this.formControl.value); | ||
|
||
if (this.readonly) { | ||
this.editorElementRef.nativeElement.contentEditable = 'false'; | ||
} | ||
} | ||
|
||
private getJsonError(json: string): string | undefined { | ||
try { | ||
JSON.parse(json); | ||
} catch (e) { | ||
return e.message; | ||
} | ||
return; | ||
} | ||
|
||
private getErrorPos(errorMessage: string | undefined): number | undefined { | ||
if (!errorMessage) { | ||
return; | ||
} | ||
const matches = errorMessage.match(/at position (\d+)/); | ||
const pos = matches?.[1]; | ||
return pos != null ? +pos : undefined; | ||
} | ||
|
||
private syntaxHighlight(json: string, errorPos: number | undefined) { | ||
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||
let hasMarkedError = false; | ||
return json.replace( | ||
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, | ||
(match, ...args) => { | ||
let cls = 'number'; | ||
if (/^"/.test(match)) { | ||
if (/:$/.test(match)) { | ||
cls = 'key'; | ||
} else { | ||
cls = 'string'; | ||
} | ||
} else if (/true|false/.test(match)) { | ||
cls = 'boolean'; | ||
} else if (/null/.test(match)) { | ||
cls = 'null'; | ||
} | ||
let errorClass = ''; | ||
if (errorPos && !hasMarkedError) { | ||
const length = args[0].length; | ||
const offset = args[4]; | ||
if (errorPos <= length + offset) { | ||
errorClass = 'je-error'; | ||
hasMarkedError = true; | ||
} | ||
} | ||
return '<span class="je-' + cls + ' ' + errorClass + '">' + match + '</span>'; | ||
}, | ||
); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -6584,6 +6584,11 @@ code-point-at@^1.0.0: | |
resolved "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" | ||
integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c= | ||
|
||
codejar@^3.5.0: | ||
version "3.5.0" | ||
resolved "https://registry.npmjs.org/codejar/-/codejar-3.5.0.tgz#be3a6a77b4c422998e56710ca854d166f8507eb2" | ||
integrity sha512-uXrFZZ+yb23YY7+WtTux2Yyokt+Lty/kBnW/OhhEGp8IW8/lrJw5Gs1wwCyt2vpMfsVdudLmV5xAgYqsZY/49A== | ||
|
||
codelyzer@^6.0.0: | ||
version "6.0.2" | ||
resolved "https://registry.npmjs.org/codelyzer/-/codelyzer-6.0.2.tgz#25d72eae641e8ff13ffd7d99b27c9c7ad5d7e135" | ||
|
@@ -10120,7 +10125,7 @@ ieee754@^1.1.13, ieee754@^1.1.4, ieee754@^1.2.1: | |
resolved "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" | ||
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== | ||
|
||
ignore-walk@^3.0.1: | ||
ignore-walk@^3.0.1, ignore-walk@^3.0.3: | ||
version "3.0.4" | ||
resolved "https://registry.npmjs.org/ignore-walk/-/ignore-walk-3.0.4.tgz#c9a09f69b7c7b479a5d74ac1a3c0d4236d2a6335" | ||
integrity sha512-PY6Ii8o1jMRA1z4F2hRkH/xN59ox43DavKvD3oDpfurRlOJyAHpifIwpbdv1n4jt4ov0jSpw3kQ4GhJnpBL6WQ== | ||
|
@@ -13844,14 +13849,24 @@ [email protected], npm-package-arg@^8.0.0, npm-package-arg@^8.0.1, npm-packa | |
semver "^7.3.4" | ||
validate-npm-package-name "^3.0.0" | ||
|
||
npm-packlist@1.1.12, npm-packlist@^1.1.6, npm-packlist@^2.1.4: | ||
npm-packlist@^1.1.6: | ||
version "1.1.12" | ||
resolved "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.1.12.tgz#22bde2ebc12e72ca482abd67afc51eb49377243a" | ||
integrity sha512-WJKFOVMeAlsU/pjXuqVdzU0WfgtIBCupkEVwn+1Y0ERAbUfWw8R4GjgVbaKnUjRoD2FoQbHOCbOyT5Mbs9Lw4g== | ||
dependencies: | ||
ignore-walk "^3.0.1" | ||
npm-bundled "^1.0.1" | ||
|
||
npm-packlist@^2.1.4: | ||
version "2.2.2" | ||
resolved "https://registry.npmjs.org/npm-packlist/-/npm-packlist-2.2.2.tgz#076b97293fa620f632833186a7a8f65aaa6148c8" | ||
integrity sha512-Jt01acDvJRhJGthnUJVF/w6gumWOZxO7IkpY/lsX9//zqQgnF7OJaxgQXcerd4uQOLu7W5bkb4mChL9mdfm+Zg== | ||
dependencies: | ||
glob "^7.1.6" | ||
ignore-walk "^3.0.3" | ||
npm-bundled "^1.1.1" | ||
npm-normalize-package-bin "^1.0.1" | ||
|
||
[email protected], npm-pick-manifest@^6.0.0, npm-pick-manifest@^6.1.1: | ||
version "6.1.1" | ||
resolved "https://registry.npmjs.org/npm-pick-manifest/-/npm-pick-manifest-6.1.1.tgz#7b5484ca2c908565f43b7f27644f36bb816f5148" | ||
|