-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add nodePasteRule to core
- Loading branch information
Showing
3 changed files
with
45 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './markPasteRule' | ||
export * from './nodePasteRule' | ||
export * from './textPasteRule' |
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,39 @@ | ||
import { NodeType } from 'prosemirror-model' | ||
|
||
import { PasteRule } from '../PasteRule' | ||
import { ExtendedRegExpMatchArray } from '../types' | ||
import { callOrReturn } from '../utilities' | ||
|
||
/** | ||
* Build an paste rule that adds a node when the | ||
* matched text is pasted into it. | ||
*/ | ||
export function nodePasteRule(config: { | ||
find: RegExp, | ||
type: NodeType, | ||
getAttributes?: | ||
| Record<string, any> | ||
| ((match: ExtendedRegExpMatchArray) => Record<string, any>) | ||
| false | ||
| null, | ||
}) { | ||
return new PasteRule({ | ||
find: config.find, | ||
handler({ match, chain, range }) { | ||
const attributes = callOrReturn(config.getAttributes, undefined, match) | ||
|
||
if (attributes === false || attributes === null) { | ||
return null | ||
} | ||
|
||
if (match.input) { | ||
chain() | ||
.deleteRange(range) | ||
.insertContent({ | ||
type: config.type.name, | ||
attrs: attributes, | ||
}) | ||
} | ||
}, | ||
}) | ||
} |
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