Skip to content
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

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

svenadlung
Copy link
Contributor

@svenadlung svenadlung commented Apr 9, 2024

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 and applyPasteRules to insertContent/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

  • The changes are not breaking the editor
  • Added tests where possible
  • Followed the guidelines
  • Fixed linting issues

@svenadlung svenadlung requested a review from bdbch as a code owner April 9, 2024 07:54
Copy link

netlify bot commented Apr 9, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit 148402f
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/6614f4415fd46800084a9a58
😎 Deploy Preview https://deploy-preview-5046--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@mkriegeskorte mkriegeskorte left a 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 ;))

Copy link
Contributor

@bdbch bdbch left a 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 }
Copy link
Contributor

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?

Copy link
Contributor Author

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,
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +274 to +296
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({
Copy link
Contributor

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.

Copy link
Contributor Author

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 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants