-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restrict blockquote marks on transaction instead of schema
- Loading branch information
Showing
9 changed files
with
146 additions
and
191 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,113 @@ | ||
import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core' | ||
import { Plugin, PluginKey } from '@tiptap/pm/state' | ||
|
||
export interface BlockquoteOptions { | ||
/** | ||
* HTML attributes to add to the blockquote element | ||
* @default {} | ||
* @example { class: 'foo' } | ||
*/ | ||
HTMLAttributes: Record<string, any> | ||
} | ||
|
||
declare module '@tiptap/core' { | ||
interface Commands<ReturnType> { | ||
blockquote: { | ||
setBlockquote: () => ReturnType | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Matches a blockquote to a `>` as input. | ||
*/ | ||
export const inputRegex = /^\s*>\s$/ | ||
|
||
/** | ||
* This extension allows you to create blockquotes, | ||
* contains only plain text paragraph and soft break (<br>). | ||
* | ||
* Forked from: | ||
* @see https://tiptap.dev/api/nodes/blockquote | ||
*/ | ||
export const Blockquote = Node.create<BlockquoteOptions>({ | ||
name: 'blockquote', | ||
|
||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
} | ||
}, | ||
|
||
group: 'block', | ||
|
||
content: 'paragraph+', | ||
|
||
defining: true, | ||
|
||
parseHTML() { | ||
return [{ tag: 'blockquote' }] | ||
}, | ||
|
||
renderHTML({ HTMLAttributes }) { | ||
return [ | ||
'blockquote', | ||
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), | ||
0, | ||
] | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
setBlockquote: | ||
() => | ||
({ chain }) => { | ||
return chain().wrapIn(this.name).run() | ||
}, | ||
} | ||
}, | ||
|
||
addKeyboardShortcuts() { | ||
return { | ||
'Mod-Shift-b': () => this.editor.commands.setBlockquote(), | ||
} | ||
}, | ||
|
||
addInputRules() { | ||
return [ | ||
wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}), | ||
] | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
return [ | ||
new Plugin({ | ||
key: new PluginKey('restrictBlockquoteMarks'), | ||
filterTransaction: (transaction, state) => { | ||
// Nothing has changed, ignore it. | ||
if (!transaction.docChanged) { | ||
return true | ||
} | ||
|
||
// Skip if not in a blockquote | ||
const $anchor = transaction.selection.$anchor | ||
const $grandParent = $anchor.node($anchor.depth - 1) | ||
const isInBlockquote = $grandParent.type.name === this.name | ||
if (!isInBlockquote) { | ||
return true | ||
} | ||
|
||
// Prevent adding marks (except <br>) to blockquote | ||
const $start = $anchor.start($anchor.depth - 1) | ||
const $end = $anchor.end($anchor.depth - 1) | ||
transaction.removeMark($start, $end) | ||
|
||
return true | ||
}, | ||
}), | ||
] | ||
}, | ||
}) |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.