@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.
@@ -13,13 +13,62 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { schema } from '@manuscripts/transform';
16
+ import { schema, } from '@manuscripts/transform';
17
+ import { trackChangesPluginKey } from '@manuscripts/track-changes-plugin';
17
18
  import { isEqual } from 'lodash';
18
- import { Plugin } from 'prosemirror-state';
19
+ import { NodeSelection, Plugin } from 'prosemirror-state';
19
20
  import { Decoration, DecorationSet } from 'prosemirror-view';
21
+ import { openCrossRefWarningModal } from '../components/cross-ref-check-modal/openModal';
20
22
  import { objectsKey } from './objects';
23
+ let modalActive = false;
24
+ let modalElement = null;
21
25
  export default () => {
26
+ let view = null;
22
27
  return new Plugin({
28
+ view(editorView) {
29
+ view = editorView;
30
+ return {
31
+ update(v) {
32
+ view = v;
33
+ },
34
+ destroy() {
35
+ view = null;
36
+ },
37
+ };
38
+ },
39
+ filterTransaction(tr, state) {
40
+ if (!view ||
41
+ !tr.docChanged ||
42
+ tr.getMeta(trackChangesPluginKey) ||
43
+ tr.getMeta('addToHistory') === false) {
44
+ return true;
45
+ }
46
+ if (modalActive) {
47
+ return false;
48
+ }
49
+ const targets = objectsKey.getState(state);
50
+ const xrefGroups = createXrefGroups(targets, state.doc, tr.doc);
51
+ if (xrefGroups.length === 0) {
52
+ return true;
53
+ }
54
+ modalActive = true;
55
+ const cleanup = () => {
56
+ modalActive = false;
57
+ if (modalElement) {
58
+ modalElement.classList.remove('modal-bottom');
59
+ modalElement.remove();
60
+ modalElement = null;
61
+ }
62
+ };
63
+ const deletedIds = new Set(xrefGroups.map((g) => g.referenced.attrs.id));
64
+ const onClose = () => {
65
+ cleanup();
66
+ };
67
+ if (view) {
68
+ modalElement = openCrossRefWarningModal(view, xrefGroups, onConfirmCreator(view, cleanup, deletedIds, tr), onClose, selectAndScrollToCreator(view));
69
+ }
70
+ return false;
71
+ },
23
72
  state: {
24
73
  init() {
25
74
  return DecorationSet.empty;
@@ -53,3 +102,114 @@ function createDecorations(doc) {
53
102
  });
54
103
  return decorations;
55
104
  }
105
+ function createXrefGroups(targets, stateDoc, resultDoc) {
106
+ const newIds = new Set();
107
+ const xrefRids = new Set();
108
+ const xrefGroups = [];
109
+ resultDoc.descendants((node) => {
110
+ if (node.attrs.id) {
111
+ newIds.add(node.attrs.id);
112
+ }
113
+ if (node.type === schema.nodes.cross_reference) {
114
+ for (const rid of node.attrs.rids) {
115
+ xrefRids.add(rid);
116
+ }
117
+ }
118
+ });
119
+ const orphanedRids = new Set();
120
+ for (const rid of xrefRids) {
121
+ if (!newIds.has(rid)) {
122
+ orphanedRids.add(rid);
123
+ }
124
+ }
125
+ if (orphanedRids.size === 0) {
126
+ return [];
127
+ }
128
+ const referencedNodes = new Map();
129
+ const xrefsByRid = new Map();
130
+ stateDoc.descendants((node, pos) => {
131
+ const id = node.attrs.id;
132
+ if (id && orphanedRids.has(id)) {
133
+ referencedNodes.set(id, node);
134
+ }
135
+ if (node.type === schema.nodes.cross_reference) {
136
+ for (const rid of node.attrs.rids) {
137
+ if (orphanedRids.has(rid)) {
138
+ let entries = xrefsByRid.get(rid);
139
+ if (!entries) {
140
+ entries = [];
141
+ xrefsByRid.set(rid, entries);
142
+ }
143
+ entries.push([node, stateDoc.resolve(pos)]);
144
+ }
145
+ }
146
+ }
147
+ });
148
+ for (const [id, referenced] of referencedNodes) {
149
+ const xrefs = xrefsByRid.get(id);
150
+ if (xrefs?.length) {
151
+ const label = targets.get(referenced.attrs.id)?.label || '';
152
+ xrefGroups.push({ referenced, label, xrefs });
153
+ }
154
+ }
155
+ return xrefGroups;
156
+ }
157
+ const onConfirmCreator = (view, cleanup, deletedIds, tr) => () => {
158
+ cleanup();
159
+ if (!view) {
160
+ return;
161
+ }
162
+ const newTr = view.state.tr;
163
+ for (const step of tr.steps) {
164
+ const result = step.apply(newTr.doc);
165
+ if (result.failed) {
166
+ console.warn('Cross-ref deletion warning: could not replay step —', result.failed);
167
+ return;
168
+ }
169
+ newTr.step(step);
170
+ }
171
+ const xrefPositions = [];
172
+ newTr.doc.descendants((node, pos) => {
173
+ if (node.type === schema.nodes.cross_reference) {
174
+ const rids = node.attrs.rids;
175
+ if (rids.some((rid) => deletedIds.has(rid))) {
176
+ xrefPositions.push({ from: pos, to: pos + node.nodeSize });
177
+ }
178
+ }
179
+ });
180
+ for (let i = xrefPositions.length - 1; i >= 0; i--) {
181
+ const { from, to } = xrefPositions[i];
182
+ newTr.delete(from, to);
183
+ }
184
+ view.dispatch(newTr);
185
+ };
186
+ const selectAndScrollToCreator = (view) => ($pos) => {
187
+ if (!view) {
188
+ return;
189
+ }
190
+ const selTr = view.state.tr;
191
+ selTr.setSelection(NodeSelection.create(view.state.doc, $pos.pos));
192
+ view.focus();
193
+ view.dispatch(selTr);
194
+ let scrollable = view.dom.parentElement;
195
+ while (scrollable != null &&
196
+ scrollable.scrollHeight <= scrollable.clientHeight) {
197
+ scrollable = scrollable.parentElement;
198
+ }
199
+ if (!scrollable) {
200
+ return;
201
+ }
202
+ const coords = view.coordsAtPos($pos.pos);
203
+ const containerRect = scrollable.getBoundingClientRect();
204
+ const offsetInContainer = coords.top - containerRect.top + scrollable.scrollTop;
205
+ const containerHeight = scrollable.clientHeight;
206
+ const scrollTo = offsetInContainer - containerHeight * 0.75;
207
+ if (scrollTo < 0) {
208
+ modalElement?.classList.add('modal-bottom');
209
+ scrollable.scrollTo({ top: 0, behavior: 'smooth' });
210
+ }
211
+ else {
212
+ modalElement?.classList.remove('modal-bottom');
213
+ scrollable.scrollTo({ top: scrollTo, behavior: 'smooth' });
214
+ }
215
+ };
@@ -1,2 +1,2 @@
1
- export const VERSION = '3.14.0';
1
+ export const VERSION = '3.15.0';
2
2
  export const MATHJAX_VERSION = '3.2.2';
@@ -26,4 +26,4 @@ export interface AuthorsAndAffiliationsModalsProps {
26
26
  addNewAffiliation?: boolean;
27
27
  }
28
28
  export declare const AuthorsAndAffiliationsModals: React.FC<AuthorsAndAffiliationsModalsProps>;
29
- export declare const openAuthorsAndAffiliationsModals: (pos: number, view: ManuscriptEditorView | EditorView | undefined, initialModal: "authors" | "affiliations") => void;
29
+ export declare const openAuthorsAndAffiliationsModals: (pos: number, view: ManuscriptEditorView | EditorView | undefined, initialModal: "authors" | "affiliations") => HTMLDivElement | undefined;
@@ -0,0 +1,29 @@
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
+ import { ManuscriptNode } from '@manuscripts/transform';
17
+ import React from 'react';
18
+ import { ResolvedPos } from 'prosemirror-model';
19
+ export type XrefGroup = {
20
+ referenced: ManuscriptNode;
21
+ label: string;
22
+ xrefs: [ManuscriptNode, ResolvedPos][];
23
+ };
24
+ export declare const CrossRefWarningModal: React.FC<{
25
+ onClose: () => void;
26
+ xrefs: XrefGroup[];
27
+ onConfirm: () => void;
28
+ selectAndScrollTo: ($pos: ResolvedPos) => void;
29
+ }>;
@@ -0,0 +1,19 @@
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
+ import { ManuscriptEditorView } from '@manuscripts/transform';
17
+ import { ResolvedPos } from 'prosemirror-model';
18
+ import { XrefGroup } from './CrossRefWarningModal';
19
+ export declare const openCrossRefWarningModal: (view: ManuscriptEditorView, xrefGroups: XrefGroup[], onConfirm: () => void, onClose: () => void, selectAndScrollTo: ($pos: ResolvedPos) => void) => HTMLDivElement;
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "3.14.0";
1
+ export declare const VERSION = "3.15.0";
2
2
  export declare const MATHJAX_VERSION = "3.2.2";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@manuscripts/body-editor",
3
3
  "description": "Prosemirror components for editing and viewing manuscripts",
4
- "version": "3.14.0",
4
+ "version": "3.15.0",
5
5
  "repository": "github:Atypon-OpenSource/manuscripts-body-editor",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/cjs",
@@ -167,4 +167,6 @@ export const openAuthorsAndAffiliationsModals = (
167
167
  )
168
168
  view.focus()
169
169
  document.body.appendChild(dialog)
170
+ // @TODO refactor to allow cleaning it up
171
+ return dialog
170
172
  }
@@ -0,0 +1,223 @@
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 { ManuscriptNode } from '@manuscripts/transform'
18
+ import React, { useState } from 'react'
19
+
20
+ import {
21
+ AttentionOrangeIcon,
22
+ CloseButton,
23
+ ModalContainer,
24
+ ModalHeader,
25
+ PrimaryButton,
26
+ StyledModal,
27
+ TertiaryButton,
28
+ TextButton,
29
+ } from '@manuscripts/style-guide'
30
+ import { ResolvedPos } from 'prosemirror-model'
31
+ import { startCase } from 'lodash'
32
+ import styled from 'styled-components'
33
+
34
+ export type XrefGroup = {
35
+ referenced: ManuscriptNode
36
+ label: string
37
+ xrefs: [ManuscriptNode, ResolvedPos][]
38
+ }
39
+
40
+ export const CrossRefWarningModal: React.FC<{
41
+ onClose: () => void
42
+ xrefs: XrefGroup[]
43
+ onConfirm: () => void
44
+ selectAndScrollTo: ($pos: ResolvedPos) => void
45
+ }> = ({ onClose, xrefs, onConfirm, selectAndScrollTo }) => {
46
+ const [isOpen, setIsOpen] = useState(true)
47
+ const handleClose = () => {
48
+ setIsOpen(false)
49
+ onClose()
50
+ }
51
+
52
+ return (
53
+ <Modal
54
+ isOpen={isOpen}
55
+ onRequestClose={() => handleClose()}
56
+ shouldCloseOnOverlayClick={false}
57
+ hideOverlay={true}
58
+ >
59
+ <Container data-cy="cross-reference-warning-modal">
60
+ <ModalHeader>
61
+ <CloseButton
62
+ onClick={() => handleClose()}
63
+ data-cy="modal-close-button"
64
+ />
65
+ </ModalHeader>
66
+ <Body>
67
+ <Title>
68
+ <AttentionOrangeIcon width={24} height={22} /> Delete referenced
69
+ content?
70
+ </Title>
71
+ <p>You are deleting content referenced elsewhere in the document:</p>
72
+ <ScrolableItems>
73
+ {xrefs.map((group, i) => (
74
+ <XrefGroupDisplay
75
+ key={i}
76
+ group={group}
77
+ selectAndScrollTo={selectAndScrollTo}
78
+ />
79
+ ))}
80
+ </ScrolableItems>
81
+ <Actions>
82
+ <TertiaryButton type="button" onClick={() => handleClose()}>
83
+ Cancel
84
+ </TertiaryButton>
85
+ <PrimaryButton
86
+ $danger={true}
87
+ type="button"
88
+ onClick={() => {
89
+ onConfirm()
90
+ setIsOpen(false)
91
+ }}
92
+ >
93
+ Delete & remove citation
94
+ </PrimaryButton>
95
+ </Actions>
96
+ </Body>
97
+ </Container>
98
+ </Modal>
99
+ )
100
+ }
101
+
102
+ const XrefGroupDisplay: React.FC<{
103
+ group: XrefGroup
104
+ selectAndScrollTo: ($pos: ResolvedPos) => void
105
+ }> = ({ group, selectAndScrollTo }) => {
106
+ return (
107
+ <div>
108
+ <h3>{group.label}</h3>
109
+ <ReferencesList>
110
+ {group.xrefs.map(([, pos], i) => {
111
+ return (
112
+ <li key={i}>
113
+ <TextButton onClick={() => selectAndScrollTo(pos)}>
114
+ {`${startCase(pos.parent.type.name)} - ${pos.parent.textContent}`}
115
+ </TextButton>
116
+ </li>
117
+ )
118
+ })}
119
+ </ReferencesList>
120
+ </div>
121
+ )
122
+ }
123
+
124
+ const Container = styled(ModalContainer)`
125
+ position: absolute;
126
+ top: 1rem;
127
+ left: 50%;
128
+ right: 0;
129
+ max-height: calc(50vh - 2rem);
130
+ min-height: 280px;
131
+ transform: translate(-50%, 0);
132
+ max-width: 480px;
133
+ transition:
134
+ top 0.2s,
135
+ transform 0.2s;
136
+ `
137
+
138
+ // since we need to scroll inside the editor when this dialog is active, we can't use dialog.showModal()
139
+ // so we recreate the appearance using classic position:fixed/after approach.
140
+ // While showModal doesn't block scrolling - it doesn't allow to focus on the editor and that kills the scrollIntoView
141
+ const Modal = styled(StyledModal)`
142
+ position: fixed;
143
+ top: 0;
144
+ left: 0;
145
+ width: 100%;
146
+ height: 100%;
147
+ z-index: 1100;
148
+ color: #6e6e6e;
149
+ margin: auto;
150
+
151
+ &.modal-bottom ${Container} {
152
+ top: calc(100% - 2rem);
153
+ transform: translate(-50%, -100%);
154
+ }
155
+
156
+ &:after {
157
+ content: '';
158
+ display: block;
159
+ position: fixed;
160
+ z-index: -1;
161
+ left: 0;
162
+ top: 0;
163
+ right: 0;
164
+ bottom: 0;
165
+ background: rgba(0, 0, 0, 0.2);
166
+ }
167
+ h3 {
168
+ font-size: 16px;
169
+ margin: 0.5em 0;
170
+ }
171
+ p {
172
+ margin: 0.5em 0;
173
+ }
174
+ `
175
+
176
+ const Body = styled.div`
177
+ margin: 1.5rem;
178
+ display: flex;
179
+ flex-flow: column;
180
+ `
181
+
182
+ const Title = styled.h2`
183
+ font-size: 18px;
184
+ font-weight: 700;
185
+ line-height: 1.5;
186
+ margin: 0;
187
+ color: #353535;
188
+ svg {
189
+ vertical-align: text-top;
190
+ }
191
+ `
192
+
193
+ const ReferencesList = styled.ul`
194
+ padding: 8px;
195
+ margin-left: 0;
196
+ list-style: none;
197
+ background: #f2f2f2;
198
+ border: 1px solid #e2e2e2;
199
+ border-radius: 3px;
200
+
201
+ ${TextButton} {
202
+ margin-left: 0;
203
+ text-decoration: underline;
204
+ &:hover {
205
+ text-decoration: none;
206
+ }
207
+ display: block;
208
+ max-width: 100%;
209
+ overflow: hidden;
210
+ color: #353535;
211
+ text-overflow: ellipsis;
212
+ }
213
+ `
214
+
215
+ const Actions = styled.footer`
216
+ text-align: right;
217
+ padding-top: 1rem;
218
+ `
219
+ const ScrolableItems = styled.div`
220
+ max-height: 16vh;
221
+ min-height: 100px;
222
+ overflow-y: auto;
223
+ `
@@ -0,0 +1,50 @@
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 { ManuscriptEditorView } from '@manuscripts/transform'
18
+ import { ResolvedPos } from 'prosemirror-model'
19
+
20
+ import { getEditorProps } from '../../plugins/editor-props'
21
+ import ReactSubView from '../../views/ReactSubView'
22
+ import { CrossRefWarningModal, XrefGroup } from './CrossRefWarningModal'
23
+
24
+ export const openCrossRefWarningModal = (
25
+ view: ManuscriptEditorView,
26
+ xrefGroups: XrefGroup[],
27
+ onConfirm: () => void,
28
+ onClose: () => void,
29
+ selectAndScrollTo: ($pos: ResolvedPos) => void
30
+ ): HTMLDivElement => {
31
+ const { state } = view
32
+ const props = getEditorProps(state)
33
+ const componentProps = {
34
+ xrefs: xrefGroups,
35
+ onConfirm,
36
+ onClose,
37
+ selectAndScrollTo,
38
+ }
39
+
40
+ const dialog = ReactSubView(
41
+ props,
42
+ CrossRefWarningModal,
43
+ componentProps,
44
+ state.doc,
45
+ () => 0,
46
+ view
47
+ )
48
+ document.body.appendChild(dialog)
49
+ return dialog
50
+ }