frappe-ui 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/experimental/Calendar/CalendarEventRow.vue +0 -4
  2. package/experimental/Calendar/calendarUtils.ts +33 -21
  3. package/experimental/CommandPalette/CommandPaletteItem.vue +9 -1
  4. package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
  5. package/experimental/MultiEmailInput/types.ts +12 -3
  6. package/experimental/SpriteIcons/IconPicker.vue +2 -1
  7. package/experimental/Stepper/StepIndicator.vue +129 -0
  8. package/experimental/Stepper/Stepper.api.md +117 -0
  9. package/experimental/Stepper/Stepper.md +95 -0
  10. package/experimental/Stepper/Stepper.vue +82 -0
  11. package/experimental/Stepper/StepperHorizontal.vue +93 -0
  12. package/experimental/Stepper/StepperVertical.vue +291 -0
  13. package/experimental/Stepper/index.ts +11 -0
  14. package/experimental/Stepper/resolve.ts +87 -0
  15. package/experimental/Stepper/types.ts +89 -0
  16. package/experimental.ts +2 -21
  17. package/package.json +1 -3
  18. package/src/charts/CalendarHeatmapChart.vue +353 -0
  19. package/src/charts/DonutChart.vue +34 -58
  20. package/src/charts/HeatmapChart.vue +26 -64
  21. package/src/charts/NumberCard.vue +62 -7
  22. package/src/charts/PercentageBarChart.vue +243 -0
  23. package/src/charts/SankeyChart.vue +14 -11
  24. package/src/charts/ScatterChart.vue +24 -20
  25. package/src/charts/axisChartCommon.ts +182 -18
  26. package/src/charts/axisFormat.ts +8 -3
  27. package/src/charts/calendarHeatmapOptions.ts +604 -0
  28. package/src/charts/colorMath.ts +26 -0
  29. package/src/charts/components/ChartContainer.vue +63 -53
  30. package/src/charts/components/ChartLegend.vue +216 -24
  31. package/src/charts/components/ChartScale.vue +85 -0
  32. package/src/charts/core/dataLabelFit.ts +45 -6
  33. package/src/charts/core/useAxisChart.ts +36 -19
  34. package/src/charts/core/useChart.ts +6 -1
  35. package/src/charts/core/usePlotKeyboard.ts +13 -2
  36. package/src/charts/docs/BarChart.md +11 -3
  37. package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
  38. package/src/charts/docs/CalendarHeatmapChart.md +33 -0
  39. package/src/charts/docs/ChartContainer.api.md +6 -0
  40. package/src/charts/docs/ChartLegend.api.md +1 -1
  41. package/src/charts/docs/ChartLegend.md +25 -3
  42. package/src/charts/docs/DonutChart.api.md +12 -0
  43. package/src/charts/docs/DonutChart.md +17 -0
  44. package/src/charts/docs/HeatmapChart.api.md +1 -1
  45. package/src/charts/docs/LineChart.md +5 -3
  46. package/src/charts/docs/NumberCard.api.md +6 -0
  47. package/src/charts/docs/NumberCard.md +6 -0
  48. package/src/charts/docs/Overview.md +13 -6
  49. package/src/charts/docs/PercentageBarChart.api.md +148 -0
  50. package/src/charts/docs/PercentageBarChart.md +42 -0
  51. package/src/charts/docs/ScatterChart.md +2 -3
  52. package/src/charts/donutChartOptions.ts +22 -130
  53. package/src/charts/format.ts +4 -1
  54. package/src/charts/heatmapOptions.ts +17 -12
  55. package/src/charts/hiddenSeries.ts +8 -3
  56. package/src/charts/index.ts +12 -0
  57. package/src/charts/legendOverflow.ts +40 -0
  58. package/src/charts/numberCardFormat.ts +19 -13
  59. package/src/charts/referenceLines.ts +2 -1
  60. package/src/charts/scatterOptions.ts +16 -3
  61. package/src/charts/seriesData.ts +8 -3
  62. package/src/charts/slices.ts +228 -0
  63. package/src/charts/tokens.ts +99 -0
  64. package/src/charts/types.ts +318 -9
  65. package/src/components/Avatar/Avatar.md +27 -2
  66. package/src/components/Avatar/Avatar.vue +29 -12
  67. package/src/components/Breadcrumbs/Breadcrumbs.vue +2 -1
  68. package/src/components/Combobox/Combobox.vue +9 -2
  69. package/src/components/Combobox/ComboboxResults.vue +6 -1
  70. package/src/components/Combobox/utils.ts +2 -1
  71. package/src/components/DatePicker/DatePicker.md +7 -0
  72. package/src/components/DatePicker/DatePicker.vue +6 -0
  73. package/src/components/DatePicker/DateTimePicker.vue +6 -0
  74. package/src/components/DatePicker/composables.ts +18 -1
  75. package/src/components/DatePicker/utils.ts +53 -0
  76. package/src/components/Dialog/Dialog.md +3 -1
  77. package/src/components/Duration/Duration.api.md +16 -3
  78. package/src/components/Duration/Duration.md +21 -0
  79. package/src/components/Duration/Duration.vue +29 -8
  80. package/src/components/Duration/duration.ts +105 -44
  81. package/src/components/Duration/index.ts +1 -0
  82. package/src/components/Duration/types.ts +30 -1
  83. package/src/components/HoverCard/HoverCard.vue +21 -5
  84. package/src/components/Menu/utils.ts +3 -2
  85. package/src/components/MobileNav/MobileNavItem.vue +3 -22
  86. package/src/components/MultiSelect/MultiSelect.api.md +5 -0
  87. package/src/components/MultiSelect/MultiSelect.vue +34 -0
  88. package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
  89. package/src/components/MultiSelect/types.ts +6 -0
  90. package/src/components/Select/Select.vue +4 -4
  91. package/src/components/Select/utils.ts +1 -0
  92. package/src/components/SettingsDialog/SettingsDialog.api.md +7 -2
  93. package/src/components/SettingsDialog/SettingsDialog.md +2 -1
  94. package/src/components/SettingsDialog/SettingsRow.vue +12 -2
  95. package/src/components/Sidebar/SidebarItem.vue +3 -33
  96. package/src/components/SidebarRail/SidebarRailItem.vue +4 -17
  97. package/src/components/Switch/Switch.vue +3 -3
  98. package/src/components/Tooltip/Tooltip.vue +8 -0
  99. package/src/components/shared/deferredTrigger.ts +174 -0
  100. package/src/components/shared/navLink.ts +45 -0
  101. package/src/components/shared/selection/utils.ts +8 -1
  102. package/src/data-fetching/useCall/useCall.ts +25 -26
  103. package/src/data-fetching/useFrappeFetch.ts +48 -2
  104. package/src/data-fetching/useList/useList.ts +11 -3
  105. package/src/data-fetching/utils.ts +15 -1
  106. package/src/molecules/editor/components/MediaNodeView.vue +10 -1
  107. package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
  108. package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
  109. package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
  110. package/src/molecules/editor/extensions/attachment/attachment-engine.ts +1 -1
  111. package/src/molecules/editor/extensions/content-paste/paste-html-utils.ts +16 -6
  112. package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +29 -10
  113. package/src/molecules/editor/extensions/iframe/iframe-allowlist.ts +35 -16
  114. package/src/molecules/editor/extensions/iframe/iframe-embed-utils.ts +1 -1
  115. package/src/molecules/editor/extensions/iframe/iframe-extension.ts +30 -30
  116. package/src/molecules/editor/extensions/link/LinkEditorPopup.vue +1 -1
  117. package/src/molecules/editor/extensions/link/link-paste-plugin.ts +1 -1
  118. package/src/molecules/editor/extensions/shared/media-upload-engine.ts +1 -1
  119. package/src/molecules/editor/useEditor.ts +12 -6
  120. package/src/molecules/list/ListRow.vue +8 -1
  121. package/src/utils/dialog.ts +57 -16
  122. package/src/{molecules/editor/extensions/shared → utils}/url-safety.ts +28 -16
  123. package/tailwind/content.js +1 -1
  124. package/tailwind/plugin.js +22 -0
  125. package/vite/README.md +5 -3
  126. package/vite/codeLanguages.js +41 -17
  127. package/vite/doctypeInterfaceGenerator.js +52 -39
  128. package/vitepress/index.node.ts +0 -2
  129. package/experimental/TextEditor/TextEditor.api.md +0 -161
  130. package/experimental/TextEditor/TextEditor.vue +0 -298
  131. package/experimental/TextEditor/classIcon.ts +0 -21
  132. package/experimental/TextEditor/commands.js +0 -344
  133. package/experimental/TextEditor/components/CodeBlockComponent.vue +0 -103
  134. package/experimental/TextEditor/components/FontColor.vue +0 -153
  135. package/experimental/TextEditor/components/ImageViewerModal.vue +0 -463
  136. package/experimental/TextEditor/components/InsertImage.vue +0 -54
  137. package/experimental/TextEditor/components/InsertLink.vue +0 -13
  138. package/experimental/TextEditor/components/InsertVideo.vue +0 -14
  139. package/experimental/TextEditor/components/LinkPopup.vue +0 -99
  140. package/experimental/TextEditor/components/MediaNodeView.vue +0 -398
  141. package/experimental/TextEditor/components/MentionList.vue +0 -95
  142. package/experimental/TextEditor/components/TextEditorBubbleMenu.vue +0 -78
  143. package/experimental/TextEditor/components/TextEditorFixedMenu.vue +0 -78
  144. package/experimental/TextEditor/components/TextEditorFloatingMenu.vue +0 -61
  145. package/experimental/TextEditor/components/TextEditorMenu.vue +0 -218
  146. package/experimental/TextEditor/components/index.js +0 -0
  147. package/experimental/TextEditor/extensions/code-block.ts +0 -119
  148. package/experimental/TextEditor/extensions/color/color-extension.ts +0 -140
  149. package/experimental/TextEditor/extensions/color/color-styles.css +0 -25
  150. package/experimental/TextEditor/extensions/color/index.ts +0 -2
  151. package/experimental/TextEditor/extensions/content-paste-extension.ts +0 -160
  152. package/experimental/TextEditor/extensions/copy-styles.ts +0 -160
  153. package/experimental/TextEditor/extensions/emoji/EmojiList.vue +0 -55
  154. package/experimental/TextEditor/extensions/emoji/emoji-extension.ts +0 -55
  155. package/experimental/TextEditor/extensions/emoji/emojis.json +0 -7422
  156. package/experimental/TextEditor/extensions/heading/heading.ts +0 -20
  157. package/experimental/TextEditor/extensions/highlight/highlight-extension.ts +0 -241
  158. package/experimental/TextEditor/extensions/highlight/highlight-styles.css +0 -31
  159. package/experimental/TextEditor/extensions/highlight/index.ts +0 -2
  160. package/experimental/TextEditor/extensions/iframe/IframeNodeView.vue +0 -308
  161. package/experimental/TextEditor/extensions/iframe/InsertIframe.vue +0 -209
  162. package/experimental/TextEditor/extensions/iframe/iframe-extension.ts +0 -404
  163. package/experimental/TextEditor/extensions/iframe/index.ts +0 -11
  164. package/experimental/TextEditor/extensions/iframe/utils.ts +0 -283
  165. package/experimental/TextEditor/extensions/image/image-extension.ts +0 -661
  166. package/experimental/TextEditor/extensions/image/index.ts +0 -5
  167. package/experimental/TextEditor/extensions/image-group/ImageGroupNodeView.vue +0 -182
  168. package/experimental/TextEditor/extensions/image-group/ImageGroupUploadDialog.vue +0 -740
  169. package/experimental/TextEditor/extensions/image-group/image-group-extension.ts +0 -187
  170. package/experimental/TextEditor/extensions/image-viewer-extension.ts +0 -162
  171. package/experimental/TextEditor/extensions/link/index.ts +0 -1
  172. package/experimental/TextEditor/extensions/link/link-extension.ts +0 -323
  173. package/experimental/TextEditor/extensions/link/linkPasteHandler.ts +0 -51
  174. package/experimental/TextEditor/extensions/mention/index.ts +0 -1
  175. package/experimental/TextEditor/extensions/mention/mention-extension.ts +0 -204
  176. package/experimental/TextEditor/extensions/mention/style.css +0 -11
  177. package/experimental/TextEditor/extensions/shared/color-utils.ts +0 -236
  178. package/experimental/TextEditor/extensions/slash-commands/SlashCommandsList.vue +0 -59
  179. package/experimental/TextEditor/extensions/slash-commands/slash-commands-extension.ts +0 -184
  180. package/experimental/TextEditor/extensions/suggestion/SuggestionList.vue +0 -130
  181. package/experimental/TextEditor/extensions/suggestion/createSuggestionExtension.ts +0 -151
  182. package/experimental/TextEditor/extensions/suggestion/index.ts +0 -5
  183. package/experimental/TextEditor/extensions/tag/index.ts +0 -1
  184. package/experimental/TextEditor/extensions/tag/tag-extension.ts +0 -170
  185. package/experimental/TextEditor/extensions/toc-node/TocNodeView.vue +0 -439
  186. package/experimental/TextEditor/extensions/toc-node/index.ts +0 -3
  187. package/experimental/TextEditor/extensions/toc-node/toc-node-extension.ts +0 -167
  188. package/experimental/TextEditor/extensions/video-extension.ts +0 -653
  189. package/experimental/TextEditor/hljs-github.css +0 -197
  190. package/experimental/TextEditor/index.ts +0 -21
  191. package/experimental/TextEditor/style.css +0 -190
  192. package/experimental/TextEditor/types.ts +0 -72
  193. package/experimental/TextEditor/utils.js +0 -11
  194. package/src/utils/url-validation.ts +0 -39
@@ -1,187 +0,0 @@
1
- // @ts-nocheck -- frozen v0 code, kept out of type-check (see tsconfig.app.json)
2
- import { Node, mergeAttributes } from '@tiptap/core'
3
- import { VueNodeViewRenderer } from '@tiptap/vue-3'
4
- import ImageGroupNodeView from './ImageGroupNodeView.vue'
5
- import { UploadedFile } from '#utils/useFileUpload'
6
-
7
- export interface ImageGroupOptions {
8
- /**
9
- * Function to handle image uploads
10
- * @default null
11
- */
12
- uploadFunction: ((file: File) => Promise<UploadedFile>) | null
13
- HTMLAttributes: Record<string, any>
14
- }
15
-
16
- export interface ImageGroupAttrs {
17
- columns?: number
18
- images: {
19
- src: string
20
- alt?: string
21
- }[]
22
- }
23
-
24
- declare module '@tiptap/core' {
25
- interface Commands<ReturnType> {
26
- imageGroup: {
27
- /**
28
- * Insert an image group/gallery node
29
- */
30
- setImageGroup: (attrs: {
31
- columns?: number
32
- images: { src: string; alt?: string }[]
33
- }) => ReturnType
34
- /**
35
- * Group selected images into an image group
36
- */
37
- groupSelectedImages: () => ReturnType
38
- }
39
- }
40
- }
41
-
42
- export const ImageGroup = Node.create<ImageGroupOptions>({
43
- name: 'imageGroup',
44
-
45
- group: 'block',
46
- content: 'image+', // one or more images
47
- selectable: true,
48
- draggable: true,
49
- isolating: true,
50
-
51
- addOptions() {
52
- return {
53
- uploadFunction: null,
54
- HTMLAttributes: {},
55
- }
56
- },
57
-
58
- addAttributes() {
59
- return {
60
- columns: {
61
- default: 4,
62
- },
63
- }
64
- },
65
-
66
- parseHTML() {
67
- return [
68
- {
69
- tag: 'div[data-type="image-group"]',
70
- getAttrs: (element) => {
71
- if (typeof element === 'string') return {}
72
- const el = element as HTMLElement
73
- return {
74
- columns: el.getAttribute('data-columns')
75
- ? Number(el.getAttribute('data-columns'))
76
- : 4,
77
- }
78
- },
79
- },
80
- ]
81
- },
82
-
83
- renderHTML({ HTMLAttributes, node }) {
84
- return [
85
- 'div',
86
- mergeAttributes(
87
- {
88
- 'data-type': 'image-group',
89
- 'data-columns': node.attrs.columns,
90
- },
91
- this.options.HTMLAttributes,
92
- HTMLAttributes,
93
- ),
94
- 0, // content placeholder for child images
95
- ]
96
- },
97
-
98
- addNodeView() {
99
- return VueNodeViewRenderer(ImageGroupNodeView)
100
- },
101
-
102
- addCommands() {
103
- return {
104
- setImageGroup:
105
- (attrs) =>
106
- ({ commands }) => {
107
- return commands.insertContent({
108
- type: this.name,
109
- attrs: {
110
- columns: attrs.columns || 4,
111
- },
112
- content: attrs.images.map((img) => ({
113
- type: 'image',
114
- attrs: img,
115
- })),
116
- })
117
- },
118
-
119
- groupSelectedImages:
120
- () =>
121
- ({ state, chain }) => {
122
- const { selection } = state
123
- const { from, to } = selection
124
-
125
- // Collect all image nodes within the selection
126
- const images: { src: string; alt?: string; pos: number }[] = []
127
-
128
- state.doc.nodesBetween(from, to, (node, pos) => {
129
- if (node.type.name === 'image') {
130
- images.push({
131
- src: node.attrs.src,
132
- alt: node.attrs.alt,
133
- pos,
134
- })
135
- }
136
- })
137
-
138
- // Need at least 2 images to create a group
139
- if (images.length < 2) {
140
- return false
141
- }
142
-
143
- // Sort by position (descending) for deletion
144
- const sortedImages = [...images].sort((a, b) => b.pos - a.pos)
145
-
146
- // Find the position to insert the group (start of selection)
147
- const insertPos = Math.min(...images.map((img) => img.pos))
148
-
149
- // Start a chain of commands
150
- let cmd = chain()
151
-
152
- // Delete all selected images (from end to start to maintain positions)
153
- sortedImages.forEach((img) => {
154
- const node = state.doc.nodeAt(img.pos)
155
- if (node) {
156
- cmd = cmd.deleteRange({
157
- from: img.pos,
158
- to: img.pos + node.nodeSize,
159
- })
160
- }
161
- })
162
-
163
- // Insert the image group at the start position
164
- cmd = cmd
165
- .insertContentAt(insertPos, {
166
- type: this.name,
167
- attrs: { columns: images.length },
168
- content: images.map((img) => ({
169
- type: 'image',
170
- attrs: { src: img.src, alt: img.alt },
171
- })),
172
- })
173
- .run()
174
-
175
- return true
176
- },
177
- }
178
- },
179
-
180
- addKeyboardShortcuts() {
181
- return {
182
- 'Mod-Shift-g': () => this.editor.commands.groupSelectedImages(),
183
- }
184
- },
185
- })
186
-
187
- export default ImageGroup
@@ -1,162 +0,0 @@
1
- // @ts-nocheck -- frozen v0 code, kept out of type-check (see tsconfig.app.json)
2
- import { Extension } from '@tiptap/core'
3
- import { Plugin, PluginKey } from '@tiptap/pm/state'
4
- import { createApp, h } from 'vue'
5
- import ImageViewerModal from '../components/ImageViewerModal.vue'
6
-
7
- interface ImageInfo {
8
- src: string
9
- alt: string | null
10
- }
11
-
12
- declare module '@tiptap/core' {
13
- interface Commands<ReturnType> {
14
- imageViewer: {
15
- /**
16
- * Open the image viewer
17
- */
18
- openImageViewer: (src: string) => ReturnType
19
- }
20
- }
21
- }
22
-
23
- export default Extension.create({
24
- name: 'imageViewer',
25
-
26
- onBeforeCreate() {
27
- // Only add the style if it doesn't already exist
28
- if (!document.querySelector('style[data-image-viewer-style]')) {
29
- // Add a CSS rule to make images clickable when the editor is not editable
30
- const style = document.createElement('style')
31
- style.textContent = `
32
- .ProseMirror:not(.ProseMirror-focused) img {
33
- cursor: pointer;
34
- }
35
- `
36
- style.setAttribute('data-image-viewer-style', 'true')
37
- document.head.appendChild(style)
38
- }
39
- },
40
-
41
- onDestroy() {
42
- // Clean up the style element when the editor instance is destroyed
43
- const style = document.querySelector('style[data-image-viewer-style]')
44
- if (style) {
45
- document.head.removeChild(style)
46
- }
47
- },
48
-
49
- addCommands() {
50
- return {
51
- openImageViewer:
52
- (src: string) =>
53
- ({ editor }) => {
54
- // Find all images in the document to populate the viewer
55
- const images: ImageInfo[] = []
56
- editor.state.doc.descendants((node) => {
57
- if (node.type.name === 'image') {
58
- images.push({
59
- src: node.attrs.src,
60
- alt: node.attrs.alt || null,
61
- })
62
- }
63
- return true
64
- })
65
-
66
- // Find the index of the clicked image to start the viewer at the correct position
67
- const currentIndex = images.findIndex((image) => image.src === src)
68
-
69
- // Create and open the image viewer modal
70
- openImageViewerModal(images, currentIndex)
71
-
72
- return true
73
- },
74
- }
75
- },
76
-
77
- addProseMirrorPlugins() {
78
- const extension = this
79
-
80
- return [
81
- new Plugin({
82
- key: new PluginKey('imageViewer'),
83
- props: {
84
- handleClick(view, pos, event) {
85
- // Only allow opening the viewer when the editor is not editable
86
- if (extension.editor.isEditable) {
87
- return false
88
- }
89
-
90
- const { state } = view
91
- const clickedNode = state.doc.nodeAt(pos)
92
-
93
- // Check if the click was directly on an image node
94
- if (clickedNode?.type.name === 'image') {
95
- event.preventDefault()
96
- const src = clickedNode.attrs.src
97
- extension.editor.commands.openImageViewer(src)
98
- return true // Indicate that the event was handled
99
- }
100
-
101
- // If the click target was an <img> element, but not directly the node at pos
102
- // (e.g., clicking near the edge, handled by browser event bubbling)
103
- if (event.target instanceof HTMLImageElement) {
104
- let foundImageNode = false
105
- // Traverse the document to find the ProseMirror node corresponding to the clicked <img>
106
- state.doc.descendants((node, nodePos) => {
107
- if (node.type.name === 'image' && !foundImageNode) {
108
- const domNode = view.nodeDOM(nodePos)
109
- // Check if the event target is the DOM representation of this node or inside it
110
- if (
111
- domNode &&
112
- (domNode === event.target || domNode.contains(event.target))
113
- ) {
114
- event.preventDefault()
115
- extension.editor.commands.openImageViewer(node.attrs.src)
116
- foundImageNode = true
117
- return false // Stop traversal once found
118
- }
119
- }
120
- return true // Continue traversal
121
- })
122
-
123
- if (foundImageNode) return true // Indicate that the event was handled
124
- }
125
-
126
- return false // Event not handled by this plugin
127
- },
128
- },
129
- }),
130
- ]
131
- },
132
- })
133
-
134
- function openImageViewerModal(images: ImageInfo[], initialIndex: number) {
135
- // Create a temporary container div for the Vue modal instance
136
- const container = document.createElement('div')
137
- document.body.appendChild(container)
138
-
139
- // Create the Vue app instance for the modal
140
- const app = createApp({
141
- render() {
142
- return h(ImageViewerModal, {
143
- show: true,
144
- images, // Pass the collected image data
145
- initialIndex, // Pass the starting index
146
- 'onUpdate:show': (value: boolean) => {
147
- // Handle the modal closing
148
- if (!value) {
149
- // Delay unmounting and removal to allow for closing animations
150
- setTimeout(() => {
151
- app.unmount()
152
- container.remove()
153
- }, 0) // Timeout 0 ensures it runs after the current execution context
154
- }
155
- },
156
- })
157
- },
158
- })
159
-
160
- // Mount the Vue app to the container div
161
- app.mount(container)
162
- }
@@ -1 +0,0 @@
1
- export { LinkExtension } from './link-extension'
@@ -1,323 +0,0 @@
1
- // @ts-nocheck -- frozen v0 code, kept out of type-check (see tsconfig.app.json)
2
- import { createApp, h } from 'vue'
3
- import Link from '@tiptap/extension-link'
4
- import tippy, { type Instance as TippyInstance } from 'tippy.js'
5
- import { getMarkRange, Range, Editor } from '@tiptap/core'
6
- import { MarkType } from '@tiptap/pm/model'
7
- import { Plugin, PluginKey } from '@tiptap/pm/state'
8
- import LinkPopup from '../../components/LinkPopup.vue'
9
- import { linkPasteHandler } from './linkPasteHandler'
10
-
11
- declare module '@tiptap/core' {
12
- interface Commands<ReturnType> {
13
- link: {
14
- /**
15
- * Opens the link editor bubble menu.
16
- */
17
- openLinkEditor: () => ReturnType
18
- }
19
- }
20
- }
21
-
22
- export const LinkExtension = Link.extend({
23
- addOptions() {
24
- return {
25
- ...this.parent?.(),
26
- openOnClick: false,
27
- autolink: true,
28
- defaultProtocol: 'https',
29
- linkOnPaste: false,
30
- }
31
- },
32
-
33
- addCommands() {
34
- return {
35
- ...this.parent?.(),
36
- openLinkEditor:
37
- () =>
38
- ({ editor }: { editor: Editor }): boolean => {
39
- const { state } = editor
40
- const { from, to } = state.selection
41
- const { doc } = state
42
-
43
- let range: Range | undefined = undefined
44
- let shouldDelayPopover = false
45
-
46
- // Check if cursor is within a link or if there's a selection
47
- if (from === to) {
48
- // Cursor is within a link
49
- const $pos = state.selection.$from
50
- const markRange = getMarkRange($pos, this.type)
51
- if (markRange) {
52
- range = markRange
53
- // Select the link text
54
- editor
55
- .chain()
56
- .setTextSelection({ from: markRange.from, to: markRange.to })
57
- .run()
58
- shouldDelayPopover = true
59
- } else {
60
- // No selection and not within a link, and cursor not in link
61
- return false
62
- }
63
- } else {
64
- // There is a selection
65
- range = { from, to }
66
- }
67
-
68
- const existingHref = this.editor.getAttributes('link').href || null
69
- const selectionFrom = range.from
70
- const selectionTo = range.to
71
-
72
- const showPopover = () => {
73
- openLinkEditor(existingHref, editor.view.dom)
74
- .then((href) => {
75
- if (href === null) {
76
- return
77
- }
78
-
79
- let chain = editor
80
- .chain()
81
- .focus(null, { scrollIntoView: false })
82
-
83
- if (href === '') {
84
- chain
85
- .setTextSelection({ from: selectionFrom, to: selectionTo })
86
- .unsetLink()
87
- .command(({ tr }) => {
88
- tr.setStoredMarks([])
89
- return true
90
- })
91
- .run()
92
- return
93
- }
94
-
95
- chain = chain
96
- .setTextSelection({ from: selectionFrom, to: selectionTo })
97
- .setLink({ href })
98
- .setTextSelection(selectionTo)
99
- .command(({ tr }) => {
100
- tr.setStoredMarks([])
101
- return true
102
- })
103
-
104
- chain.run()
105
- })
106
- .catch(() => {})
107
- }
108
-
109
- if (shouldDelayPopover) {
110
- requestAnimationFrame(showPopover)
111
- } else {
112
- showPopover()
113
- }
114
-
115
- return true
116
- },
117
- }
118
- },
119
-
120
- addKeyboardShortcuts() {
121
- return {
122
- 'Mod-k': () => this.editor.commands.openLinkEditor(),
123
- }
124
- },
125
-
126
- addProseMirrorPlugins() {
127
- let plugins = this.parent?.() || []
128
- plugins.push(
129
- linkPasteHandler({
130
- editor: this.editor,
131
- defaultProtocol: this.options.defaultProtocol,
132
- type: this.type,
133
- }),
134
- )
135
-
136
- plugins.push(
137
- clearLinkOnBoundaryPlugin({
138
- editor: this.editor,
139
- type: this.type,
140
- }),
141
- )
142
- plugins.push(
143
- new Plugin({
144
- props: {
145
- handleClick: (view, pos, event) => {
146
- if (!this.editor.isEditable) return
147
- if (!this.editor.isActive('link')) return false
148
- event.preventDefault()
149
- if (event.metaKey) {
150
- const url = this.editor.getAttributes('link').href
151
- if (url) window.open(url, '_blank')
152
- this.editor.commands.focus()
153
- } else {
154
- this.editor.commands.openLinkEditor()
155
- }
156
- },
157
- },
158
- }),
159
- )
160
- return plugins
161
- },
162
- })
163
-
164
- function openLinkEditor(href: string, anchor: HTMLElement): Promise<string> {
165
- return new Promise((resolve, reject) => {
166
- const container = document.createElement('div')
167
- document.body.appendChild(container)
168
-
169
- let virtualReference: {
170
- getBoundingClientRect: () => DOMRect | { [key: string]: any }
171
- }
172
-
173
- const selection = window.getSelection()
174
- if (selection && selection.rangeCount > 0) {
175
- const range = selection.getRangeAt(0)
176
- const rect = range.getBoundingClientRect()
177
- const isCollapsed = range.collapsed
178
-
179
- virtualReference = {
180
- getBoundingClientRect: () => ({
181
- width: 0,
182
- height: rect.height,
183
- top: rect.top,
184
- right: isCollapsed ? rect.left : rect.right,
185
- bottom: rect.bottom,
186
- left: rect.left,
187
- x: rect.left,
188
- y: rect.top,
189
- toJSON: () => {},
190
- }),
191
- }
192
- } else {
193
- virtualReference = {
194
- getBoundingClientRect: () => anchor.getBoundingClientRect(),
195
- }
196
- }
197
-
198
- let app: ReturnType<typeof createApp> | null = null
199
- let tippyInstance: TippyInstance | null = null
200
- let isDestroyed = false
201
- let promiseSettled = false
202
-
203
- const settlePromise = (action: 'resolve' | 'reject', value?: any) => {
204
- if (promiseSettled) return
205
- promiseSettled = true
206
- if (action === 'resolve') {
207
- resolve(value)
208
- } else {
209
- reject(value)
210
- }
211
- }
212
-
213
- const destroy = () => {
214
- if (isDestroyed) return
215
- isDestroyed = true
216
-
217
- settlePromise('reject', 'Link editing cancelled or destroyed')
218
-
219
- requestAnimationFrame(() => {
220
- tippyInstance?.destroy()
221
- app?.unmount()
222
- container?.remove()
223
- app = null
224
- tippyInstance = null
225
- })
226
- }
227
-
228
- app = createApp({
229
- render() {
230
- return h(LinkPopup, {
231
- href,
232
- onClose: () => {
233
- settlePromise('reject', 'Link editing cancelled')
234
- destroy()
235
- },
236
- onUpdateHref: (newHref: string) => {
237
- settlePromise('resolve', newHref)
238
- destroy()
239
- },
240
- })
241
- },
242
- })
243
-
244
- app.mount(container)
245
-
246
- tippyInstance = tippy(anchor, {
247
- getReferenceClientRect: () => virtualReference.getBoundingClientRect(),
248
- content: container,
249
- trigger: 'manual',
250
- interactive: true,
251
- appendTo: () => anchor.closest('[role="dialog"]') || document.body,
252
- placement: 'top',
253
- arrow: false,
254
- theme: 'link-editor',
255
- maxWidth: 'none',
256
- onHidden() {
257
- destroy()
258
- },
259
- hideOnClick: true,
260
- interactiveDebounce: 75,
261
- })
262
-
263
- if (!tippyInstance) {
264
- container.remove()
265
- settlePromise('reject', 'Failed to initialize link editor tooltip')
266
- return
267
- }
268
-
269
- tippyInstance.show()
270
- })
271
- }
272
-
273
- function clearLinkOnBoundaryPlugin(options: {
274
- editor: Editor
275
- type: MarkType
276
- }) {
277
- return new Plugin({
278
- key: new PluginKey('clearLinkMarkOnBoundary'),
279
- appendTransaction: (transactions, oldState, newState) => {
280
- if (!options.editor.isEditable) {
281
- return null
282
- }
283
-
284
- const { tr, doc, selection, storedMarks } = newState
285
- const { $from, empty } = selection
286
-
287
- if (!empty || !storedMarks || storedMarks.length === 0) {
288
- // Only apply for cursor selections and if there are stored marks
289
- return null
290
- }
291
-
292
- const linkMarkType = options.type
293
- const hasStoredLinkMark = storedMarks.some(
294
- (mark) => mark.type === linkMarkType,
295
- )
296
-
297
- if (!hasStoredLinkMark) {
298
- return null
299
- }
300
-
301
- // Check if the cursor position itself has an active link mark in the document
302
- const marksAtCursor = $from.marks()
303
- const activeLinkAtCursor = marksAtCursor.some(
304
- (mark) => mark.type === linkMarkType,
305
- )
306
-
307
- if (activeLinkAtCursor) {
308
- // If there's an actual link mark active in the document at the cursor,
309
- // then it's correct for the stored mark to be there.
310
- return null
311
- }
312
-
313
- // If we are here, it means:
314
- // 1. Selection is a cursor (empty).
315
- // 2. There's a stored link mark.
316
- // 3. There's no active link mark in the document at the cursor position.
317
- // This indicates the stored link mark should be cleared.
318
- return tr.setStoredMarks([])
319
- },
320
- })
321
- }
322
-
323
- export default LinkExtension