Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed May 27, 2024
1 parent daa5e52 commit fddfe31
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
24 changes: 21 additions & 3 deletions packages/core/src/InputRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {
Fragment,
Node as ProseMirrorNode,
} from '@tiptap/pm/model'
import { EditorState, Plugin, TextSelection } from '@tiptap/pm/state'

import { CommandManager } from './CommandManager.js'
import { Editor } from './Editor.js'
import { createChainableState } from './helpers/createChainableState.js'
import { getHTMLFromFragment } from './helpers/getHTMLFromFragment.js'
import { getTextContentFromNodes } from './helpers/getTextContentFromNodes.js'
import {
CanCommands,
Expand Down Expand Up @@ -184,20 +189,33 @@ export function inputRulesPlugin(props: { editor: Editor; rules: InputRule[] }):
init() {
return null
},
apply(tr, prev) {
apply(tr, prev, state) {
const stored = tr.getMeta(plugin)

if (stored) {
return stored
}

// if InputRule is triggered by insertContent()
const simulatedInputMeta = tr.getMeta('applyInputRules')
const simulatedInputMeta = tr.getMeta('applyInputRules') as { from: number; text: string | ProseMirrorNode | Fragment } | undefined
const isSimulatedInput = !!simulatedInputMeta

if (isSimulatedInput) {
setTimeout(() => {
const { from, text } = simulatedInputMeta

let { text } = simulatedInputMeta

if (typeof text === 'string') {
text = text as string
} else {
if ('content' in text) {
text = text.content
}

text = getHTMLFromFragment(text, state.schema)
}

const { from } = simulatedInputMeta
const to = from + text.length

run({
Expand Down
37 changes: 33 additions & 4 deletions packages/core/src/PasteRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {
Fragment,
Node as ProseMirrorNode,
} from '@tiptap/pm/model'
import { EditorState, Plugin } from '@tiptap/pm/state'

import { CommandManager } from './CommandManager.js'
import { Editor } from './Editor.js'
import { createChainableState } from './helpers/createChainableState.js'
import { getHTMLFromFragment } from './helpers/getHTMLFromFragment.js'
import {
CanCommands,
ChainedCommands,
Expand Down Expand Up @@ -179,7 +184,13 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
let isPastedFromProseMirror = false
let isDroppedFromProseMirror = false
let pasteEvent = typeof ClipboardEvent !== 'undefined' ? new ClipboardEvent('paste') : null
let dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
let dropEvent: DragEvent | null

try {
dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
} catch (e) {
dropEvent = null
}

const processEvent = ({
state,
Expand Down Expand Up @@ -214,7 +225,11 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
return
}

dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
try {
dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
} catch (e) {
dropEvent = null
}
pasteEvent = typeof ClipboardEvent !== 'undefined' ? new ClipboardEvent('paste') : null

return tr
Expand Down Expand Up @@ -266,7 +281,7 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror

// if PasteRule is triggered by insertContent()
const simulatedPasteMeta = transaction.getMeta('applyPasteRules')
const simulatedPasteMeta = transaction.getMeta('applyPasteRules') as { from: number; text: string | ProseMirrorNode | Fragment } | undefined
const isSimulatedPaste = !!simulatedPasteMeta

if (!isPaste && !isDrop && !isSimulatedPaste) {
Expand All @@ -275,8 +290,22 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):

// Handle simulated paste
if (isSimulatedPaste) {
const { from, text } = simulatedPasteMeta

let { text } = simulatedPasteMeta

if (typeof text === 'string') {
text = text as string
} else {
if ('content' in text) {
text = text.content
}

text = getHTMLFromFragment(text, state.schema)
}

const { from } = simulatedPasteMeta
const to = from + text.length

const pasteEvt = createClipboardPasteEvent(text)

return processEvent({
Expand Down

0 comments on commit fddfe31

Please sign in to comment.