import { Plugin, PluginKey } from '@milkdown/prose/state' import { ReplaceStep } from '@milkdown/prose/transform' import { $ctx, $prose } from '@milkdown/utils' import { withMeta } from '../__internal__' /// This slice holds the nodes that ignore a hardbreak inside them. export const hardbreakFilterNodes = $ctx( ['table', 'code_block'], 'hardbreakFilterNodes' ) withMeta(hardbreakFilterNodes, { displayName: 'Ctx', group: 'Prose', }) /// This plugin filters the hardbreak node. It ignores a hardbreak that /// lands inside a node listed in `hardbreakFilterNodes`. export const hardbreakFilterPlugin = $prose((ctx) => { const notIn = ctx.get(hardbreakFilterNodes.key) return new Plugin({ key: new PluginKey('MILKDOWN_HARDBREAK_FILTER'), filterTransaction: (tr, state) => { const isInsertHr = tr.getMeta('hardbreak') const [step] = tr.steps if (isInsertHr && step instanceof ReplaceStep) { const $from = state.doc.resolve(step.from) let curDepth = $from.depth let canApply = true while (curDepth > 0) { if (notIn.includes($from.node(curDepth).type.name)) canApply = false curDepth-- } return canApply } return true }, }) }) withMeta(hardbreakFilterPlugin, { displayName: 'Prose', group: 'Prose', })