Created
May 4, 2022 12:20
-
-
Save gregveres/8757756d56becc2c053c46540cb6b314 to your computer and use it in GitHub Desktop.
line-heights for tiptap 2
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
import { Extension } from "@tiptap/core"; | |
export interface LineHeightOptions { | |
types: string[]; | |
heights: string[]; | |
defaultHeight: string; | |
} | |
declare module "@tiptap/core" { | |
interface Commands<ReturnType> { | |
lineHeight: { | |
/** | |
* Set the line height attribute | |
*/ | |
setLineHeight: (height: string) => ReturnType; | |
/** | |
* Unset the text align attribute | |
*/ | |
unsetLineHeight: () => ReturnType; | |
}; | |
} | |
} | |
export const LineHeight = Extension.create<LineHeightOptions>({ | |
name: "lineHeight", | |
addOptions() { | |
return { | |
types: ["heading", "paragraph"], | |
heights: ["100%", "115%", "150%", "200%", "250%", "300%"], | |
defaultHeight: "100%", | |
}; | |
}, | |
addGlobalAttributes() { | |
return [ | |
{ | |
types: this.options.types, | |
attributes: { | |
lineHeight: { | |
default: this.options.defaultHeight, | |
parseHTML: (element) => | |
element.style.lineHeight || this.options.defaultHeight, | |
renderHTML: (attributes) => { | |
if (attributes.lineHeight === this.options.defaultHeight) { | |
return {}; | |
} | |
return { style: `line-height: ${attributes.lineHeight}` }; | |
}, | |
}, | |
}, | |
}, | |
]; | |
}, | |
addCommands() { | |
return { | |
setLineHeight: | |
(height: string) => | |
({ commands }) => { | |
if (!this.options.heights.includes(height)) { | |
return false; | |
} | |
return this.options.types.every((type) => | |
commands.updateAttributes(type, { lineHeight: height }) | |
); | |
}, | |
unsetLineHeight: | |
() => | |
({ commands }) => { | |
return this.options.types.every((type) => | |
commands.resetAttributes(type, "lineHeight") | |
); | |
}, | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
canya do a reactjs nextjs / .js version, simplified, tiptap.dev - tks for the consideration