Skip to content

Commit

Permalink
feat: Add createEmptyParagraphEnd command
Browse files Browse the repository at this point in the history
  • Loading branch information
rfgamaral committed Sep 12, 2023
1 parent ae4cd49 commit 13d51ac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { RawCommands } from '@tiptap/core'
import { TextSelection } from '@tiptap/pm/state'

/**
* Augment the official `@tiptap/core` module with extra commands so that the compiler knows about
* them. For this to work externally, a wildcard export needs to be added to the root `index.ts`.
*/
declare module '@tiptap/core' {
interface Commands<ReturnType> {
createEmptyParagraphEnd: {
/**
* Creates an empty paragraph at the end of the document.
*/
createEmptyParagraphEnd: () => ReturnType
}
}
}

/**
* Creates an empty paragraph at the end of the document.
*
* last node before creating the paragraph, using the build int fn
*/
function createEmptyParagraphEnd(): ReturnType<RawCommands['createEmptyParagraphEnd']> {
return ({ state, tr, chain, dispatch }) => {
// Check if the transaction should be dispatched
// ref: https://tiptap.dev/api/commands#dry-run-for-commands
if (dispatch) {
return chain()
.command(() => {
tr.setSelection(
TextSelection.create(
tr.doc,
state.doc.content.size,
state.doc.content.size,
),
)

return true
})
.createParagraphNear()
.run()
}

return true
}
}

export { createEmptyParagraphEnd }
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Extension } from '@tiptap/core'

import { createEmptyParagraphEnd } from './commands/create-empty-paragraph-end'
import { extendWordRange } from './commands/extend-word-range'
import { insertMarkdownContent } from './commands/insert-markdown-content'

Expand All @@ -12,6 +13,7 @@ const ExtraEditorCommands = Extension.create({
name: 'extraEditorCommands',
addCommands() {
return {
createEmptyParagraphEnd,
extendWordRange,
insertMarkdownContent,
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type {
} from './components/typist-editor'
export { TypistEditor } from './components/typist-editor'
export * from './constants/extension-priorities'
export * from './extensions/core/extra-editor-commands/commands/create-empty-paragraph-end'
export * from './extensions/core/extra-editor-commands/commands/extend-word-range'
export * from './extensions/core/extra-editor-commands/commands/insert-markdown-content'
export { PlainTextKit } from './extensions/plain-text/plain-text-kit'
Expand Down
10 changes: 10 additions & 0 deletions stories/typist-editor/plain-text-functions.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const Commands: StoryObj<typeof TypistEditor> = {
(Story, context) => {
const typistEditorRef = useRef<TypistEditorRef>(null)

const handleCreateEmptyParagraphEndClick = useCallback(() => {
typistEditorRef.current?.getEditor().chain().focus().createEmptyParagraphEnd().run()
}, [])

const handleExtendWordRangeClick = useCallback(() => {
typistEditorRef.current?.getEditor().chain().focus().extendWordRange().run()
}, [])
Expand All @@ -49,6 +53,12 @@ export const Commands: StoryObj<typeof TypistEditor> = {
renderBottomFunctions={() => {
return (
<>
<Button
variant="secondary"
onClick={handleCreateEmptyParagraphEndClick}
>
createEmptyParagraphEnd
</Button>
<Button variant="secondary" onClick={handleExtendWordRangeClick}>
extendWordRange
</Button>
Expand Down
10 changes: 10 additions & 0 deletions stories/typist-editor/rich-text-functions.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const Commands: StoryObj<typeof TypistEditor> = {
(Story, context) => {
const typistEditorRef = useRef<TypistEditorRef>(null)

const handleCreateEmptyParagraphEndClick = useCallback(() => {
typistEditorRef.current?.getEditor().chain().focus().createEmptyParagraphEnd().run()
}, [])

const handleExtendWordRangeClick = useCallback(() => {
typistEditorRef.current?.getEditor().chain().focus().extendWordRange().run()
}, [])
Expand All @@ -49,6 +53,12 @@ export const Commands: StoryObj<typeof TypistEditor> = {
renderBottomFunctions={() => {
return (
<>
<Button
variant="secondary"
onClick={handleCreateEmptyParagraphEndClick}
>
createEmptyParagraphEnd
</Button>
<Button variant="secondary" onClick={handleExtendWordRangeClick}>
extendWordRange
</Button>
Expand Down

0 comments on commit 13d51ac

Please sign in to comment.