Skip to content

Commit

Permalink
feat(ngx-codejar): readonly property
Browse files Browse the repository at this point in the history
set this property if you don't want that a user makes code changes
  • Loading branch information
julianpoemp committed Nov 21, 2023
1 parent 4bc964e commit d908e52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ If you want to use codejar with [prism.js](https://prismjs.com/) you should do t
<td><code>(editor: CodeJarContainer) => {}</code></td>
</tr>
<tr>
<td>Property</td>
<td>readonly</td>
<td>defines if user is allowed to make code changes</td>
<td>false</td>
</tr>
<tr>
<td>Event</td>
<td>update</td>
<td>this event is triggered after the code was updated.</td>
Expand Down
1 change: 1 addition & 0 deletions libs/ngx-codejar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Angular wrapper for CodeJar supporting Prism.js and Highlight.js. With this you can easily add code-editors to your Angular app.",
"peerDependencies": {
"codejar": "^4.0.0",
"codejar-linenumbers": "^1.0.0",
"@angular/common": ">= 14.0.0",
"@angular/core": ">= 14.0.0"
},
Expand Down
30 changes: 26 additions & 4 deletions libs/ngx-codejar/src/lib/ngx-code-jar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {CommonModule} from '@angular/common';
}
`]
})
export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges, OnDestroy {
export class NgxCodeJarComponent implements AfterViewInit, OnChanges, OnDestroy {
constructor(private el: ElementRef, private renderer: Renderer2) {
this.update = new EventEmitter<string>();
}
Expand All @@ -86,6 +86,7 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges, On

@Input() highlighter: 'prism' | 'hljs' = 'hljs';
@Input() options: CodeJarOptions = {};
@Input() readonly = false;

// Events
/**
Expand All @@ -100,18 +101,30 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges, On
if (this.codeJar) {
this.codeJar.updateOptions(changes['options']?.currentValue);
this.applyAdditionalOptions();
this.checkReadonly();
}
}

if (changes['readonly']) {
this.checkReadonly();
}
}

ngOnInit(): void {
private checkReadonly() {
if (this.editor?.nativeElement) {
if (this.readonly) {
this.renderer.setAttribute(this.editor?.nativeElement, 'contenteditable', 'none');
} else {
this.renderer.setAttribute(this.editor?.nativeElement, 'contenteditable', 'plaintext-only');
}
}
}

ngAfterViewInit() {
this.int();
this.init();
}

private int() {
private init() {
if (this.editor !== undefined) {
const highlightMethod = (this.showLineNumbers) ? withLineNumbers(this.highlightMethod) : this.highlightMethod;

Expand All @@ -126,6 +139,8 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges, On
});
this.updateCode(this._code);
this.update.emit(this._code);
this.editor.nativeElement.addEventListener('keydown', this.onKeyDown);
this.checkReadonly();
}
}

Expand Down Expand Up @@ -212,4 +227,11 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges, On
}
}
}

onKeyDown = (event: KeyboardEvent) => {
if (this.readonly) {
event.stopPropagation();
event.preventDefault();
}
}
}

0 comments on commit d908e52

Please sign in to comment.