@manuscripts/body-editor 1.10.11-LEAN-3519.0 → 1.11.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 (61) hide show
  1. package/dist/cjs/commands.js +76 -122
  2. package/dist/cjs/configs/editor-plugins.js +5 -5
  3. package/dist/cjs/index.js +3 -6
  4. package/dist/cjs/lib/comments.js +49 -0
  5. package/dist/cjs/lib/context-menu.js +19 -8
  6. package/dist/cjs/menus.js +2 -2
  7. package/dist/cjs/plugins/comments.js +209 -0
  8. package/dist/cjs/plugins/editor-props.js +22 -0
  9. package/dist/cjs/plugins/footnotes/index.js +1 -1
  10. package/dist/cjs/plugins/sections.js +1 -4
  11. package/dist/cjs/toolbar.js +1 -1
  12. package/dist/cjs/views/bibliography_element.js +64 -41
  13. package/dist/cjs/views/citation.js +9 -6
  14. package/dist/cjs/views/citation_editable.js +2 -2
  15. package/dist/cjs/views/contributors.js +2 -6
  16. package/dist/cjs/views/editable_block.js +1 -2
  17. package/dist/cjs/views/keyword_group.js +0 -1
  18. package/dist/es/commands.js +75 -119
  19. package/dist/es/configs/editor-plugins.js +5 -5
  20. package/dist/es/index.js +2 -2
  21. package/dist/es/lib/comments.js +42 -0
  22. package/dist/es/lib/context-menu.js +20 -9
  23. package/dist/es/menus.js +3 -3
  24. package/dist/es/plugins/comments.js +204 -0
  25. package/dist/es/plugins/editor-props.js +18 -0
  26. package/dist/es/plugins/footnotes/index.js +1 -1
  27. package/dist/es/plugins/sections.js +1 -4
  28. package/dist/es/toolbar.js +2 -2
  29. package/dist/es/views/bibliography_element.js +65 -42
  30. package/dist/es/views/citation.js +9 -6
  31. package/dist/es/views/citation_editable.js +3 -3
  32. package/dist/es/views/contributors.js +2 -6
  33. package/dist/es/views/editable_block.js +1 -2
  34. package/dist/es/views/keyword_group.js +0 -1
  35. package/dist/types/commands.d.ts +3 -7
  36. package/dist/types/configs/ManuscriptsEditor.d.ts +1 -3
  37. package/dist/types/index.d.ts +2 -2
  38. package/dist/types/lib/comments.d.ts +33 -0
  39. package/dist/types/lib/context-menu.d.ts +2 -2
  40. package/dist/types/lib/references.d.ts +3 -2
  41. package/dist/types/plugins/comments.d.ts +15 -0
  42. package/dist/types/plugins/editor-props.d.ts +7 -0
  43. package/dist/types/views/bibliography_element.d.ts +3 -8
  44. package/dist/types/views/citation_editable.d.ts +1 -1
  45. package/dist/types/views/editable_block.d.ts +0 -2
  46. package/dist/types/views/keyword_group.d.ts +0 -1
  47. package/package.json +3 -3
  48. package/styles/AdvancedEditor.css +45 -54
  49. package/styles/Editor.css +22 -13
  50. package/dist/cjs/plugins/comment_annotation.js +0 -191
  51. package/dist/cjs/plugins/highlight/findHighlightMarkers.js +0 -26
  52. package/dist/cjs/plugins/highlight/index.js +0 -85
  53. package/dist/cjs/plugins/highlight/types.js +0 -2
  54. package/dist/es/plugins/comment_annotation.js +0 -184
  55. package/dist/es/plugins/highlight/findHighlightMarkers.js +0 -22
  56. package/dist/es/plugins/highlight/index.js +0 -79
  57. package/dist/es/plugins/highlight/types.js +0 -1
  58. package/dist/types/plugins/comment_annotation.d.ts +0 -14
  59. package/dist/types/plugins/highlight/findHighlightMarkers.d.ts +0 -3
  60. package/dist/types/plugins/highlight/index.d.ts +0 -26
  61. package/dist/types/plugins/highlight/types.d.ts +0 -18
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.insertTableFootnote = exports.updateCommentAnnotationState = exports.addComment = exports.deleteHighlightMarkers = exports.insertHighlight = exports.createAndFillTableElement = exports.selectAllIsolating = exports.ignoreAtomBlockNodeForward = exports.isAtEndOfTextBlock = exports.ignoreMetaNodeBackspaceCommand = exports.ignoreAtomBlockNodeBackward = exports.isTextSelection = exports.isAtStartOfTextBlock = exports.ifInTableBody = exports.insertTOCSection = exports.insertBibliographySection = exports.insertList = exports.insertSection = exports.insertGraphicalAbstract = exports.insertInlineFootnote = exports.insertInlineEquation = exports.insertCrossReference = exports.insertInlineCitation = exports.insertLink = exports.insertSectionLabel = exports.insertBreak = exports.deleteBlock = exports.insertBlock = exports.insertFileAsFigure = exports.insertGeneralFootnote = exports.createBlock = exports.createSelection = exports.canInsert = exports.blockActive = exports.isNodeSelection = exports.markActive = void 0;
18
+ exports.insertTableFootnote = exports.addInlineComment = exports.addNodeComment = exports.createAndFillTableElement = exports.selectAllIsolating = exports.ignoreAtomBlockNodeForward = exports.isAtEndOfTextBlock = exports.ignoreMetaNodeBackspaceCommand = exports.ignoreAtomBlockNodeBackward = exports.isTextSelection = exports.isAtStartOfTextBlock = exports.ifInTableBody = exports.insertTOCSection = exports.insertBibliographySection = exports.insertList = exports.insertSection = exports.insertGraphicalAbstract = exports.insertInlineFootnote = exports.insertInlineEquation = exports.insertCrossReference = exports.insertInlineCitation = exports.insertLink = exports.insertSectionLabel = exports.insertBreak = exports.deleteBlock = exports.insertBlock = exports.insertFileAsFigure = exports.insertGeneralFootnote = exports.createBlock = exports.createSelection = exports.canInsert = exports.blockActive = exports.isNodeSelection = exports.markActive = void 0;
19
19
  const json_schema_1 = require("@manuscripts/json-schema");
20
20
  const track_changes_plugin_1 = require("@manuscripts/track-changes-plugin");
21
21
  const transform_1 = require("@manuscripts/transform");
@@ -24,12 +24,13 @@ const prosemirror_schema_list_1 = require("prosemirror-schema-list");
24
24
  const prosemirror_state_1 = require("prosemirror-state");
25
25
  const prosemirror_transform_1 = require("prosemirror-transform");
26
26
  const prosemirror_utils_1 = require("prosemirror-utils");
27
+ const comments_1 = require("./lib/comments");
27
28
  const helpers_1 = require("./lib/helpers");
28
29
  const track_changes_utils_1 = require("./lib/track-changes-utils");
29
30
  const utils_1 = require("./lib/utils");
30
- const comment_annotation_1 = require("./plugins/comment_annotation");
31
+ const comments_2 = require("./plugins/comments");
32
+ const editor_props_1 = require("./plugins/editor-props");
31
33
  const footnotes_utils_1 = require("./plugins/footnotes/footnotes-utils");
32
- const highlight_1 = require("./plugins/highlight");
33
34
  const markActive = (type) => (state) => {
34
35
  const { from, $from, to, empty } = state.selection;
35
36
  return empty
@@ -128,12 +129,9 @@ exports.createBlock = createBlock;
128
129
  const insertGeneralFootnote = (tableNode, position, view, tableElementFooter) => {
129
130
  var _a;
130
131
  const { state, dispatch } = view;
131
- const paragraph = state.schema.nodes.paragraph.create({
132
+ const generalNote = state.schema.nodes.paragraph.create({
132
133
  placeholder: 'Add general note here',
133
134
  });
134
- const generalNote = state.schema.nodes.general_table_footnote.create({}, [
135
- paragraph,
136
- ]);
137
135
  const tableColGroup = (0, prosemirror_utils_1.findChildrenByType)(tableNode, transform_1.schema.nodes.table_colgroup)[0];
138
136
  const tr = state.tr;
139
137
  const pos = (tableElementFooter === null || tableElementFooter === void 0 ? void 0 : tableElementFooter.length)
@@ -662,83 +660,6 @@ const createAndFillFigcaptionElement = (state) => state.schema.nodes.figcaption.
662
660
  state.schema.nodes.caption_title.create(),
663
661
  state.schema.nodes.caption.create(),
664
662
  ]);
665
- const insertHighlight = (state, comment, dispatch) => {
666
- const { from, to } = state.selection;
667
- const fromNode = state.schema.nodes.highlight_marker.create({
668
- id: comment._id,
669
- tid: comment.target,
670
- position: 'start',
671
- });
672
- const toNode = state.schema.nodes.highlight_marker.create({
673
- id: comment._id,
674
- tid: comment.target,
675
- position: 'end',
676
- });
677
- const tr = state.tr
678
- .insert(from, fromNode)
679
- .insert(to + 1, toNode)
680
- .setMeta(highlight_1.highlightKey, {
681
- [highlight_1.SET_COMMENT]: Object.assign(Object.assign({}, comment), { selector: { from, to }, originalText: state.tr.doc.textBetween(from, to, '\n') }),
682
- });
683
- tr.setMeta('addToHistory', false);
684
- if (dispatch) {
685
- dispatch(tr);
686
- }
687
- return true;
688
- };
689
- exports.insertHighlight = insertHighlight;
690
- const deleteHighlightMarkers = (rid) => (state, dispatch) => {
691
- var _a;
692
- const markersToDelete = [];
693
- (_a = highlight_1.highlightKey.getState(state)) === null || _a === void 0 ? void 0 : _a.highlights.forEach((highlight) => {
694
- if (highlight.id === rid) {
695
- markersToDelete.push(highlight.start - 1);
696
- markersToDelete.push(highlight.end);
697
- }
698
- });
699
- if (markersToDelete.length === 0) {
700
- return false;
701
- }
702
- const { tr } = state;
703
- markersToDelete
704
- .sort((a, b) => b - a)
705
- .forEach((pos) => {
706
- tr.delete(pos, pos + 1);
707
- });
708
- tr.setMeta('track-changes-skip-tracking', true);
709
- dispatch && dispatch(tr);
710
- return true;
711
- };
712
- exports.deleteHighlightMarkers = deleteHighlightMarkers;
713
- function addComment(state, dispatch, viewNode, resolvePos) {
714
- const { selection } = state;
715
- const hasText = selection.content().size > 0;
716
- const parent = getParentNode(selection);
717
- if (!parent) {
718
- return false;
719
- }
720
- if (viewNode && resolvePos) {
721
- const viewNode = getParentNode(prosemirror_state_1.TextSelection.near(resolvePos));
722
- if (!viewNode) {
723
- return false;
724
- }
725
- if (hasText && parent.attrs.id === viewNode.attrs.id) {
726
- return addHighlightComment(viewNode, state, dispatch);
727
- }
728
- else {
729
- return addBlockComment(viewNode, state, dispatch);
730
- }
731
- }
732
- else {
733
- if (hasText) {
734
- return addHighlightComment(parent, state, dispatch);
735
- }
736
- else {
737
- return addBlockComment(parent, state, dispatch);
738
- }
739
- }
740
- }
741
- exports.addComment = addComment;
742
663
  const getParentNode = (selection) => {
743
664
  var _a;
744
665
  const parentNode = (0, utils_1.findParentNodeWithId)(selection);
@@ -748,61 +669,94 @@ const getParentNode = (selection) => {
748
669
  }
749
670
  return node;
750
671
  };
751
- const isAllowedType = (type) => type === transform_1.schema.nodes.section ||
672
+ const isCommentingAllowed = (type) => type === transform_1.schema.nodes.section ||
673
+ type === transform_1.schema.nodes.citation ||
674
+ type === transform_1.schema.nodes.bibliography_item ||
752
675
  type === transform_1.schema.nodes.footnotes_section ||
753
676
  type === transform_1.schema.nodes.bibliography_section ||
754
677
  type === transform_1.schema.nodes.keyword_group ||
755
678
  type === transform_1.schema.nodes.paragraph ||
756
679
  type === transform_1.schema.nodes.figure_element ||
757
680
  type === transform_1.schema.nodes.table_element;
758
- const getNode = (node) => {
759
- if (node.type === transform_1.schema.nodes.keywords) {
760
- const keywordGroups = (0, prosemirror_utils_1.findChildrenByType)(node, transform_1.schema.nodes.keyword_group, true);
761
- return keywordGroups.length ? keywordGroups[0].node : node;
681
+ const addNodeComment = (node, state, dispatch) => {
682
+ if (!isCommentingAllowed(node.type)) {
683
+ return false;
762
684
  }
763
- return node;
764
- };
765
- const addBlockComment = (node, state, dispatch) => {
766
- const { attrs: { id }, type, } = getNode(node);
767
- const comment = (0, transform_1.buildComment)(id);
768
- const tr = state.tr.setMeta(comment_annotation_1.commentAnnotation, {
769
- [highlight_1.SET_COMMENT]: comment,
770
- });
771
- tr.setMeta('addToHistory', false);
772
- if (isAllowedType(type)) {
773
- dispatch && dispatch(tr);
774
- return true;
685
+ const props = (0, editor_props_1.getEditorProps)(state);
686
+ const contribution = (0, json_schema_1.buildContribution)(props.userID);
687
+ const attrs = {
688
+ id: (0, transform_1.generateID)(json_schema_1.ObjectTypes.CommentAnnotation),
689
+ contents: '',
690
+ target: node.attrs.id,
691
+ contributions: [contribution],
692
+ };
693
+ const comment = transform_1.schema.nodes.comment.create(attrs);
694
+ const comments = (0, prosemirror_utils_1.findChildrenByType)(state.doc, transform_1.schema.nodes.comments)[0];
695
+ const pos = comments.pos + 1;
696
+ const tr = state.tr.insert(pos, comment);
697
+ const key = (0, comments_1.getCommentKey)(attrs, undefined, node);
698
+ (0, comments_2.setCommentSelection)(tr, key, attrs.id, true);
699
+ if (dispatch) {
700
+ dispatch(tr);
775
701
  }
776
- else {
702
+ return true;
703
+ };
704
+ exports.addNodeComment = addNodeComment;
705
+ const addInlineComment = (state, dispatch) => {
706
+ const selection = state.selection;
707
+ const node = getParentNode(selection);
708
+ if (!node || !isCommentingAllowed(node.type)) {
777
709
  return false;
778
710
  }
779
- };
780
- const addHighlightComment = (node, state, dispatch) => {
781
- const { attrs: { id }, type, } = node;
782
- const comment = (0, transform_1.buildComment)(id);
783
- if (type === transform_1.schema.nodes.paragraph) {
784
- const { $anchor, $head } = state.selection;
785
- const isAllTextSelected = ($anchor.textOffset === 0 && $head.textOffset === 0) ||
786
- $anchor.textOffset === $head.textOffset;
787
- if (isAllTextSelected) {
788
- return addBlockComment(node, state, dispatch);
789
- }
790
- else {
791
- return (0, exports.insertHighlight)(state, comment, dispatch);
792
- }
711
+ const from = selection.from;
712
+ const to = selection.to;
713
+ const props = (0, editor_props_1.getEditorProps)(state);
714
+ const contribution = (0, json_schema_1.buildContribution)(props.userID);
715
+ const attrs = {
716
+ id: (0, transform_1.generateID)(json_schema_1.ObjectTypes.CommentAnnotation),
717
+ contents: '',
718
+ target: node.attrs.id,
719
+ contributions: [contribution],
720
+ originalText: selectedText(),
721
+ selector: {
722
+ from,
723
+ to,
724
+ },
725
+ };
726
+ const comment = transform_1.schema.nodes.comment.create(attrs);
727
+ const comments = (0, prosemirror_utils_1.findChildrenByType)(state.doc, transform_1.schema.nodes.comments)[0];
728
+ const pos = comments.pos + 1;
729
+ const tr = state.tr.insert(pos, comment);
730
+ if (from === to) {
731
+ const point = transform_1.schema.nodes.highlight_marker.create({
732
+ id: comment.attrs.id,
733
+ tid: node.attrs.id,
734
+ position: 'point',
735
+ });
736
+ tr.insert(from, point);
793
737
  }
794
738
  else {
795
- return (0, exports.insertHighlight)(state, comment, dispatch);
739
+ const start = transform_1.schema.nodes.highlight_marker.create({
740
+ id: comment.attrs.id,
741
+ tid: node.attrs.id,
742
+ position: 'start',
743
+ });
744
+ const end = transform_1.schema.nodes.highlight_marker.create({
745
+ id: comment.attrs.id,
746
+ tid: node.attrs.id,
747
+ position: 'end',
748
+ });
749
+ tr.insert(from, start).insert(to + 1, end);
796
750
  }
797
- };
798
- const updateCommentAnnotationState = (state, dispatch) => {
799
- const tr = state.tr.setMeta(comment_annotation_1.commentAnnotation, {});
800
- tr.setMeta('addToHistory', false);
751
+ const range = (0, comments_1.getCommentRange)(attrs);
752
+ const key = (0, comments_1.getCommentKey)(attrs, range, node);
753
+ (0, comments_2.setCommentSelection)(tr, key, attrs.id, true);
801
754
  if (dispatch) {
802
755
  dispatch(tr);
803
756
  }
757
+ return true;
804
758
  };
805
- exports.updateCommentAnnotationState = updateCommentAnnotationState;
759
+ exports.addInlineComment = addInlineComment;
806
760
  const insertTableFootnote = (node, position, view, inlineFootnote) => {
807
761
  var _a;
808
762
  const { state, dispatch } = view;
@@ -28,10 +28,10 @@ const prosemirror_tables_1 = require("prosemirror-tables");
28
28
  const keys_1 = __importDefault(require("../keys"));
29
29
  const affiliations_1 = __importDefault(require("../plugins/affiliations"));
30
30
  const bibliography_1 = __importDefault(require("../plugins/bibliography"));
31
- const comment_annotation_1 = __importDefault(require("../plugins/comment_annotation"));
31
+ const comments_1 = __importDefault(require("../plugins/comments"));
32
+ const editor_props_1 = __importDefault(require("../plugins/editor-props"));
32
33
  const elements_1 = __importDefault(require("../plugins/elements"));
33
34
  const footnotes_1 = __importDefault(require("../plugins/footnotes"));
34
- const highlight_1 = __importDefault(require("../plugins/highlight"));
35
35
  const objects_1 = __importDefault(require("../plugins/objects"));
36
36
  const paragraphs_1 = __importDefault(require("../plugins/paragraphs"));
37
37
  const persist_1 = __importDefault(require("../plugins/persist"));
@@ -63,14 +63,14 @@ exports.default = (props) => {
63
63
  (0, bibliography_1.default)(props),
64
64
  (0, objects_1.default)(props),
65
65
  (0, affiliations_1.default)(),
66
- (0, comment_annotation_1.default)(props),
66
+ (0, comments_1.default)(),
67
67
  (0, paragraphs_1.default)(),
68
68
  (0, placeholder_1.default)(),
69
69
  (0, prosemirror_tables_1.tableEditing)(),
70
- (0, highlight_1.default)(props),
70
+ (0, selected_suggestion_1.default)(),
71
71
  (0, footnotes_1.default)(props),
72
72
  (0, table_footnote_1.default)(),
73
- (0, selected_suggestion_1.default)(),
73
+ (0, editor_props_1.default)(props),
74
74
  ];
75
75
  if (props.collabProvider) {
76
76
  allPlugins.push((0, prosemirror_collab_1.collab)({ version: props.collabProvider.currentVersion }));
package/dist/cjs/index.js CHANGED
@@ -29,7 +29,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
29
29
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
30
30
  };
31
31
  Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.isHighlightComment = exports.isThereSelector = exports.commentScroll = exports.selectedSuggestionKey = exports.PopperManager = exports.CollabProvider = exports.LevelSelector = exports.OutlineItemIcon = exports.ManuscriptOutline = void 0;
32
+ exports.selectedSuggestionKey = exports.PopperManager = exports.CollabProvider = exports.LevelSelector = exports.OutlineItemIcon = exports.ManuscriptOutline = void 0;
33
33
  __exportStar(require("./commands"), exports);
34
34
  var ManuscriptOutline_1 = require("./components/outline/ManuscriptOutline");
35
35
  Object.defineProperty(exports, "ManuscriptOutline", { enumerable: true, get: function () { return ManuscriptOutline_1.ManuscriptOutline; } });
@@ -43,13 +43,10 @@ Object.defineProperty(exports, "CollabProvider", { enumerable: true, get: functi
43
43
  var popper_1 = require("./lib/popper");
44
44
  Object.defineProperty(exports, "PopperManager", { enumerable: true, get: function () { return popper_1.PopperManager; } });
45
45
  __exportStar(require("./toolbar"), exports);
46
+ __exportStar(require("./lib/comments"), exports);
47
+ __exportStar(require("./plugins/comments"), exports);
46
48
  var selected_suggestion_1 = require("./plugins/selected-suggestion");
47
49
  Object.defineProperty(exports, "selectedSuggestionKey", { enumerable: true, get: function () { return selected_suggestion_1.selectedSuggestionKey; } });
48
- __exportStar(require("./plugins/highlight"), exports);
49
- var comment_annotation_1 = require("./plugins/comment_annotation");
50
- Object.defineProperty(exports, "commentScroll", { enumerable: true, get: function () { return comment_annotation_1.commentScroll; } });
51
- Object.defineProperty(exports, "isThereSelector", { enumerable: true, get: function () { return comment_annotation_1.isThereSelector; } });
52
- Object.defineProperty(exports, "isHighlightComment", { enumerable: true, get: function () { return comment_annotation_1.isHighlightComment; } });
53
50
  __exportStar(require("./lib/utils"), exports);
54
51
  __exportStar(require("./lib/track-changes-utils"), exports);
55
52
  __exportStar(require("./useEditor"), exports);
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCommentMarker = exports.getCommentRange = exports.getCommentKey = exports.isNodeComment = void 0;
4
+ const isNodeComment = (comment) => !comment.range;
5
+ exports.isNodeComment = isNodeComment;
6
+ const getCommentKey = (comment, range, target) => {
7
+ if (!range) {
8
+ return target.attrs.id;
9
+ }
10
+ else if (!range.size) {
11
+ return `${target.attrs.id}-${range.pos}`;
12
+ }
13
+ else {
14
+ return comment.id;
15
+ }
16
+ };
17
+ exports.getCommentKey = getCommentKey;
18
+ const getCommentRange = (comment) => {
19
+ if (!comment.selector) {
20
+ return;
21
+ }
22
+ return {
23
+ pos: comment.selector.from,
24
+ size: comment.selector.to - comment.selector.from,
25
+ };
26
+ };
27
+ exports.getCommentRange = getCommentRange;
28
+ const createCommentMarker = (tagName, key, count) => {
29
+ const element = document.createElement(tagName);
30
+ element.id = getMarkerID(key);
31
+ element.dataset.key = key;
32
+ element.classList.add('comment-marker');
33
+ if (count && count > 1) {
34
+ element.dataset.count = String(count);
35
+ }
36
+ element.innerHTML = `
37
+ <svg width="16" height="13" viewBox="0 0 16 13" fill="none" xmlns="http://www.w3.org/2000/svg">
38
+ <path d="M4.0625 2.9375V7.3125L1.4375 11.6875H12.8125C13.7794 11.6875 14.5625 10.9044 14.5625 9.9375V2.9375C14.5625 1.97062 13.7794 1.1875 12.8125 1.1875H5.8125C4.84562 1.1875 4.0625 1.97062 4.0625 2.9375Z"
39
+ fill="#FFFCDB" stroke="#FFBD26" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
40
+ <path d="M6.6875 4.6875H11.9375" stroke="#FFBD26" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
41
+ <path d="M6.6875 8.1875H9.3125" stroke="#FFBD26" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
42
+ </svg>
43
+ `;
44
+ return element;
45
+ };
46
+ exports.createCommentMarker = createCommentMarker;
47
+ const getMarkerID = (id) => {
48
+ return `${id}-comment-marker`;
49
+ };
@@ -27,7 +27,6 @@ const footnotes_utils_1 = require("../plugins/footnotes/footnotes-utils");
27
27
  const ReactSubView_1 = __importDefault(require("../views/ReactSubView"));
28
28
  const popper_1 = require("./popper");
29
29
  const track_changes_utils_1 = require("./track-changes-utils");
30
- const utils_1 = require("./utils");
31
30
  const popper = new popper_1.PopperManager();
32
31
  const listTypes = [transform_1.schema.nodes.ordered_list, transform_1.schema.nodes.bullet_list];
33
32
  const readonlyTypes = [transform_1.schema.nodes.keywords, transform_1.schema.nodes.bibliography_element];
@@ -144,7 +143,7 @@ class ContextMenu {
144
143
  this.addPopperEventListeners();
145
144
  };
146
145
  this.showEditMenu = (target) => {
147
- var _a;
146
+ var _a, _b;
148
147
  const menu = document.createElement('div');
149
148
  menu.className = 'menu';
150
149
  const $pos = this.resolvePos();
@@ -165,12 +164,12 @@ class ContextMenu {
165
164
  }
166
165
  }));
167
166
  }
168
- const { setComment } = this.actions;
169
- if (setComment) {
167
+ const { addComment } = this.actions;
168
+ if (addComment) {
170
169
  menu.appendChild(this.createMenuSection((section) => {
171
170
  section.appendChild(this.createMenuItem('Comment', () => {
172
- const { state, dispatch } = this.view;
173
- (0, commands_1.addComment)(state, dispatch, this.node, this.resolvePos());
171
+ const target = this.getCommentTarget();
172
+ (0, commands_1.addNodeComment)(target, this.view.state, this.view.dispatch);
174
173
  popper.destroy();
175
174
  }));
176
175
  }));
@@ -180,9 +179,9 @@ class ContextMenu {
180
179
  const tableElementFooter = (0, prosemirror_utils_1.findChildrenByType)(this.node, transform_1.schema.nodes.table_element_footer);
181
180
  let isDeletedOrRejected = false;
182
181
  const hasGeneralNote = tableElementFooter.length &&
183
- (0, utils_1.getChildOfType)(tableElementFooter[0].node, transform_1.schema.nodes.general_table_footnote, true);
182
+ ((_a = tableElementFooter[0].node.firstChild) === null || _a === void 0 ? void 0 : _a.type) === transform_1.schema.nodes.paragraph;
184
183
  if (hasGeneralNote) {
185
- const generalFootnote = (_a = tableElementFooter[0]) === null || _a === void 0 ? void 0 : _a.node.firstChild;
184
+ const generalFootnote = (_b = tableElementFooter[0]) === null || _b === void 0 ? void 0 : _b.node.firstChild;
186
185
  if (generalFootnote) {
187
186
  isDeletedOrRejected =
188
187
  (0, track_changes_utils_1.isDeleted)(generalFootnote) || (0, track_changes_utils_1.isRejectedInsert)(generalFootnote);
@@ -379,6 +378,18 @@ class ContextMenu {
379
378
  this.trimTitle = (title, max) => {
380
379
  return title.length > max ? title.substring(0, max) + '…' : title;
381
380
  };
381
+ this.getCommentTarget = () => {
382
+ if (this.node.type === transform_1.schema.nodes.section_title) {
383
+ const $pos = this.resolvePos();
384
+ const parent = $pos.parent;
385
+ if (parent.type === transform_1.schema.nodes.keywords) {
386
+ const groups = (0, prosemirror_utils_1.findChildrenByType)(parent, transform_1.schema.nodes.keyword_group);
387
+ return groups.length ? groups[0].node : this.node;
388
+ }
389
+ return parent;
390
+ }
391
+ return this.node;
392
+ };
382
393
  this.node = node;
383
394
  this.view = view;
384
395
  this.getPos = getPos;
package/dist/cjs/menus.js CHANGED
@@ -215,8 +215,8 @@ const getEditorMenus = (editor) => {
215
215
  {
216
216
  id: 'insert-comment',
217
217
  label: 'Comment',
218
- isEnabled: isCommandValid(commands_1.addComment),
219
- run: doCommand(commands_1.addComment),
218
+ isEnabled: isCommandValid(commands_1.addInlineComment),
219
+ run: doCommand(commands_1.addInlineComment),
220
220
  },
221
221
  ],
222
222
  };
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearCommentSelection = exports.setCommentSelection = exports.commentsKey = void 0;
4
+ const transform_1 = require("@manuscripts/transform");
5
+ const prosemirror_state_1 = require("prosemirror-state");
6
+ const prosemirror_utils_1 = require("prosemirror-utils");
7
+ const prosemirror_view_1 = require("prosemirror-view");
8
+ const comments_1 = require("../lib/comments");
9
+ exports.commentsKey = new prosemirror_state_1.PluginKey('comments');
10
+ const COMMENT_SELECTION = 'comment-selection';
11
+ const EMPTY_SELECTION = {};
12
+ exports.default = () => {
13
+ return new prosemirror_state_1.Plugin({
14
+ key: exports.commentsKey,
15
+ state: {
16
+ init: (_, state) => {
17
+ return buildPluginState(state.doc);
18
+ },
19
+ apply: (tr, value) => {
20
+ if (tr.docChanged || tr.getMeta(COMMENT_SELECTION)) {
21
+ const selection = tr.getMeta(COMMENT_SELECTION) || value.selection;
22
+ return buildPluginState(tr.doc, selection === EMPTY_SELECTION ? undefined : selection);
23
+ }
24
+ return value;
25
+ },
26
+ },
27
+ props: {
28
+ decorations: (state) => { var _a; return (_a = exports.commentsKey.getState(state)) === null || _a === void 0 ? void 0 : _a.decorations; },
29
+ handleClick: (view, pos, e) => {
30
+ const state = view.state;
31
+ const com = exports.commentsKey.getState(state);
32
+ const target = e.target;
33
+ const marker = target.closest('[data-key]');
34
+ if (!marker && !(com === null || com === void 0 ? void 0 : com.selection)) {
35
+ return;
36
+ }
37
+ const tr = state.tr;
38
+ if (marker) {
39
+ const key = marker.dataset.key;
40
+ (0, exports.setCommentSelection)(tr, key, undefined, false);
41
+ }
42
+ else {
43
+ (0, exports.clearCommentSelection)(tr);
44
+ }
45
+ view.dispatch(tr);
46
+ },
47
+ },
48
+ });
49
+ };
50
+ const setCommentSelection = (tr, key, id, isNew) => {
51
+ tr.setMeta(COMMENT_SELECTION, {
52
+ key,
53
+ id,
54
+ isNew,
55
+ });
56
+ tr.setMeta('origin', exports.commentsKey);
57
+ return tr;
58
+ };
59
+ exports.setCommentSelection = setCommentSelection;
60
+ const clearCommentSelection = (tr) => {
61
+ tr.setMeta(COMMENT_SELECTION, EMPTY_SELECTION);
62
+ tr.setMeta('origin', exports.commentsKey);
63
+ return tr;
64
+ };
65
+ exports.clearCommentSelection = clearCommentSelection;
66
+ const findCommentRanges = (doc) => {
67
+ const nodes = (0, prosemirror_utils_1.findChildrenByType)(doc, transform_1.schema.nodes.highlight_marker);
68
+ const ranges = new Map();
69
+ let start;
70
+ for (const { node, pos } of nodes) {
71
+ const attrs = node.attrs;
72
+ const position = attrs.position;
73
+ if (position === 'point') {
74
+ ranges.set(attrs.id, {
75
+ pos: pos + 1,
76
+ size: 0,
77
+ });
78
+ }
79
+ else if (position === 'start') {
80
+ start = pos + 1;
81
+ }
82
+ else if (position === 'end' && start) {
83
+ ranges.set(attrs.id, {
84
+ pos: start,
85
+ size: pos - start,
86
+ });
87
+ start = undefined;
88
+ }
89
+ }
90
+ return ranges;
91
+ };
92
+ const findComments = (doc) => {
93
+ const nodes = (0, prosemirror_utils_1.findChildrenByType)(doc, transform_1.schema.nodes.comment);
94
+ const commentsByTarget = new Map();
95
+ for (const { node } of nodes) {
96
+ const attrs = node.attrs;
97
+ const targetID = attrs.target;
98
+ const comments = commentsByTarget.get(targetID) || [];
99
+ commentsByTarget.set(targetID, [...comments, attrs]);
100
+ }
101
+ return commentsByTarget;
102
+ };
103
+ const buildPluginState = (doc, selection) => {
104
+ const commentsByTarget = findComments(doc);
105
+ const ranges = findCommentRanges(doc);
106
+ const decorations = [];
107
+ const allComments = [];
108
+ doc.descendants((node, pos) => {
109
+ const id = node.attrs.id;
110
+ const comments = commentsByTarget.get(id);
111
+ if (!comments || !comments.length) {
112
+ return;
113
+ }
114
+ const target = {
115
+ node,
116
+ pos,
117
+ };
118
+ const nodeComments = [];
119
+ const highlightComments = [];
120
+ const pointComments = [];
121
+ for (const attrs of comments) {
122
+ const id = attrs.id;
123
+ const range = ranges.get(id);
124
+ const key = (0, comments_1.getCommentKey)(attrs, range, node);
125
+ const comment = {
126
+ key,
127
+ attrs,
128
+ target,
129
+ range,
130
+ };
131
+ allComments.push(comment);
132
+ if (!comment.range) {
133
+ nodeComments.push(comment);
134
+ }
135
+ else if (comment.range.size) {
136
+ highlightComments.push(comment);
137
+ }
138
+ else {
139
+ pointComments.push(comment);
140
+ }
141
+ }
142
+ for (const comment of highlightComments) {
143
+ const key = comment.key;
144
+ const range = comment.range;
145
+ decorations.push(createHighlightDecoration(key, range, selection));
146
+ }
147
+ if (pointComments.length) {
148
+ groupByKey(pointComments).forEach((comments, key) => {
149
+ const count = comments.length;
150
+ const pos = comments[0].range.pos;
151
+ decorations.push(createMarkerDecoration(key, 'span', pos, count, selection));
152
+ });
153
+ }
154
+ if (nodeComments.length) {
155
+ const count = nodeComments.length;
156
+ const key = nodeComments[0].key;
157
+ const tagName = node.isAtom ? 'span' : 'div';
158
+ const dpos = getDecorationPos(node, pos);
159
+ decorations.push(createMarkerDecoration(key, tagName, dpos, count, selection));
160
+ }
161
+ });
162
+ return {
163
+ decorations: prosemirror_view_1.DecorationSet.create(doc, decorations),
164
+ comments: new Map(allComments.map((c) => [c.attrs.id, c])),
165
+ commentsByKey: groupByKey(allComments),
166
+ selection,
167
+ };
168
+ };
169
+ const groupByKey = (comments) => {
170
+ const map = new Map();
171
+ for (const comment of comments) {
172
+ const key = comment.key;
173
+ const value = map.get(key) || [];
174
+ map.set(key, [...value, comment]);
175
+ }
176
+ return map;
177
+ };
178
+ const getDecorationPos = (node, pos) => {
179
+ switch (node.type) {
180
+ case transform_1.schema.nodes.keywords:
181
+ return pos + 2;
182
+ default:
183
+ return pos + 1;
184
+ }
185
+ };
186
+ const createHighlightDecoration = (key, range, selection) => {
187
+ const classNames = ['highlight'];
188
+ if (key === (selection === null || selection === void 0 ? void 0 : selection.key)) {
189
+ classNames.push('selected-comment');
190
+ }
191
+ return prosemirror_view_1.Decoration.inline(range.pos, range.pos + range.size, {
192
+ nodeName: 'span',
193
+ class: classNames.join(' '),
194
+ 'data-key': key,
195
+ });
196
+ };
197
+ const createMarkerDecoration = (key, tagName, pos, count, selection) => {
198
+ const isSelected = (selection === null || selection === void 0 ? void 0 : selection.key) === key;
199
+ const toDOM = () => {
200
+ const marker = (0, comments_1.createCommentMarker)(tagName, key, count);
201
+ if (isSelected) {
202
+ marker.classList.add('selected-comment');
203
+ }
204
+ return marker;
205
+ };
206
+ return prosemirror_view_1.Decoration.widget(pos, toDOM, {
207
+ key: `${key}-${count}-${isSelected}`,
208
+ });
209
+ };