diff --git a/projects/ngx-editor/src/lib/EditorCommands.ts b/projects/ngx-editor/src/lib/EditorCommands.ts index 3d40d6eb..fa55f0a5 100644 --- a/projects/ngx-editor/src/lib/EditorCommands.ts +++ b/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, @@ -27,6 +27,8 @@ const execMark = (name: string, toggle = false) => { }; }; +type FocusPosition = 'start' | 'end'; + class EditorCommands { private view: EditorView; private state: EditorState; @@ -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; } diff --git a/projects/ngx-editor/src/lib/editor.spec.ts b/projects/ngx-editor/src/lib/editor.spec.ts index e7c1ca58..39b5df24 100644 --- a/projects/ngx-editor/src/lib/editor.spec.ts +++ b/projects/ngx-editor/src/lib/editor.spec.ts @@ -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'); + }); +});