|
| 1 | +import { |
| 2 | + createBlockSpecFromStronglyTypedTiptapNode, |
| 3 | + createStronglyTypedTiptapNode, |
| 4 | +} from "../../schema/index.js"; |
| 5 | +import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers.js"; |
| 6 | +import { defaultProps } from "../defaultProps.js"; |
| 7 | +import { getBlockInfoFromSelection } from "../../api/getBlockInfoFromPos.js"; |
| 8 | +import { updateBlockCommand } from "../../api/blockManipulation/commands/updateBlock/updateBlock.js"; |
| 9 | +import { InputRule } from "@tiptap/core"; |
| 10 | + |
| 11 | +export const quotePropSchema = { |
| 12 | + ...defaultProps, |
| 13 | +}; |
| 14 | + |
| 15 | +export const QuoteBlockContent = createStronglyTypedTiptapNode({ |
| 16 | + name: "quote", |
| 17 | + content: "inline*", |
| 18 | + group: "blockContent", |
| 19 | + |
| 20 | + addInputRules() { |
| 21 | + return [ |
| 22 | + // Creates a block quote when starting with ">". |
| 23 | + new InputRule({ |
| 24 | + find: new RegExp(`^>\\s$`), |
| 25 | + handler: ({ state, chain, range }) => { |
| 26 | + const blockInfo = getBlockInfoFromSelection(state); |
| 27 | + if ( |
| 28 | + !blockInfo.isBlockContainer || |
| 29 | + blockInfo.blockContent.node.type.spec.content !== "inline*" |
| 30 | + ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + chain() |
| 35 | + .command( |
| 36 | + updateBlockCommand( |
| 37 | + this.options.editor, |
| 38 | + blockInfo.bnBlock.beforePos, |
| 39 | + { |
| 40 | + type: "quote", |
| 41 | + props: {}, |
| 42 | + } |
| 43 | + ) |
| 44 | + ) |
| 45 | + // Removes the ">" character used to set the list. |
| 46 | + .deleteRange({ from: range.from, to: range.to }); |
| 47 | + }, |
| 48 | + }), |
| 49 | + ]; |
| 50 | + }, |
| 51 | + |
| 52 | + addKeyboardShortcuts() { |
| 53 | + return { |
| 54 | + "Mod-Alt-q": () => { |
| 55 | + const blockInfo = getBlockInfoFromSelection(this.editor.state); |
| 56 | + if ( |
| 57 | + !blockInfo.isBlockContainer || |
| 58 | + blockInfo.blockContent.node.type.spec.content !== "inline*" |
| 59 | + ) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + return this.editor.commands.command( |
| 64 | + updateBlockCommand(this.options.editor, blockInfo.bnBlock.beforePos, { |
| 65 | + type: "quote", |
| 66 | + }) |
| 67 | + ); |
| 68 | + }, |
| 69 | + }; |
| 70 | + }, |
| 71 | + |
| 72 | + parseHTML() { |
| 73 | + return [ |
| 74 | + { tag: "div[data-content-type=" + this.name + "]" }, |
| 75 | + { |
| 76 | + tag: "blockquote", |
| 77 | + node: "quote", |
| 78 | + }, |
| 79 | + ]; |
| 80 | + }, |
| 81 | + |
| 82 | + renderHTML({ HTMLAttributes }) { |
| 83 | + return createDefaultBlockDOMOutputSpec( |
| 84 | + this.name, |
| 85 | + "blockquote", |
| 86 | + { |
| 87 | + ...(this.options.domAttributes?.blockContent || {}), |
| 88 | + ...HTMLAttributes, |
| 89 | + }, |
| 90 | + this.options.domAttributes?.inlineContent || {} |
| 91 | + ); |
| 92 | + }, |
| 93 | +}); |
| 94 | + |
| 95 | +export const Quote = createBlockSpecFromStronglyTypedTiptapNode( |
| 96 | + QuoteBlockContent, |
| 97 | + quotePropSchema |
| 98 | +); |
0 commit comments