@manuscripts/body-editor 3.7.16 → 3.7.18

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.changeIndentation = exports.unindentParagraph = exports.unindentSection = exports.indentParagraph = exports.indentSection = exports.isIndentationAllowed = exports.promoteParagraphToSection = exports.demoteSectionToParagraph = exports.findSelectedOption = exports.titleCase = exports.optionName = exports.shouldSkipNode = void 0;
3
+ exports.changeIndentation = exports.unindentParagraph = exports.unindentSection = exports.indentParagraph = exports.indentSection = exports.isIndentationAllowed = exports.promoteParagraphToSection = exports.demoteSectionToParagraph = exports.getDeepestSubsectionPosition = exports.findSelectedOption = exports.titleCase = exports.optionName = exports.shouldSkipNode = void 0;
4
4
  const track_changes_plugin_1 = require("@manuscripts/track-changes-plugin");
5
5
  const transform_1 = require("@manuscripts/transform");
6
6
  const prosemirror_model_1 = require("prosemirror-model");
@@ -44,6 +44,7 @@ const getDeepestSubsectionPosition = (subsection, position) => {
44
44
  }
45
45
  return position + subsection.content.size;
46
46
  };
47
+ exports.getDeepestSubsectionPosition = getDeepestSubsectionPosition;
47
48
  const getInsertDataTracked = (node) => {
48
49
  const dataTracked = node.attrs.dataTracked;
49
50
  const change = dataTracked?.find((c) => c.operation === 'insert');
@@ -84,7 +85,7 @@ const demoteSectionToParagraph = (state, dispatch, view) => {
84
85
  if (previousNode && (0, transform_1.isSectionNodeType)(previousNode.type)) {
85
86
  const hasSubsections = previousNode.lastChild && (0, transform_1.isSectionNodeType)(previousNode.lastChild.type);
86
87
  if (hasSubsections) {
87
- beforeSection = getDeepestSubsectionPosition(previousNode, beforeSection - previousNode.nodeSize);
88
+ beforeSection = (0, exports.getDeepestSubsectionPosition)(previousNode, beforeSection - previousNode.nodeSize);
88
89
  }
89
90
  else {
90
91
  beforeSection = beforeSection - 1;
@@ -21,6 +21,7 @@ const prosemirror_model_1 = require("prosemirror-model");
21
21
  const prosemirror_state_1 = require("prosemirror-state");
22
22
  const prosemirror_utils_1 = require("prosemirror-utils");
23
23
  const url_1 = require("./url");
24
+ const helpers_1 = require("../components/toolbar/helpers");
24
25
  const removeFirstParagraphIfEmpty = (slice) => {
25
26
  const firstChild = slice.content.firstChild;
26
27
  if (firstChild &&
@@ -71,22 +72,54 @@ const transformPasted = (slice) => {
71
72
  };
72
73
  exports.transformPasted = transformPasted;
73
74
  const transformPastedHTML = (html) => {
74
- if (html.includes('table')) {
75
- const doc = new DOMParser().parseFromString(html, 'text/html');
76
- doc.querySelectorAll('table').forEach((table) => {
77
- if (table.parentElement?.tagName !== 'figure') {
78
- const tableElement = document.createElement('figure');
79
- tableElement.className = 'table';
80
- table.removeAttribute('data-pm-slice');
81
- table.parentElement?.insertBefore(tableElement, table);
82
- tableElement.append(table);
75
+ const doc = new DOMParser().parseFromString(html, 'text/html');
76
+ wrapHeadingWithSection(doc);
77
+ wrapTableWithFigure(doc);
78
+ console.log(doc.body.innerHTML);
79
+ return doc.body.innerHTML;
80
+ };
81
+ exports.transformPastedHTML = transformPastedHTML;
82
+ const wrapHeadingWithSection = (doc) => {
83
+ if (!doc.body.querySelector('h1, h2, h3, h4, h5, h6')) {
84
+ return;
85
+ }
86
+ const getHeadingLevel = (element) => {
87
+ const level = element.tagName.match(/^H(\d)$/);
88
+ return level ? parseInt(level[1]) : -1;
89
+ };
90
+ const root = doc.createElement('div');
91
+ const google_doc_document = doc.body.querySelector('b[id^="docs-internal-guid"]');
92
+ const elements = Array.from(google_doc_document?.children || doc.body.children);
93
+ const stack = [{ level: 0, element: root }];
94
+ for (const element of elements) {
95
+ const headingLevel = getHeadingLevel(element);
96
+ if (headingLevel > 0) {
97
+ while (stack.length > 1 &&
98
+ stack[stack.length - 1].level >= headingLevel) {
99
+ stack.pop();
83
100
  }
84
- });
85
- return doc.body.innerHTML;
101
+ const section = doc.createElement('section');
102
+ section.appendChild(element.cloneNode(true));
103
+ stack[stack.length - 1].element.appendChild(section);
104
+ stack.push({ level: headingLevel, element: section });
105
+ }
106
+ else {
107
+ stack[stack.length - 1].element.appendChild(element.cloneNode(true));
108
+ }
86
109
  }
87
- return html;
110
+ doc.body.innerHTML = root.innerHTML;
111
+ };
112
+ const wrapTableWithFigure = (doc) => {
113
+ doc.body.querySelectorAll('table').forEach((table) => {
114
+ if (table.parentElement?.tagName !== 'FIGURE') {
115
+ const tableElement = doc.createElement('figure');
116
+ tableElement.className = 'table';
117
+ table.removeAttribute('data-pm-slice');
118
+ table.parentElement?.insertBefore(tableElement, table);
119
+ tableElement.append(table);
120
+ }
121
+ });
88
122
  };
89
- exports.transformPastedHTML = transformPastedHTML;
90
123
  const handlePaste = (view, event, slice) => {
91
124
  if (event.type !== 'paste') {
92
125
  return false;
@@ -101,12 +134,27 @@ const handlePaste = (view, event, slice) => {
101
134
  dispatch(tr.insert(selection.from, prosemirror_model_1.Fragment.from(link)).scrollIntoView());
102
135
  return true;
103
136
  }
104
- const parent = (0, prosemirror_utils_1.findParentNode)((node) => node.type === transform_1.schema.nodes.section)(tr.selection);
105
- if (slice.content.firstChild?.type === transform_1.schema.nodes.section && parent) {
106
- const $pos = tr.doc.resolve(parent.start);
107
- const insertPos = $pos.after($pos.depth);
137
+ const parent = (0, prosemirror_utils_1.findParentNode)((node) => node.type === transform_1.schema.nodes.section || node.type === transform_1.schema.nodes.body)(selection);
138
+ if ((slice.content.firstChild?.type === transform_1.schema.nodes.section ||
139
+ slice.content.lastChild?.type === transform_1.schema.nodes.section) &&
140
+ parent) {
141
+ const $pos = tr.doc.resolve((0, helpers_1.getDeepestSubsectionPosition)(parent.node, parent.pos));
142
+ const insertPos = $pos.end();
108
143
  tr.insert(insertPos, slice.content);
109
- dispatch(tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, insertPos)).scrollIntoView());
144
+ dispatch(tr
145
+ .setSelection(prosemirror_state_1.NodeSelection.create(tr.doc, tr.steps[0]['to'] || insertPos))
146
+ .scrollIntoView());
147
+ return true;
148
+ }
149
+ if ((slice.content.firstChild?.type === transform_1.schema.nodes.body ||
150
+ slice.content.lastChild?.type === transform_1.schema.nodes.backmatter) &&
151
+ parent) {
152
+ const $pos = tr.doc.resolve((0, helpers_1.getDeepestSubsectionPosition)(parent.node, parent.pos));
153
+ const insertPos = $pos.end();
154
+ tr.insert(insertPos, slice.content.firstChild.content.append(slice.content.lastChild.content));
155
+ dispatch(tr
156
+ .setSelection(prosemirror_state_1.NodeSelection.create(tr.doc, tr.doc.resolve(insertPos).after()))
157
+ .scrollIntoView());
110
158
  return true;
111
159
  }
112
160
  if (selection instanceof prosemirror_state_1.TextSelection &&
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MATHJAX_VERSION = exports.VERSION = void 0;
4
- exports.VERSION = '3.7.16';
4
+ exports.VERSION = '3.7.18';
5
5
  exports.MATHJAX_VERSION = '3.2.2';
@@ -25,7 +25,7 @@ export const findSelectedOption = (options) => {
25
25
  }
26
26
  }
27
27
  };
28
- const getDeepestSubsectionPosition = (subsection, position) => {
28
+ export const getDeepestSubsectionPosition = (subsection, position) => {
29
29
  while (subsection.lastChild && isSectionNodeType(subsection.lastChild.type)) {
30
30
  if (isDeleted(subsection.lastChild)) {
31
31
  return (position +
@@ -15,9 +15,10 @@
15
15
  */
16
16
  import { generateNodeID, isElementNodeType, schema, } from '@manuscripts/transform';
17
17
  import { Fragment, Slice } from 'prosemirror-model';
18
- import { TextSelection } from 'prosemirror-state';
18
+ import { NodeSelection, TextSelection } from 'prosemirror-state';
19
19
  import { findParentNode } from 'prosemirror-utils';
20
20
  import { allowedHref } from './url';
21
+ import { getDeepestSubsectionPosition } from '../components/toolbar/helpers';
21
22
  const removeFirstParagraphIfEmpty = (slice) => {
22
23
  const firstChild = slice.content.firstChild;
23
24
  if (firstChild &&
@@ -67,20 +68,52 @@ export const transformPasted = (slice) => {
67
68
  return slice;
68
69
  };
69
70
  export const transformPastedHTML = (html) => {
70
- if (html.includes('table')) {
71
- const doc = new DOMParser().parseFromString(html, 'text/html');
72
- doc.querySelectorAll('table').forEach((table) => {
73
- if (table.parentElement?.tagName !== 'figure') {
74
- const tableElement = document.createElement('figure');
75
- tableElement.className = 'table';
76
- table.removeAttribute('data-pm-slice');
77
- table.parentElement?.insertBefore(tableElement, table);
78
- tableElement.append(table);
71
+ const doc = new DOMParser().parseFromString(html, 'text/html');
72
+ wrapHeadingWithSection(doc);
73
+ wrapTableWithFigure(doc);
74
+ console.log(doc.body.innerHTML);
75
+ return doc.body.innerHTML;
76
+ };
77
+ const wrapHeadingWithSection = (doc) => {
78
+ if (!doc.body.querySelector('h1, h2, h3, h4, h5, h6')) {
79
+ return;
80
+ }
81
+ const getHeadingLevel = (element) => {
82
+ const level = element.tagName.match(/^H(\d)$/);
83
+ return level ? parseInt(level[1]) : -1;
84
+ };
85
+ const root = doc.createElement('div');
86
+ const google_doc_document = doc.body.querySelector('b[id^="docs-internal-guid"]');
87
+ const elements = Array.from(google_doc_document?.children || doc.body.children);
88
+ const stack = [{ level: 0, element: root }];
89
+ for (const element of elements) {
90
+ const headingLevel = getHeadingLevel(element);
91
+ if (headingLevel > 0) {
92
+ while (stack.length > 1 &&
93
+ stack[stack.length - 1].level >= headingLevel) {
94
+ stack.pop();
79
95
  }
80
- });
81
- return doc.body.innerHTML;
96
+ const section = doc.createElement('section');
97
+ section.appendChild(element.cloneNode(true));
98
+ stack[stack.length - 1].element.appendChild(section);
99
+ stack.push({ level: headingLevel, element: section });
100
+ }
101
+ else {
102
+ stack[stack.length - 1].element.appendChild(element.cloneNode(true));
103
+ }
82
104
  }
83
- return html;
105
+ doc.body.innerHTML = root.innerHTML;
106
+ };
107
+ const wrapTableWithFigure = (doc) => {
108
+ doc.body.querySelectorAll('table').forEach((table) => {
109
+ if (table.parentElement?.tagName !== 'FIGURE') {
110
+ const tableElement = doc.createElement('figure');
111
+ tableElement.className = 'table';
112
+ table.removeAttribute('data-pm-slice');
113
+ table.parentElement?.insertBefore(tableElement, table);
114
+ tableElement.append(table);
115
+ }
116
+ });
84
117
  };
85
118
  export const handlePaste = (view, event, slice) => {
86
119
  if (event.type !== 'paste') {
@@ -96,12 +129,27 @@ export const handlePaste = (view, event, slice) => {
96
129
  dispatch(tr.insert(selection.from, Fragment.from(link)).scrollIntoView());
97
130
  return true;
98
131
  }
99
- const parent = findParentNode((node) => node.type === schema.nodes.section)(tr.selection);
100
- if (slice.content.firstChild?.type === schema.nodes.section && parent) {
101
- const $pos = tr.doc.resolve(parent.start);
102
- const insertPos = $pos.after($pos.depth);
132
+ const parent = findParentNode((node) => node.type === schema.nodes.section || node.type === schema.nodes.body)(selection);
133
+ if ((slice.content.firstChild?.type === schema.nodes.section ||
134
+ slice.content.lastChild?.type === schema.nodes.section) &&
135
+ parent) {
136
+ const $pos = tr.doc.resolve(getDeepestSubsectionPosition(parent.node, parent.pos));
137
+ const insertPos = $pos.end();
103
138
  tr.insert(insertPos, slice.content);
104
- dispatch(tr.setSelection(TextSelection.create(tr.doc, insertPos)).scrollIntoView());
139
+ dispatch(tr
140
+ .setSelection(NodeSelection.create(tr.doc, tr.steps[0]['to'] || insertPos))
141
+ .scrollIntoView());
142
+ return true;
143
+ }
144
+ if ((slice.content.firstChild?.type === schema.nodes.body ||
145
+ slice.content.lastChild?.type === schema.nodes.backmatter) &&
146
+ parent) {
147
+ const $pos = tr.doc.resolve(getDeepestSubsectionPosition(parent.node, parent.pos));
148
+ const insertPos = $pos.end();
149
+ tr.insert(insertPos, slice.content.firstChild.content.append(slice.content.lastChild.content));
150
+ dispatch(tr
151
+ .setSelection(NodeSelection.create(tr.doc, tr.doc.resolve(insertPos).after()))
152
+ .scrollIntoView());
105
153
  return true;
106
154
  }
107
155
  if (selection instanceof TextSelection &&
@@ -1,2 +1,2 @@
1
- export const VERSION = '3.7.16';
1
+ export const VERSION = '3.7.18';
2
2
  export const MATHJAX_VERSION = '3.2.2';
@@ -8,6 +8,7 @@ export declare const shouldSkipNode: (node: Node) => boolean;
8
8
  export declare const optionName: (nodeType: ManuscriptNodeType) => string;
9
9
  export declare const titleCase: (text: string) => string;
10
10
  export declare const findSelectedOption: (options: Option[]) => Option | undefined;
11
+ export declare const getDeepestSubsectionPosition: (subsection: Node, position: number) => number;
11
12
  export declare const demoteSectionToParagraph: (state: EditorState, dispatch: (tr: Transaction) => void, view?: ManuscriptEditorView) => void;
12
13
  export declare const promoteParagraphToSection: (state: EditorState, dispatch: (tr: Transaction) => void, view?: ManuscriptEditorView) => void;
13
14
  export declare const isIndentationAllowed: (action: string) => (state: EditorState) => boolean;
@@ -1,6 +1,6 @@
1
1
  import { CSSObjectWithLabel } from 'react-select';
2
2
  import { Option } from './TypeSelector';
3
- export declare const StyledSelect: import("styled-components").StyledComponent<(props: Omit<import("react-select/dist/declarations/src/Select").PublicBaseSelectProps<Option, false, import("react-select").GroupBase<Option>>, "value" | "onChange" | "inputValue" | "menuIsOpen" | "onInputChange" | "onMenuOpen" | "onMenuClose"> & Partial<import("react-select/dist/declarations/src/Select").PublicBaseSelectProps<Option, false, import("react-select").GroupBase<Option>>> & import("react-select/dist/declarations/src/useStateManager").StateManagerAdditionalProps<Option> & import("react").RefAttributes<import("react-select/dist/declarations/src/Select").default<Option, false, import("react-select").GroupBase<Option>>>) => import("react").ReactElement, any, {}, never>;
3
+ export declare const StyledSelect: import("styled-components").StyledComponent<(props: Omit<import("react-select/dist/declarations/src/Select").PublicBaseSelectProps<Option, false, import("react-select").GroupBase<Option>>, "onChange" | "inputValue" | "menuIsOpen" | "onInputChange" | "onMenuOpen" | "onMenuClose" | "value"> & Partial<import("react-select/dist/declarations/src/Select").PublicBaseSelectProps<Option, false, import("react-select").GroupBase<Option>>> & import("react-select/dist/declarations/src/useStateManager").StateManagerAdditionalProps<Option> & import("react").RefAttributes<import("react-select/dist/declarations/src/Select").default<Option, false, import("react-select").GroupBase<Option>>>) => import("react").ReactElement, any, {}, never>;
4
4
  export declare const customStyles: {
5
5
  control: (styles: CSSObjectWithLabel) => CSSObjectWithLabel;
6
6
  indicatorSeparator: () => CSSObjectWithLabel;
@@ -1,3 +1,3 @@
1
1
  import { DropTargetMonitor } from 'react-dnd';
2
2
  export type DropSide = 'before' | 'after';
3
- export declare const getDropSide: (element: Element, monitor: DropTargetMonitor) => "before" | "after";
3
+ export declare const getDropSide: (element: Element, monitor: DropTargetMonitor) => "after" | "before";
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "3.7.16";
1
+ export declare const VERSION = "3.7.18";
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.7.16",
4
+ "version": "3.7.18",
5
5
  "repository": "github:Atypon-OpenSource/manuscripts-body-editor",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/cjs",
@@ -39,7 +39,7 @@
39
39
  "@iarna/word-count": "1.1.2",
40
40
  "@manuscripts/json-schema": "2.2.12",
41
41
  "@manuscripts/style-guide": "3.3.10",
42
- "@manuscripts/track-changes-plugin": "2.2.1",
42
+ "@manuscripts/track-changes-plugin": "2.2.2",
43
43
  "@manuscripts/transform": "4.3.12",
44
44
  "@popperjs/core": "2.11.8",
45
45
  "citeproc": "2.4.63",