-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds the ability to support horizontal rule in article editor
- Loading branch information
Showing
4 changed files
with
46 additions
and
2 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,40 @@ | ||
import { inputRules } from 'prosemirror-inputrules'; | ||
import { safeInsert } from 'prosemirror-utils'; | ||
import { createInputRule } from '../utils'; | ||
|
||
function createHorizontalRuleInputRule(type) { | ||
return createInputRule( | ||
/^(?:---|___|\*\*\*)\s$/, // Ensures rule is triggered with space after "---", "___", or "***" | ||
(state, match, start, end) => { | ||
if (!match[0]) { | ||
return null; // If no match found, return null | ||
} | ||
|
||
// Deletes the matched sequence including the space | ||
let tr = state.tr.delete(start, end); | ||
const hrPos = start; // Position where the horizontal rule should be inserted | ||
|
||
// Insert the horizontal rule at the position | ||
tr = safeInsert(type.create(), hrPos)(tr); | ||
|
||
// Insert a paragraph node after the horizontal rule | ||
tr = safeInsert(state.schema.nodes.paragraph.create(), tr.mapping.map(hrPos + 1))(tr); | ||
|
||
return tr; | ||
} | ||
); | ||
} | ||
|
||
export function hrInputRules(schema) { | ||
if (!schema.nodes.horizontal_rule) { | ||
return null; // Ensures that horizontal_rule is part of the schema | ||
} | ||
|
||
const hrRule = createHorizontalRuleInputRule(schema.nodes.horizontal_rule); | ||
|
||
return inputRules({ | ||
rules: [hrRule], | ||
}); | ||
} | ||
|
||
export default hrInputRules; |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { listInputRules } from './lists'; | ||
export { linksInputRules } from './links'; | ||
export { blocksInputRule } from './blocks'; | ||
export { hrInputRules } from './hr'; | ||
export { baseKeyMaps } from '../keymap'; | ||
|
||
export { textFormattingInputRules } from './marks'; |
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