-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WEB-372] fix: horizontal rule extension now always divider adds belo…
…w nodes (#3890) * fix: horizontal rule extension now always divider adds below nodes * chore: removing duplicate horizontal rule extension
- Loading branch information
1 parent
3d09a69
commit cace132
Showing
2 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
packages/editor/core/src/ui/extensions/horizontal-rule/horizontal-rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { isNodeSelection, mergeAttributes, Node, nodeInputRule } from "@tiptap/core"; | ||
import { NodeSelection, TextSelection } from "@tiptap/pm/state"; | ||
|
||
export interface HorizontalRuleOptions { | ||
HTMLAttributes: Record<string, any>; | ||
} | ||
|
||
declare module "@tiptap/core" { | ||
interface Commands<ReturnType> { | ||
horizontalRule: { | ||
/** | ||
* Add a horizontal rule | ||
*/ | ||
setHorizontalRule: () => ReturnType; | ||
}; | ||
} | ||
} | ||
|
||
export const CustomHorizontalRule = Node.create<HorizontalRuleOptions>({ | ||
name: "horizontalRule", | ||
|
||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
}; | ||
}, | ||
|
||
group: "block", | ||
|
||
parseHTML() { | ||
return [{ tag: "hr" }]; | ||
}, | ||
|
||
renderHTML({ HTMLAttributes }) { | ||
return ["hr", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]; | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
setHorizontalRule: | ||
() => | ||
({ chain, state }) => { | ||
const { selection } = state; | ||
const { $from: $originFrom, $to: $originTo } = selection; | ||
|
||
const currentChain = chain(); | ||
|
||
if ($originFrom.parentOffset === 0) { | ||
currentChain.insertContentAt( | ||
{ | ||
from: Math.max($originFrom.pos - 1, 0), | ||
to: $originTo.pos, | ||
}, | ||
{ | ||
type: this.name, | ||
} | ||
); | ||
} else if (isNodeSelection(selection)) { | ||
currentChain.insertContentAt($originTo.pos, { | ||
type: this.name, | ||
}); | ||
} else { | ||
currentChain.insertContent({ type: this.name }); | ||
} | ||
|
||
return ( | ||
currentChain | ||
// set cursor after horizontal rule | ||
.command(({ tr, dispatch }) => { | ||
if (dispatch) { | ||
const { $to } = tr.selection; | ||
const posAfter = $to.end(); | ||
|
||
if ($to.nodeAfter) { | ||
if ($to.nodeAfter.isTextblock) { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos + 1)); | ||
} else if ($to.nodeAfter.isBlock) { | ||
tr.setSelection(NodeSelection.create(tr.doc, $to.pos)); | ||
} else { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos)); | ||
} | ||
} else { | ||
// add node after horizontal rule if it’s the end of the document | ||
const node = $to.parent.type.contentMatch.defaultType?.create(); | ||
|
||
if (node) { | ||
tr.insert(posAfter, node); | ||
tr.setSelection(TextSelection.create(tr.doc, posAfter + 1)); | ||
} | ||
} | ||
|
||
tr.scrollIntoView(); | ||
} | ||
|
||
return true; | ||
}) | ||
.run() | ||
); | ||
}, | ||
}; | ||
}, | ||
|
||
addInputRules() { | ||
return [ | ||
nodeInputRule({ | ||
find: /^(?:---|—-|___\s|\*\*\*\s)$/, | ||
type: this.type, | ||
}), | ||
]; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters