|
| 1 | +import type { NodeWithPos } from "@tiptap/core" |
| 2 | +import { Extension } from "@tiptap/core" |
| 3 | +import type { EditorState, Transaction } from "@tiptap/pm/state" |
| 4 | +import { getSelectedNodesOfType } from "@/lib/tiptap-utils" |
| 5 | +import { updateNodesAttr } from "@/lib/tiptap-utils" |
| 6 | + |
| 7 | +declare module "@tiptap/core" { |
| 8 | + interface Commands<ReturnType> { |
| 9 | + nodeBackground: { |
| 10 | + setNodeBackgroundColor: (backgroundColor: string) => ReturnType |
| 11 | + unsetNodeBackgroundColor: () => ReturnType |
| 12 | + toggleNodeBackgroundColor: (backgroundColor: string) => ReturnType |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export interface NodeBackgroundOptions { |
| 18 | + /** |
| 19 | + * Node types that should support background colors |
| 20 | + * @default ["paragraph", "heading", "blockquote", "taskList", "bulletList", "orderedList", "tableCell", "tableHeader"] |
| 21 | + */ |
| 22 | + types: string[] |
| 23 | + /** |
| 24 | + * Use inline style instead of data attribute |
| 25 | + * @default true |
| 26 | + */ |
| 27 | + useStyle?: boolean |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Determines the target color for toggle operations |
| 32 | + */ |
| 33 | +function getToggleColor( |
| 34 | + targets: NodeWithPos[], |
| 35 | + inputColor: string |
| 36 | +): string | null { |
| 37 | + if (targets.length === 0) return null |
| 38 | + |
| 39 | + for (const target of targets) { |
| 40 | + const currentColor = target.node.attrs?.backgroundColor ?? null |
| 41 | + if (currentColor !== inputColor) { |
| 42 | + return inputColor |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return null |
| 47 | +} |
| 48 | + |
| 49 | +export const NodeBackground = Extension.create<NodeBackgroundOptions>({ |
| 50 | + name: "nodeBackground", |
| 51 | + |
| 52 | + addOptions() { |
| 53 | + return { |
| 54 | + types: [ |
| 55 | + "paragraph", |
| 56 | + "heading", |
| 57 | + "blockquote", |
| 58 | + "taskList", |
| 59 | + "bulletList", |
| 60 | + "orderedList", |
| 61 | + "tableCell", |
| 62 | + "tableHeader", |
| 63 | + ], |
| 64 | + useStyle: true, |
| 65 | + } |
| 66 | + }, |
| 67 | + |
| 68 | + addGlobalAttributes() { |
| 69 | + return [ |
| 70 | + { |
| 71 | + types: this.options.types, |
| 72 | + attributes: { |
| 73 | + backgroundColor: { |
| 74 | + default: null as string | null, |
| 75 | + |
| 76 | + parseHTML: (element: HTMLElement) => { |
| 77 | + const styleColor = element.style?.backgroundColor |
| 78 | + if (styleColor) return styleColor |
| 79 | + |
| 80 | + const dataColor = element.getAttribute("data-background-color") |
| 81 | + return dataColor || null |
| 82 | + }, |
| 83 | + |
| 84 | + renderHTML: (attributes) => { |
| 85 | + const color = attributes.backgroundColor as string | null |
| 86 | + if (!color) return {} |
| 87 | + |
| 88 | + if (this.options.useStyle) { |
| 89 | + return { |
| 90 | + style: `background-color: ${color}`, |
| 91 | + } |
| 92 | + } else { |
| 93 | + return { |
| 94 | + "data-background-color": color, |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + ] |
| 102 | + }, |
| 103 | + |
| 104 | + addCommands() { |
| 105 | + /** |
| 106 | + * Generic command executor for background color operations |
| 107 | + */ |
| 108 | + const executeBackgroundCommand = ( |
| 109 | + getTargetColor: ( |
| 110 | + targets: NodeWithPos[], |
| 111 | + inputColor?: string |
| 112 | + ) => string | null |
| 113 | + ) => { |
| 114 | + return (inputColor?: string) => |
| 115 | + ({ state, tr }: { state: EditorState; tr: Transaction }) => { |
| 116 | + const targets = getSelectedNodesOfType( |
| 117 | + state.selection, |
| 118 | + this.options.types |
| 119 | + ) |
| 120 | + |
| 121 | + if (targets.length === 0) return false |
| 122 | + |
| 123 | + const targetColor = getTargetColor(targets, inputColor) |
| 124 | + |
| 125 | + return updateNodesAttr(tr, targets, "backgroundColor", targetColor) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return { |
| 130 | + /** |
| 131 | + * Set background color to specific value |
| 132 | + */ |
| 133 | + setNodeBackgroundColor: executeBackgroundCommand( |
| 134 | + (_, inputColor) => inputColor || null |
| 135 | + ), |
| 136 | + |
| 137 | + /** |
| 138 | + * Remove background color |
| 139 | + */ |
| 140 | + unsetNodeBackgroundColor: executeBackgroundCommand(() => null), |
| 141 | + |
| 142 | + /** |
| 143 | + * Toggle background color (set if different/missing, unset if all have it) |
| 144 | + */ |
| 145 | + toggleNodeBackgroundColor: executeBackgroundCommand( |
| 146 | + (targets, inputColor) => getToggleColor(targets, inputColor || "") |
| 147 | + ), |
| 148 | + } |
| 149 | + }, |
| 150 | +}) |
0 commit comments