@manuscripts/body-editor 3.13.19 → 3.15.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 (52) hide show
  1. package/dist/cjs/commands.js +30 -2
  2. package/dist/cjs/components/authors-affiliations/AuthorsAndAffiliationsModals.js +1 -0
  3. package/dist/cjs/components/cross-ref-check-modal/CrossRefWarningModal.js +182 -0
  4. package/dist/cjs/components/cross-ref-check-modal/openModal.js +38 -0
  5. package/dist/cjs/components/views/DeleteSupplementDialog.js +75 -0
  6. package/dist/cjs/icons.js +2 -1
  7. package/dist/cjs/index.js +4 -1
  8. package/dist/cjs/lib/files.js +17 -5
  9. package/dist/cjs/lib/supplements.js +61 -0
  10. package/dist/cjs/plugins/cross-references.js +160 -0
  11. package/dist/cjs/versions.js +1 -1
  12. package/dist/cjs/views/caption_title.js +1 -0
  13. package/dist/cjs/views/supplement.js +21 -1
  14. package/dist/es/commands.js +26 -0
  15. package/dist/es/components/authors-affiliations/AuthorsAndAffiliationsModals.js +1 -0
  16. package/dist/es/components/cross-ref-check-modal/CrossRefWarningModal.js +142 -0
  17. package/dist/es/components/cross-ref-check-modal/openModal.js +31 -0
  18. package/dist/es/components/views/DeleteSupplementDialog.js +35 -0
  19. package/dist/es/icons.js +2 -1
  20. package/dist/es/index.js +2 -0
  21. package/dist/es/lib/files.js +17 -5
  22. package/dist/es/lib/supplements.js +55 -0
  23. package/dist/es/plugins/cross-references.js +162 -2
  24. package/dist/es/versions.js +1 -1
  25. package/dist/es/views/caption_title.js +1 -0
  26. package/dist/es/views/supplement.js +22 -2
  27. package/dist/types/commands.d.ts +2 -0
  28. package/dist/types/components/authors-affiliations/AuthorsAndAffiliationsModals.d.ts +1 -1
  29. package/dist/types/components/cross-ref-check-modal/CrossRefWarningModal.d.ts +29 -0
  30. package/dist/types/components/cross-ref-check-modal/openModal.d.ts +19 -0
  31. package/dist/types/components/views/DeleteSupplementDialog.d.ts +2 -0
  32. package/dist/types/icons.d.ts +1 -0
  33. package/dist/types/index.d.ts +2 -0
  34. package/dist/types/lib/files.d.ts +2 -0
  35. package/dist/types/lib/supplements.d.ts +30 -0
  36. package/dist/types/versions.d.ts +1 -1
  37. package/package.json +2 -2
  38. package/src/commands.ts +42 -0
  39. package/src/components/authors-affiliations/AuthorsAndAffiliationsModals.tsx +2 -0
  40. package/src/components/cross-ref-check-modal/CrossRefWarningModal.tsx +223 -0
  41. package/src/components/cross-ref-check-modal/openModal.ts +50 -0
  42. package/src/components/views/DeleteSupplementDialog.tsx +88 -0
  43. package/src/icons.ts +2 -0
  44. package/src/index.ts +2 -0
  45. package/src/lib/files.ts +20 -6
  46. package/src/lib/supplements.ts +97 -0
  47. package/src/plugins/cross-references.ts +232 -6
  48. package/src/versions.ts +1 -1
  49. package/src/views/caption_title.ts +2 -0
  50. package/src/views/supplement.ts +27 -2
  51. package/src/views/supplements.ts +2 -1
  52. package/styles/Editor.css +13 -0
@@ -0,0 +1,97 @@
1
+ /*!
2
+ * © 2026 Atypon Systems LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import {
18
+ ManuscriptEditorView,
19
+ ManuscriptNode,
20
+ schema,
21
+ SupplementNode,
22
+ } from '@manuscripts/transform'
23
+ import { findChildrenByType, findParentNodeClosestToPos } from 'prosemirror-utils'
24
+
25
+ import { allowedHref } from './url'
26
+
27
+ export type NodeWeblink = {
28
+ node: SupplementNode
29
+ pos: number
30
+ }
31
+
32
+ export const getSupplementDisplayLabel = (
33
+ node: SupplementNode,
34
+ files: { id: string; name: string }[]
35
+ ): string => {
36
+ const href = node.attrs.href
37
+ if (allowedHref(href)) {
38
+ return href
39
+ }
40
+
41
+ const file = files.find((f) => f.id === href)
42
+ return file?.name ?? 'Untitled supplement'
43
+ }
44
+
45
+ export const performDeleteSupplement = (
46
+ view: ManuscriptEditorView,
47
+ pos: number
48
+ ): boolean => {
49
+ const node = view.state.doc.nodeAt(pos)
50
+ if (!node || node.type !== schema.nodes.supplement) {
51
+ return false
52
+ }
53
+
54
+ const from = pos
55
+ const to = from + node.nodeSize
56
+ const { from: deleteFrom, to: deleteTo } = deleteSupplementAtPos(
57
+ view.state.doc,
58
+ from,
59
+ to
60
+ )
61
+ view.dispatch(view.state.tr.delete(deleteFrom, deleteTo))
62
+ return true
63
+ }
64
+
65
+ export const deleteSupplementAtPos = (
66
+ doc: ManuscriptNode,
67
+ from: number,
68
+ to: number
69
+ ) => {
70
+ const resolvedPos = doc.resolve(from)
71
+ const supplementsNodeWithPos = findParentNodeClosestToPos(
72
+ resolvedPos,
73
+ (node) => node.type === schema.nodes.supplements
74
+ )
75
+
76
+ if (!supplementsNodeWithPos) {
77
+ return { from, to, deleteWholeSection: false }
78
+ }
79
+
80
+ const { node: supplementsNode, pos: supplementsPos } = supplementsNodeWithPos
81
+
82
+ const supplements = findChildrenByType(
83
+ supplementsNode,
84
+ schema.nodes.supplement
85
+ )
86
+ const isDeletingLastSupplement = supplements.length === 1
87
+
88
+ if (isDeletingLastSupplement) {
89
+ return {
90
+ from: supplementsPos,
91
+ to: supplementsPos + supplementsNode.nodeSize,
92
+ deleteWholeSection: true,
93
+ }
94
+ }
95
+
96
+ return { from, to, deleteWholeSection: false }
97
+ }
@@ -14,24 +14,102 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import { schema } from '@manuscripts/transform'
17
+ import {
18
+ ManuscriptEditorView,
19
+ ManuscriptNode,
20
+ schema,
21
+ Target,
22
+ } from '@manuscripts/transform'
23
+ import { trackChangesPluginKey } from '@manuscripts/track-changes-plugin'
18
24
  import { isEqual } from 'lodash'
19
- import { Node } from 'prosemirror-model'
20
- import { Plugin } from 'prosemirror-state'
21
- import { Decoration, DecorationSet } from 'prosemirror-view'
25
+ import { Node, ResolvedPos } from 'prosemirror-model'
26
+ import { NodeSelection, Plugin, Transaction } from 'prosemirror-state'
27
+ import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
22
28
 
29
+ import { XrefGroup } from '../components/cross-ref-check-modal/CrossRefWarningModal'
30
+ import { openCrossRefWarningModal } from '../components/cross-ref-check-modal/openModal'
23
31
  import { objectsKey } from './objects'
24
32
 
33
+ let modalActive = false
34
+ let modalElement: HTMLDivElement | null = null
35
+
25
36
  export default () => {
37
+ let view: ManuscriptEditorView | null = null
38
+
26
39
  return new Plugin({
40
+ view(editorView) {
41
+ view = editorView as ManuscriptEditorView
42
+ return {
43
+ update(v) {
44
+ view = v as ManuscriptEditorView
45
+ },
46
+ destroy() {
47
+ view = null
48
+ },
49
+ }
50
+ },
51
+
52
+ filterTransaction(tr, state) {
53
+ // plugins working with content silently are not the subject to a warning. Especially the Collab plugin!
54
+ if (
55
+ !view ||
56
+ !tr.docChanged ||
57
+ tr.getMeta(trackChangesPluginKey) ||
58
+ tr.getMeta('addToHistory') === false
59
+ ) {
60
+ return true
61
+ }
62
+
63
+ // Block doc-changing transactions while modal is active - freezing to stabilize doc so same steps can be applied onConfirm
64
+ if (modalActive) {
65
+ return false
66
+ }
67
+ const targets = objectsKey.getState(state) as Map<string, Target>
68
+ const xrefGroups = createXrefGroups(targets, state.doc, tr.doc)
69
+
70
+ // Orphaned rids were already broken before this transaction — allow
71
+ if (xrefGroups.length === 0) {
72
+ return true
73
+ }
74
+ // Block the transaction and show a warning modal
75
+ modalActive = true
76
+
77
+ const cleanup = () => {
78
+ modalActive = false
79
+ if (modalElement) {
80
+ modalElement.classList.remove('modal-bottom')
81
+ modalElement.remove()
82
+ modalElement = null
83
+ }
84
+ }
85
+
86
+ const deletedIds = new Set(
87
+ xrefGroups.map((g) => g.referenced.attrs.id as string)
88
+ )
89
+
90
+ const onClose = () => {
91
+ cleanup()
92
+ }
93
+
94
+ if (view) {
95
+ modalElement = openCrossRefWarningModal(
96
+ view,
97
+ xrefGroups,
98
+ onConfirmCreator(view, cleanup, deletedIds, tr),
99
+ onClose,
100
+ selectAndScrollToCreator(view)
101
+ )
102
+ }
103
+
104
+ return false
105
+ },
106
+
27
107
  state: {
28
108
  init() {
29
- // Initialize decorations with an empty set
30
109
  return DecorationSet.empty
31
110
  },
32
111
  apply(tr, oldDecorationSet, oldState, newState) {
33
112
  let decoSet = oldDecorationSet
34
- // Check if document or targets have changed
35
113
  const oldTargets = objectsKey.getState(oldState)
36
114
  const newTargets = objectsKey.getState(newState)
37
115
  if (tr.docChanged && !isEqual(oldTargets, newTargets)) {
@@ -68,3 +146,151 @@ function createDecorations(doc: Node): Decoration[] {
68
146
  })
69
147
  return decorations
70
148
  }
149
+
150
+ function createXrefGroups(
151
+ targets: Map<string, Target>,
152
+ stateDoc: ManuscriptNode,
153
+ resultDoc: ManuscriptNode
154
+ ) {
155
+ const newIds = new Set<string>()
156
+ const xrefRids = new Set<string>()
157
+ const xrefGroups: XrefGroup[] = []
158
+
159
+ resultDoc.descendants((node) => {
160
+ if (node.attrs.id) {
161
+ newIds.add(node.attrs.id)
162
+ }
163
+ if (node.type === schema.nodes.cross_reference) {
164
+ for (const rid of node.attrs.rids as string[]) {
165
+ xrefRids.add(rid)
166
+ }
167
+ }
168
+ })
169
+
170
+ // Find xref rids that point to ids no longer present in the resulting doc
171
+ const orphanedRids = new Set<string>()
172
+ for (const rid of xrefRids) {
173
+ if (!newIds.has(rid)) {
174
+ orphanedRids.add(rid)
175
+ }
176
+ }
177
+
178
+ // No broken xrefs — allow the transaction
179
+ if (orphanedRids.size === 0) {
180
+ return []
181
+ }
182
+ // collect the referenced nodes and their xrefs with resolved positions for the modal.
183
+ const referencedNodes = new Map<string, ManuscriptNode>()
184
+ const xrefsByRid = new Map<string, [ManuscriptNode, ResolvedPos][]>()
185
+
186
+ stateDoc.descendants((node, pos) => {
187
+ const id = node.attrs.id
188
+ if (id && orphanedRids.has(id)) {
189
+ referencedNodes.set(id, node as ManuscriptNode)
190
+ }
191
+ if (node.type === schema.nodes.cross_reference) {
192
+ for (const rid of node.attrs.rids as string[]) {
193
+ if (orphanedRids.has(rid)) {
194
+ let entries = xrefsByRid.get(rid)
195
+ if (!entries) {
196
+ entries = []
197
+ xrefsByRid.set(rid, entries)
198
+ }
199
+ entries.push([node as ManuscriptNode, stateDoc.resolve(pos)])
200
+ }
201
+ }
202
+ }
203
+ })
204
+
205
+ for (const [id, referenced] of referencedNodes) {
206
+ const xrefs = xrefsByRid.get(id)
207
+ if (xrefs?.length) {
208
+ const label = targets.get(referenced.attrs.id)?.label || ''
209
+ xrefGroups.push({ referenced, label, xrefs })
210
+ }
211
+ }
212
+ return xrefGroups
213
+ }
214
+
215
+ const onConfirmCreator =
216
+ (
217
+ view: EditorView,
218
+ cleanup: () => void,
219
+ deletedIds: Set<string>,
220
+ tr: Transaction
221
+ ) =>
222
+ () => {
223
+ cleanup()
224
+ if (!view) {
225
+ return
226
+ }
227
+ // Replay the intercepted transaction's steps on the current state
228
+ const newTr = view.state.tr
229
+ for (const step of tr.steps) {
230
+ const result = step.apply(newTr.doc)
231
+ if (result.failed) {
232
+ console.warn(
233
+ 'Cross-ref deletion warning: could not replay step —',
234
+ result.failed
235
+ )
236
+ return
237
+ }
238
+ newTr.step(step)
239
+ }
240
+ // Remove cross-references that pointed to the now-deleted nodes.
241
+ // Collect positions in reverse order so deletions don't shift
242
+ // positions of earlier entries.
243
+ const xrefPositions: { from: number; to: number }[] = []
244
+ newTr.doc.descendants((node, pos) => {
245
+ if (node.type === schema.nodes.cross_reference) {
246
+ const rids = node.attrs.rids as string[]
247
+ if (rids.some((rid) => deletedIds.has(rid))) {
248
+ xrefPositions.push({ from: pos, to: pos + node.nodeSize })
249
+ }
250
+ }
251
+ })
252
+ for (let i = xrefPositions.length - 1; i >= 0; i--) {
253
+ const { from, to } = xrefPositions[i]
254
+ newTr.delete(from, to)
255
+ }
256
+ view.dispatch(newTr)
257
+ }
258
+
259
+ const selectAndScrollToCreator = (view: EditorView) => ($pos: ResolvedPos) => {
260
+ if (!view) {
261
+ return
262
+ }
263
+
264
+ const selTr = view.state.tr
265
+ selTr.setSelection(NodeSelection.create(view.state.doc, $pos.pos))
266
+ view.focus()
267
+ view.dispatch(selTr)
268
+ // Standard PM's scrollIntoView doesn't allow placement control - hence switching to native DOM's peer method.
269
+ let scrollable = view.dom.parentElement
270
+
271
+ while (
272
+ scrollable != null &&
273
+ scrollable.scrollHeight <= scrollable.clientHeight
274
+ ) {
275
+ scrollable = scrollable.parentElement
276
+ }
277
+ if (!scrollable) {
278
+ return // will need more advance handling not to overlap if there is no scroll. The plethora of edge-cases suggest that the warning shouldn't be overlapping the editor really
279
+ }
280
+ const coords = view.coordsAtPos($pos.pos)
281
+ const containerRect = scrollable.getBoundingClientRect()
282
+ const offsetInContainer =
283
+ coords.top - containerRect.top + scrollable.scrollTop
284
+ const containerHeight = scrollable.clientHeight
285
+ // We want the element at 75% of the container (middle of bottom half)
286
+ const scrollTo = offsetInContainer - containerHeight * 0.75
287
+ if (scrollTo < 0) {
288
+ // Element is too close to the top of the document to scroll into
289
+ // the bottom half — move the modal to the bottom instead.
290
+ modalElement?.classList.add('modal-bottom')
291
+ scrollable.scrollTo({ top: 0, behavior: 'smooth' })
292
+ } else {
293
+ modalElement?.classList.remove('modal-bottom')
294
+ scrollable.scrollTo({ top: scrollTo, behavior: 'smooth' })
295
+ }
296
+ }
package/src/versions.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  //THIS FILE WAS GENERATED BY versions.mjs
2
- export const VERSION = '3.13.19';
2
+ export const VERSION = '3.15.0';
3
3
  export const MATHJAX_VERSION = '3.2.2';
@@ -22,7 +22,9 @@ import { createNodeView } from './creators'
22
22
  export class CaptionTitleView extends BaseNodeView<CaptionTitleNode> {
23
23
  public initialise = () => {
24
24
  this.createDOM()
25
+ this.updateContents()
25
26
  }
27
+
26
28
  protected createDOM() {
27
29
  this.dom = document.createElement('label')
28
30
  this.dom.className = 'caption-title placeholder'
@@ -18,12 +18,14 @@ import { getFileIcon } from '@manuscripts/style-guide'
18
18
  import { SupplementNode } from '@manuscripts/transform'
19
19
  import { renderToStaticMarkup } from 'react-dom/server'
20
20
 
21
- import { draggableIcon } from '../icons'
21
+ import { draggableIcon, webLinkIcon } from '../icons'
22
22
  import { findNodeByID } from '../lib/doc'
23
+ import { allowedHref } from '../lib/url'
23
24
  import { Trackable } from '../types'
24
25
  import { BaseNodeView } from './base_node_view'
25
26
  import { createNodeView } from './creators'
26
27
 
28
+
27
29
  export class SupplementView extends BaseNodeView<Trackable<SupplementNode>> {
28
30
  private supplementInfoEl: HTMLDivElement
29
31
  private static currentDragSupplementId: string | null = null
@@ -188,9 +190,32 @@ export class SupplementView extends BaseNodeView<Trackable<SupplementNode>> {
188
190
  this.supplementInfoEl.classList.add('supplement-file-info')
189
191
  this.supplementInfoEl.contentEditable = 'false'
190
192
 
193
+ const href = this.node.attrs.href
194
+
195
+ if (allowedHref(href)) {
196
+ const iconElement = document.createElement('span')
197
+ iconElement.classList.add('supplement-file-icon')
198
+ iconElement.innerHTML = webLinkIcon
199
+ this.supplementInfoEl.appendChild(iconElement)
200
+
201
+ const urlLink = document.createElement('a')
202
+ urlLink.classList.add('supplement-weblink-url')
203
+ urlLink.textContent = href
204
+ if (allowedHref(href)) {
205
+ urlLink.href = href
206
+ urlLink.target = '_blank'
207
+ urlLink.rel = 'noopener noreferrer'
208
+ }
209
+ urlLink.addEventListener('mousedown', (e) => e.stopPropagation())
210
+ this.supplementInfoEl.appendChild(urlLink)
211
+
212
+ this.dom.appendChild(this.supplementInfoEl)
213
+ return
214
+ }
215
+
191
216
  // Get the file from the file management system
192
217
  const files = this.props.getFiles()
193
- const file = files.find((f) => f.id === this.node.attrs.href)
218
+ const file = files.find((f) => f.id === href)
194
219
 
195
220
  if (file) {
196
221
  const iconElement = document.createElement('span')
@@ -32,6 +32,7 @@ export class SupplementsView extends BlockView<Trackable<SupplementsNode>> {
32
32
  this.toggleButton = document.createElement('button')
33
33
  this.toggleButton.classList.add('supplements-toggle-btn', 'button-reset')
34
34
  this.toggleButton.innerHTML = arrowUp
35
+
35
36
  const handleToggle = () => {
36
37
  this.collapsed = !this.collapsed
37
38
  this.toggleContent()
@@ -62,4 +63,4 @@ export class SupplementsView extends BlockView<Trackable<SupplementsNode>> {
62
63
  }
63
64
  }
64
65
 
65
- export default createNodeView(SupplementsView)
66
+ export default createNodeView(SupplementsView)
package/styles/Editor.css CHANGED
@@ -1248,6 +1248,17 @@
1248
1248
  font-size: 14px;
1249
1249
  }
1250
1250
 
1251
+ .ProseMirror .supplement-weblink-url {
1252
+ font-size: 14px;
1253
+ color: #20aedf;
1254
+ text-decoration: none;
1255
+ word-break: break-all;
1256
+ }
1257
+
1258
+ .ProseMirror .supplement-weblink-url:hover {
1259
+ text-decoration: underline;
1260
+ }
1261
+
1251
1262
  .ProseMirror .supplements-toggle-btn {
1252
1263
  background: none;
1253
1264
  border: none;
@@ -1300,6 +1311,8 @@
1300
1311
  padding: 8px 12px !important;
1301
1312
  font-size: 14px;
1302
1313
  display: flex;
1314
+ align-items: center;
1315
+ gap: 8px;
1303
1316
  margin-top: -1px;
1304
1317
  color: #6e6e6e !important;
1305
1318
  }