index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Extension } from "@tiptap/core";
  2. import { createLineHeightCommand } from "./utils";
  3. import LineHeightExt from "./LineHeight.vue";
  4. export interface LineHeightOptions {
  5. types: string[];
  6. lineHeights: string[];
  7. defaultHeight: string;
  8. }
  9. declare module "@tiptap/core" {
  10. interface Commands<ReturnType> {
  11. lineHeight: {
  12. setLineHeight: (lineHeight: string) => ReturnType;
  13. unsetLineHeight: () => ReturnType;
  14. };
  15. }
  16. }
  17. export function useLineHeight() {
  18. return Extension.create<LineHeightOptions>({
  19. name: "lineHeight",
  20. addOptions() {
  21. return {
  22. ...this.parent?.(),
  23. types: ["paragraph", "heading", "list_item", "todo_item"],
  24. lineHeights: ["100%", "115%", "150%", "200%", "250%", "300%"],
  25. defaultHeight: "100%",
  26. };
  27. },
  28. addGlobalAttributes() {
  29. return [
  30. {
  31. types: this.options.types,
  32. attributes: {
  33. lineHeight: {
  34. default: null,
  35. parseHTML: (element) => {
  36. return element.style.lineHeight || this.options.defaultHeight;
  37. },
  38. renderHTML: (attributes) => {
  39. if (
  40. attributes.lineHeight === this.options.defaultHeight ||
  41. !attributes.lineHeight
  42. ) {
  43. return {};
  44. }
  45. return { style: `line-height: ${attributes.lineHeight}` };
  46. },
  47. },
  48. },
  49. },
  50. ];
  51. },
  52. addCommands() {
  53. return {
  54. setLineHeight: (lineHeight) => createLineHeightCommand(lineHeight),
  55. unsetLineHeight:
  56. () =>
  57. ({ commands }) => {
  58. return this.options.types.every((type) => commands.resetAttributes(type, "lineHeight"));
  59. },
  60. };
  61. },
  62. });
  63. }
  64. export { LineHeightExt };