@manuscripts/body-editor 1.6.10 → 1.6.12

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 (95) hide show
  1. package/dist/cjs/commands.js +57 -1
  2. package/dist/cjs/components/toolbar/LevelSelector.js +2 -2
  3. package/dist/cjs/index.js +4 -9
  4. package/dist/cjs/keys/list.js +9 -9
  5. package/dist/cjs/lib/context-menu.js +81 -163
  6. package/dist/cjs/menus.js +353 -405
  7. package/dist/cjs/toolbar.js +30 -34
  8. package/dist/cjs/views/editable_block.js +12 -4
  9. package/dist/es/commands.js +57 -2
  10. package/dist/es/components/toolbar/LevelSelector.js +2 -2
  11. package/dist/es/index.js +2 -4
  12. package/dist/es/keys/list.js +7 -7
  13. package/dist/es/lib/context-menu.js +81 -163
  14. package/dist/es/menus.js +353 -405
  15. package/dist/es/toolbar.js +31 -35
  16. package/dist/es/views/editable_block.js +13 -5
  17. package/dist/types/commands.d.ts +1 -0
  18. package/dist/types/components/toolbar/LevelSelector.d.ts +2 -2
  19. package/dist/types/configs/editor-views.d.ts +14 -14
  20. package/dist/types/index.d.ts +2 -4
  21. package/dist/types/keys/list.d.ts +1 -1
  22. package/dist/types/lib/context-menu.d.ts +0 -7
  23. package/dist/types/menus.d.ts +2 -3
  24. package/dist/types/toolbar.d.ts +18 -1
  25. package/dist/types/views/bibliography_element_editable.d.ts +1 -1
  26. package/dist/types/views/blockquote_element_editable.d.ts +1 -1
  27. package/dist/types/views/bullet_list_editable.d.ts +1 -1
  28. package/dist/types/views/editable_block.d.ts +1 -1
  29. package/dist/types/views/equation_element_editable.d.ts +1 -1
  30. package/dist/types/views/figure_element_editable.d.ts +1 -1
  31. package/dist/types/views/footnote_editable.d.ts +1 -1
  32. package/dist/types/views/footnotes_element_editable.d.ts +1 -1
  33. package/dist/types/views/listing_element_editable.d.ts +1 -1
  34. package/dist/types/views/ordered_list_editable.d.ts +1 -1
  35. package/dist/types/views/paragraph_editable.d.ts +1 -1
  36. package/dist/types/views/placeholder_element_editable.d.ts +1 -1
  37. package/dist/types/views/pullquote_element_editable.d.ts +1 -1
  38. package/dist/types/views/section_title_editable.d.ts +1 -1
  39. package/dist/types/views/table_element_editable.d.ts +1 -1
  40. package/dist/types/views/toc_element_editable.d.ts +1 -1
  41. package/package.json +3 -5
  42. package/dist/cjs/components/application-menu/ApplicationMenu.js +0 -63
  43. package/dist/cjs/components/application-menu/MenuItemContainer.js +0 -127
  44. package/dist/cjs/components/application-menu/Shortcut.js +0 -63
  45. package/dist/cjs/components/application-menu/index.js +0 -22
  46. package/dist/cjs/components/application-menu/menuState.js +0 -49
  47. package/dist/cjs/components/application-menu/types.js +0 -17
  48. package/dist/cjs/components/application-menu/useApplicationMenus.js +0 -154
  49. package/dist/cjs/components/dialog/TableCellBorderOptions.js +0 -64
  50. package/dist/cjs/components/dialog/TableCellOptions.js +0 -50
  51. package/dist/cjs/components/dialog/TableOptions.js +0 -31
  52. package/dist/cjs/components/dialog/commands.js +0 -185
  53. package/dist/cjs/components/dialog/index.js +0 -26
  54. package/dist/cjs/components/dialog/shared.js +0 -48
  55. package/dist/cjs/components/dialog/types.js +0 -9
  56. package/dist/cjs/components/toolbar/ListStyleSelector.js +0 -150
  57. package/dist/cjs/components/toolbar/ManuscriptToolbar.js +0 -104
  58. package/dist/cjs/components/toolbar/OrderListSelector.js +0 -103
  59. package/dist/cjs/views/EditableBlock.js +0 -37
  60. package/dist/es/components/application-menu/ApplicationMenu.js +0 -56
  61. package/dist/es/components/application-menu/MenuItemContainer.js +0 -120
  62. package/dist/es/components/application-menu/Shortcut.js +0 -56
  63. package/dist/es/components/application-menu/index.js +0 -17
  64. package/dist/es/components/application-menu/menuState.js +0 -44
  65. package/dist/es/components/application-menu/types.js +0 -16
  66. package/dist/es/components/application-menu/useApplicationMenus.js +0 -148
  67. package/dist/es/components/dialog/TableCellBorderOptions.js +0 -36
  68. package/dist/es/components/dialog/TableCellOptions.js +0 -22
  69. package/dist/es/components/dialog/TableOptions.js +0 -26
  70. package/dist/es/components/dialog/commands.js +0 -177
  71. package/dist/es/components/dialog/index.js +0 -18
  72. package/dist/es/components/dialog/shared.js +0 -21
  73. package/dist/es/components/dialog/types.js +0 -6
  74. package/dist/es/components/toolbar/ListStyleSelector.js +0 -120
  75. package/dist/es/components/toolbar/ManuscriptToolbar.js +0 -97
  76. package/dist/es/components/toolbar/OrderListSelector.js +0 -72
  77. package/dist/es/views/EditableBlock.js +0 -29
  78. package/dist/types/components/application-menu/ApplicationMenu.d.ts +0 -24
  79. package/dist/types/components/application-menu/MenuItemContainer.d.ts +0 -27
  80. package/dist/types/components/application-menu/Shortcut.d.ts +0 -26
  81. package/dist/types/components/application-menu/index.d.ts +0 -18
  82. package/dist/types/components/application-menu/menuState.d.ts +0 -18
  83. package/dist/types/components/application-menu/types.d.ts +0 -47
  84. package/dist/types/components/application-menu/useApplicationMenus.d.ts +0 -12
  85. package/dist/types/components/dialog/TableCellBorderOptions.d.ts +0 -9
  86. package/dist/types/components/dialog/TableCellOptions.d.ts +0 -4
  87. package/dist/types/components/dialog/TableOptions.d.ts +0 -4
  88. package/dist/types/components/dialog/commands.d.ts +0 -28
  89. package/dist/types/components/dialog/index.d.ts +0 -4
  90. package/dist/types/components/dialog/shared.d.ts +0 -16
  91. package/dist/types/components/dialog/types.d.ts +0 -16
  92. package/dist/types/components/toolbar/ListStyleSelector.d.ts +0 -36
  93. package/dist/types/components/toolbar/ManuscriptToolbar.d.ts +0 -50
  94. package/dist/types/components/toolbar/OrderListSelector.d.ts +0 -14
  95. package/dist/types/views/EditableBlock.d.ts +0 -19
@@ -14,7 +14,6 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { isInBibliographySection, nodeNames, schema, } from '@manuscripts/transform';
17
- import { Fragment, Slice } from 'prosemirror-model';
18
17
  import { addComment, createBlock } from '../commands';
19
18
  import { PopperManager } from './popper';
20
19
  const popper = new PopperManager();
@@ -28,118 +27,96 @@ export const sectionLevel = (depth) => {
28
27
  return 'Sub' + 'sub'.repeat(depth - 2) + 'section';
29
28
  }
30
29
  };
30
+ const hasAny = (set, ...items) => {
31
+ return items.some((i) => set.has(i));
32
+ };
31
33
  export class ContextMenu {
32
34
  constructor(node, view, getPos, actions = {}) {
33
- this.suppressibleAttrs = new Map([
34
- ['suppressHeader', 'Header'],
35
- ['suppressFooter', 'Footer'],
36
- ]);
37
35
  this.showAddMenu = (target, after) => {
38
- let _after = after;
39
- const { nodes } = this.view.state.schema;
40
36
  const menu = document.createElement('div');
41
37
  menu.className = 'menu';
42
38
  const $pos = this.resolvePos();
43
- _after = isInBibliographySection($pos) ? false : _after;
44
- const insertPos = _after ? $pos.after($pos.depth) : $pos.before($pos.depth);
39
+ if (isInBibliographySection($pos)) {
40
+ after = false;
41
+ }
42
+ const insertPos = after ? $pos.after($pos.depth) : $pos.before($pos.depth);
45
43
  const endPos = $pos.end();
46
- const insertableTypes = this.insertableTypes(_after, insertPos, endPos);
47
- if (this.showMenuSection(insertableTypes, ['section', 'subsection'])) {
44
+ const types = this.insertableTypes(after, insertPos, endPos);
45
+ const insertNode = (type, pos) => {
46
+ const { state, dispatch } = this.view;
47
+ if (pos === undefined) {
48
+ pos = after ? this.getPos() + this.node.nodeSize : this.getPos();
49
+ }
50
+ createBlock(type, pos, state, dispatch);
51
+ };
52
+ if (hasAny(types, 'section', 'subsection')) {
48
53
  menu.appendChild(this.createMenuSection((section) => {
49
- const labelPosition = _after ? 'After' : 'Before';
50
54
  const sectionTitle = $pos.node($pos.depth).child(0).textContent;
51
55
  const itemTitle = sectionTitle
52
56
  ? `“${this.trimTitle(sectionTitle, 30)}”`
53
57
  : 'This Section';
54
- const itemLabel = `New ${sectionLevel($pos.depth - 1)} ${labelPosition} ${itemTitle}`;
55
- if (insertableTypes.section) {
56
- section.appendChild(this.createMenuItem(itemLabel, () => {
57
- this.addBlock(nodes.section, _after, insertPos);
58
+ if (types.has('section')) {
59
+ const labelPosition = after ? 'After' : 'Before';
60
+ const level = sectionLevel($pos.depth - 1);
61
+ const label = `New ${level} ${labelPosition} ${itemTitle}`;
62
+ section.appendChild(this.createMenuItem(label, () => {
63
+ insertNode(schema.nodes.section, insertPos);
58
64
  popper.destroy();
59
65
  }));
60
66
  }
61
- if (insertableTypes.subsection) {
62
- const subItemLabel = `New ${sectionLevel($pos.depth)} to ${itemTitle}`;
63
- section.appendChild(this.createMenuItem(subItemLabel, () => {
64
- this.addBlock(nodes.section, _after, endPos);
67
+ if (types.has('subsection')) {
68
+ const level = sectionLevel($pos.depth);
69
+ const label = `New ${level} to ${itemTitle}`;
70
+ section.appendChild(this.createMenuItem(label, () => {
71
+ insertNode(schema.nodes.section, endPos);
65
72
  popper.destroy();
66
73
  }));
67
74
  }
68
75
  }));
69
76
  }
70
- if (this.showMenuSection(insertableTypes, [
71
- 'paragraph',
72
- 'orderedList',
73
- 'bulletList',
74
- ])) {
77
+ if (hasAny(types, 'paragraph', 'ordered_list', 'bullet_list')) {
75
78
  menu.appendChild(this.createMenuSection((section) => {
76
- if (insertableTypes.paragraphElement) {
79
+ if (types.has('paragraph')) {
77
80
  section.appendChild(this.createMenuItem('Paragraph', () => {
78
- this.addBlock(nodes.paragraph, _after);
81
+ insertNode(schema.nodes.paragraph);
79
82
  popper.destroy();
80
83
  }));
81
84
  }
82
- if (insertableTypes.orderedList) {
85
+ if (types.has('ordered_list')) {
83
86
  section.appendChild(this.createMenuItem('Numbered List', () => {
84
- this.addBlock(nodes.ordered_list, _after);
87
+ insertNode(schema.nodes.ordered_list);
85
88
  popper.destroy();
86
89
  }));
87
90
  }
88
- if (insertableTypes.bulletList) {
91
+ if (types.has('bullet_list')) {
89
92
  section.appendChild(this.createMenuItem('Bullet List', () => {
90
- this.addBlock(nodes.bullet_list, _after);
93
+ insertNode(schema.nodes.bullet_list);
91
94
  popper.destroy();
92
95
  }));
93
96
  }
94
97
  }));
95
98
  }
96
- if (this.showMenuSection(insertableTypes, [
97
- 'figure',
98
- 'tableElement',
99
- 'equationElement',
100
- 'listingElement',
101
- ])) {
99
+ if (hasAny(types, 'figure_element', 'equation_element')) {
102
100
  menu.appendChild(this.createMenuSection((section) => {
103
- if (insertableTypes.figureElement) {
101
+ if (types.has('figure_element')) {
104
102
  section.appendChild(this.createMenuItem('Figure Panel', () => {
105
- this.addBlock(nodes.figure_element, _after);
103
+ insertNode(schema.nodes.figure_element);
106
104
  popper.destroy();
107
105
  }));
108
106
  }
109
- if (insertableTypes.tableElement) {
110
- section.appendChild(this.createMenuItem('Table', () => {
111
- this.addBlock(nodes.table_element, _after);
112
- popper.destroy();
113
- }));
114
- }
115
- if (insertableTypes.equationElement) {
107
+ if (types.has('equation_element')) {
116
108
  section.appendChild(this.createMenuItem('Equation', () => {
117
- this.addBlock(nodes.equation_element, _after);
118
- popper.destroy();
119
- }));
120
- }
121
- if (insertableTypes.listingElement) {
122
- section.appendChild(this.createMenuItem('Listing', () => {
123
- this.addBlock(nodes.listing_element, _after);
109
+ insertNode(schema.nodes.equation_element);
124
110
  popper.destroy();
125
111
  }));
126
112
  }
127
113
  }));
128
114
  }
129
- if (this.showMenuSection(insertableTypes, [
130
- 'blockquoteElement',
131
- 'pullquoteElement',
132
- ])) {
115
+ if (hasAny(types, 'blockquote_element')) {
133
116
  menu.appendChild(this.createMenuSection((section) => {
134
- if (insertableTypes.blockquoteElement) {
117
+ if (types.has('blockquote_element')) {
135
118
  section.appendChild(this.createMenuItem('Block Quote', () => {
136
- this.addBlock(nodes.blockquote_element, _after);
137
- popper.destroy();
138
- }));
139
- }
140
- if (insertableTypes.pullquoteElement) {
141
- section.appendChild(this.createMenuItem('Pull Quote', () => {
142
- this.addBlock(nodes.pullquote_element, _after);
119
+ insertNode(schema.nodes.blockquote_element);
143
120
  popper.destroy();
144
121
  }));
145
122
  }
@@ -156,19 +133,18 @@ export class ContextMenu {
156
133
  const menu = document.createElement('div');
157
134
  menu.className = 'menu';
158
135
  const $pos = this.resolvePos();
159
- const nodeType = this.node.type;
160
- const nodes = schema.nodes;
161
- if (listTypes.includes(nodeType)) {
136
+ const type = this.node.type;
137
+ if (listTypes.includes(type)) {
162
138
  menu.appendChild(this.createMenuSection((section) => {
163
- if (nodeType === nodes.bullet_list) {
139
+ if (type === schema.nodes.bullet_list) {
164
140
  section.appendChild(this.createMenuItem('Change to Numbered List', () => {
165
- this.changeNodeType(nodes.ordered_list);
141
+ this.changeNodeType(schema.nodes.ordered_list);
166
142
  popper.destroy();
167
143
  }));
168
144
  }
169
- if (nodeType === nodes.ordered_list) {
145
+ if (type === schema.nodes.ordered_list) {
170
146
  section.appendChild(this.createMenuItem('Change to Bullet List', () => {
171
- this.changeNodeType(nodes.bullet_list);
147
+ this.changeNodeType(schema.nodes.bullet_list);
172
148
  popper.destroy();
173
149
  }));
174
150
  }
@@ -184,34 +160,12 @@ export class ContextMenu {
184
160
  }));
185
161
  }));
186
162
  }
187
- const suppressOptions = this.buildSuppressOptions();
188
- if (suppressOptions.length) {
189
- menu.appendChild(this.createMenuSection((section) => {
190
- for (const option of suppressOptions) {
191
- const label = option.attrs[option.attr]
192
- ? `Show ${option.label}`
193
- : `Hide ${option.label}`;
194
- section.appendChild(this.createMenuItem(label, () => {
195
- this.toggleNodeAttr(option);
196
- popper.destroy();
197
- }));
198
- }
199
- }));
200
- }
201
- if (nodeType === nodes.paragraph && $pos.parent.type === nodes.section) {
202
- menu.appendChild(this.createMenuSection((section) => {
203
- section.appendChild(this.createMenuItem(`Split to New ${sectionLevel($pos.depth)}`, () => {
204
- this.splitSection();
205
- popper.destroy();
206
- }));
207
- }));
208
- }
209
- if (!readonlyTypes.includes(nodeType) &&
163
+ if (!readonlyTypes.includes(type) &&
210
164
  !readonlyTypes.includes($pos.parent.type)) {
211
165
  menu.appendChild(this.createMenuSection((section) => {
212
- const nodeName = nodeNames.get(nodeType) || '';
166
+ const nodeName = nodeNames.get(type) || '';
213
167
  section.appendChild(this.createMenuItem(`Delete ${nodeName}`, () => {
214
- this.deleteNode(nodeType);
168
+ this.deleteNode(type);
215
169
  popper.destroy();
216
170
  }));
217
171
  }));
@@ -223,22 +177,6 @@ export class ContextMenu {
223
177
  });
224
178
  this.addPopperEventListeners();
225
179
  };
226
- this.addBlock = (nodeType, after, position) => {
227
- const { state, dispatch } = this.view;
228
- if (position === undefined) {
229
- position = after ? this.getPos() + this.node.nodeSize : this.getPos();
230
- }
231
- createBlock(nodeType, position, state, dispatch);
232
- };
233
- this.canAddBlock = (nodeType, after, position) => {
234
- const { state: { doc }, } = this.view;
235
- if (position === undefined) {
236
- position = after ? this.getPos() + this.node.nodeSize : this.getPos();
237
- }
238
- const $position = doc.resolve(position);
239
- const index = $position.index();
240
- return $position.parent.canReplaceWith(index, index, nodeType);
241
- };
242
180
  this.createMenuItem = (contents, handler) => {
243
181
  const item = document.createElement('div');
244
182
  item.className = 'menu-item';
@@ -256,22 +194,37 @@ export class ContextMenu {
256
194
  return section;
257
195
  };
258
196
  this.insertableTypes = (after, insertPos, endPos) => {
259
- const { nodes } = this.view.state.schema;
260
- return {
261
- section: this.canAddBlock(nodes.section, after, insertPos),
262
- subsection: this.canAddBlock(nodes.section, after, endPos),
263
- paragraphElement: this.canAddBlock(nodes.paragraph, after),
264
- orderedList: this.canAddBlock(nodes.ordered_list, after),
265
- bulletList: this.canAddBlock(nodes.bullet_list, after),
266
- figureElement: this.canAddBlock(nodes.figure_element, after),
267
- tableElement: this.canAddBlock(nodes.table_element, after),
268
- equationElement: this.canAddBlock(nodes.equation_element, after),
269
- listingElement: this.canAddBlock(nodes.listing_element, after),
270
- blockquoteElement: this.canAddBlock(nodes.blockquote_element, after),
271
- pullquoteElement: this.canAddBlock(nodes.pullquote_element, after),
197
+ const { nodes } = schema;
198
+ const insertable = new Set();
199
+ const doc = this.view.state.doc;
200
+ const getPos = (pos) => {
201
+ if (pos === undefined) {
202
+ pos = after ? this.getPos() + this.node.nodeSize : this.getPos();
203
+ }
204
+ return pos;
205
+ };
206
+ const canInsertAt = (type, pos) => {
207
+ const $pos = doc.resolve(getPos(pos));
208
+ const index = $pos.index();
209
+ return $pos.parent.canReplaceWith(index, index, type);
210
+ };
211
+ const checkNode = (node, pos) => {
212
+ canInsertAt(nodes[node], pos) && insertable.add(node);
272
213
  };
214
+ if (canInsertAt(nodes.section, endPos)) {
215
+ insertable.add('subsection');
216
+ }
217
+ checkNode('section', insertPos);
218
+ checkNode('paragraph');
219
+ checkNode('ordered_list');
220
+ checkNode('bullet_list');
221
+ checkNode('figure_element');
222
+ checkNode('table_element');
223
+ checkNode('equation_element');
224
+ checkNode('blockquote_element');
225
+ checkNode('pullquote_element');
226
+ return insertable;
273
227
  };
274
- this.showMenuSection = (insertableTypes, types) => types.some((type) => insertableTypes[type]);
275
228
  this.changeNodeType = (nodeType) => {
276
229
  this.view.dispatch(this.view.state.tr.setNodeMarkup(this.getPos(), nodeType, {
277
230
  id: this.node.attrs.id,
@@ -300,41 +253,6 @@ export class ContextMenu {
300
253
  }
301
254
  };
302
255
  this.resolvePos = () => this.view.state.doc.resolve(this.getPos());
303
- this.splitSection = () => {
304
- const { schema, tr } = this.view.state;
305
- const from = this.getPos();
306
- const to = from + this.node.nodeSize;
307
- const slice = new Slice(Fragment.from([
308
- schema.nodes.section.create(),
309
- schema.nodes.section.create({}, [
310
- schema.nodes.section_title.create(),
311
- this.node,
312
- ]),
313
- ]), 1, 1);
314
- this.view.dispatch(tr.replaceRange(from, to, slice));
315
- };
316
- this.toggleNodeAttr = (option) => {
317
- const { getPos, attr, attrs } = option;
318
- this.view.dispatch(this.view.state.tr.setNodeMarkup(getPos(), undefined, Object.assign(Object.assign({}, attrs), { [attr]: !attrs[attr] })));
319
- };
320
- this.buildSuppressOptions = () => {
321
- const items = [];
322
- let attrs = this.node.attrs;
323
- if (this.node.attrs.suppressTitle === undefined) {
324
- attrs = Object.assign(this.node.attrs, { suppressTitle: false });
325
- }
326
- for (const [attr, label] of this.suppressibleAttrs.entries()) {
327
- if (attr in attrs) {
328
- items.push({
329
- attr,
330
- attrs,
331
- label,
332
- getPos: this.getPos,
333
- });
334
- }
335
- }
336
- return items;
337
- };
338
256
  this.addPopperEventListeners = () => {
339
257
  const mouseListener = () => {
340
258
  window.requestAnimationFrame(() => {
@@ -352,7 +270,7 @@ export class ContextMenu {
352
270
  window.addEventListener('keydown', keyListener);
353
271
  };
354
272
  this.trimTitle = (title, max) => {
355
- return title.length > max ? title.substr(0, max) + '…' : title;
273
+ return title.length > max ? title.substring(0, max) + '…' : title;
356
274
  };
357
275
  this.node = node;
358
276
  this.view = view;