add keepOnSplit option for marks

This commit is contained in:
Philipp Kühn
2021-04-01 19:06:40 +02:00
parent 9cdc76a082
commit f2ca9322ee
3 changed files with 22 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import getNodeType from './helpers/getNodeType'
import splitExtensions from './helpers/splitExtensions' import splitExtensions from './helpers/splitExtensions'
import getAttributesFromExtensions from './helpers/getAttributesFromExtensions' import getAttributesFromExtensions from './helpers/getAttributesFromExtensions'
import getRenderedAttributes from './helpers/getRenderedAttributes' import getRenderedAttributes from './helpers/getRenderedAttributes'
import callOrReturn from './utilities/callOrReturn'
export default class ExtensionManager { export default class ExtensionManager {
@@ -20,6 +21,8 @@ export default class ExtensionManager {
extensions: Extensions extensions: Extensions
splittableMarks: string[] = []
constructor(extensions: Extensions, editor: Editor) { constructor(extensions: Extensions, editor: Editor) {
this.editor = editor this.editor = editor
this.extensions = extensions this.extensions = extensions
@@ -32,6 +35,14 @@ export default class ExtensionManager {
type: getSchemaTypeByName(extension.config.name, this.schema), type: getSchemaTypeByName(extension.config.name, this.schema),
} }
if (extension.type === 'mark') {
const keepOnSplit = callOrReturn(extension.config.keepOnSplit, context) ?? true
if (keepOnSplit) {
this.splittableMarks.push(extension.config.name)
}
}
if (typeof extension.config.onCreate === 'function') { if (typeof extension.config.onCreate === 'function') {
this.editor.on('create', extension.config.onCreate.bind(context)) this.editor.on('create', extension.config.onCreate.bind(context))
} }

View File

@@ -191,6 +191,11 @@ declare module '@tiptap/core' {
type: MarkType, type: MarkType,
}) => void) | null, }) => void) | null,
/**
* Keep mark after split node
*/
keepOnSplit?: boolean | (() => boolean),
/** /**
* Inclusive * Inclusive
*/ */

View File

@@ -15,12 +15,14 @@ function defaultBlockAt(match: ContentMatch) {
return null return null
} }
function ensureMarks(state: EditorState) { function ensureMarks(state: EditorState, splittableMarks?: string[]) {
const marks = state.storedMarks const marks = state.storedMarks
|| (state.selection.$to.parentOffset && state.selection.$from.marks()) || (state.selection.$to.parentOffset && state.selection.$from.marks())
if (marks) { if (marks) {
state.tr.ensureMarks(marks) const filteredMarks = marks.filter(mark => splittableMarks?.includes(mark.type.name))
state.tr.ensureMarks(filteredMarks)
} }
} }
@@ -57,7 +59,7 @@ export const splitBlock: RawCommands['splitBlock'] = ({ keepMarks = true } = {})
if (dispatch) { if (dispatch) {
if (keepMarks) { if (keepMarks) {
ensureMarks(state) ensureMarks(state, editor.extensionManager.splittableMarks)
} }
tr.split($from.pos).scrollIntoView() tr.split($from.pos).scrollIntoView()
@@ -118,7 +120,7 @@ export const splitBlock: RawCommands['splitBlock'] = ({ keepMarks = true } = {})
} }
if (keepMarks) { if (keepMarks) {
ensureMarks(state) ensureMarks(state, editor.extensionManager.splittableMarks)
} }
tr.scrollIntoView() tr.scrollIntoView()