Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace usage of useEvent with useCallback #98

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"react-icons": "4.7.1",
"react-markdown": "8.0.5",
"react-syntax-highlighter": "15.5.0",
"react-use-event-hook": "0.9.3",
"rehype-raw": "6.1.1",
"remark-gfm": "3.0.1",
"rimraf": "3.0.2",
Expand All @@ -158,7 +159,6 @@
"marked": "^4.1.1",
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0",
"react-use-event-hook": "^0.9.3",
"turndown": "^7.1.1"
}
}
82 changes: 42 additions & 40 deletions src/components/typist-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { forwardRef, useImperativeHandle, useMemo } from 'react'
import { useEvent } from 'react-use-event-hook'
import { forwardRef, useCallback, useImperativeHandle, useMemo } from 'react'

import { getSchema } from '@tiptap/core'
import { Placeholder } from '@tiptap/extension-placeholder'
Expand Down Expand Up @@ -303,45 +302,48 @@ const TypistEditor = forwardRef<TypistEditorRef, TypistEditorProps>(function Typ
[ariaDescribedBy, ariaLabel, ariaLabelledBy, editable, schema],
)

const handleCreate = useEvent(function handleCreate(props: CreateProps) {
const { view } = props.editor

// Apply a selection to the document if one was given and `autoFocus` is `true`
if (autoFocus && contentSelection) {
view.dispatch(
view.state.tr
.setSelection(resolveContentSelection(view.state.doc, contentSelection))
.scrollIntoView(),
)
}

// Move the suggestion plugins to the top of the plugins list so they have a higher priority
// than all input rules (such as the ones used for Markdown shortcuts)
// ref: https://github.com/ueberdosis/tiptap/issues/2570
if (view.state.plugins.length > 0) {
const restOfPlugins: Plugin[] = []
const suggestionPlugins: Plugin[] = []

view.state.plugins.forEach((plugin) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: The `Plugin` type does not include `key`
if ((plugin.key as string).includes('Suggestion')) {
suggestionPlugins.push(plugin)
} else {
restOfPlugins.push(plugin)
}
})

view.updateState(
view.state.reconfigure({
plugins: [...suggestionPlugins, ...restOfPlugins],
}),
)
}
const handleCreate = useCallback(
function handleCreate(props: CreateProps) {
const { view } = props.editor

// Apply a selection to the document if one was given and `autoFocus` is `true`
if (autoFocus && contentSelection) {
view.dispatch(
view.state.tr
.setSelection(resolveContentSelection(view.state.doc, contentSelection))
.scrollIntoView(),
)
}

// Move the suggestion plugins to the top of the plugins list so they have a higher priority
// than all input rules (such as the ones used for Markdown shortcuts)
// ref: https://github.com/ueberdosis/tiptap/issues/2570
if (view.state.plugins.length > 0) {
const restOfPlugins: Plugin[] = []
const suggestionPlugins: Plugin[] = []

view.state.plugins.forEach((plugin) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: The `Plugin` type does not include `key`
if ((plugin.key as string).includes('Suggestion')) {
suggestionPlugins.push(plugin)
} else {
restOfPlugins.push(plugin)
}
})

view.updateState(
view.state.reconfigure({
plugins: [...suggestionPlugins, ...restOfPlugins],
}),
)
}

// Invoke the user `onCreate` handle after all internal initializations
onCreate?.(props)
})
// Invoke the user `onCreate` handle after all internal initializations
onCreate?.(props)
},
[autoFocus, contentSelection, onCreate],
)

const editor = useEditor(
{
Expand Down