tooltip.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Mark, mergeAttributes } from "@tiptap/core";
  2. export interface TooltipOptions {
  3. HTMLAttributes: Record<string, any>;
  4. }
  5. declare module "@tiptap/core" {
  6. interface Commands<ReturnType> {
  7. tooltip: {
  8. /**
  9. * Set a link mark
  10. */
  11. setTooltip: (attributes: { "data-title": string, "data-text": string }) => ReturnType;
  12. /**
  13. * Toggle a link mark
  14. */
  15. toggleTooltip: (attributes: { "data-title": string, "data-text": string }) => ReturnType;
  16. /**
  17. * Unset a link mark
  18. */
  19. unsetTooltip: () => ReturnType;
  20. };
  21. }
  22. }
  23. export default Mark.create<TooltipOptions>({
  24. name: "tooltip",
  25. priority: 1000,
  26. inclusive: false,
  27. defaultOptions: {
  28. HTMLAttributes: {
  29. class: "tooltip",
  30. target: "_blank"
  31. }
  32. },
  33. addAttributes() {
  34. return {
  35. "data-title": {
  36. default: ""
  37. },
  38. "data-text": {
  39. default: ""
  40. }
  41. };
  42. },
  43. parseHTML() {
  44. return [
  45. {
  46. tag: "span",
  47. class: "tooltip"
  48. }
  49. ];
  50. },
  51. renderHTML({ HTMLAttributes }) {
  52. return [
  53. "span",
  54. mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
  55. 0
  56. ];
  57. },
  58. addCommands() {
  59. return {
  60. setTooltip: attributes => ({ commands }) => {
  61. return commands.setMark("tooltip", attributes);
  62. },
  63. toggleTooltip: attributes => ({ commands }) => {
  64. return commands.toggleMark("tooltip", attributes, {
  65. extendEmptyMarkRange: true
  66. });
  67. },
  68. unsetTooltip: () => ({ commands }) => {
  69. return commands.unsetMark("tooltip", { extendEmptyMarkRange: true });
  70. }
  71. };
  72. }
  73. });