-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unlike other components, addresses use hardbreaks between lines of the address rather than new paragraphs. This PR disables the shift+enter mapping for linebreaks and instead adds custom mapping to the enter key to insert linebreaks when in an address instead. When the user is at the end of an address and presses enter for a second time we instead insert a paragraph and return false to let the default handler lift the paragraph out of the address.
- Loading branch information
1 parent
ba8968f
commit 787ada2
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Selection } from "prosemirror-state"; | ||
import { keymap } from "prosemirror-keymap"; | ||
|
||
const newlineInAddress = (schema) => { | ||
return (state, dispatch) => { | ||
const { $head, $anchor } = state.selection; | ||
if (!$head.sameParent($anchor)) return false; | ||
|
||
const paragraphParent = $head.node($head.depth - 1); | ||
if (!(paragraphParent.type === schema.nodes.address)) return false; | ||
|
||
if ( | ||
$head.nodeBefore.type === schema.nodes.hard_break && | ||
$head.parentOffset === $head.parent.content.size | ||
) { | ||
const pos = $head.after(); | ||
const tr = state.tr.deleteRange(pos - 2, pos); | ||
tr.replaceWith(pos - 2, pos - 2, schema.nodes.paragraph.createAndFill()); | ||
tr.setSelection(Selection.near(tr.doc.resolve(pos - 1), 1)); | ||
tr.split(pos - 1); | ||
dispatch(tr.scrollIntoView()); | ||
return false; | ||
} | ||
|
||
const hardBreak = schema.node("hard_break"); | ||
dispatch(state.tr.replaceSelectionWith(hardBreak).scrollIntoView()); | ||
return true; | ||
}; | ||
}; | ||
|
||
export default function customKeymap(schema) { | ||
return keymap({ | ||
Enter: newlineInAddress(schema), | ||
}); | ||
} |