Spaces:
Running
Running
File size: 6,597 Bytes
7b0525c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | import { Blockquote } from '@tiptap/extension-blockquote';
import { ReactNodeViewRenderer } from '@tiptap/react';
import { QuoteComponent } from '../QuoteComponent';
export interface CustomBlockquoteOptions {
HTMLAttributes: Record<string, any>;
}
export const CustomBlockquote = Blockquote.extend<CustomBlockquoteOptions>({
name: 'customBlockquote',
addNodeView() {
return ReactNodeViewRenderer(QuoteComponent);
},
addAttributes() {
return {
borderColor: {
default: 'gray',
parseHTML: (element) => element.style.borderLeftColor,
renderHTML: (attributes) => {
if (!attributes.borderColor) {
return {};
}
return {
style: `border-left-color: ${attributes.borderColor}`,
};
},
},
borderWidth: {
default: '4px',
parseHTML: (element) => element.style.borderLeftWidth,
renderHTML: (attributes) => {
if (!attributes.borderWidth) return {};
return { style: `border-left-width: ${attributes.borderWidth}` };
}
},
backgroundColor: {
default: 'transparent',
parseHTML: (element) => element.style.backgroundColor,
renderHTML: (attributes) => {
if (!attributes.backgroundColor) {
return {};
}
return {
style: `background-color: ${attributes.backgroundColor}`,
};
},
},
textColor: {
default: 'inherit',
parseHTML: (element) => element.style.color,
renderHTML: (attributes) => {
if (!attributes.textColor) {
return {};
}
return {
style: `color: ${attributes.textColor}`,
};
},
},
padding: {
default: '4px',
parseHTML: (element) => element.style.padding,
renderHTML: (attributes) => {
if (!attributes.padding) return {};
return { style: `padding: ${attributes.padding}` };
}
},
fontSize: {
default: 'inherit',
parseHTML: (element) => element.style.fontSize,
renderHTML: (attributes) => {
if (!attributes.fontSize) return {};
return { style: `font-size: ${attributes.fontSize}` };
}
},
borderRadius: {
default: '0px',
parseHTML: (element) => element.style.borderRadius,
renderHTML: (attributes) => {
if (!attributes.borderRadius) return {};
return { style: `border-radius: ${attributes.borderRadius}` };
}
},
width: {
default: '100%',
parseHTML: (element) => element.style.width,
renderHTML: (attributes) => {
if (!attributes.width) return {};
return { style: `width: ${attributes.width}` };
}
},
height: {
default: 'auto',
parseHTML: (element) => element.style.height,
renderHTML: (attributes) => {
if (!attributes.height) return {};
return { style: `height: ${attributes.height}` };
}
},
italic: {
default: false,
parseHTML: (element) => element.style.fontStyle === 'italic',
renderHTML: (attributes) => {
if (!attributes.italic) return {};
return { style: `font-style: italic` };
}
},
showCopyButton: {
default: false,
parseHTML: (element) => element.getAttribute('data-show-copy') === 'true',
renderHTML: (attributes) => {
return { 'data-show-copy': attributes.showCopyButton };
}
},
fontFamily: {
default: 'inherit',
parseHTML: (element) => element.style.fontFamily,
renderHTML: (attributes) => {
if (!attributes.fontFamily) return {};
return { style: `font-family: ${attributes.fontFamily}` };
}
},
isCodeFont: {
default: false,
parseHTML: (element) => element.getAttribute('data-is-code-font') === 'true',
renderHTML: (attributes) => {
return { 'data-is-code-font': attributes.isCodeFont };
}
},
showDownloadButton: {
default: false,
parseHTML: (element) => element.getAttribute('data-show-download') === 'true',
renderHTML: (attributes) => {
return { 'data-show-download': attributes.showDownloadButton };
}
},
collapsible: {
default: true,
parseHTML: (element) => element.getAttribute('data-collapsible') === 'true',
renderHTML: (attributes) => {
return { 'data-collapsible': attributes.collapsible };
}
}
};
},
renderHTML({ HTMLAttributes }) {
// Merge all style attributes into a single style string to avoid multiple style attributes
const styleParts: string[] = [];
// Helper to extract style from HTMLAttributes if it exists as a separate property
// (though renderHTML in addAttributes usually handles adding it to HTMLAttributes.style)
// However, TipTap merges them. Let's ensure we have a clean style string.
// Actually, TipTap's default renderHTML merges the attributes returned by individual renderHTML functions.
// So we just need to return the tag.
return ['blockquote', HTMLAttributes, 0];
},
});
|