Skip to content

Commit

Permalink
feat(ngx-codejar): add options provided by CodeJar
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoemp committed Jul 7, 2023
1 parent d9da2d9 commit 03379ac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,27 @@ If you want to use codejar with [prism.js](https://prismjs.com/) you should do t
<tbody>
<tr>
<td>Property</td>
<td>code</td>
<td>property for two-way data-binding.</td>
<td>-</td>
</tr>
<tr>
<td>Property</td>
<td>highlighter</td>
<td>selects which highlighter should be used ('prism' or 'hljs')</td>
<td>'hljs'</td>
</tr>
<tr>
<td>Property</td>
<td>showLineNumbers</td>
<td>adds line numbers</td>
<td>false</td>
<td>options</td>
<td>Options provided by CodeJar + additional Options (tabSize)</td>
<td>Defaults provided by Codejar <a href="https://github.com/antonmedv/codejar">Defaults by CodeJar</a></td>
</tr>
<tr>
<td>Property</td>
<td>code</td>
<td>property for two-way data-binding.</td>
<td>-</td>
<td>showLineNumbers</td>
<td>adds line numbers</td>
<td>false</td>
</tr>
<tr>
<td>Property</td>
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"polyfills": ["zone.js"],
"tsConfig": "apps/demo/tsconfig.app.json",
"assets": ["apps/demo/src/favicon.ico", "apps/demo/src/assets"],
"styles": ["apps/demo/src/styles.scss"],
"styles": ["node_modules/highlight.js/styles/github-dark.css", "node_modules/prismjs/themes/prism-dark.css","apps/demo/src/styles.scss"],
"scripts": []
},
"configurations": {
Expand Down
13 changes: 13 additions & 0 deletions libs/ngx-codejar/src/lib/codejar.typings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export interface CodeJarContainer extends HTMLElement {
textContent: string | null;
}

export interface CodeJarOptions{
tab?: string;
tabSize?: number;
indentOn?: RegExp;
moveToNewLine?: RegExp;
spellcheck?: boolean;
catchTab?: boolean;
preserveIdent?: boolean;
addClosing?: boolean;
history?: boolean;
window?: typeof window;
}
32 changes: 29 additions & 3 deletions libs/ngx-codejar/src/lib/ngx-code-jar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
ElementRef,
EventEmitter,
Input,
OnChanges,
OnInit,
Output,
Renderer2,
SimpleChanges,
ViewChild
} from '@angular/core';
import {CodeJar, Position} from 'codejar';
import {CodeJarContainer} from './codejar.typings';
import {CodeJarContainer, CodeJarOptions} from './codejar.typings';
import {withLineNumbers} from 'codejar/linenumbers.js';

@Component({
Expand Down Expand Up @@ -55,7 +57,7 @@ import {withLineNumbers} from 'codejar/linenumbers.js';
}
`]
})
export class NgxCodeJarComponent implements OnInit, AfterViewInit {
export class NgxCodeJarComponent implements OnInit, AfterViewInit, OnChanges {
constructor(private el: ElementRef, private renderer: Renderer2) {
this.update = new EventEmitter<string>();
}
Expand All @@ -79,6 +81,7 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit {
@Output() codeChange = new EventEmitter<string>();

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

// Events
/**
Expand All @@ -88,15 +91,30 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit {
@Input() highlightMethod: (editor: CodeJarContainer, pos?: Position) => void = () => {
}

ngOnChanges(changes: SimpleChanges) {
if (changes['options']?.currentValue && changes['options'].currentValue !== changes['options']) {
if (this.codeJar) {
this.codeJar.updateOptions(changes['options']?.currentValue);
this.applyAdditionalOptions();
}
}
}

ngOnInit(): void {
}

ngAfterViewInit() {
this.int();
}

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

this.codeJar = CodeJar(this.editor.nativeElement, highlightMethod, {tab: '\t'});
this.codeJar = CodeJar(this.editor.nativeElement, highlightMethod, this.options);
this.applyCustomizations();
this.applyAdditionalOptions();

this.codeJar.onUpdate((newCode: string) => {
this._code = newCode;
this.codeChange.emit(newCode);
Expand All @@ -107,6 +125,14 @@ export class NgxCodeJarComponent implements OnInit, AfterViewInit {
}
}

applyAdditionalOptions() {
if (this.editor) {
if (this.options.tabSize) {
this.renderer.setStyle(this.editor.nativeElement, 'tab-size', this.options.tabSize);
}
}
}

/***
* updates the code and preserves the position
* @param newCode new code
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03379ac

Please sign in to comment.