@bagelink/vue 1.20.52 → 1.20.54

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/vue",
3
3
  "type": "module",
4
- "version": "1.20.52",
4
+ "version": "1.20.54",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
@@ -63,7 +63,7 @@
63
63
  "@types/signature_pad": "^4.0.0",
64
64
  "vue": "^3.5.43",
65
65
  "vue-component-type-helpers": "^2.2.12",
66
- "@bagelink/sdk": "1.20.52"
66
+ "@bagelink/sdk": "1.20.54"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "@bagelink/sdk": "*",
@@ -2672,7 +2672,7 @@ function setupAutoWrapping(doc: Document) {
2672
2672
  const paragraphDirection = paragraph.getAttribute('dir') as 'ltr' | 'rtl'
2673
2673
  || getTextDirection(paragraph.textContent || '', (doc.body.dir || 'ltr') as 'ltr' | 'rtl')
2674
2674
 
2675
- const newParagraph = doc.createElement('p')
2675
+ let newParagraph = doc.createElement('p')
2676
2676
  newParagraph.dir = paragraphDirection
2677
2677
  if (paragraph.hasAttribute('data-dir-explicit')) {
2678
2678
  newParagraph.setAttribute('data-dir-explicit', 'true')
@@ -2699,11 +2699,34 @@ function setupAutoWrapping(doc: Document) {
2699
2699
  }
2700
2700
  })
2701
2701
 
2702
+ const hasTrailingContent = Boolean(trailingFragment.textContent?.length
2703
+ || trailingFragment.querySelector('img, video, iframe'))
2704
+ if (/^H[1-6]$/.test(paragraph.tagName) && hasTrailingContent) {
2705
+ const heading = paragraph.cloneNode(false) as HTMLElement
2706
+ heading.removeAttribute('id')
2707
+ newParagraph = heading
2708
+ if (!paragraph.textContent?.length && !paragraph.querySelector('img, video, iframe')) {
2709
+ // At the start, keep the title as a heading and insert a plain blank line before it.
2710
+ const blank = doc.createElement('p')
2711
+ blank.dir = paragraphDirection
2712
+ blank.innerHTML = '<br>'
2713
+ paragraph.replaceWith(blank, newParagraph)
2714
+ newParagraph.appendChild(trailingFragment)
2715
+ const startRange = doc.createRange()
2716
+ startRange.selectNodeContents(newParagraph)
2717
+ startRange.collapse(true)
2718
+ selection.removeAllRanges()
2719
+ selection.addRange(startRange)
2720
+ updateContentWithHistory(doc)
2721
+ return
2722
+ }
2723
+ }
2724
+
2702
2725
  if (!paragraph.textContent?.trim() && !paragraph.querySelector('img, br, video, iframe')) {
2703
2726
  paragraph.innerHTML = '<br>'
2704
2727
  }
2705
2728
 
2706
- if (trailingFragment.textContent?.trim() || trailingFragment.querySelector('img, video, iframe')) {
2729
+ if (hasTrailingContent) {
2707
2730
  newParagraph.appendChild(trailingFragment)
2708
2731
  } else {
2709
2732
  newParagraph.innerHTML = '<br>'
@@ -1,7 +1,7 @@
1
1
  import type { EditorState } from '../richTextTypes'
2
2
  import { pushHistorySnapshot, restoreHistoryNodes } from '../composables/useEditor'
3
3
  import { insertLink } from './media'
4
- import { isStyleActive } from './selection'
4
+ import { isStyleActive, rangeOverlapsNodeContent } from './selection'
5
5
  import { toggleFullscreen, toggleSplitView } from './viewMode'
6
6
  import {
7
7
  addRow,
@@ -43,17 +43,6 @@ function getCurrentSelection(state: EditorState): { selection: Selection, range:
43
43
  return { selection, range }
44
44
  }
45
45
 
46
- function rangeOverlapsNodeContent(range: Range, node: Node): boolean {
47
- const nodeRange = node.ownerDocument?.createRange()
48
- if (!nodeRange) { return false }
49
- nodeRange.selectNodeContents(node)
50
-
51
- // `intersectsNode()` treats touching boundaries as an intersection. A browser
52
- // selection that ends at offset 0 of the next paragraph must not format it.
53
- return range.compareBoundaryPoints(Range.END_TO_START, nodeRange) < 0
54
- && range.compareBoundaryPoints(Range.START_TO_END, nodeRange) > 0
55
- }
56
-
57
46
  function getSelectedListItems(doc: Document, range: Range): HTMLLIElement[] {
58
47
  const items = Array.from(doc.body.querySelectorAll<HTMLLIElement>('li'))
59
48
  .filter(item => rangeOverlapsNodeContent(range, item))
@@ -1069,10 +1058,21 @@ export function createCommandRegistry(state: EditorState): CommandRegistry {
1069
1058
  const blocks = getSelectedBlocks(s.doc, range)
1070
1059
  if (!blocks.length) { return }
1071
1060
 
1072
- const allMatch = blocks.every(b => b.tagName.toLowerCase() === cmd.toLowerCase())
1061
+ // Blank spacing blocks in a multi-block selection are not headings.
1062
+ // Keep single-block formatting available for a new, empty heading.
1063
+ const targets = blocks.length > 1
1064
+ ? blocks.filter(block => block.textContent?.replace(/\u200B/g, '').trim()
1065
+ || block.querySelector('img, video, iframe, audio, hr, [contenteditable="false"]'))
1066
+ : blocks
1067
+ if (!targets.length) { return }
1068
+
1069
+ const allMatch = targets.every(b => b.tagName.toLowerCase() === cmd.toLowerCase())
1073
1070
  const newTag = allMatch ? 'p' : cmd
1074
1071
 
1075
- const converted = blocks.map(block => convertBlockTag(s.doc!, block, newTag))
1072
+ const converted = blocks.map(block => {
1073
+ if (targets.includes(block)) { return convertBlockTag(s.doc!, block, newTag) }
1074
+ return /^H[1-6]$/.test(block.tagName) ? convertBlockTag(s.doc!, block, 'p') : block
1075
+ })
1076
1076
  const lastConverted = converted[converted.length - 1]
1077
1077
 
1078
1078
  if (!allMatch && lastConverted && !lastConverted.nextElementSibling) {
@@ -98,6 +98,45 @@ export function insertPastedContent(doc: Document, selection: Selection, content
98
98
  if (!fragment.hasChildNodes()) { return false }
99
99
  range.deleteContents()
100
100
  const lastNode = fragment.lastChild
101
+ const startElement = range.startContainer.nodeType === Node.ELEMENT_NODE
102
+ ? range.startContainer as Element
103
+ : range.startContainer.parentElement
104
+ const block = startElement?.closest('p,h1,h2,h3,h4,h5,h6,pre')
105
+ const hasBlocks = Boolean(fragment.querySelector('p,h1,h2,h3,h4,h5,h6,blockquote,ul,ol,pre,hr'))
106
+ if (block && hasBlocks && lastNode) {
107
+ // Block paste must split, not nest inside, a phrasing-only container.
108
+ const tailRange = doc.createRange()
109
+ tailRange.setStart(range.startContainer, range.startOffset)
110
+ tailRange.setEnd(block, block.childNodes.length)
111
+ const tail = block.cloneNode(false) as HTMLElement
112
+ tail.removeAttribute('id')
113
+ tail.appendChild(tailRange.extractContents())
114
+ range.setStartAfter(block)
115
+ range.collapse(true)
116
+ range.insertNode(fragment)
117
+ if (tail.textContent?.length || tail.querySelector('img,video,iframe,hr')) {
118
+ lastNode.parentNode?.insertBefore(tail, lastNode.nextSibling)
119
+ }
120
+ if (!block.textContent?.length && !block.querySelector('img,video,iframe,hr')) {
121
+ block.remove()
122
+ }
123
+ // Place the caret inside the last pasted text block, ready to continue typing.
124
+ const caretBlock = lastNode.nodeType === Node.ELEMENT_NODE
125
+ ? (lastNode as Element).matches('p,h1,h2,h3,h4,h5,h6,li,pre,blockquote')
126
+ ? lastNode
127
+ : (lastNode as Element).querySelector('li:last-child,p:last-child')
128
+ : null
129
+ if (caretBlock) {
130
+ range.selectNodeContents(caretBlock)
131
+ range.collapse(false)
132
+ } else {
133
+ range.setStartAfter(lastNode)
134
+ range.collapse(true)
135
+ }
136
+ selection.removeAllRanges()
137
+ selection.addRange(range)
138
+ return true
139
+ }
101
140
  range.insertNode(fragment)
102
141
 
103
142
  if (lastNode) {
@@ -35,9 +35,29 @@ export function getSelectionRange(state: any) {
35
35
  return state.range
36
36
  }
37
37
 
38
- function rangeOverlapsNodeContent(range: Range, node: Node): boolean {
38
+ function isAtNodeBoundary(container: Node, offset: number, node: Node, end: boolean): boolean {
39
+ while (container === node || node.contains(container)) {
40
+ const length = container.nodeType === Node.TEXT_NODE
41
+ ? container.textContent?.length ?? 0
42
+ : container.childNodes.length
43
+ if (offset !== (end ? length : 0)) { return false }
44
+ if (container === node) { return true }
45
+ const parent = container.parentNode
46
+ if (!parent) { return false }
47
+ offset = Array.prototype.indexOf.call(parent.childNodes, container) + (end ? 1 : 0)
48
+ container = parent
49
+ }
50
+ return false
51
+ }
52
+
53
+ export function rangeOverlapsNodeContent(range: Range, node: Node): boolean {
39
54
  const nodeRange = node.ownerDocument?.createRange()
40
55
  if (!nodeRange) { return false }
56
+ // Text offset 0 and element offset 0 can describe the same visible boundary.
57
+ if (isAtNodeBoundary(range.endContainer, range.endOffset, node, false)
58
+ || isAtNodeBoundary(range.startContainer, range.startOffset, node, true)) {
59
+ return false
60
+ }
41
61
  nodeRange.selectNodeContents(node)
42
62
  return range.compareBoundaryPoints(Range.END_TO_START, nodeRange) < 0
43
63
  && range.compareBoundaryPoints(Range.START_TO_END, nodeRange) > 0