@nuasite/cms-mdx-editor 0.57.0 → 0.58.1

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.
@@ -7,6 +7,16 @@
7
7
  import { type Editor, editorViewCtx } from '@milkdown/core'
8
8
  import type { EditorView } from '@milkdown/prose/view'
9
9
 
10
+ /** The image node the selection covers exactly — a click on an inline image selects the node itself. */
11
+ export interface SelectedImage {
12
+ pos: number
13
+ src: string
14
+ alt: string
15
+ title: string
16
+ }
17
+
18
+ export type ImageAttrs = Omit<SelectedImage, 'pos'>
19
+
10
20
  export interface ActiveFormats {
11
21
  bold: boolean
12
22
  italic: boolean
@@ -18,6 +28,7 @@ export interface ActiveFormats {
18
28
  listStyle: string | null
19
29
  blockquote: boolean
20
30
  heading: number | null
31
+ image: SelectedImage | null
21
32
  }
22
33
 
23
34
  export const defaultActiveFormats: ActiveFormats = {
@@ -31,6 +42,7 @@ export const defaultActiveFormats: ActiveFormats = {
31
42
  listStyle: null,
32
43
  blockquote: false,
33
44
  heading: null,
45
+ image: null,
34
46
  }
35
47
 
36
48
  /** Detect active inline/block formats at the current selection. */
@@ -90,7 +102,41 @@ export function getActiveFormats(view: EditorView): ActiveFormats {
90
102
  heading = typeof level === 'number' ? level : null
91
103
  }
92
104
 
93
- return { bold, italic, strikethrough, link, linkHref, bulletList, orderedList, listStyle, blockquote, heading }
105
+ return { bold, italic, strikethrough, link, linkHref, bulletList, orderedList, listStyle, blockquote, heading, image: getSelectedImage(view) }
106
+ }
107
+
108
+ function getSelectedImage(view: EditorView): SelectedImage | null {
109
+ const { doc, selection } = view.state
110
+ const node = doc.nodeAt(selection.from)
111
+ if (node === null || node.type.name !== 'image' || selection.to - selection.from !== node.nodeSize) return null
112
+ return { pos: selection.from, src: stringAttr(node.attrs.src), alt: stringAttr(node.attrs.alt), title: stringAttr(node.attrs.title) }
113
+ }
114
+
115
+ function stringAttr(value: unknown): string {
116
+ return typeof value === 'string' ? value : ''
117
+ }
118
+
119
+ /**
120
+ * Rewrite the attributes of the image at `image.pos`, leaving everything around it as it is.
121
+ * Does nothing when that position no longer holds the same image — the document changed while the
122
+ * image was being edited, and writing to whatever sits there now would hit the wrong one.
123
+ *
124
+ * Attribute steps rather than a node replacement, so the node selection survives the edit.
125
+ */
126
+ export function updateImage(view: EditorView, image: SelectedImage, attrs: ImageAttrs): boolean {
127
+ const node = view.state.doc.nodeAt(image.pos)
128
+ if (node === null || node.type.name !== 'image' || stringAttr(node.attrs.src) !== image.src) return false
129
+ const tr = view.state.tr
130
+ .setNodeAttribute(image.pos, 'src', attrs.src)
131
+ .setNodeAttribute(image.pos, 'alt', attrs.alt)
132
+ .setNodeAttribute(image.pos, 'title', attrs.title)
133
+ view.dispatch(tr)
134
+ return true
135
+ }
136
+
137
+ function imagesEqual(a: SelectedImage | null, b: SelectedImage | null): boolean {
138
+ if (a === null || b === null) return a === b
139
+ return a.pos === b.pos && a.src === b.src && a.alt === b.alt && a.title === b.title
94
140
  }
95
141
 
96
142
  /** Whether the current selection is inside a list of the given node-type name. */
@@ -152,6 +198,7 @@ function formatsEqual(a: ActiveFormats, b: ActiveFormats): boolean {
152
198
  && a.listStyle === b.listStyle
153
199
  && a.blockquote === b.blockquote
154
200
  && a.heading === b.heading
201
+ && imagesEqual(a.image, b.image)
155
202
  }
156
203
 
157
204
  /**
@@ -7,6 +7,9 @@
7
7
  *
8
8
  * Emits `onChange` on blur (not per keystroke) so typing never triggers a node-view
9
9
  * re-render that would clobber the focused editor.
10
+ *
11
+ * `media` serves an image already in the slot (its preview, "Replace image"); the toolbar
12
+ * still offers no image insertion here.
10
13
  */
11
14
  import { defaultValueCtx, Editor, rootCtx } from '@milkdown/core'
12
15
  import { listener, listenerCtx } from '@milkdown/plugin-listener'
@@ -15,11 +18,20 @@ import { gfm } from '@milkdown/preset-gfm'
15
18
  import { replaceAll } from '@milkdown/utils'
16
19
  import { useEffect, useRef, useState } from 'react'
17
20
  import { FormatToolbar } from './format-toolbar'
21
+ import { markWhitespacePlugin } from './mark-whitespace'
22
+ import type { MediaContext, MediaSource } from './media-source'
18
23
 
19
24
  const wrapper: React.CSSProperties = { border: '1px solid #e4e4e7', borderRadius: 6, background: '#fff' }
20
25
  const host: React.CSSProperties = { padding: '6px 10px', minHeight: 60, fontSize: 13, lineHeight: 1.5, outline: 'none' }
21
26
 
22
- export function SlotEditor({ value, onChange }: { value: string; onChange: (markdown: string) => void }) {
27
+ export interface SlotEditorProps {
28
+ value: string
29
+ onChange: (markdown: string) => void
30
+ media?: MediaSource
31
+ mediaContext?: MediaContext
32
+ }
33
+
34
+ export function SlotEditor({ value, onChange, media, mediaContext }: SlotEditorProps) {
23
35
  const hostRef = useRef<HTMLDivElement>(null)
24
36
  const editorRef = useRef<Editor | null>(null)
25
37
  const [editor, setEditor] = useState<Editor | null>(null)
@@ -44,6 +56,7 @@ export function SlotEditor({ value, onChange }: { value: string; onChange: (mark
44
56
  })
45
57
  .use(commonmark)
46
58
  .use(gfm)
59
+ .use(markWhitespacePlugin)
47
60
  .use(listener)
48
61
  .create()
49
62
 
@@ -75,7 +88,7 @@ export function SlotEditor({ value, onChange }: { value: string; onChange: (mark
75
88
 
76
89
  return (
77
90
  <div style={wrapper}>
78
- <FormatToolbar editor={editor} />
91
+ <FormatToolbar editor={editor} media={media} mediaContext={mediaContext} field="children" allowImageInsert={false} />
79
92
  <div
80
93
  ref={hostRef}
81
94
  style={host}