Skip to content

Commit

Permalink
feat: support validations
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Dec 7, 2020
1 parent 4e44733 commit 6af3ce0
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ yarn add ngx-editor

### Usage

**Note**: By default the editor comes with bare minimal features enabled to reduce the size of the bundle. Refer the [demo](#demo) and [documentation](#documentation) for more details and examples.
**Note**: By default the editor comes with bare minimal features enabled to reduce the size of the bundle. Refer the [demo](#demo) and [documentation] for more details and examples.

Import `ngx-editor` module

Expand Down Expand Up @@ -113,4 +113,5 @@ Edit the stackblitz here https://stackblitz.com/edit/ngx-editor
[npm]: https://www.npmjs.com/
[yarn]: https://yarnpkg.com/lang/en/
[github]: https://sibiraj-s.github.io/
[documentation]: https://sibiraj.dev/ngx-editor/
[wiki]: https://github.com/sibiraj-s/ngx-editor/wiki/ngxEditor
13 changes: 8 additions & 5 deletions demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
</div>
</div>

<div class="editor">
<ngx-editor [ngModel]="editorContent" (ngModelChange)="editorContentChange($event)" (init)="init($event)"
[customMenuRef]="customMenu">
</ngx-editor>
</div>
<form [formGroup]="form">

<div class="editor">
<ngx-editor formControlName="editorContent" (init)="init($event)" [customMenuRef]="customMenu" (blur)="onBlur($event)">
</ngx-editor>
</div>

</form>

<!-- custom menu -->
<ng-template #customMenu>
Expand Down
10 changes: 7 additions & 3 deletions demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, ViewEncapsulation } from '@angular/core';
import { AbstractControl, FormControl, FormGroup } from '@angular/forms';
import { EditorView } from 'prosemirror-view';
import { environment } from '../environments/environment';
import { Validators } from 'ngx-editor';

import jsonDoc from './doc';

Expand All @@ -15,10 +17,12 @@ export class AppComponent {
isProdMode = environment.production;
editorView: EditorView;

editorContent: object = jsonDoc;
form = new FormGroup({
editorContent: new FormControl(jsonDoc, Validators.required())
});

editorContentChange(doc: object): void {
this.editorContent = doc;
get doc(): AbstractControl {
return this.form.get('editorContent');
}

init(view: EditorView): void {
Expand Down
3 changes: 2 additions & 1 deletion demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { NgxEditorModule } from 'ngx-editor';
Expand All @@ -16,6 +16,7 @@ import { CustomMenuComponent } from './components/custom-menu/custom-menu.compon
CommonModule,
BrowserModule,
FormsModule,
ReactiveFormsModule,
NgxEditorModule.forRoot({
schema,
plugins,
Expand Down
28 changes: 28 additions & 0 deletions src/lib/Validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AbstractControl, ValidatorFn } from '@angular/forms';
import { Schema } from 'prosemirror-model';

import schema from './schema';

export class Validators {

static required(customSchema?: Schema): ValidatorFn {
return (c: AbstractControl) => {

const userSchema = customSchema || schema;

if (!c.value) {
return null;
}

const node = userSchema.nodeFromJSON(c.value);

const isEmpty = node.childCount === 1
&& node?.firstChild?.isTextblock
&& node.firstChild.content.size === 0;

return {
required: isEmpty
};
};
}
}
9 changes: 7 additions & 2 deletions src/lib/editor.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Component, ViewChild, ElementRef,
forwardRef, OnDestroy, ViewEncapsulation, OnInit, Output, EventEmitter, Input, TemplateRef
forwardRef, OnDestroy, ViewEncapsulation, OnInit,
Output, EventEmitter, Input, TemplateRef
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';

Expand All @@ -27,6 +28,7 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr

view: EditorView;
private onChange: (value: object) => void;
private onTouched: () => void;

config: NgxEditorServiceConfig;

Expand All @@ -53,7 +55,9 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr
this.onChange = fn;
}

registerOnTouched(): void { }
registerOnTouched(fn: any): void {
this.onTouched = fn;
}

private parseDoc(contentJson: object): ProsemirrorNode {
if (!contentJson) {
Expand Down Expand Up @@ -130,6 +134,7 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr
return true;
},
blur: () => {
this.onTouched();
this.focusOut.emit();
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export const schema = new Schema({
marks,
nodes
});

export default schema;
1 change: 1 addition & 0 deletions src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './lib/editor.component';
export * from './lib/editor.module';

export * from './lib/schema';
export * from './lib/Validators';

export * from './lib/types';

0 comments on commit 6af3ce0

Please sign in to comment.