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

fix: convert content to a string before passing it to applyPasteRules meta value in insertContentAt.ts #5152

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions demos/src/Commands/InsertContentApplyingRules/React/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ const MenuBar = () => {
>
Insert "*this is a test*"
</button>
<button
onClick={() => {
editor
.chain()
.insertContent('<p>*This is an italic text*</p>', {
applyInputRules: useInputRules,
applyPasteRules: usePasteRules,
})
.focus()
.run()
}}
data-test-8
>
Insert <p>*This is an italic text*</p>
</button>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ context('/src/Commands/InsertContentApplyingRules/React/', () => {
cy.get('.tiptap').should('contain.html', '<p><em>This is an italic text</em></p>')
})
})

it('should apply paste rules to html', () => { // todo: naming
cy.get('.tiptap').then(([{ editor }]) => {
const content = '<p>*This is an italic text*</p>' // TODO: create special case here

editor.commands.insertContent(content, { applyPasteRules: true })
cy.get('.tiptap').should('contain.html', '<p><em>This is an italic text</em></p>') // TODO: check if paste rules applied.
})
})

})
80 changes: 71 additions & 9 deletions packages/core/src/PasteRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function run(config: {
const textToMatch = node.textBetween(resolvedFrom - pos, resolvedTo - pos, undefined, '\ufffc')

const matches = pasteRuleMatcherHandler(textToMatch, rule.find, pasteEvent)
const toCompare = /(?:^|\s)(\*(?!\s+\*)((?:[^*]+))\*(?!\s+\*))/g

if (rule.find.toString() === toCompare.toString()) {
console.log(resolvedFrom, resolvedTo, pos, node.content.size)
}

matches.forEach(match => {
if (match.index === undefined) {
Expand Down Expand Up @@ -220,6 +225,45 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
return tr
}

const processEventv2 = ({
state,
from,
to,
rule,
pasteEvt,
}: {
state: EditorState
from: number
to: { b: number }
rule: PasteRule
pasteEvt: ClipboardEvent | null
}) => {
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,
rule,
pasteEvent: pasteEvt,
dropEvent,
})

if (!handler || !tr.steps.length) {
return tr
}

dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
pasteEvent = typeof ClipboardEvent !== 'undefined' ? new ClipboardEvent('paste') : null

return tr
}

const plugins = rules.map(rule => {
return new Plugin({
// we register a global drag handler to track the current drag source element
Expand Down Expand Up @@ -275,17 +319,35 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):

// Handle simulated paste
if (isSimulatedPaste) {
const { from, text } = simulatedPasteMeta
const to = from + text.length
const text = simulatedPasteMeta.text
let from = simulatedPasteMeta.from
const pasteEvt = createClipboardPasteEvent(text)

return processEvent({
rule,
state,
from,
to: { b: to },
pasteEvt,
})
if (typeof text === 'string') {
const to = from + text.length

return processEvent({
rule,
state,
from,
to: { b: to },
pasteEvt,
})
}
let tr = state.tr

for (let i = 0; i < text.content.length; i += 1) {
console.log(from, from + text.content[i].content.size)
tr = processEventv2({
rule,
state,
from,
to: { b: text.content[i].content.size + 1 },
pasteEvt,
})
from += text.content[i].content.size
}
return tr
}

// handle actual paste/drop
Expand Down