Skip to content

Commit

Permalink
feat: add option to focus at start
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Feb 6, 2023
1 parent e0b2d67 commit 92d342b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
13 changes: 11 additions & 2 deletions projects/ngx-editor/src/lib/EditorCommands.ts
@@ -1,4 +1,4 @@
import { EditorState, Transaction } from 'prosemirror-state';
import { EditorState, Selection, Transaction } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import {
chainCommands, createParagraphNear, liftEmptyBlock,
Expand Down Expand Up @@ -27,6 +27,8 @@ const execMark = (name: string, toggle = false) => {
};
};

type FocusPosition = 'start' | 'end';

class EditorCommands {
private view: EditorView;
private state: EditorState;
Expand Down Expand Up @@ -68,7 +70,14 @@ class EditorCommands {
return true;
}

focus(): this {
focus(position: FocusPosition = 'end'): this {
const selection = position === 'start'
? Selection.atStart(this.state.doc)
: Selection.atEnd(this.state.doc);

this.tr.setSelection(selection);
this.applyTrx();

this.view.focus();
return this;
}
Expand Down
65 changes: 65 additions & 0 deletions projects/ngx-editor/src/lib/editor.spec.ts
Expand Up @@ -18,3 +18,68 @@ describe('NgxEditorComponent', () => {
expect(editor.view.dom.getAttribute('enterKeyHint')).toBe('enter');
});
});

describe('Editor: Commands', () => {
it('should expose all the commands', () => {
const editor = new Editor();
expect(editor.commands.exec).toBeInstanceOf(Function);
expect(editor.commands.align).toBeInstanceOf(Function);
expect(editor.commands.applyMark).toBeInstanceOf(Function);
expect(editor.commands.backgroundColor).toBeInstanceOf(Function);
expect(editor.commands.bold).toBeInstanceOf(Function);
expect(editor.commands.code).toBeInstanceOf(Function);
expect(editor.commands.focus).toBeInstanceOf(Function);
expect(editor.commands.insertHTML).toBeInstanceOf(Function);
expect(editor.commands.insertImage).toBeInstanceOf(Function);
expect(editor.commands.insertLink).toBeInstanceOf(Function);
expect(editor.commands.insertNewLine).toBeInstanceOf(Function);
expect(editor.commands.insertText).toBeInstanceOf(Function);
expect(editor.commands.italics).toBeInstanceOf(Function);
expect(editor.commands.removeBackgroundColor).toBeInstanceOf(Function);
expect(editor.commands.removeTextColor).toBeInstanceOf(Function);
expect(editor.commands.scrollIntoView).toBeInstanceOf(Function);
expect(editor.commands.strike).toBeInstanceOf(Function);
expect(editor.commands.textColor).toBeInstanceOf(Function);
expect(editor.commands.toggleBold).toBeInstanceOf(Function);
expect(editor.commands.toggleBulletList).toBeInstanceOf(Function);
expect(editor.commands.toggleCode).toBeInstanceOf(Function);
expect(editor.commands.toggleHeading).toBeInstanceOf(Function);
expect(editor.commands.toggleItalics).toBeInstanceOf(Function);
expect(editor.commands.toggleMark).toBeInstanceOf(Function);
expect(editor.commands.toggleOrderedList).toBeInstanceOf(Function);
expect(editor.commands.toggleStrike).toBeInstanceOf(Function);
expect(editor.commands.toggleUnderline).toBeInstanceOf(Function);
expect(editor.commands.underline).toBeInstanceOf(Function);
expect(editor.commands.updateLink).toBeInstanceOf(Function);
});

it('should set focus at the end correctly', () => {
const editor = new Editor({
content: 'Hello there',
});

editor.commands.focus().insertText('!').exec();
expect(editor.view.state.doc.textContent).toBe('Hello there!');

editor.commands.focus('end').insertText('!').exec();
expect(editor.view.state.doc.textContent).toBe('Hello there!!');
});

it('should set focus at the start correctly', () => {
const editor = new Editor({
content: 'world!',
});

editor.commands.focus('start').insertText('Hello ').exec();
expect(editor.view.state.doc.textContent).toBe('Hello world!');
});

it('should insert text correctly', () => {
const editor = new Editor({
content: 'Hello',
});

editor.commands.focus().insertText(' there').exec();
expect(editor.view.state.doc.textContent).toBe('Hello there');
});
});

0 comments on commit 92d342b

Please sign in to comment.