Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
gabemagee-ev committed Jun 5, 2024
1 parent 421d9a9 commit 12a46e4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
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
10 changes: 10 additions & 0 deletions demos/src/Commands/InsertContentApplyingRules/React/index.spec.js
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.
})
})

})
81 changes: 71 additions & 10 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,18 +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 to = from + (typeof text === 'string' ? text.length : text.size)
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

0 comments on commit 12a46e4

Please sign in to comment.