Allow overriding getPlainText (and HTML?)
See original GitHub issueProblem
slate-react internally generates the Slate fragment, HTML, and plain-text representations of the editor’s state when interacting with the clipboard, using ReactEditor.setFragmentData
. This is called from onCopy
, onCut
, and onDragStart
.
Internally, this function does a bunch of different operations and checks, and eventually generates a blank DOM node which it generates the text/plain and text/html types from. This calls utils/dom/getPlainText
, which walks the DOM tree to get the text nodes (and also handles newline generation).
Transforming on input is relatively easy via editor.insertData
(as shown in the paste-html example), but output is not so much; it requires repeating some of slate-react’s internal logic in setFragmentData
, and attaching to every DOM event. This gets unwieldy, and is brittle to changes and fixes in Slate.
Solution
I’d like to define my own getPlainText
function (getPlainText(node: DOMNode) => string
) to allow for transforming the text being set on the DataTransfer. This would let me check for my void nodes and pick different data to be serialized.
The ability to override the text/html representation may also be useful (although I don’t personally need it).
Alternatives
Currently, you can either override the whole setFragmentData
with custom code, or wrap the existing behaviour.
Wrapping the existing behaviour means recreating many of the checks setFragmentData
, as the processed data isn’t actually exposed (which makes sense). This ends up being essentially equivalent then to overriding setFragmentData
and rewriting it. This… is not super fun.
Context
In my particular use case, I have dynamic values within my editor represented by void nodes. I’d like to serialize these manually to placeholders (e.g. {{some_value}}
) when copying out of the editor, and transform back when copying in. (My particular use case is copying data to and from code editors.)
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (2 by maintainers)
Top GitHub Comments
That’ll teach me to read an isolated example. 😉
@bryanph it would be amazing to see how you’ve replaced setFragmentData as I feel like my custom extension is rather simplistic!