Skip to content

Commit

Permalink
fix: handle null value input correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s authored Jun 1, 2020
1 parent 4d3a273 commit 84104c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
3 changes: 1 addition & 2 deletions demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
</div>
</div>
<div class="editor">
<ngx-editor [ngModel]="editorContent" (ngModelChange)="editorContentChange($event)">
</ngx-editor>
<ngx-editor [ngModel]="editorContent" (ngModelChange)="editorContentChange($event)"></ngx-editor>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions demo/src/app/app.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
flex-direction: column;
align-items: center;
text-align: center;
margin-bottom: 0.5rem;
}

.badges {
Expand Down
2 changes: 2 additions & 0 deletions demo/src/app/plugins/menu/codemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { isNodeActive, toggleBlockType, ToolbarCustomMenuItem } from 'ngx-editor

import schema from '../../schema';

// Ref: https://prosemirror.net/examples/codemirror/

const codeMirror: ToolbarCustomMenuItem = (editorView) => {
const dom: HTMLElement = document.createElement('div');
dom.innerHTML = 'CodeMirror';
Expand Down
24 changes: 19 additions & 5 deletions src/lib/ngx-editor.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Component, ViewChild, ElementRef,
forwardRef, OnDestroy, OnInit, ViewEncapsulation
forwardRef, OnDestroy, ViewEncapsulation, AfterViewInit
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';

Expand All @@ -22,20 +22,22 @@ import { NgxEditorService, NgxEditorServiceConfig } from './ngx-editor.service';
encapsulation: ViewEncapsulation.None
})

export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestroy {
export class NgxEditorComponent implements ControlValueAccessor, AfterViewInit, OnDestroy {
@ViewChild('ngxEditor', { static: true }) ngxEditor: ElementRef;

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

private config: NgxEditorServiceConfig;

private editorInitialized = false;

constructor(ngxEditorService: NgxEditorService) {
this.config = ngxEditorService.config;
}

writeValue(value: object | null) {
if (!value) {
if (!this.editorInitialized) {
return;
}

Expand All @@ -49,6 +51,10 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr
registerOnTouched(): void { }

private parseDoc(contentJson: object): ProsemirrorNode {
if (!contentJson) {
return null;
}

const { schema } = this.config;
return schema.nodeFromJSON(contentJson);
}
Expand All @@ -57,6 +63,12 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr
try {
const doc = this.parseDoc(value);
const state = this.view.state;

// don't emit if both content is same
if (doc !== null && state.doc.eq(doc)) {
return;
}

const tr = state.tr;
tr.replaceWith(0, state.doc.content.size, doc);
this.view.dispatch(tr);
Expand All @@ -82,17 +94,19 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnDestr
this.view = new EditorView(this.ngxEditor.nativeElement, {
state: EditorState.create({
schema,
plugins,
plugins
}),
nodeViews,
dispatchTransaction: this.handleTransactions.bind(this),
attributes: {
class: 'NgxEditor__Content'
},
});

this.editorInitialized = true;
}

ngOnInit() {
ngAfterViewInit() {
this.createEditor();
}

Expand Down

0 comments on commit 84104c2

Please sign in to comment.