Skip to content

Commit

Permalink
refactor: remove dom logic from editor module
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jan 31, 2021
1 parent 02f4d9e commit 3804990
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
14 changes: 1 addition & 13 deletions src/lib/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class Editor {
el: DocumentFragment;

valueChange = new Subject<JSONDoc>();
focus = new Subject<void>();
blur = new Subject<void>();
update = new Subject();

constructor(options: Options = DEFAULT_OPTIONS) {
Expand Down Expand Up @@ -120,17 +118,7 @@ class Editor {
plugins,
}),
nodeViews,
dispatchTransaction: this.handleTransactions.bind(this),
handleDOMEvents: {
focus: () => {
this.focus.next();
return false;
},
blur: () => {
this.blur.next();
return false;
}
}
dispatchTransaction: this.handleTransactions.bind(this)
});
}

Expand Down
28 changes: 14 additions & 14 deletions src/lib/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnChang
this.editor.registerPlugin(plugins.attributes({
class: 'NgxEditor__Content'
}));

this.editor.registerPlugin(plugins.focus(() => {
this.focusIn.emit();
}));

this.editor.registerPlugin(plugins.focus(() => {
this.focusIn.emit();
}));

this.editor.registerPlugin(plugins.blur(() => {
this.focusOut.emit();
this.onTouched();
}));
}

ngOnInit(): void {
Expand All @@ -105,20 +118,7 @@ export class NgxEditorComponent implements ControlValueAccessor, OnInit, OnChang
this.handleChange(jsonDoc);
});

const blurSubscription = this.editor.blur.subscribe(() => {
this.focusOut.emit();
this.onTouched();
});

const focusScbscription = this.editor.focus.subscribe(() => {
this.focusIn.emit();
});

this.subscriptions.push(
contentChangeSubscription,
blurSubscription,
focusScbscription
);
this.subscriptions.push(contentChangeSubscription);
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/plugins/blur.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Plugin, PluginKey } from 'prosemirror-state';

const blurPlugin = (cb = () => { }) => {
return new Plugin({
key: new PluginKey('blur'),
props: {
handleDOMEvents: {
blur: () => {
cb();
return false;
}
}
}
});
};

export default blurPlugin;
17 changes: 17 additions & 0 deletions src/lib/plugins/focus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Plugin, PluginKey } from 'prosemirror-state';

const focusPlugin = (cb = () => { }) => {
return new Plugin({
key: new PluginKey('focus'),
props: {
handleDOMEvents: {
focus: () => {
cb();
return false;
}
}
}
});
};

export default focusPlugin;
2 changes: 2 additions & 0 deletions src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as editable } from './editable';
export { default as placeholder } from './placeholder';
export { default as attributes } from './attributes';
export { default as focus } from './focus';
export { default as blur } from './blur';

0 comments on commit 3804990

Please sign in to comment.