@manuscripts/body-editor 1.6.13 → 1.6.14

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 (55) hide show
  1. package/dist/cjs/citation-sources/crossref.js +27 -54
  2. package/dist/cjs/commands.js +3 -3
  3. package/dist/cjs/configs/editor-views.js +4 -4
  4. package/dist/cjs/lib/array-reducer.js +45 -0
  5. package/dist/cjs/plugins/bibliography/index.js +11 -1
  6. package/dist/cjs/testing/default-editor-data.js +0 -22
  7. package/dist/cjs/views/CitationEditorWrapper.js +65 -0
  8. package/dist/cjs/views/ReferencesEditor.js +50 -0
  9. package/dist/cjs/views/base_node_view.js +1 -1
  10. package/dist/cjs/views/bibliography_element.js +20 -27
  11. package/dist/cjs/views/citation.js +1 -72
  12. package/dist/cjs/views/citation_editable.js +91 -106
  13. package/dist/cjs/views/cross_reference_editable.js +12 -10
  14. package/dist/cjs/views/link_editable.js +42 -34
  15. package/dist/es/citation-sources/crossref.js +27 -54
  16. package/dist/es/commands.js +3 -3
  17. package/dist/es/configs/editor-views.js +4 -4
  18. package/dist/es/lib/array-reducer.js +41 -0
  19. package/dist/es/plugins/bibliography/index.js +11 -1
  20. package/dist/es/testing/default-editor-data.js +0 -22
  21. package/dist/es/views/CitationEditorWrapper.js +38 -0
  22. package/dist/es/views/ReferencesEditor.js +23 -0
  23. package/dist/es/views/base_node_view.js +1 -1
  24. package/dist/es/views/bibliography_element.js +21 -28
  25. package/dist/es/views/citation.js +1 -49
  26. package/dist/es/views/citation_editable.js +92 -107
  27. package/dist/es/views/cross_reference_editable.js +12 -10
  28. package/dist/es/views/editable_block.js +1 -1
  29. package/dist/es/views/link_editable.js +42 -34
  30. package/dist/types/citation-sources/crossref.d.ts +2 -11
  31. package/dist/types/commands.d.ts +3 -3
  32. package/dist/types/components/views/LinkForm.d.ts +2 -2
  33. package/dist/types/configs/ManuscriptsEditor.d.ts +1 -6
  34. package/dist/types/configs/editor-plugins.d.ts +1 -2
  35. package/dist/types/configs/editor-views.d.ts +1 -3
  36. package/dist/types/lib/array-reducer.d.ts +25 -0
  37. package/dist/types/lib/context-menu.d.ts +1 -1
  38. package/dist/types/plugins/bibliography/index.d.ts +1 -1
  39. package/dist/types/views/CitationEditorWrapper.d.ts +3 -0
  40. package/dist/types/views/ReferencesEditor.d.ts +4 -0
  41. package/dist/types/views/base_node_view.d.ts +0 -3
  42. package/dist/types/views/bibliography_element.d.ts +6 -5
  43. package/dist/types/views/citation.d.ts +2 -14
  44. package/dist/types/views/citation_editable.d.ts +12 -6
  45. package/dist/types/views/cross_reference.d.ts +0 -1
  46. package/dist/types/views/editable_block.d.ts +2 -4
  47. package/dist/types/views/keyword_group.d.ts +3 -15
  48. package/dist/types/views/link_editable.d.ts +3 -0
  49. package/package.json +3 -3
  50. package/dist/cjs/plugins/models.js +0 -48
  51. package/dist/cjs/views/ReactView.js +0 -94
  52. package/dist/es/plugins/models.js +0 -45
  53. package/dist/es/views/ReactView.js +0 -66
  54. package/dist/types/plugins/models.d.ts +0 -28
  55. package/dist/types/views/ReactView.d.ts +0 -33
@@ -16,8 +16,6 @@
16
16
  import { getAllPermitted } from '@manuscripts/style-guide';
17
17
  import { schema } from '@manuscripts/transform';
18
18
  import { createBrowserHistory } from 'history';
19
- import { uniqueId } from 'lodash';
20
- import ReactDOM from 'react-dom';
21
19
  import { PopperManager } from '../lib/popper';
22
20
  import emptyEditorDocJson from './empty-editor-doc.json';
23
21
  const theme = {};
@@ -64,10 +62,6 @@ export const defaultEditorProps = {
64
62
  projectID: 'test-project-id',
65
63
  getCurrentUser: () => TEST_DATA.USER,
66
64
  history: createBrowserHistory(),
67
- renderReactComponent: (child, container) => {
68
- ReactDOM.render(child, container);
69
- },
70
- unmountReactComponent: ReactDOM.unmountComponentAtNode,
71
65
  theme,
72
66
  getFiles: () => [],
73
67
  fileManagement: {
@@ -83,29 +77,13 @@ export const defaultEditorProps = {
83
77
  },
84
78
  getCitationProvider: () => undefined,
85
79
  plugins: [],
86
- saveModel: (model) => {
87
- var _a, _b;
88
- const id = (_a = model._id) !== null && _a !== void 0 ? _a : '';
89
- const oldModel = (_b = TEST_DATA.MODEL_MAP.get(id)) !== null && _b !== void 0 ? _b : {};
90
- const updatedModel = Object.assign(Object.assign({ createdAt: Date.now() / 1000, updatedAt: Date.now() / 1000, _id: uniqueId() }, oldModel), model);
91
- TEST_DATA.MODEL_MAP.set(id, updatedModel);
92
- return Promise.resolve(updatedModel);
93
- },
94
80
  deleteModel: (id) => {
95
81
  TEST_DATA.MODEL_MAP.delete(id);
96
82
  return Promise.resolve(id);
97
83
  },
98
- getLibraryItem: () => undefined,
99
- setLibraryItem: () => undefined,
100
- matchLibraryItemByIdentifier: () => undefined,
101
- filterLibraryItems: () => Promise.resolve([]),
102
- removeLibraryItem: () => undefined,
103
84
  retrySync: () => Promise.resolve(),
104
85
  setComment: () => undefined,
105
86
  setSelectedComment: () => undefined,
106
87
  setEditorSelectedSuggestion: () => undefined,
107
- components: {},
108
88
  environment: undefined,
109
- setCiteprocCitations: () => undefined,
110
- getCiteprocCitations: () => new Map(),
111
89
  };
@@ -0,0 +1,38 @@
1
+ import { CitationEditor } from '@manuscripts/style-guide';
2
+ import React, { useReducer } from 'react';
3
+ import { arrayReducer } from '../lib/array-reducer';
4
+ const itemsReducer = arrayReducer((a, b) => a._id === b._id);
5
+ const ridsReducer = arrayReducer();
6
+ export const CitationEditorWrapper = (props) => {
7
+ const [items, dispatchItems] = useReducer(itemsReducer, props.items);
8
+ const [rids, dispatchRids] = useReducer(ridsReducer, props.rids);
9
+ const handleSave = (item) => {
10
+ props.onSave(item);
11
+ dispatchItems({
12
+ type: 'update',
13
+ items: [item],
14
+ });
15
+ };
16
+ const handleDelete = (item) => {
17
+ props.onDelete(item);
18
+ dispatchItems({
19
+ type: 'delete',
20
+ item,
21
+ });
22
+ };
23
+ const handleCite = (items) => {
24
+ props.onCite(items);
25
+ dispatchRids({
26
+ type: 'update',
27
+ items: items.map((i) => i._id),
28
+ });
29
+ };
30
+ const handleUncite = (rid) => {
31
+ props.onUncite(rid);
32
+ dispatchRids({
33
+ type: 'delete',
34
+ item: rid,
35
+ });
36
+ };
37
+ return (React.createElement(CitationEditor, Object.assign({}, props, { rids: rids, items: items, onCite: handleCite, onUncite: handleUncite, onSave: handleSave, onDelete: handleDelete })));
38
+ };
@@ -0,0 +1,23 @@
1
+ import { ReferencesModal } from '@manuscripts/style-guide';
2
+ import React, { useReducer, useState } from 'react';
3
+ import { arrayReducer } from '../lib/array-reducer';
4
+ const itemsReducer = arrayReducer((a, b) => a._id === b._id);
5
+ export const ReferencesEditor = (props) => {
6
+ const [isOpen, setOpen] = useState(true);
7
+ const [items, dispatch] = useReducer(itemsReducer, props.items);
8
+ const handleSave = (item) => {
9
+ props.onSave(item);
10
+ dispatch({
11
+ type: 'update',
12
+ items: [item],
13
+ });
14
+ };
15
+ const handleDelete = (item) => {
16
+ props.onDelete(item);
17
+ dispatch({
18
+ type: 'delete',
19
+ item: item,
20
+ });
21
+ };
22
+ return (React.createElement(ReferencesModal, Object.assign({}, props, { isOpen: isOpen, onCancel: () => setOpen(false), items: items, onSave: handleSave, onDelete: handleDelete })));
23
+ };
@@ -72,7 +72,7 @@ export class BaseNodeView {
72
72
  };
73
73
  this.deleteNode = (id) => {
74
74
  this.view.state.doc.descendants((node, pos) => {
75
- if ((node.attrs.id || node.attrs.rid) === id) {
75
+ if (node.attrs.id === id) {
76
76
  this.view.dispatch(this.view.state.tr.delete(pos, pos + node.nodeSize));
77
77
  }
78
78
  });
@@ -22,20 +22,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22
22
  step((generator = generator.apply(thisArg, _arguments || [])).next());
23
23
  });
24
24
  };
25
- var __rest = (this && this.__rest) || function (s, e) {
26
- var t = {};
27
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
28
- t[p] = s[p];
29
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
30
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
31
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
32
- t[p[i]] = s[p[i]];
33
- }
34
- return t;
35
- };
36
25
  import { CHANGE_OPERATION, CHANGE_STATUS, } from '@manuscripts/track-changes-plugin';
37
- import { buildComment } from '@manuscripts/transform';
38
- import React from 'react';
26
+ import { buildComment, Decoder, } from '@manuscripts/transform';
39
27
  import { commentIcon, editIcon } from '../assets';
40
28
  import { sanitize } from '../lib/dompurify';
41
29
  import { getBibliographyPluginState } from '../plugins/bibliography';
@@ -43,25 +31,24 @@ import { commentAnnotation } from '../plugins/comment_annotation';
43
31
  import { getAttrsTrackingButton, getMarkDecoration, } from '../plugins/tracking-mark';
44
32
  import BlockView from './block_view';
45
33
  import { createNodeView } from './creators';
34
+ import ReactSubView from './ReactSubView';
35
+ import { ReferencesEditor } from './ReferencesEditor';
46
36
  export class BibliographyElementBlockView extends BlockView {
47
37
  constructor() {
48
38
  super(...arguments);
39
+ this.decoder = new Decoder(new Map());
49
40
  this.gutterButtons = () => [];
50
- this.showPopper = (referenceID) => {
51
- const { renderReactComponent, components: { ReferencesEditor }, } = this.props;
52
- const handleSave = (data) => __awaiter(this, void 0, void 0, function* () {
53
- const _a = data, { _id: id, 'container-title': containerTitle, DOI: doi } = _a, rest = __rest(_a, ["_id", 'container-title', "DOI"]);
54
- this.updateNodeAttrs(Object.assign({ id,
55
- containerTitle,
56
- doi }, rest));
57
- });
58
- if (!this.popperContainer) {
59
- this.popperContainer = document.createElement('div');
60
- this.popperContainer.className = 'references';
61
- }
41
+ this.showPopper = (id) => {
62
42
  const bib = getBibliographyPluginState(this.view.state);
63
- renderReactComponent(React.createElement(ReferencesEditor, { saveModel: handleSave, deleteModel: this.deleteNode, modelMap: bib.bibliographyItems, referenceID: referenceID }), this.popperContainer);
64
- this.props.popper.show(this.dom, this.popperContainer, 'right');
43
+ const componentProps = {
44
+ items: Array.from(bib.bibliographyItems.values()),
45
+ citationCounts: bib.citationCounts,
46
+ item: bib.bibliographyItems.get(id),
47
+ onSave: this.handleSave,
48
+ onDelete: this.handleDelete,
49
+ };
50
+ this.editor = ReactSubView(this.props, ReferencesEditor, componentProps, this.node, this.getPos, this.view, 'references-modal');
51
+ this.props.popper.show(this.dom, this.editor, 'right');
65
52
  };
66
53
  this.stopEvent = () => true;
67
54
  this.ignoreMutation = () => true;
@@ -106,7 +93,6 @@ export class BibliographyElementBlockView extends BlockView {
106
93
  editButton.addEventListener('click', (e) => {
107
94
  e.preventDefault();
108
95
  this.showPopper(element.id);
109
- this.popperContainer = undefined;
110
96
  });
111
97
  editButton.innerHTML = editIcon;
112
98
  commentButton.innerHTML = commentIcon;
@@ -147,6 +133,13 @@ export class BibliographyElementBlockView extends BlockView {
147
133
  this.dom.setAttribute('contenteditable', 'false');
148
134
  this.dom.appendChild(this.container);
149
135
  };
136
+ this.handleSave = (item) => {
137
+ const node = this.decoder.decode(item);
138
+ this.updateNodeAttrs(node.attrs);
139
+ };
140
+ this.handleDelete = (item) => {
141
+ return this.deleteNode(item._id);
142
+ };
150
143
  }
151
144
  }
152
145
  export default createNodeView(BibliographyElementBlockView);
@@ -13,9 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { ObjectTypes } from '@manuscripts/json-schema';
17
16
  import { DOMSerializer } from 'prosemirror-model';
18
- import React, { createElement } from 'react';
17
+ import { createElement } from 'react';
19
18
  import ReactDOM from 'react-dom';
20
19
  import { TrackChangesReview } from '../components/track-changes/TrackChangesReview';
21
20
  import { sanitize } from '../lib/dompurify';
@@ -27,38 +26,7 @@ import { createNodeView } from './creators';
27
26
  export class CitationView extends BaseNodeView {
28
27
  constructor() {
29
28
  super(...arguments);
30
- this.showPopper = () => {
31
- const { components: { CitationViewer }, projectID, renderReactComponent, } = this.props;
32
- const items = this.getBibliographyItems();
33
- if (!this.popperContainer) {
34
- this.popperContainer = document.createElement('div');
35
- this.popperContainer.className = 'citation-editor';
36
- }
37
- renderReactComponent(React.createElement(CitationViewer, { items: items, projectID: projectID, scheduleUpdate: this.props.popper.update }), this.popperContainer);
38
- this.props.popper.show(this.dom, this.popperContainer, 'right');
39
- };
40
29
  this.ignoreMutation = () => true;
41
- this.selectNode = () => {
42
- var _a;
43
- const isDeleted = !!((_a = this.node.attrs.dataTracked) === null || _a === void 0 ? void 0 : _a.find(({ operation, status }) => operation === 'delete' && status !== 'rejected'));
44
- if (!isDeleted) {
45
- this.showPopper();
46
- this.dom.classList.add('ProseMirror-selectednode');
47
- }
48
- };
49
- this.deselectNode = () => {
50
- this.dom.classList.remove('ProseMirror-selectednode');
51
- this.props.popper.destroy();
52
- if (this.popperContainer) {
53
- this.props.unmountReactComponent(this.popperContainer);
54
- }
55
- };
56
- this.destroy = () => {
57
- this.props.popper.destroy();
58
- if (this.popperContainer) {
59
- this.props.unmountReactComponent(this.popperContainer);
60
- }
61
- };
62
30
  this.initialise = () => {
63
31
  if (!this.node.type.spec.toDOM) {
64
32
  throw Error(`Node view ${this.node.type} doesn't have toDOM method`);
@@ -109,22 +77,6 @@ export class CitationView extends BaseNodeView {
109
77
  }
110
78
  return citation;
111
79
  };
112
- this.getBibliographyItems = () => {
113
- const bib = getBibliographyPluginState(this.view.state);
114
- const citation = this.node;
115
- return citation.attrs.rids.map((rid) => {
116
- const item = bib.bibliographyItems.get(rid);
117
- if (!item) {
118
- const placeholder = {
119
- _id: rid,
120
- objectType: ObjectTypes.BibliographyItem,
121
- title: '[missing library item]',
122
- };
123
- return placeholder;
124
- }
125
- return item;
126
- });
127
- };
128
80
  }
129
81
  }
130
82
  export default createNodeView(CitationView);
@@ -13,92 +13,92 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
- var __rest = (this && this.__rest) || function (s, e) {
26
- var t = {};
27
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
28
- t[p] = s[p];
29
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
30
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
31
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
32
- t[p[i]] = s[p[i]];
33
- }
34
- return t;
35
- };
36
16
  import { ObjectTypes, } from '@manuscripts/json-schema';
37
17
  import { findMatchingBibliographyItem } from '@manuscripts/library';
38
- import { schema } from '@manuscripts/transform';
18
+ import { CitationViewer, } from '@manuscripts/style-guide';
19
+ import { buildComment, Decoder, schema, } from '@manuscripts/transform';
39
20
  import { TextSelection } from 'prosemirror-state';
40
- import React from 'react';
21
+ import { findChildren, findChildrenByType } from 'prosemirror-utils';
22
+ import { crossref } from '../citation-sources';
23
+ import { isDeleted } from '../lib/track-changes-utils';
41
24
  import { getBibliographyPluginState } from '../plugins/bibliography';
42
25
  import { CitationView } from './citation';
26
+ import { CitationEditorWrapper } from './CitationEditorWrapper';
43
27
  import { createEditableNodeView } from './creators';
44
- const createBibliographySection = (bibItem) => schema.nodes.bibliography_section.createAndFill({}, [
45
- schema.nodes.section_title.create({}, schema.text('References')),
46
- schema.nodes.bibliography_element.create({}, bibItem ? [bibItem] : []),
28
+ import ReactSubView from './ReactSubView';
29
+ const createBibliographySection = (node) => schema.nodes.bibliography_section.createAndFill({}, [
30
+ schema.nodes.section_title.create({}, schema.text('Bibliography')),
31
+ schema.nodes.bibliography_element.create({}, node ? [node] : []),
47
32
  ]);
48
33
  export class CitationEditableView extends CitationView {
49
34
  constructor() {
50
35
  super(...arguments);
36
+ this.decoder = new Decoder(new Map());
37
+ this.selectNode = () => {
38
+ if (!isDeleted(this.node)) {
39
+ this.showPopper();
40
+ this.dom.classList.add('ProseMirror-selectednode');
41
+ }
42
+ };
43
+ this.deselectNode = () => {
44
+ var _a;
45
+ (_a = this.editor) === null || _a === void 0 ? void 0 : _a.remove();
46
+ this.dom.classList.remove('ProseMirror-selectednode');
47
+ this.props.popper.destroy();
48
+ };
49
+ this.destroy = () => {
50
+ var _a;
51
+ (_a = this.editor) === null || _a === void 0 ? void 0 : _a.remove();
52
+ this.props.popper.destroy();
53
+ };
51
54
  this.showPopper = () => {
52
- const { components: { CitationEditor, CitationViewer }, getCapabilities, projectID, renderReactComponent, } = this.props;
53
- const capabilities = getCapabilities();
55
+ const can = this.props.getCapabilities();
54
56
  const citation = this.getCitation();
55
- const items = this.getBibliographyItems();
56
- const updateCitation = (data) => __awaiter(this, void 0, void 0, function* () {
57
- this.updateNode(Object.assign(Object.assign({}, citation), data));
58
- this.props.popper.update();
59
- });
60
- const findPosition = (doc, id) => {
61
- let nodePos = undefined;
62
- doc.descendants((node, pos) => {
63
- if (node.attrs.id === id) {
64
- nodePos = pos;
65
- }
66
- });
67
- return nodePos;
68
- };
69
- const handleSave = (data) => __awaiter(this, void 0, void 0, function* () {
70
- if (data._id && !findPosition(this.view.state.doc, data._id)) {
71
- this.insertBibliographyNode(this.view, data);
72
- }
73
- else {
74
- const _a = data, { _id: id, 'container-title': containerTitle, DOI: doi } = _a, rest = __rest(_a, ["_id", 'container-title', "DOI"]);
75
- this.updateNodeAttrs(Object.assign({ id,
76
- containerTitle,
77
- doi }, rest));
78
- }
79
- });
80
- if (!this.popperContainer) {
81
- this.popperContainer = document.createElement('div');
82
- this.popperContainer.className = 'citation-editor';
83
- }
57
+ const rids = citation.embeddedCitationItems.map((i) => i.bibliographyItem);
84
58
  const bib = getBibliographyPluginState(this.view.state);
85
- const component = (capabilities === null || capabilities === void 0 ? void 0 : capabilities.editArticle) ? (React.createElement(CitationEditor, { items: items, saveModel: handleSave, deleteModel: this.deleteNode, insertBibliographyNode: (item) => this.insertBibliographyNode(this.view, item), modelMap: bib.bibliographyItems, importItems: this.importItems, selectedText: this.node.attrs.selectedText, setComment: this.props.setComment, handleCancel: this.handleCancel, handleClose: this.handleClose, handleRemove: this.handleRemove, updatePopper: this.updatePopper, handleCite: this.handleCite, canEdit: capabilities.editCitationsAndRefs, citation: citation, updateCitation: updateCitation, projectID: projectID, scheduleUpdate: this.props.popper.update })) : (React.createElement(CitationViewer, { items: items, setComment: this.props.setComment, scheduleUpdate: this.props.popper.update }));
86
- renderReactComponent(component, this.popperContainer);
87
- this.props.popper.show(this.dom, this.popperContainer, 'right');
59
+ const items = Array.from(bib.bibliographyItems.values());
60
+ if (can.editArticle) {
61
+ const query = this.node.attrs.selectedText;
62
+ const componentProps = {
63
+ query,
64
+ rids,
65
+ items,
66
+ citationCounts: bib.citationCounts,
67
+ sources: [crossref],
68
+ onCite: this.handleCite,
69
+ onUncite: this.handleUncite,
70
+ onSave: this.handleSave,
71
+ onDelete: this.handleDelete,
72
+ onComment: this.handleComment,
73
+ onCancel: this.handleCancel,
74
+ canEdit: can.editCitationsAndRefs,
75
+ };
76
+ this.editor = ReactSubView(this.props, CitationEditorWrapper, componentProps, this.node, this.getPos, this.view, 'citation-editor');
77
+ }
78
+ else {
79
+ const componentProps = {
80
+ rids,
81
+ items,
82
+ };
83
+ this.editor = ReactSubView(this.props, CitationViewer, componentProps, this.node, this.getPos, this.view, 'citation-editor');
84
+ }
85
+ this.props.popper.show(this.dom, this.editor, 'right');
88
86
  };
89
- this.handleClose = () => {
87
+ this.handleCancel = () => {
90
88
  const selection = TextSelection.create(this.view.state.tr.doc, this.getPos() + 1);
91
89
  this.view.dispatch(this.view.state.tr.setSelection(selection));
92
90
  this.props.popper.destroy();
93
91
  };
94
- this.handleCancel = () => {
95
- const { state } = this.view;
96
- const pos = this.getPos();
97
- const tr = state.tr.delete(pos, pos + this.node.nodeSize);
98
- const selection = TextSelection.create(tr.doc, pos);
99
- this.view.dispatch(tr.setSelection(selection));
92
+ this.handleSave = (item) => {
93
+ if (item._id && !this.findPosition(item._id)) {
94
+ this.insertBibliographyNode(item);
95
+ }
96
+ else {
97
+ const node = this.decoder.decode(item);
98
+ this.updateNodeAttrs(node.attrs);
99
+ }
100
100
  };
101
- this.handleRemove = (id) => __awaiter(this, void 0, void 0, function* () {
101
+ this.handleUncite = (id) => {
102
102
  const citation = this.getCitation();
103
103
  const items = citation.embeddedCitationItems.filter((i) => i.bibliographyItem !== id);
104
104
  if (items.length > 0) {
@@ -115,23 +115,19 @@ export class CitationEditableView extends CitationView {
115
115
  this.view.dispatch(tr);
116
116
  }
117
117
  this.props.popper.destroy();
118
- });
119
- this.updatePopper = () => {
120
- this.props.popper.destroy();
121
- window.setTimeout(() => {
122
- this.showPopper();
123
- }, 100);
124
118
  };
125
- this.handleCite = (items) => __awaiter(this, void 0, void 0, function* () {
119
+ this.handleCite = (items) => {
126
120
  const bib = getBibliographyPluginState(this.view.state);
127
121
  const citation = this.getCitation();
122
+ const rids = citation.embeddedCitationItems.map((i) => i.bibliographyItem);
123
+ items = items.filter((i) => !rids.includes(i._id));
128
124
  for (const item of items) {
129
125
  const existingItem = findMatchingBibliographyItem(item, bib.bibliographyItems);
130
126
  if (existingItem) {
131
127
  item._id = existingItem._id;
132
128
  }
133
129
  else {
134
- this.insertBibliographyNode(this.view, item);
130
+ this.insertBibliographyNode(item);
135
131
  }
136
132
  citation.embeddedCitationItems.push({
137
133
  _id: '_',
@@ -140,20 +136,15 @@ export class CitationEditableView extends CitationView {
140
136
  });
141
137
  }
142
138
  this.updateNode(citation);
143
- this.handleClose();
144
- });
145
- this.importItems = (items) => __awaiter(this, void 0, void 0, function* () {
146
- const bib = getBibliographyPluginState(this.view.state);
147
- const newItems = [];
148
- for (const item of items) {
149
- const existingItem = findMatchingBibliographyItem(item, bib.bibliographyItems);
150
- if (!existingItem) {
151
- this.insertBibliographyNode(this.view, item);
152
- newItems.push(item);
153
- }
154
- }
155
- return newItems;
156
- });
139
+ this.handleCancel();
140
+ };
141
+ this.handleDelete = (item) => {
142
+ return this.deleteNode(item._id);
143
+ };
144
+ this.handleComment = () => {
145
+ const comment = buildComment(this.node.attrs.id);
146
+ this.props.setComment(comment);
147
+ };
157
148
  this.updateNode = (citation) => {
158
149
  const rids = citation.embeddedCitationItems.map((i) => i.bibliographyItem);
159
150
  const { tr } = this.view.state;
@@ -162,28 +153,22 @@ export class CitationEditableView extends CitationView {
162
153
  rids,
163
154
  }));
164
155
  };
156
+ this.findPosition = (id) => {
157
+ const doc = this.view.state.doc;
158
+ const children = findChildren(doc, (n) => n.attrs.id === id);
159
+ return children.length ? children[0] : undefined;
160
+ };
165
161
  }
166
- insertBibliographyNode(view, item) {
167
- const { doc, tr } = view.state;
168
- const { DOI: doi, ['container-title']: containerTitle } = item, restAttr = __rest(item, ["DOI", 'container-title']);
169
- let bibElement = null;
170
- let pos = null;
171
- doc.descendants((node, nodePos) => {
172
- if (node.type === schema.nodes.bibliography_element) {
173
- bibElement = node;
174
- pos = nodePos;
175
- }
176
- });
177
- let newTr;
178
- const bibItem = schema.nodes.bibliography_item.create(Object.assign({ id: item._id, doi,
179
- containerTitle }, restAttr));
180
- if (bibElement && pos) {
181
- newTr = tr.insert(pos + 1, bibItem);
162
+ insertBibliographyNode(item) {
163
+ const { doc, tr } = this.view.state;
164
+ const elements = findChildrenByType(doc, schema.nodes.bibliography_element, true);
165
+ const node = this.decoder.decode(item);
166
+ if (elements.length) {
167
+ this.view.dispatch(tr.insert(elements[0].pos + 1, node));
182
168
  }
183
169
  else {
184
- newTr = tr.insert(tr.doc.content.size, createBibliographySection(bibItem));
170
+ this.view.dispatch(tr.insert(tr.doc.content.size, createBibliographySection(node)));
185
171
  }
186
- view.dispatch(newTr);
187
172
  return false;
188
173
  }
189
174
  }
@@ -24,34 +24,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
24
24
  };
25
25
  import { skipTracking } from '@manuscripts/track-changes-plugin';
26
26
  import { TextSelection } from 'prosemirror-state';
27
- import React from 'react';
28
27
  import { CrossReferenceItems } from '../components/views/CrossReferenceItems';
29
28
  import { objectsKey } from '../plugins/objects';
30
29
  import { createEditableNodeView } from './creators';
31
30
  import { CrossReferenceView } from './cross_reference';
31
+ import ReactSubView from './ReactSubView';
32
32
  export class CrossReferenceEditableView extends CrossReferenceView {
33
33
  constructor() {
34
34
  super(...arguments);
35
35
  this.selectNode = () => {
36
- const { getCapabilities, renderReactComponent } = this.props;
36
+ const { getCapabilities } = this.props;
37
37
  const xref = this.node;
38
38
  const rids = xref.attrs.rids;
39
39
  const can = getCapabilities();
40
40
  if (!(can === null || can === void 0 ? void 0 : can.editArticle) || rids.length) {
41
41
  return;
42
42
  }
43
- if (!this.popperContainer) {
44
- this.popperContainer = document.createElement('div');
45
- this.popperContainer.className = 'cross-reference-editor';
46
- }
47
- renderReactComponent(React.createElement(CrossReferenceItems, { handleSelect: this.handleSelect, targets: this.getTargets(), handleCancel: this.handleCancel, currentTargetId: rids[0], currentCustomLabel: this.node.attrs.customLabel }), this.popperContainer);
43
+ const componentProps = {
44
+ handleSelect: this.handleSelect,
45
+ targets: this.getTargets(),
46
+ handleCancel: this.handleCancel,
47
+ currentTargetId: rids[0],
48
+ currentCustomLabel: this.node.attrs.customLabel,
49
+ };
50
+ this.popperContainer = ReactSubView(this.props, CrossReferenceItems, componentProps, this.node, this.getPos, this.view, 'cross-reference-editor');
48
51
  this.props.popper.show(this.dom, this.popperContainer, 'right');
49
52
  };
50
53
  this.destroy = () => {
54
+ var _a;
51
55
  this.props.popper.destroy();
52
- if (this.popperContainer) {
53
- this.props.unmountReactComponent(this.popperContainer);
54
- }
56
+ (_a = this.popperContainer) === null || _a === void 0 ? void 0 : _a.remove();
55
57
  };
56
58
  this.deselectNode = () => {
57
59
  this.handleCancel();
@@ -22,7 +22,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22
22
  step((generator = generator.apply(thisArg, _arguments || [])).next());
23
23
  });
24
24
  };
25
- import { nodeNames, schema, } from '@manuscripts/transform';
25
+ import { nodeNames, schema } from '@manuscripts/transform';
26
26
  import { findParentNodeOfTypeClosestToPos } from 'prosemirror-utils';
27
27
  import { ContextMenu } from '../lib/context-menu';
28
28
  const isNotNull = (a) => a !== null;