@manuscripts/body-editor 3.14.0 → 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.
- package/dist/cjs/components/authors-affiliations/AuthorsAndAffiliationsModals.js +1 -0
- package/dist/cjs/components/cross-ref-check-modal/CrossRefWarningModal.js +182 -0
- package/dist/cjs/components/cross-ref-check-modal/openModal.js +38 -0
- package/dist/cjs/plugins/cross-references.js +160 -0
- package/dist/cjs/versions.js +1 -1
- package/dist/es/components/authors-affiliations/AuthorsAndAffiliationsModals.js +1 -0
- package/dist/es/components/cross-ref-check-modal/CrossRefWarningModal.js +142 -0
- package/dist/es/components/cross-ref-check-modal/openModal.js +31 -0
- package/dist/es/plugins/cross-references.js +162 -2
- package/dist/es/versions.js +1 -1
- package/dist/types/components/authors-affiliations/AuthorsAndAffiliationsModals.d.ts +1 -1
- package/dist/types/components/cross-ref-check-modal/CrossRefWarningModal.d.ts +29 -0
- package/dist/types/components/cross-ref-check-modal/openModal.d.ts +19 -0
- package/dist/types/versions.d.ts +1 -1
- package/package.json +1 -1
- package/src/components/authors-affiliations/AuthorsAndAffiliationsModals.tsx +2 -0
- package/src/components/cross-ref-check-modal/CrossRefWarningModal.tsx +223 -0
- package/src/components/cross-ref-check-modal/openModal.ts +50 -0
- package/src/plugins/cross-references.ts +232 -6
- package/src/versions.ts +1 -1
|
@@ -14,24 +14,102 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
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