Horizontal Rule #953
Replies: 6 comments 3 replies
-
@bensquire Did you find a solution for this yet? Wondering if it's possible to create a custom plugin for horizontal rule tags |
Beta Was this translation helpful? Give feedback.
-
@alexchoiweb We did actually create a custom plugin, I'll try and get a PR in over the weekend. |
Beta Was this translation helpful? Give feedback.
-
@bensquire Thanks! Even a code snippet of the plugin would help, I'm currently blocked on trying to integrate this at work XD |
Beta Was this translation helpful? Give feedback.
-
Working on this now, expect a PR soon. |
Beta Was this translation helpful? Give feedback.
-
@alexchoiweb @zbeyens Sorry, I'm trying to get stuff over the line atm. This might help: import React from 'react'
import { getPlatePluginTypes, getPlatePluginOptions, getRenderElement } from '@udecode/plate-core'
import { getElementDeserializer, insertNodes } from '@udecode/plate-common'
import { Editor } from 'slate'
export const ELEMENT_HR = 'hr'
const getHorizontalRuleDeserialize = () => editor => {
const options = getPlatePluginOptions(editor, ELEMENT_HR)
return {
element: getElementDeserializer({
type: ELEMENT_HR,
rules: [{ nodeNames: 'HR' }],
...options.deserialize
})
}
}
export const createHorizontalRulePlugin = options => ({
pluginKeys: ELEMENT_HR,
renderElement: getRenderElement(ELEMENT_HR),
voidTypes: getPlatePluginTypes(ELEMENT_HR),
deserialize: getHorizontalRuleDeserialize(),
serialize: {
element: ({ element }) => {
if (element.type === ELEMENT_HR) {
return <hr />
}
}
}
})
export const insertHorizontalRule = (editor) => {
const selectionPath = Editor.path(editor, editor.selection)
const insertPath = selectionPath.slice(0, 1)
insertNodes(
editor,
{
type: ELEMENT_HR,
children: [{ text: '' }]
},
{
at: insertPath
}
)
} Component Logic const HrElement = ({ attributes, children, nodeProps }) => {
const selected = useSelected()
return (
<div {...attributes} {...nodeProps}>
<div contentEditable={false}>
<Hr selected={selected} />
</div>
{children}
</div>
)
}
const OPTIONS = {
[ELEMENT_HR]: withProps(HrElement)
} |
Beta Was this translation helpful? Give feedback.
-
Done in #1054 |
Beta Was this translation helpful? Give feedback.
-
Maybe I'm missing something, but is there a Horizontal Rule plugin? It feels like it should be really simple, but then it also might be the only "type" that contains no actual content and is just syntax.
P.S. I did a search of issues, PRs and discussions and found nothing, so maybe it's just me.
Beta Was this translation helpful? Give feedback.
All reactions