|
|
|
|
|
import { |
|
commandsCtx, |
|
editorViewCtx, |
|
parserCtx, |
|
serializerCtx |
|
} from '@milkdown/core'; |
|
import { $command, $useKeymap } from '@milkdown/utils'; |
|
|
|
import { handlePrompt } from './handle-prompt'; |
|
|
|
export function makeEnterPlugins({ workerConnection }) { |
|
|
|
const myEnterCommand = $command('MyEnterCommand', (ctx) => { |
|
return () => (state, dispatch) => { |
|
const view = ctx.get(editorViewCtx); |
|
const toMarkdown = ctx.get(serializerCtx); |
|
const fromMarkdown = ctx.get(parserCtx); |
|
const markdown = (toMarkdown(view.state.doc) || '').trim(); |
|
|
|
if (markdown) { |
|
handlePrompt({ promptMarkdown: markdown, workerConnection }); |
|
} |
|
|
|
|
|
const emptyDoc = fromMarkdown(''); |
|
const tr = view.state.tr.replaceWith(0, view.state.doc.content.size, emptyDoc.content); |
|
view.dispatch(tr); |
|
return true; |
|
}; |
|
}); |
|
|
|
const myEnterKeymap = $useKeymap('MyEnterKeymap', { |
|
MyEnter: { |
|
shortcuts: 'Enter', |
|
command: (ctx) => { |
|
const commands = ctx.get(commandsCtx); |
|
return () => { |
|
|
|
const slashMenu = document.querySelector('.milkdown-slash-menu[data-show="true"]'); |
|
if (slashMenu) { |
|
|
|
return false; |
|
} |
|
return commands.call(myEnterCommand.key); |
|
}; |
|
}, |
|
priority: 50, |
|
}, |
|
}); |
|
|
|
return [ |
|
myEnterCommand, |
|
myEnterKeymap |
|
]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setupCrepeEnterKey(crepeInput, workerConnection) { |
|
|
|
crepeInput.editor.action((ctx) => { |
|
const view = ctx.get(editorViewCtx); |
|
if (view.dom) { |
|
view.dom.addEventListener('keydown', (e) => { |
|
if (e.key === 'Enter' && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { |
|
|
|
const slashMenu = document.querySelector('.milkdown-slash-menu[data-show="true"]'); |
|
if (slashMenu) { |
|
return false; |
|
} |
|
|
|
e.preventDefault(); |
|
|
|
|
|
const toMarkdown = ctx.get(serializerCtx); |
|
const markdown = toMarkdown(view.state.doc).trim(); |
|
|
|
if (markdown) { |
|
handlePrompt({ promptMarkdown: markdown, workerConnection }); |
|
|
|
const fromMarkdown = ctx.get(parserCtx); |
|
const emptyDoc = fromMarkdown(''); |
|
const tr = view.state.tr.replaceWith(0, view.state.doc.content.size, emptyDoc.content); |
|
view.dispatch(tr); |
|
} |
|
|
|
return true; |
|
} |
|
return false; |
|
}); |
|
} |
|
}); |
|
} |