Skip to content

Commit

Permalink
feat: undo inputrule when backspace (#1175)
Browse files Browse the repository at this point in the history
* feat: undo inputrule when backspace

* test: add e2e
  • Loading branch information
Saul-Mirone authored Nov 27, 2023
1 parent 9b50b16 commit 5719e40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions e2e/cypress/e2e/preset-commonmark/input.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,12 @@ describe('input:', () => {
cy.isMarkdown('The lunatic is `on the grass`\n')
})
})

it('undo input rule when press backspace', () => {
cy.get('.editor').type('The lunatic is *on the grass*')
cy.get('.editor').get('em').should('have.text', 'on the grass')
cy.get('.editor').type('{backspace}')
cy.isMarkdown('The lunatic is \\*on the grass\\*\n')
})
})
})
17 changes: 15 additions & 2 deletions packages/core/src/internal-plugin/editor-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { MilkdownPlugin, TimerType } from '@milkdown/ctx'
import { createSlice, createTimer } from '@milkdown/ctx'
import { docTypeError } from '@milkdown/exception'
import { customInputRules as createInputRules } from '@milkdown/prose'
import { baseKeymap } from '@milkdown/prose/commands'
import { baseKeymap, chainCommands, deleteSelection, joinBackward, selectNodeBackward } from '@milkdown/prose/commands'
import { undoInputRule } from '@milkdown/prose/inputrules'
import { keymap as createKeymap } from '@milkdown/prose/keymap'
import type { Schema } from '@milkdown/prose/model'
import { DOMParser, Node } from '@milkdown/prose/model'
import type { Command } from '@milkdown/prose/state'
import { EditorState, Plugin, PluginKey } from '@milkdown/prose/state'
import type { JSONRecord, Parser } from '@milkdown/transformer'

Expand Down Expand Up @@ -55,6 +57,17 @@ export function getDoc(defaultValue: DefaultValue, parser: Parser, schema: Schem

const key = new PluginKey('MILKDOWN_STATE_TRACKER')

function overrideBaseKeymap(keymap: Record<string, Command>) {
const handleBackspace = chainCommands(
undoInputRule,
deleteSelection,
joinBackward,
selectNodeBackward,
)
keymap.Backspace = handleBackspace
return keymap
}

/// The editor state plugin.
/// This plugin will create a prosemirror editor state.
///
Expand Down Expand Up @@ -91,7 +104,7 @@ export const editorState: MilkdownPlugin = (ctx) => {
},
}),
createInputRules({ rules }),
createKeymap(baseKeymap),
createKeymap(overrideBaseKeymap(baseKeymap)),
]

ctx.set(prosePluginsCtx, plugins)
Expand Down

0 comments on commit 5719e40

Please sign in to comment.