@jvs-milkdown/components 1.0.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 (192) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/lib/__internal__/components/icon.d.ts +24 -0
  4. package/lib/__internal__/components/icon.d.ts.map +1 -0
  5. package/lib/__internal__/components/image-input.d.ts +17 -0
  6. package/lib/__internal__/components/image-input.d.ts.map +1 -0
  7. package/lib/__internal__/keep-alive.d.ts +2 -0
  8. package/lib/__internal__/keep-alive.d.ts.map +1 -0
  9. package/lib/__internal__/meta.d.ts +3 -0
  10. package/lib/__internal__/meta.d.ts.map +1 -0
  11. package/lib/__tests__/setup.d.ts +2 -0
  12. package/lib/__tests__/setup.d.ts.map +1 -0
  13. package/lib/code-block/config.d.ts +23 -0
  14. package/lib/code-block/config.d.ts.map +1 -0
  15. package/lib/code-block/index.d.ts +5 -0
  16. package/lib/code-block/index.d.ts.map +1 -0
  17. package/lib/code-block/index.js +4160 -0
  18. package/lib/code-block/index.js.map +1 -0
  19. package/lib/code-block/view/components/code-block.d.ts +16 -0
  20. package/lib/code-block/view/components/code-block.d.ts.map +1 -0
  21. package/lib/code-block/view/components/copy-button.d.ts +9 -0
  22. package/lib/code-block/view/components/copy-button.d.ts.map +1 -0
  23. package/lib/code-block/view/components/language-picker.d.ts +5 -0
  24. package/lib/code-block/view/components/language-picker.d.ts.map +1 -0
  25. package/lib/code-block/view/components/preview-panel.d.ts +9 -0
  26. package/lib/code-block/view/components/preview-panel.d.ts.map +1 -0
  27. package/lib/code-block/view/index.d.ts +3 -0
  28. package/lib/code-block/view/index.d.ts.map +1 -0
  29. package/lib/code-block/view/loader.d.ts +13 -0
  30. package/lib/code-block/view/loader.d.ts.map +1 -0
  31. package/lib/code-block/view/node-view.d.ts +40 -0
  32. package/lib/code-block/view/node-view.d.ts.map +1 -0
  33. package/lib/image-block/config.d.ts +16 -0
  34. package/lib/image-block/config.d.ts.map +1 -0
  35. package/lib/image-block/index.d.ts +7 -0
  36. package/lib/image-block/index.d.ts.map +1 -0
  37. package/lib/image-block/index.js +660 -0
  38. package/lib/image-block/index.js.map +1 -0
  39. package/lib/image-block/remark-plugin.d.ts +2 -0
  40. package/lib/image-block/remark-plugin.d.ts.map +1 -0
  41. package/lib/image-block/schema.d.ts +3 -0
  42. package/lib/image-block/schema.d.ts.map +1 -0
  43. package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts +2 -0
  44. package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts.map +1 -0
  45. package/lib/image-block/view/components/image-block.d.ts +18 -0
  46. package/lib/image-block/view/components/image-block.d.ts.map +1 -0
  47. package/lib/image-block/view/components/image-viewer.d.ts +3 -0
  48. package/lib/image-block/view/components/image-viewer.d.ts.map +1 -0
  49. package/lib/image-block/view/index.d.ts +3 -0
  50. package/lib/image-block/view/index.d.ts.map +1 -0
  51. package/lib/image-inline/components/image-inline.d.ts +18 -0
  52. package/lib/image-inline/components/image-inline.d.ts.map +1 -0
  53. package/lib/image-inline/config.d.ts +11 -0
  54. package/lib/image-inline/config.d.ts.map +1 -0
  55. package/lib/image-inline/index.d.ts +5 -0
  56. package/lib/image-inline/index.d.ts.map +1 -0
  57. package/lib/image-inline/index.js +377 -0
  58. package/lib/image-inline/index.js.map +1 -0
  59. package/lib/image-inline/view.d.ts +3 -0
  60. package/lib/image-inline/view.d.ts.map +1 -0
  61. package/lib/index.d.ts +2 -0
  62. package/lib/index.d.ts.map +1 -0
  63. package/lib/index.js +35 -0
  64. package/lib/index.js.map +1 -0
  65. package/lib/link-tooltip/command.d.ts +2 -0
  66. package/lib/link-tooltip/command.d.ts.map +1 -0
  67. package/lib/link-tooltip/configure.d.ts +3 -0
  68. package/lib/link-tooltip/configure.d.ts.map +1 -0
  69. package/lib/link-tooltip/edit/component.d.ts +11 -0
  70. package/lib/link-tooltip/edit/component.d.ts.map +1 -0
  71. package/lib/link-tooltip/edit/edit-configure.d.ts +3 -0
  72. package/lib/link-tooltip/edit/edit-configure.d.ts.map +1 -0
  73. package/lib/link-tooltip/edit/edit-view.d.ts +15 -0
  74. package/lib/link-tooltip/edit/edit-view.d.ts.map +1 -0
  75. package/lib/link-tooltip/index.d.ts +7 -0
  76. package/lib/link-tooltip/index.d.ts.map +1 -0
  77. package/lib/link-tooltip/index.js +2526 -0
  78. package/lib/link-tooltip/index.js.map +1 -0
  79. package/lib/link-tooltip/preview/component.d.ts +11 -0
  80. package/lib/link-tooltip/preview/component.d.ts.map +1 -0
  81. package/lib/link-tooltip/preview/preview-configure.d.ts +3 -0
  82. package/lib/link-tooltip/preview/preview-configure.d.ts.map +1 -0
  83. package/lib/link-tooltip/preview/preview-view.d.ts +14 -0
  84. package/lib/link-tooltip/preview/preview-view.d.ts.map +1 -0
  85. package/lib/link-tooltip/slices.d.ts +34 -0
  86. package/lib/link-tooltip/slices.d.ts.map +1 -0
  87. package/lib/link-tooltip/tooltips.d.ts +3 -0
  88. package/lib/link-tooltip/tooltips.d.ts.map +1 -0
  89. package/lib/link-tooltip/utils.d.ts +14 -0
  90. package/lib/link-tooltip/utils.d.ts.map +1 -0
  91. package/lib/list-item-block/component.d.ts +19 -0
  92. package/lib/list-item-block/component.d.ts.map +1 -0
  93. package/lib/list-item-block/config.d.ts +13 -0
  94. package/lib/list-item-block/config.d.ts.map +1 -0
  95. package/lib/list-item-block/index.d.ts +6 -0
  96. package/lib/list-item-block/index.d.ts.map +1 -0
  97. package/lib/list-item-block/index.js +2149 -0
  98. package/lib/list-item-block/index.js.map +1 -0
  99. package/lib/list-item-block/view.d.ts +3 -0
  100. package/lib/list-item-block/view.d.ts.map +1 -0
  101. package/lib/table-block/config.d.ts +8 -0
  102. package/lib/table-block/config.d.ts.map +1 -0
  103. package/lib/table-block/dnd/calc-drag-over.d.ts +3 -0
  104. package/lib/table-block/dnd/calc-drag-over.d.ts.map +1 -0
  105. package/lib/table-block/dnd/create-drag-handler.d.ts +5 -0
  106. package/lib/table-block/dnd/create-drag-handler.d.ts.map +1 -0
  107. package/lib/table-block/dnd/drag-over-handler.d.ts +3 -0
  108. package/lib/table-block/dnd/drag-over-handler.d.ts.map +1 -0
  109. package/lib/table-block/dnd/prepare-dnd-context.d.ts +3 -0
  110. package/lib/table-block/dnd/prepare-dnd-context.d.ts.map +1 -0
  111. package/lib/table-block/dnd/preview.d.ts +3 -0
  112. package/lib/table-block/dnd/preview.d.ts.map +1 -0
  113. package/lib/table-block/index.d.ts +5 -0
  114. package/lib/table-block/index.d.ts.map +1 -0
  115. package/lib/table-block/index.js +3961 -0
  116. package/lib/table-block/index.js.map +1 -0
  117. package/lib/table-block/view/component.d.ts +16 -0
  118. package/lib/table-block/view/component.d.ts.map +1 -0
  119. package/lib/table-block/view/drag.d.ts +7 -0
  120. package/lib/table-block/view/drag.d.ts.map +1 -0
  121. package/lib/table-block/view/index.d.ts +2 -0
  122. package/lib/table-block/view/index.d.ts.map +1 -0
  123. package/lib/table-block/view/operation.d.ts +11 -0
  124. package/lib/table-block/view/operation.d.ts.map +1 -0
  125. package/lib/table-block/view/pointer.d.ts +7 -0
  126. package/lib/table-block/view/pointer.d.ts.map +1 -0
  127. package/lib/table-block/view/types.d.ts +32 -0
  128. package/lib/table-block/view/types.d.ts.map +1 -0
  129. package/lib/table-block/view/utils.d.ts +21 -0
  130. package/lib/table-block/view/utils.d.ts.map +1 -0
  131. package/lib/table-block/view/view.d.ts +22 -0
  132. package/lib/table-block/view/view.d.ts.map +1 -0
  133. package/lib/tsconfig.tsbuildinfo +1 -0
  134. package/package.json +110 -0
  135. package/src/__internal__/components/icon.tsx +38 -0
  136. package/src/__internal__/components/image-input.tsx +182 -0
  137. package/src/__internal__/keep-alive.ts +3 -0
  138. package/src/__internal__/meta.ts +15 -0
  139. package/src/__tests__/setup.ts +6 -0
  140. package/src/code-block/config.ts +54 -0
  141. package/src/code-block/index.ts +12 -0
  142. package/src/code-block/view/components/code-block.tsx +170 -0
  143. package/src/code-block/view/components/copy-button.tsx +96 -0
  144. package/src/code-block/view/components/language-picker.tsx +239 -0
  145. package/src/code-block/view/components/preview-panel.tsx +79 -0
  146. package/src/code-block/view/index.ts +24 -0
  147. package/src/code-block/view/loader.ts +40 -0
  148. package/src/code-block/view/node-view.ts +310 -0
  149. package/src/image-block/config.ts +37 -0
  150. package/src/image-block/index.ts +18 -0
  151. package/src/image-block/remark-plugin.ts +51 -0
  152. package/src/image-block/schema.ts +71 -0
  153. package/src/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.tsx +42 -0
  154. package/src/image-block/view/components/image-block.tsx +80 -0
  155. package/src/image-block/view/components/image-viewer.tsx +186 -0
  156. package/src/image-block/view/index.ts +111 -0
  157. package/src/image-inline/components/image-inline.tsx +85 -0
  158. package/src/image-inline/config.ts +30 -0
  159. package/src/image-inline/index.ts +12 -0
  160. package/src/image-inline/view.ts +109 -0
  161. package/src/index.ts +1 -0
  162. package/src/link-tooltip/command.ts +19 -0
  163. package/src/link-tooltip/configure.ts +9 -0
  164. package/src/link-tooltip/edit/component.tsx +82 -0
  165. package/src/link-tooltip/edit/edit-configure.ts +29 -0
  166. package/src/link-tooltip/edit/edit-view.ts +165 -0
  167. package/src/link-tooltip/index.ts +19 -0
  168. package/src/link-tooltip/preview/component.tsx +87 -0
  169. package/src/link-tooltip/preview/preview-configure.ts +65 -0
  170. package/src/link-tooltip/preview/preview-view.ts +101 -0
  171. package/src/link-tooltip/slices.ts +69 -0
  172. package/src/link-tooltip/tooltips.ts +22 -0
  173. package/src/link-tooltip/utils.ts +56 -0
  174. package/src/list-item-block/component.tsx +133 -0
  175. package/src/list-item-block/config.ts +39 -0
  176. package/src/list-item-block/index.ts +13 -0
  177. package/src/list-item-block/view.ts +130 -0
  178. package/src/table-block/config.ts +53 -0
  179. package/src/table-block/dnd/calc-drag-over.ts +46 -0
  180. package/src/table-block/dnd/create-drag-handler.ts +99 -0
  181. package/src/table-block/dnd/drag-over-handler.ts +113 -0
  182. package/src/table-block/dnd/prepare-dnd-context.ts +46 -0
  183. package/src/table-block/dnd/preview.ts +58 -0
  184. package/src/table-block/index.ts +9 -0
  185. package/src/table-block/view/component.tsx +219 -0
  186. package/src/table-block/view/drag.ts +121 -0
  187. package/src/table-block/view/index.ts +1 -0
  188. package/src/table-block/view/operation.ts +148 -0
  189. package/src/table-block/view/pointer.ts +165 -0
  190. package/src/table-block/view/types.ts +35 -0
  191. package/src/table-block/view/utils.ts +192 -0
  192. package/src/table-block/view/view.ts +165 -0
@@ -0,0 +1,30 @@
1
+ import { $ctx } from '@jvs-milkdown/utils'
2
+
3
+ import { withMeta } from '../__internal__/meta'
4
+
5
+ export interface InlineImageConfig {
6
+ imageIcon: string | undefined
7
+ uploadButton: string | undefined
8
+ confirmButton: string | undefined
9
+ uploadPlaceholderText: string
10
+ onUpload: (file: File) => Promise<string>
11
+ proxyDomURL?: (url: string) => Promise<string> | string
12
+ }
13
+
14
+ export const defaultInlineImageConfig: InlineImageConfig = {
15
+ imageIcon: '🌌',
16
+ uploadButton: 'Upload',
17
+ confirmButton: '⏎',
18
+ uploadPlaceholderText: '/Paste',
19
+ onUpload: (file) => Promise.resolve(URL.createObjectURL(file)),
20
+ }
21
+
22
+ export const inlineImageConfig = $ctx(
23
+ defaultInlineImageConfig,
24
+ 'inlineImageConfigCtx'
25
+ )
26
+
27
+ withMeta(inlineImageConfig, {
28
+ displayName: 'Config<image-inline>',
29
+ group: 'ImageInline',
30
+ })
@@ -0,0 +1,12 @@
1
+ import type { MilkdownPlugin } from '@jvs-milkdown/ctx'
2
+
3
+ import { inlineImageConfig } from './config'
4
+ import { inlineImageView } from './view'
5
+
6
+ export * from './config'
7
+ export * from './view'
8
+
9
+ export const imageInlineComponent: MilkdownPlugin[] = [
10
+ inlineImageConfig,
11
+ inlineImageView,
12
+ ]
@@ -0,0 +1,109 @@
1
+ import type { Node } from '@jvs-milkdown/prose/model'
2
+ import type { NodeViewConstructor } from '@jvs-milkdown/prose/view'
3
+
4
+ import { imageSchema } from '@jvs-milkdown/preset-commonmark'
5
+ import { $view } from '@jvs-milkdown/utils'
6
+ import DOMPurify from 'dompurify'
7
+ import { createApp, ref, watchEffect } from 'vue'
8
+
9
+ import { withMeta } from '../__internal__/meta'
10
+ import { MilkdownImageInline } from './components/image-inline'
11
+ import { inlineImageConfig } from './config'
12
+
13
+ export const inlineImageView = $view(
14
+ imageSchema.node,
15
+ (ctx): NodeViewConstructor => {
16
+ return (initialNode, view, getPos) => {
17
+ const src = ref(initialNode.attrs.src)
18
+ const alt = ref(initialNode.attrs.alt)
19
+ const title = ref(initialNode.attrs.title)
20
+ const selected = ref(false)
21
+ const readonly = ref(!view.editable)
22
+ const setAttr = (attr: string, value: unknown) => {
23
+ if (!view.editable) return
24
+ const pos = getPos()
25
+ if (pos == null) return
26
+ view.dispatch(
27
+ view.state.tr.setNodeAttribute(
28
+ pos,
29
+ attr,
30
+ attr === 'src' ? DOMPurify.sanitize(value as string) : value
31
+ )
32
+ )
33
+ }
34
+
35
+ const config = ctx.get(inlineImageConfig.key)
36
+ const app = createApp(MilkdownImageInline, {
37
+ src,
38
+ alt,
39
+ title,
40
+ selected,
41
+ readonly,
42
+ setAttr,
43
+ config,
44
+ })
45
+ const dom = document.createElement('span')
46
+ dom.className = 'milkdown-image-inline'
47
+ const disposeSelectedWatcher = watchEffect(() => {
48
+ const isSelected = selected.value
49
+ if (isSelected) {
50
+ dom.classList.add('selected')
51
+ } else {
52
+ dom.classList.remove('selected')
53
+ }
54
+ })
55
+ const proxyDomURL = config.proxyDomURL
56
+ const bindAttrs = (node: Node) => {
57
+ if (!proxyDomURL) {
58
+ src.value = node.attrs.src
59
+ } else {
60
+ const proxiedURL = proxyDomURL(node.attrs.src)
61
+ if (typeof proxiedURL === 'string') {
62
+ src.value = proxiedURL
63
+ } else {
64
+ proxiedURL
65
+ .then((url) => {
66
+ src.value = url
67
+ })
68
+ .catch(console.error)
69
+ }
70
+ }
71
+ alt.value = node.attrs.alt
72
+ title.value = node.attrs.title
73
+ }
74
+ bindAttrs(initialNode)
75
+ app.mount(dom)
76
+
77
+ return {
78
+ dom,
79
+ update: (updatedNode) => {
80
+ if (updatedNode.type !== initialNode.type) return false
81
+
82
+ bindAttrs(updatedNode)
83
+ return true
84
+ },
85
+ stopEvent: (e) => {
86
+ if (e.target instanceof HTMLInputElement) return true
87
+
88
+ return false
89
+ },
90
+ selectNode: () => {
91
+ selected.value = true
92
+ },
93
+ deselectNode: () => {
94
+ selected.value = false
95
+ },
96
+ destroy: () => {
97
+ disposeSelectedWatcher()
98
+ app.unmount()
99
+ dom.remove()
100
+ },
101
+ }
102
+ }
103
+ }
104
+ )
105
+
106
+ withMeta(inlineImageView, {
107
+ displayName: 'NodeView<image-inline>',
108
+ group: 'ImageInline',
109
+ })
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Icon } from './__internal__/components/icon'
@@ -0,0 +1,19 @@
1
+ import { linkSchema } from '@jvs-milkdown/preset-commonmark'
2
+ import { $command } from '@jvs-milkdown/utils'
3
+
4
+ import { linkTooltipAPI } from './slices'
5
+
6
+ export const toggleLinkCommand = $command('ToggleLink', (ctx) => {
7
+ return () => (state) => {
8
+ const { doc, selection } = state
9
+ const mark = linkSchema.type(ctx)
10
+ const hasLink = doc.rangeHasMark(selection.from, selection.to, mark)
11
+ if (hasLink) {
12
+ ctx.get(linkTooltipAPI.key).removeLink(selection.from, selection.to)
13
+ return true
14
+ }
15
+
16
+ ctx.get(linkTooltipAPI.key).addLink(selection.from, selection.to)
17
+ return true
18
+ }
19
+ })
@@ -0,0 +1,9 @@
1
+ import type { Ctx } from '@jvs-milkdown/ctx'
2
+
3
+ import { configureLinkEditTooltip } from './edit/edit-configure'
4
+ import { configureLinkPreviewTooltip } from './preview/preview-configure'
5
+
6
+ export function configureLinkTooltip(ctx: Ctx) {
7
+ configureLinkPreviewTooltip(ctx)
8
+ configureLinkEditTooltip(ctx)
9
+ }
@@ -0,0 +1,82 @@
1
+ import { defineComponent, ref, watch, type Ref, h } from 'vue'
2
+
3
+ import type { LinkTooltipConfig } from '../slices'
4
+
5
+ import { Icon } from '../../__internal__/components/icon'
6
+ import { keepAlive } from '../../__internal__/keep-alive'
7
+
8
+ keepAlive(h)
9
+
10
+ type EditLinkProps = {
11
+ config: Ref<LinkTooltipConfig>
12
+ src: Ref<string>
13
+ onConfirm: (href: string) => void
14
+ onCancel: () => void
15
+ }
16
+
17
+ export const EditLink = defineComponent<EditLinkProps>({
18
+ props: {
19
+ config: {
20
+ type: Object,
21
+ required: true,
22
+ },
23
+ src: {
24
+ type: Object,
25
+ required: true,
26
+ },
27
+ onConfirm: {
28
+ type: Function,
29
+ required: true,
30
+ },
31
+ onCancel: {
32
+ type: Function,
33
+ required: true,
34
+ },
35
+ },
36
+ setup({ config, src, onConfirm, onCancel }) {
37
+ const link = ref(src)
38
+
39
+ watch(src, (value) => {
40
+ link.value = value
41
+ })
42
+
43
+ const onConfirmEdit = () => {
44
+ onConfirm(link.value)
45
+ }
46
+
47
+ const onKeydown = (e: KeyboardEvent) => {
48
+ e.stopPropagation()
49
+ if (e.key === 'Enter') {
50
+ e.preventDefault()
51
+ onConfirmEdit()
52
+ }
53
+ if (e.key === 'Escape') {
54
+ e.preventDefault()
55
+ onCancel()
56
+ }
57
+ }
58
+
59
+ return () => {
60
+ return (
61
+ <div class="link-edit">
62
+ <input
63
+ class="input-area"
64
+ placeholder={config.value.inputPlaceholder}
65
+ onKeydown={onKeydown}
66
+ onInput={(e: Event) => {
67
+ link.value = (e.target as HTMLInputElement).value
68
+ }}
69
+ value={link.value}
70
+ />
71
+ {link.value ? (
72
+ <Icon
73
+ class="button confirm"
74
+ icon={config.value.confirmButton}
75
+ onClick={onConfirmEdit}
76
+ />
77
+ ) : null}
78
+ </div>
79
+ )
80
+ }
81
+ },
82
+ })
@@ -0,0 +1,29 @@
1
+ import type { Ctx } from '@jvs-milkdown/ctx'
2
+
3
+ import { linkTooltipAPI } from '../slices'
4
+ import { linkEditTooltip } from '../tooltips'
5
+ import { LinkEditTooltip } from './edit-view'
6
+
7
+ export function configureLinkEditTooltip(ctx: Ctx) {
8
+ let linkEditTooltipView: LinkEditTooltip | null
9
+
10
+ ctx.update(linkTooltipAPI.key, (api) => ({
11
+ ...api,
12
+ addLink: (from, to) => {
13
+ linkEditTooltipView?.addLink(from, to)
14
+ },
15
+ editLink: (mark, from, to) => {
16
+ linkEditTooltipView?.editLink(mark, from, to)
17
+ },
18
+ removeLink: (from, to) => {
19
+ linkEditTooltipView?.removeLink(from, to)
20
+ },
21
+ }))
22
+
23
+ ctx.set(linkEditTooltip.key, {
24
+ view: (view) => {
25
+ linkEditTooltipView = new LinkEditTooltip(ctx, view)
26
+ return linkEditTooltipView
27
+ },
28
+ })
29
+ }
@@ -0,0 +1,165 @@
1
+ import type { Ctx } from '@jvs-milkdown/ctx'
2
+ import type { Mark } from '@jvs-milkdown/prose/model'
3
+ import type { PluginView } from '@jvs-milkdown/prose/state'
4
+ import type { EditorView } from '@jvs-milkdown/prose/view'
5
+
6
+ import { editorViewCtx } from '@jvs-milkdown/core'
7
+ import { TooltipProvider } from '@jvs-milkdown/plugin-tooltip'
8
+ import { linkSchema } from '@jvs-milkdown/preset-commonmark'
9
+ import { posToDOMRect } from '@jvs-milkdown/prose'
10
+ import { TextSelection } from '@jvs-milkdown/prose/state'
11
+ import DOMPurify from 'dompurify'
12
+ import { createApp, ref, type App, type Ref } from 'vue'
13
+
14
+ import {
15
+ linkTooltipConfig,
16
+ linkTooltipState,
17
+ type LinkTooltipConfig,
18
+ } from '../slices'
19
+ import { EditLink } from './component'
20
+
21
+ interface Data {
22
+ from: number
23
+ to: number
24
+ mark: Mark | null
25
+ }
26
+
27
+ const defaultData: Data = {
28
+ from: -1,
29
+ to: -1,
30
+ mark: null,
31
+ }
32
+
33
+ export class LinkEditTooltip implements PluginView {
34
+ #content: HTMLElement
35
+ #provider: TooltipProvider
36
+ #data: Data = { ...defaultData }
37
+ #app: App
38
+ #config: Ref<LinkTooltipConfig>
39
+ #src = ref('')
40
+
41
+ constructor(
42
+ readonly ctx: Ctx,
43
+ view: EditorView
44
+ ) {
45
+ this.#config = ref(this.ctx.get(linkTooltipConfig.key))
46
+
47
+ const content = document.createElement('div')
48
+ content.className = 'milkdown-link-edit'
49
+
50
+ const app = createApp(EditLink, {
51
+ config: this.#config,
52
+ src: this.#src,
53
+ onConfirm: this.#confirmEdit,
54
+ onCancel: this.#reset,
55
+ })
56
+ app.mount(content)
57
+ this.#app = app
58
+
59
+ this.#content = content
60
+ this.#provider = new TooltipProvider({
61
+ content,
62
+ debounce: 0,
63
+ shouldShow: () => false,
64
+ })
65
+ this.#provider.onHide = () => {
66
+ requestAnimationFrame(() => {
67
+ view.dom.focus({ preventScroll: true })
68
+ })
69
+ }
70
+ this.#provider.update(view)
71
+ }
72
+
73
+ #reset = () => {
74
+ this.#provider.hide()
75
+ this.ctx.update(linkTooltipState.key, (state) => ({
76
+ ...state,
77
+ mode: 'preview' as const,
78
+ }))
79
+ this.#data = { ...defaultData }
80
+ }
81
+
82
+ #confirmEdit = (href: string) => {
83
+ const view = this.ctx.get(editorViewCtx)
84
+ const { from, to, mark } = this.#data
85
+ const type = linkSchema.type(this.ctx)
86
+ const link = DOMPurify.sanitize(href)
87
+ if (mark && mark.attrs.href === link) {
88
+ this.#reset()
89
+ return
90
+ }
91
+
92
+ const tr = view.state.tr
93
+ if (mark) tr.removeMark(from, to, mark)
94
+
95
+ tr.addMark(from, to, type.create({ href: link }))
96
+ view.dispatch(tr)
97
+
98
+ this.#reset()
99
+ }
100
+
101
+ #enterEditMode = (value: string, from: number, to: number) => {
102
+ const config = this.ctx.get(linkTooltipConfig.key)
103
+ this.#config.value = config
104
+ this.#src.value = value
105
+ this.ctx.update(linkTooltipState.key, (state) => ({
106
+ ...state,
107
+ mode: 'edit' as const,
108
+ }))
109
+ const view = this.ctx.get(editorViewCtx)
110
+ view.dispatch(
111
+ view.state.tr.setSelection(TextSelection.create(view.state.doc, from, to))
112
+ )
113
+ this.#provider.show(
114
+ { getBoundingClientRect: () => posToDOMRect(view, from, to) },
115
+ view
116
+ )
117
+ requestAnimationFrame(() => {
118
+ this.#content.querySelector('input')?.focus()
119
+ })
120
+ }
121
+
122
+ update = (view: EditorView) => {
123
+ const { state } = view
124
+ const { selection } = state
125
+ if (!(selection instanceof TextSelection)) return
126
+ const { from, to } = selection
127
+ if (from === this.#data.from && to === this.#data.to) return
128
+
129
+ this.#reset()
130
+ }
131
+
132
+ destroy = () => {
133
+ this.#app.unmount()
134
+ this.#provider.destroy()
135
+ this.#content.remove()
136
+ }
137
+
138
+ addLink = (from: number, to: number) => {
139
+ this.#data = {
140
+ from,
141
+ to,
142
+ mark: null,
143
+ }
144
+ this.#enterEditMode('', from, to)
145
+ }
146
+
147
+ editLink = (mark: Mark, from: number, to: number) => {
148
+ this.#data = {
149
+ from,
150
+ to,
151
+ mark,
152
+ }
153
+ this.#enterEditMode(mark.attrs.href, from, to)
154
+ }
155
+
156
+ removeLink = (from: number, to: number) => {
157
+ const view = this.ctx.get(editorViewCtx)
158
+
159
+ const tr = view.state.tr
160
+ tr.removeMark(from, to, linkSchema.type(this.ctx))
161
+ view.dispatch(tr)
162
+
163
+ this.#reset()
164
+ }
165
+ }
@@ -0,0 +1,19 @@
1
+ import type { MilkdownPlugin } from '@jvs-milkdown/ctx'
2
+
3
+ import { toggleLinkCommand } from './command'
4
+ import { linkTooltipAPI, linkTooltipConfig, linkTooltipState } from './slices'
5
+ import { linkEditTooltip, linkPreviewTooltip } from './tooltips'
6
+
7
+ export * from './slices'
8
+ export * from './configure'
9
+ export * from './tooltips'
10
+ export * from './command'
11
+
12
+ export const linkTooltipPlugin: MilkdownPlugin[] = [
13
+ linkTooltipState,
14
+ linkTooltipAPI,
15
+ linkTooltipConfig,
16
+ linkPreviewTooltip,
17
+ linkEditTooltip,
18
+ toggleLinkCommand,
19
+ ].flat()
@@ -0,0 +1,87 @@
1
+ import { defineComponent, type Ref, h } from 'vue'
2
+
3
+ import type { LinkTooltipConfig } from '../slices'
4
+
5
+ import { Icon } from '../../__internal__/components/icon'
6
+ import { keepAlive } from '../../__internal__/keep-alive'
7
+
8
+ keepAlive(h)
9
+
10
+ type PreviewLinkProps = {
11
+ config: Ref<LinkTooltipConfig>
12
+ src: Ref<string>
13
+ onEdit: Ref<() => void>
14
+ onRemove: Ref<() => void>
15
+ }
16
+
17
+ export const PreviewLink = defineComponent<PreviewLinkProps>({
18
+ props: {
19
+ config: {
20
+ type: Object,
21
+ required: true,
22
+ },
23
+ src: {
24
+ type: Object,
25
+ required: true,
26
+ },
27
+ onEdit: {
28
+ type: Object,
29
+ required: true,
30
+ },
31
+ onRemove: {
32
+ type: Object,
33
+ required: true,
34
+ },
35
+ },
36
+ setup({ config, src, onEdit, onRemove }) {
37
+ const onClickEditButton = (e: Event) => {
38
+ e.preventDefault()
39
+ e.stopPropagation()
40
+ onEdit.value()
41
+ }
42
+
43
+ const onClickRemoveButton = (e: Event) => {
44
+ e.preventDefault()
45
+ e.stopPropagation()
46
+ onRemove.value()
47
+ }
48
+
49
+ const onClickPreview = (e: Event) => {
50
+ e.preventDefault()
51
+ const link = src.value
52
+ if (navigator.clipboard && link) {
53
+ navigator.clipboard
54
+ .writeText(link)
55
+ .then(() => {
56
+ config.value.onCopyLink(link)
57
+ })
58
+ .catch((e) => console.error(e))
59
+ }
60
+ }
61
+
62
+ return () => {
63
+ return (
64
+ <div class="link-preview">
65
+ <Icon
66
+ class="button link-icon"
67
+ icon={config.value.linkIcon}
68
+ onClick={onClickPreview}
69
+ />
70
+ <a href={src.value} target="_blank" class="link-display">
71
+ {src.value}
72
+ </a>
73
+ <Icon
74
+ class="button link-edit-button"
75
+ icon={config.value.editButton}
76
+ onClick={onClickEditButton}
77
+ />
78
+ <Icon
79
+ class="button link-remove-button"
80
+ icon={config.value.removeButton}
81
+ onClick={onClickRemoveButton}
82
+ />
83
+ </div>
84
+ )
85
+ }
86
+ },
87
+ })
@@ -0,0 +1,65 @@
1
+ import type { Ctx } from '@jvs-milkdown/ctx'
2
+ import type { EditorView } from '@jvs-milkdown/prose/view'
3
+
4
+ import { posToDOMRect } from '@jvs-milkdown/prose'
5
+ import { debounce } from 'lodash-es'
6
+
7
+ import { linkTooltipState } from '../slices'
8
+ import { linkPreviewTooltip } from '../tooltips'
9
+ import { findMarkPosition, shouldShowPreviewWhenHover } from '../utils'
10
+ import { LinkPreviewTooltip } from './preview-view'
11
+
12
+ export function configureLinkPreviewTooltip(ctx: Ctx) {
13
+ let linkPreviewTooltipView: LinkPreviewTooltip | null
14
+
15
+ const DELAY = 50
16
+ const onMouseMove = debounce((view: EditorView, event: MouseEvent) => {
17
+ if (!linkPreviewTooltipView) return
18
+ if (!view.hasFocus()) return
19
+
20
+ const state = ctx.get(linkTooltipState.key)
21
+ if (state.mode === 'edit') return
22
+
23
+ const result = shouldShowPreviewWhenHover(ctx, view, event)
24
+ if (result) {
25
+ const position = view.state.doc.resolve(result.pos)
26
+ const markPosition = findMarkPosition(
27
+ result.mark,
28
+ result.node,
29
+ view.state.doc,
30
+ position.before(),
31
+ position.after()
32
+ )
33
+ const from = markPosition.start
34
+ const to = markPosition.end
35
+ linkPreviewTooltipView.show(
36
+ result.mark,
37
+ from,
38
+ to,
39
+ posToDOMRect(view, from, to)
40
+ )
41
+ return
42
+ }
43
+
44
+ linkPreviewTooltipView.hide()
45
+ }, DELAY)
46
+
47
+ const onMouseLeave = () => {
48
+ setTimeout(() => {
49
+ linkPreviewTooltipView?.hide()
50
+ }, DELAY)
51
+ }
52
+
53
+ ctx.set(linkPreviewTooltip.key, {
54
+ props: {
55
+ handleDOMEvents: {
56
+ mousemove: onMouseMove,
57
+ mouseleave: onMouseLeave,
58
+ },
59
+ },
60
+ view: (view) => {
61
+ linkPreviewTooltipView = new LinkPreviewTooltip(ctx, view)
62
+ return linkPreviewTooltipView
63
+ },
64
+ })
65
+ }