-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally apply input and paste rules when using insertContent methods #5046
Conversation
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding the changes are reasonable.
And as you haven’t added any dots in our test suite I’d say LGTM (looking at you xz ;))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - just had a few comments. :)
}: { | ||
state: EditorState | ||
from: number | ||
to: { b: number } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why to is not just the number too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why to is not just the number too?
Yeah, this is a lil dirty. This is what findDiffX()
returns here. { a: number; b: number }
from: Math.max(from - 1, 0), | ||
to: to.b - 1, | ||
rule, | ||
pasteEvent: pasteEvt, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we take pasteEvent
as a variable name and also use a shorthand here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a global variable here with that name: https://github.com/ueberdosis/tiptap/pull/5046/files#diff-6eddae5591c39b131ab5597e8ac574df71fafda96023c4573c45fd73ed066b9fR177
const { from, text } = simulatedPasteMeta | ||
const to = from + text.length | ||
const pasteEvt = createClipboardPasteEvent(text) | ||
|
||
return processEvent({ | ||
rule, | ||
state, | ||
from, | ||
to: { b: to }, | ||
pasteEvt, | ||
}) | ||
} | ||
|
||
// handle actual paste/drop | ||
const from = oldState.doc.content.findDiffStart(state.doc.content) | ||
const to = oldState.doc.content.findDiffEnd(state.doc.content) | ||
|
||
// stop if there is no changed range | ||
if (!isNumber(from) || !to || from === to.b) { | ||
return | ||
} | ||
|
||
// build a chainable state | ||
// so we can use a single transaction for all paste rules | ||
const tr = state.tr | ||
const chainableState = createChainableState({ | ||
state, | ||
transaction: tr, | ||
}) | ||
|
||
const handler = run({ | ||
editor, | ||
state: chainableState, | ||
from: Math.max(from - 1, 0), | ||
to: to.b - 1, | ||
return processEvent({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see why this processEvent
function was extracted, but all that it is doing is switching the arguments that it is operating on.
I think it'd be clearer to inline it again, and have the if (isSimulatedPaste) {
switch the values it operates on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, tbh that's what I had done before and I thought it might be easier to understand that way 😂
Please describe your changes
First call to enable the option of applying input and paste rules when using
insertContent
/insertContentAt
. This is interesting, for example, if streamed content contains markdown characters and characters are added character by character within the editor.How did you accomplish your changes
Added
applyInputRules
andapplyPasteRules
toinsertContent
/insertContentAt
options. Within the InputRule and PasteRule plugin I created some logic to trigger the rules.How have you tested your changes
I wrote tests and checked a few scenarios by hand.
How can we verify your changes
Check the "InsertContentApplyingRules" demo.
https://deploy-preview-5046--tiptap-embed.netlify.app/src/commands/insertcontentapplyingrules/react/
Checklist