feat: setHardBreak now keeps splittable marks by default, fix #1934
This commit is contained in:
@@ -13,10 +13,16 @@ npm install @tiptap/extension-hard-break
|
||||
yarn add @tiptap/extension-hard-break
|
||||
```
|
||||
|
||||
## Settings
|
||||
| Option | Type | Default | Description |
|
||||
| -------------- | --------- | ------- | ---------------------------------------------------------------------------------------------- |
|
||||
| HTMLAttributes | `Object` | `{}` | Custom HTML attributes that should be added to the rendered HTML tag. |
|
||||
| keepMarks | `Boolean` | `true` | Decides whether to keep marks after a line break. Based on the `keepOnSplit` option for marks. |
|
||||
|
||||
## Commands
|
||||
| Command | Parameters | Description |
|
||||
| --------- | ---------- | ----------------- |
|
||||
| hardBreak | — | Add a line break. |
|
||||
| Command | Parameters | Description |
|
||||
| ------------ | ---------- | ----------------- |
|
||||
| setHardBreak | — | Add a line break. |
|
||||
|
||||
## Keyboard shortcuts
|
||||
* `Shift` `Enter`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Node, mergeAttributes } from '@tiptap/core'
|
||||
|
||||
export interface HardBreakOptions {
|
||||
keepMarks: boolean,
|
||||
HTMLAttributes: Record<string, any>,
|
||||
}
|
||||
|
||||
@@ -19,6 +20,7 @@ export const HardBreak = Node.create<HardBreakOptions>({
|
||||
name: 'hardBreak',
|
||||
|
||||
defaultOptions: {
|
||||
keepMarks: true,
|
||||
HTMLAttributes: {},
|
||||
},
|
||||
|
||||
@@ -44,10 +46,34 @@ export const HardBreak = Node.create<HardBreakOptions>({
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
setHardBreak: () => ({ commands }) => {
|
||||
setHardBreak: () => ({
|
||||
commands,
|
||||
chain,
|
||||
state,
|
||||
editor,
|
||||
}) => {
|
||||
return commands.first([
|
||||
() => commands.exitCode(),
|
||||
() => commands.insertContent({ type: this.name }),
|
||||
() => commands.command(() => {
|
||||
const { keepMarks } = this.options
|
||||
const { splittableMarks } = editor.extensionManager
|
||||
const marks = state.storedMarks
|
||||
|| (state.selection.$to.parentOffset && state.selection.$from.marks())
|
||||
|
||||
return chain()
|
||||
.insertContent({ type: this.name })
|
||||
.command(({ tr, dispatch }) => {
|
||||
if (dispatch && marks && keepMarks) {
|
||||
const filteredMarks = marks
|
||||
.filter(mark => splittableMarks.includes(mark.type.name))
|
||||
|
||||
tr.ensureMarks(filteredMarks)
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
.run()
|
||||
}),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user