@atlaskit/editor-core 187.34.2 → 187.35.1

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 (96) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/index.js +10 -42
  3. package/dist/cjs/plugins/block-type/commands/block-type.js +19 -211
  4. package/dist/cjs/plugins/block-type/commands/index.js +2 -28
  5. package/dist/cjs/plugins/block-type/index.js +21 -4
  6. package/dist/cjs/plugins/block-type/pm-plugins/input-rule.js +9 -64
  7. package/dist/cjs/plugins/block-type/pm-plugins/keymap.js +1 -1
  8. package/dist/cjs/plugins/code-block/actions.js +83 -6
  9. package/dist/cjs/plugins/code-block/index.js +27 -12
  10. package/dist/cjs/plugins/code-block/pm-plugins/input-rule.js +68 -0
  11. package/dist/cjs/plugins/date/ui/DatePicker/index.js +3 -2
  12. package/dist/cjs/plugins/expand/commands.js +3 -3
  13. package/dist/cjs/plugins/expand/index.js +2 -2
  14. package/dist/cjs/plugins/index.js +1 -7
  15. package/dist/cjs/plugins/insert-block/index.js +34 -29
  16. package/dist/cjs/plugins/media/ui/ResizableMediaSingle/ResizableMediaSingleNext.js +36 -26
  17. package/dist/cjs/plugins/panel/actions.js +26 -2
  18. package/dist/cjs/plugins/panel/index.js +9 -5
  19. package/dist/cjs/version-wrapper.js +1 -1
  20. package/dist/es2019/index.js +16 -2
  21. package/dist/es2019/plugins/block-type/commands/block-type.js +15 -217
  22. package/dist/es2019/plugins/block-type/commands/index.js +1 -3
  23. package/dist/es2019/plugins/block-type/index.js +21 -4
  24. package/dist/es2019/plugins/block-type/pm-plugins/input-rule.js +3 -56
  25. package/dist/es2019/plugins/block-type/pm-plugins/keymap.js +2 -2
  26. package/dist/es2019/plugins/code-block/actions.js +84 -3
  27. package/dist/es2019/plugins/code-block/index.js +22 -7
  28. package/dist/es2019/plugins/code-block/pm-plugins/input-rule.js +60 -0
  29. package/dist/es2019/plugins/date/ui/DatePicker/index.js +3 -2
  30. package/dist/es2019/plugins/expand/commands.js +1 -1
  31. package/dist/es2019/plugins/expand/index.js +1 -1
  32. package/dist/es2019/plugins/index.js +0 -1
  33. package/dist/es2019/plugins/insert-block/index.js +47 -42
  34. package/dist/es2019/plugins/media/ui/ResizableMediaSingle/ResizableMediaSingleNext.js +35 -26
  35. package/dist/es2019/plugins/panel/actions.js +26 -2
  36. package/dist/es2019/plugins/panel/index.js +8 -4
  37. package/dist/es2019/version-wrapper.js +1 -1
  38. package/dist/esm/index.js +16 -2
  39. package/dist/esm/plugins/block-type/commands/block-type.js +17 -205
  40. package/dist/esm/plugins/block-type/commands/index.js +1 -3
  41. package/dist/esm/plugins/block-type/index.js +21 -4
  42. package/dist/esm/plugins/block-type/pm-plugins/input-rule.js +3 -58
  43. package/dist/esm/plugins/block-type/pm-plugins/keymap.js +2 -2
  44. package/dist/esm/plugins/code-block/actions.js +76 -3
  45. package/dist/esm/plugins/code-block/index.js +25 -11
  46. package/dist/esm/plugins/code-block/pm-plugins/input-rule.js +62 -0
  47. package/dist/esm/plugins/date/ui/DatePicker/index.js +3 -2
  48. package/dist/esm/plugins/expand/commands.js +1 -1
  49. package/dist/esm/plugins/expand/index.js +1 -1
  50. package/dist/esm/plugins/index.js +0 -1
  51. package/dist/esm/plugins/insert-block/index.js +27 -22
  52. package/dist/esm/plugins/media/ui/ResizableMediaSingle/ResizableMediaSingleNext.js +37 -27
  53. package/dist/esm/plugins/panel/actions.js +24 -2
  54. package/dist/esm/plugins/panel/index.js +8 -4
  55. package/dist/esm/version-wrapper.js +1 -1
  56. package/dist/types/index.d.ts +16 -3
  57. package/dist/types/labs/next/presets/default.d.ts +6 -0
  58. package/dist/types/plugins/block-type/commands/block-type.d.ts +1 -25
  59. package/dist/types/plugins/block-type/commands/index.d.ts +1 -3
  60. package/dist/types/plugins/block-type/index.d.ts +10 -2
  61. package/dist/types/plugins/code-block/actions.d.ts +11 -0
  62. package/dist/types/plugins/code-block/index.d.ts +6 -1
  63. package/dist/types/plugins/code-block/pm-plugins/input-rule.d.ts +3 -0
  64. package/dist/types/plugins/date/ui/DatePicker/index.d.ts +2 -2
  65. package/dist/types/plugins/index.d.ts +0 -2
  66. package/dist/types/plugins/insert-block/types.d.ts +7 -1
  67. package/dist/types/plugins/media/ui/ResizableMediaSingle/ResizableMediaSingleNext.d.ts +1 -3
  68. package/dist/types/plugins/panel/actions.d.ts +4 -1
  69. package/dist/types/plugins/panel/index.d.ts +11 -2
  70. package/dist/types-ts4.5/index.d.ts +16 -3
  71. package/dist/types-ts4.5/labs/next/presets/default.d.ts +6 -0
  72. package/dist/types-ts4.5/plugins/block-type/commands/block-type.d.ts +1 -25
  73. package/dist/types-ts4.5/plugins/block-type/commands/index.d.ts +1 -3
  74. package/dist/types-ts4.5/plugins/block-type/index.d.ts +10 -2
  75. package/dist/types-ts4.5/plugins/code-block/actions.d.ts +11 -0
  76. package/dist/types-ts4.5/plugins/code-block/index.d.ts +6 -1
  77. package/dist/types-ts4.5/plugins/code-block/pm-plugins/input-rule.d.ts +3 -0
  78. package/dist/types-ts4.5/plugins/date/ui/DatePicker/index.d.ts +2 -2
  79. package/dist/types-ts4.5/plugins/index.d.ts +0 -2
  80. package/dist/types-ts4.5/plugins/insert-block/types.d.ts +7 -1
  81. package/dist/types-ts4.5/plugins/media/ui/ResizableMediaSingle/ResizableMediaSingleNext.d.ts +1 -3
  82. package/dist/types-ts4.5/plugins/panel/actions.d.ts +4 -1
  83. package/dist/types-ts4.5/plugins/panel/index.d.ts +9 -2
  84. package/package.json +3 -3
  85. package/report.api.md +62 -23
  86. package/tmp/api-report-tmp.d.ts +49 -15
  87. package/dist/cjs/plugins/block-type/commands/insert-block.js +0 -60
  88. package/dist/es2019/plugins/block-type/commands/insert-block.js +0 -55
  89. package/dist/esm/plugins/block-type/commands/insert-block.js +0 -53
  90. package/dist/types/plugins/block-type/commands/insert-block.d.ts +0 -5
  91. package/dist/types-ts4.5/plugins/block-type/commands/insert-block.d.ts +0 -5
  92. /package/dist/cjs/plugins/{block-type/commands → code-block}/transform-to-code-block.js +0 -0
  93. /package/dist/es2019/plugins/{block-type/commands → code-block}/transform-to-code-block.js +0 -0
  94. /package/dist/esm/plugins/{block-type/commands → code-block}/transform-to-code-block.js +0 -0
  95. /package/dist/types/plugins/{block-type/commands → code-block}/transform-to-code-block.d.ts +0 -0
  96. /package/dist/types-ts4.5/plugins/{block-type/commands → code-block}/transform-to-code-block.d.ts +0 -0
@@ -1,16 +1,11 @@
1
1
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
2
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
3
3
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
4
- import { findWrapping } from '@atlaskit/editor-prosemirror/transform';
5
- import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
6
- import { PanelType } from '@atlaskit/adf-schema';
7
4
  import { CellSelection } from '@atlaskit/editor-tables';
8
- import { shouldSplitSelectedNodeOnNodeInsertion } from '@atlaskit/editor-common/insert';
9
5
  import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
10
6
  import { withAnalytics } from '@atlaskit/editor-common/editor-analytics';
11
- import { filterChildrenBetween, removeBlockMarks } from '@atlaskit/editor-common/utils';
12
- import { CODE_BLOCK, BLOCK_QUOTE, PANEL, HEADINGS_BY_NAME, NORMAL_TEXT } from '../types';
13
- import { transformToCodeBlockAction } from './transform-to-code-block';
7
+ import { filterChildrenBetween, wrapSelectionIn } from '@atlaskit/editor-common/utils';
8
+ import { HEADINGS_BY_NAME, NORMAL_TEXT } from '../types';
14
9
  export function setBlockType(name) {
15
10
  return function (state, dispatch) {
16
11
  var nodes = state.schema.nodes;
@@ -137,25 +132,11 @@ export var setHeadingWithAnalytics = function setHeadingWithAnalytics(newHeading
137
132
  })(setHeading(newHeadingLevel));
138
133
  });
139
134
  };
140
- export function insertBlockType(name) {
135
+ function insertBlockQuote() {
141
136
  return function (state, dispatch) {
142
137
  var nodes = state.schema.nodes;
143
- switch (name) {
144
- case BLOCK_QUOTE.name:
145
- if (nodes.paragraph && nodes.blockquote) {
146
- return wrapSelectionIn(nodes.blockquote)(state, dispatch);
147
- }
148
- break;
149
- case CODE_BLOCK.name:
150
- if (nodes.codeBlock) {
151
- return insertCodeBlock()(state, dispatch);
152
- }
153
- break;
154
- case PANEL.name:
155
- if (nodes.panel && nodes.paragraph) {
156
- return wrapSelectionIn(nodes.panel)(state, dispatch);
157
- }
158
- break;
138
+ if (nodes.paragraph && nodes.blockquote) {
139
+ return wrapSelectionIn(nodes.blockquote)(state, dispatch);
159
140
  }
160
141
  return false;
161
142
  };
@@ -169,189 +150,20 @@ export function insertBlockType(name) {
169
150
  * otherwise Editor becomes very sad and crashes
170
151
  * @returns - command that inserts block type
171
152
  */
172
- export var insertBlockTypesWithAnalytics = function insertBlockTypesWithAnalytics(name, inputMethod, editorAnalyticsApi) {
173
- switch (name) {
174
- case BLOCK_QUOTE.name:
175
- return withAnalytics(editorAnalyticsApi, {
176
- action: ACTION.FORMATTED,
177
- actionSubject: ACTION_SUBJECT.TEXT,
178
- eventType: EVENT_TYPE.TRACK,
179
- actionSubjectId: ACTION_SUBJECT_ID.FORMAT_BLOCK_QUOTE,
180
- attributes: {
181
- inputMethod: inputMethod
182
- }
183
- })(insertBlockType(name));
184
- case CODE_BLOCK.name:
185
- return withAnalytics(editorAnalyticsApi, {
186
- action: ACTION.INSERTED,
187
- actionSubject: ACTION_SUBJECT.DOCUMENT,
188
- actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
189
- attributes: {
190
- inputMethod: inputMethod
191
- },
192
- eventType: EVENT_TYPE.TRACK
193
- })(insertBlockType(name));
194
- case PANEL.name:
195
- return withAnalytics(editorAnalyticsApi, {
196
- action: ACTION.INSERTED,
197
- actionSubject: ACTION_SUBJECT.DOCUMENT,
198
- actionSubjectId: ACTION_SUBJECT_ID.PANEL,
199
- attributes: {
200
- inputMethod: inputMethod,
201
- panelType: PanelType.INFO // only info panels can be inserted from toolbar
202
- },
203
-
204
- eventType: EVENT_TYPE.TRACK
205
- })(insertBlockType(name));
206
- default:
207
- return insertBlockType(name);
208
- }
209
- };
210
-
211
- /**
212
- * This function creates a new transaction that wraps the current selection
213
- * in the specified node type if it results in a valid transaction.
214
- * If not valid, it performs a safe insert operation.
215
- *
216
- * Example of when wrapping might not be valid is when attempting to wrap
217
- * content that is already inside a panel with another panel
218
- */
219
- export function createWrapSelectionTransaction(_ref3) {
220
- var state = _ref3.state,
221
- type = _ref3.type,
222
- nodeAttributes = _ref3.nodeAttributes;
223
- var tr = state.tr;
224
- var _state$schema$marks = state.schema.marks,
225
- alignment = _state$schema$marks.alignment,
226
- indentation = _state$schema$marks.indentation;
227
-
228
- /** Alignment or Indentation is not valid inside block types */
229
- var removeAlignTr = removeBlockMarks(state, [alignment, indentation]);
230
- tr = removeAlignTr || tr;
231
-
232
- /**Get range and wrapping needed for the selection*/
233
- var _getWrappingOptions = getWrappingOptions(state, type, nodeAttributes),
234
- range = _getWrappingOptions.range,
235
- wrapping = _getWrappingOptions.wrapping;
236
- if (wrapping) {
237
- tr.wrap(range, wrapping).scrollIntoView();
238
- } else {
239
- /** We always want to append a block type */
240
- safeInsert(type.createAndFill(nodeAttributes))(tr).scrollIntoView();
241
- }
242
- return tr;
243
- }
244
- function getWrappingOptions(state, type, nodeAttributes) {
245
- var _state$selection = state.selection,
246
- $from = _state$selection.$from,
247
- $to = _state$selection.$to;
248
- var range = $from.blockRange($to);
249
- var isAllowedChild = true;
250
- /**
251
- * Added a check to avoid wrapping codeblock
252
- */
253
- if (state.selection.empty) {
254
- state.doc.nodesBetween($from.pos, $to.pos, function (node) {
255
- if (!isAllowedChild) {
256
- return false;
257
- }
258
- return isAllowedChild = node.type !== state.schema.nodes.codeBlock;
259
- });
260
- }
261
- var wrapping = isAllowedChild && range && findWrapping(range, type, nodeAttributes);
262
- return {
263
- range: range,
264
- wrapping: wrapping
265
- };
266
- }
267
-
268
- /**
269
- * Function will add wrapping node.
270
- * 1. If currently selected blocks can be wrapped in the wrapper type it will wrap them.
271
- * 2. If current block can not be wrapped inside wrapping block it will create a new block below selection,
272
- * and set selection on it.
273
- */
274
- function wrapSelectionIn(type) {
275
- return function (state, dispatch) {
276
- var tr = createWrapSelectionTransaction({
277
- state: state,
278
- type: type
279
- });
280
- if (dispatch) {
281
- dispatch(tr);
282
- }
283
- return true;
284
- };
285
- }
286
-
287
- /**
288
- * This function creates a new transaction that inserts a code block,
289
- * if there is text selected it will wrap the current selection if not it will
290
- * append the codeblock to the end of the document.
291
- */
292
- export function createInsertCodeBlockTransaction(_ref4) {
293
- var _state$selection$$fro;
294
- var state = _ref4.state;
295
- var tr = state.tr;
296
- var from = state.selection.from;
297
- var codeBlock = state.schema.nodes.codeBlock;
298
- var grandParentNodeType = (_state$selection$$fro = state.selection.$from.node(-1)) === null || _state$selection$$fro === void 0 ? void 0 : _state$selection$$fro.type;
299
- var parentNodeType = state.selection.$from.parent.type;
300
-
301
- /** We always want to append a codeBlock unless we're inserting into a paragraph
302
- * AND it's a valid child of the grandparent node.
303
- * Insert the current selection as codeBlock content unless it contains nodes other
304
- * than paragraphs and inline.
305
- */
306
- var canInsertCodeBlock = shouldSplitSelectedNodeOnNodeInsertion({
307
- parentNodeType: parentNodeType,
308
- grandParentNodeType: grandParentNodeType,
309
- content: codeBlock.createAndFill()
310
- }) && contentAllowedInCodeBlock(state);
311
- if (canInsertCodeBlock) {
312
- tr = transformToCodeBlockAction(state, from);
313
- } else {
314
- safeInsert(codeBlock.createAndFill())(tr).scrollIntoView();
315
- }
316
- return tr;
317
- }
318
-
319
- /**
320
- * Function will insert code block at current selection if block is empty or below current selection and set focus on it.
321
- */
322
- function insertCodeBlock() {
323
- return function (state, dispatch) {
324
- var tr = createInsertCodeBlockTransaction({
325
- state: state
326
- });
327
- if (dispatch) {
328
- dispatch(tr);
153
+ export var insertBlockQuoteWithAnalytics = function insertBlockQuoteWithAnalytics(inputMethod, editorAnalyticsApi) {
154
+ return withAnalytics(editorAnalyticsApi, {
155
+ action: ACTION.FORMATTED,
156
+ actionSubject: ACTION_SUBJECT.TEXT,
157
+ eventType: EVENT_TYPE.TRACK,
158
+ actionSubjectId: ACTION_SUBJECT_ID.FORMAT_BLOCK_QUOTE,
159
+ attributes: {
160
+ inputMethod: inputMethod
329
161
  }
330
- return true;
331
- };
332
- }
333
-
334
- /**
335
- * Check if the current selection contains any nodes that are not permitted
336
- * as codeBlock child nodes. Note that this allows paragraphs and inline nodes
337
- * as we extract their text content.
338
- */
339
- function contentAllowedInCodeBlock(state) {
340
- var _state$selection2 = state.selection,
341
- $from = _state$selection2.$from,
342
- $to = _state$selection2.$to;
343
- var isAllowedChild = true;
344
- state.doc.nodesBetween($from.pos, $to.pos, function (node) {
345
- if (!isAllowedChild) {
346
- return false;
347
- }
348
- return isAllowedChild = node.type === state.schema.nodes.listItem || node.type === state.schema.nodes.bulletList || node.type === state.schema.nodes.orderedList || node.type === state.schema.nodes.paragraph || node.isInline || node.isText;
349
- });
350
- return isAllowedChild;
351
- }
162
+ })(insertBlockQuote());
163
+ };
352
164
  export var cleanUpAtTheStartOfDocument = function cleanUpAtTheStartOfDocument(state, dispatch) {
353
- var _ref5 = state.selection,
354
- $cursor = _ref5.$cursor;
165
+ var _ref3 = state.selection,
166
+ $cursor = _ref3.$cursor;
355
167
  if ($cursor && !$cursor.nodeBefore && !$cursor.nodeAfter && $cursor.pos === 1) {
356
168
  var tr = state.tr,
357
169
  schema = state.schema;
@@ -1,5 +1,3 @@
1
- export { cleanUpAtTheStartOfDocument, insertBlockType, insertBlockTypesWithAnalytics, setBlockType, setBlockTypeWithAnalytics, setHeading, setHeadingWithAnalytics, setNormalText, setNormalTextWithAnalytics } from './block-type';
2
- export { insertBlock } from './insert-block';
3
- export { isConvertableToCodeBlock, transformToCodeBlockAction } from './transform-to-code-block';
1
+ export { cleanUpAtTheStartOfDocument, insertBlockQuoteWithAnalytics, setBlockType, setBlockTypeWithAnalytics, setHeading, setHeadingWithAnalytics, setNormalText, setNormalTextWithAnalytics } from './block-type';
4
2
  export { deleteAndMoveCursor } from './delete-and-move-cursor';
5
3
  export { deleteBlockContent } from './delete-block-content';
@@ -12,6 +12,7 @@ import inputRulePlugin from './pm-plugins/input-rule';
12
12
  import ToolbarBlockType from './ui/ToolbarBlockType';
13
13
  import { setBlockTypeWithAnalytics } from './commands';
14
14
  import { messages } from './messages';
15
+ import { insertBlockQuoteWithAnalytics } from './commands/block-type';
15
16
  var headingPluginOptions = function headingPluginOptions(_ref, isAllowed, editorAnalyticsApi) {
16
17
  var formatMessage = _ref.formatMessage;
17
18
  if (!isAllowed) {
@@ -137,6 +138,22 @@ var blockTypePlugin = function blockTypePlugin(_ref3) {
137
138
  }
138
139
  }];
139
140
  },
141
+ actions: {
142
+ insertBlockQuote: function insertBlockQuote(inputMethod) {
143
+ var _api$analytics4;
144
+ return insertBlockQuoteWithAnalytics(inputMethod, api === null || api === void 0 ? void 0 : (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : _api$analytics4.actions);
145
+ },
146
+ setBlockType: function setBlockType(name, inputMethod) {
147
+ var _api$analytics5;
148
+ return setBlockTypeWithAnalytics(name, inputMethod, api === null || api === void 0 ? void 0 : (_api$analytics5 = api.analytics) === null || _api$analytics5 === void 0 ? void 0 : _api$analytics5.actions);
149
+ }
150
+ },
151
+ getSharedState: function getSharedState(editorState) {
152
+ if (!editorState) {
153
+ return;
154
+ }
155
+ return pluginKey.getState(editorState);
156
+ },
140
157
  primaryToolbarComponent: function primaryToolbarComponent(_ref7) {
141
158
  var editorView = _ref7.editorView,
142
159
  popupsMountPoint = _ref7.popupsMountPoint,
@@ -148,8 +165,8 @@ var blockTypePlugin = function blockTypePlugin(_ref3) {
148
165
  eventDispatcher = _ref7.eventDispatcher;
149
166
  var isSmall = options && options.isUndoRedoButtonsEnabled ? toolbarSize < ToolbarSize.XXL : toolbarSize < ToolbarSize.XL;
150
167
  var boundSetBlockType = function boundSetBlockType(name) {
151
- var _api$analytics4;
152
- return setBlockTypeWithAnalytics(name, INPUT_METHOD.TOOLBAR, api === null || api === void 0 ? void 0 : (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : _api$analytics4.actions)(editorView.state, editorView.dispatch);
168
+ var _api$analytics6;
169
+ return setBlockTypeWithAnalytics(name, INPUT_METHOD.TOOLBAR, api === null || api === void 0 ? void 0 : (_api$analytics6 = api.analytics) === null || _api$analytics6 === void 0 ? void 0 : _api$analytics6.actions)(editorView.state, editorView.dispatch);
153
170
  };
154
171
  return /*#__PURE__*/React.createElement(WithPluginState, {
155
172
  editorView: editorView,
@@ -174,9 +191,9 @@ var blockTypePlugin = function blockTypePlugin(_ref3) {
174
191
  },
175
192
  pluginsOptions: {
176
193
  quickInsert: function quickInsert(intl) {
177
- var _api$analytics5, _api$analytics6;
194
+ var _api$analytics7, _api$analytics8;
178
195
  var exclude = options && options.allowBlockType && options.allowBlockType.exclude ? options.allowBlockType.exclude : [];
179
- return [].concat(_toConsumableArray(blockquotePluginOptions(intl, exclude.indexOf('blockquote') === -1, api === null || api === void 0 ? void 0 : (_api$analytics5 = api.analytics) === null || _api$analytics5 === void 0 ? void 0 : _api$analytics5.actions)), _toConsumableArray(headingPluginOptions(intl, exclude.indexOf('heading') === -1, api === null || api === void 0 ? void 0 : (_api$analytics6 = api.analytics) === null || _api$analytics6 === void 0 ? void 0 : _api$analytics6.actions)));
196
+ return [].concat(_toConsumableArray(blockquotePluginOptions(intl, exclude.indexOf('blockquote') === -1, api === null || api === void 0 ? void 0 : (_api$analytics7 = api.analytics) === null || _api$analytics7 === void 0 ? void 0 : _api$analytics7.actions)), _toConsumableArray(headingPluginOptions(intl, exclude.indexOf('heading') === -1, api === null || api === void 0 ? void 0 : (_api$analytics8 = api.analytics) === null || _api$analytics8 === void 0 ? void 0 : _api$analytics8.actions)));
180
197
  }
181
198
  }
182
199
  };
@@ -1,10 +1,8 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
- import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
3
2
  import { createRule, createPlugin, leafNodeReplacementCharacter } from '@atlaskit/prosemirror-input-rules';
4
3
  import { INPUT_METHOD, ACTION, ACTION_SUBJECT, EVENT_TYPE, ACTION_SUBJECT_ID } from '@atlaskit/editor-common/analytics';
5
4
  import { inputRuleWithAnalytics } from '@atlaskit/editor-common/utils';
6
- import { isConvertableToCodeBlock, transformToCodeBlockAction } from '../commands/transform-to-code-block';
7
- import { insertBlock } from '../commands/insert-block';
5
+ import { insertBlock } from '@atlaskit/editor-common/commands';
8
6
  import { createJoinNodesRule, createWrappingTextBlockRule } from '../utils';
9
7
  var MAX_HEADING_LEVEL = 6;
10
8
  function getHeadingLevel(match) {
@@ -34,7 +32,7 @@ function getHeadingRules(editorAnalyticsAPI, schema) {
34
32
  var hashRule = headingRule(schema.nodes.heading, MAX_HEADING_LEVEL);
35
33
  var leftNodeReplacementHashRule = createRule(new RegExp("".concat(leafNodeReplacementCharacter, "(#{1,6})\\s$")), function (state, match, start, end) {
36
34
  var level = match[1].length;
37
- return insertBlock(state, schema.nodes.heading, "heading".concat(level), start, end, {
35
+ return insertBlock(state, schema.nodes.heading, start, end, {
38
36
  level: level
39
37
  });
40
38
  });
@@ -65,7 +63,7 @@ function getBlockQuoteRules(editorAnalyticsAPI, schema) {
65
63
  // '> ' for blockquote
66
64
  var greatherThanRule = blockQuoteRule(schema.nodes.blockquote);
67
65
  var leftNodeReplacementGreatherRule = createRule(new RegExp("".concat(leafNodeReplacementCharacter, "\\s*>\\s$")), function (state, _match, start, end) {
68
- return insertBlock(state, schema.nodes.blockquote, 'blockquote', start, end);
66
+ return insertBlock(state, schema.nodes.blockquote, start, end);
69
67
  });
70
68
 
71
69
  // Analytics V3 handler
@@ -80,56 +78,6 @@ function getBlockQuoteRules(editorAnalyticsAPI, schema) {
80
78
  }, editorAnalyticsAPI);
81
79
  return [ruleWithBlockQuoteAnalytics(greatherThanRule), ruleWithBlockQuoteAnalytics(leftNodeReplacementGreatherRule)];
82
80
  }
83
-
84
- /**
85
- * Get all code block input rules
86
- *
87
- * @param {Schema} schema
88
- * @returns {InputRuleWithHandler[]}
89
- */
90
- function getCodeBlockRules(editorAnalyticsAPI, schema) {
91
- var ruleAnalytics = inputRuleWithAnalytics({
92
- action: ACTION.INSERTED,
93
- actionSubject: ACTION_SUBJECT.DOCUMENT,
94
- actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
95
- attributes: {
96
- inputMethod: INPUT_METHOD.FORMATTING
97
- },
98
- eventType: EVENT_TYPE.TRACK
99
- }, editorAnalyticsAPI);
100
- var validMatchLength = function validMatchLength(match) {
101
- return match.length > 0 && match[0].length === 3;
102
- };
103
- var threeTildeRule = createRule(/(?!\s)(`{3,})$/, function (state, match, start, end) {
104
- if (!validMatchLength(match)) {
105
- return null;
106
- }
107
- var attributes = {};
108
- if (match[4]) {
109
- attributes.language = match[4];
110
- }
111
- if (isConvertableToCodeBlock(state)) {
112
- return transformToCodeBlockAction(state, start, attributes);
113
- }
114
- var tr = state.tr;
115
- tr.delete(start, end);
116
- var codeBlock = tr.doc.type.schema.nodes.codeBlock.createChecked();
117
- safeInsert(codeBlock)(tr);
118
- return tr;
119
- });
120
- var leftNodeReplacementThreeTildeRule = createRule(new RegExp("((".concat(leafNodeReplacementCharacter, "`{3,})|^\\s(`{3,}))(\\S*)$")), function (state, match, start, end) {
121
- if (!validMatchLength(match)) {
122
- return null;
123
- }
124
- var attributes = {};
125
- if (match[4]) {
126
- attributes.language = match[4];
127
- }
128
- var inlineStart = Math.max(match.index + state.selection.$from.start(), 1);
129
- return insertBlock(state, schema.nodes.codeBlock, 'codeblock', inlineStart, end, attributes);
130
- });
131
- return [ruleAnalytics(threeTildeRule), ruleAnalytics(leftNodeReplacementThreeTildeRule)];
132
- }
133
81
  function inputRulePlugin(editorAnalyticsAPI, schema, featureFlags) {
134
82
  var rules = [];
135
83
  if (schema.nodes.heading) {
@@ -138,9 +86,6 @@ function inputRulePlugin(editorAnalyticsAPI, schema, featureFlags) {
138
86
  if (schema.nodes.blockquote) {
139
87
  rules.push.apply(rules, _toConsumableArray(getBlockQuoteRules(editorAnalyticsAPI, schema)));
140
88
  }
141
- if (schema.nodes.codeBlock) {
142
- rules.push.apply(rules, _toConsumableArray(getCodeBlockRules(editorAnalyticsAPI, schema)));
143
- }
144
89
  if (rules.length !== 0) {
145
90
  return createPlugin('block-type', rules, {
146
91
  isBlockNodeRule: true
@@ -4,7 +4,7 @@ import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
4
4
  import { bindKeymapWithCommand, moveUp, moveDown, findKeyMapForBrowser, undo as undoKeymap, redo as redoKeymap, backspace, deleteKey, forwardDelete, toggleBlockQuote, keymap, insertNewLine, findShortcutByKeymap } from '@atlaskit/editor-common/keymaps';
5
5
  import { insertNewLineWithAnalytics, createNewParagraphAbove, createNewParagraphBelow, deleteEmptyParagraphAndMoveBlockUp } from '@atlaskit/editor-common/utils';
6
6
  import * as blockTypes from '../types';
7
- import { cleanUpAtTheStartOfDocument, deleteAndMoveCursor, deleteBlockContent, insertBlockTypesWithAnalytics } from '../commands';
7
+ import { cleanUpAtTheStartOfDocument, deleteAndMoveCursor, deleteBlockContent, insertBlockQuoteWithAnalytics } from '../commands';
8
8
  import { isNodeAWrappingBlockNode } from '../utils';
9
9
  var backspaceCommand = chainCommands(cleanUpAtTheStartOfDocument, deleteBlockContent(isNodeAWrappingBlockNode), deleteAndMoveCursor);
10
10
  var del = chainCommands(deleteEmptyParagraphAndMoveBlockUp(isNodeAWrappingBlockNode), deleteBlockContent(isNodeAWrappingBlockNode), deleteAndMoveCursor);
@@ -19,7 +19,7 @@ export default function keymapPlugin(editorAnalyticsApi, schema, _featureFlags)
19
19
  bindKeymapWithCommand(deleteKey.common, del, list);
20
20
  bindKeymapWithCommand(forwardDelete.mac, del, list);
21
21
  if (schema.nodes[blockTypes.BLOCK_QUOTE.nodeName]) {
22
- bindKeymapWithCommand(findShortcutByKeymap(toggleBlockQuote), insertBlockTypesWithAnalytics(blockTypes.BLOCK_QUOTE.name, INPUT_METHOD.KEYBOARD, editorAnalyticsApi), list);
22
+ bindKeymapWithCommand(findShortcutByKeymap(toggleBlockQuote), insertBlockQuoteWithAnalytics(INPUT_METHOD.KEYBOARD, editorAnalyticsApi), list);
23
23
  }
24
24
  return keymap(list);
25
25
  }
@@ -1,11 +1,14 @@
1
- import { findParentNodeOfType, findSelectedNodeOfType, removeSelectedNode, removeParentNodeOfType, isNodeSelection } from '@atlaskit/editor-prosemirror/utils';
1
+ import { findParentNodeOfType, findSelectedNodeOfType, removeSelectedNode, removeParentNodeOfType, isNodeSelection, safeInsert } from '@atlaskit/editor-prosemirror/utils';
2
2
  import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
3
3
  import { pluginKey } from './plugin-key';
4
4
  import { copySelectionPluginKey } from './pm-plugins/codeBlockCopySelectionPlugin';
5
5
  import { ACTIONS } from './pm-plugins/actions';
6
6
  import { copyToClipboard } from '../../utils/clipboard';
7
7
  import { addAnalytics } from '../analytics/utils';
8
- import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '../analytics/types';
8
+ import { shouldSplitSelectedNodeOnNodeInsertion } from '@atlaskit/editor-common/insert';
9
+ import { transformToCodeBlockAction } from './transform-to-code-block';
10
+ import { withAnalytics } from '@atlaskit/editor-common/editor-analytics';
11
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
9
12
  export var removeCodeBlock = function removeCodeBlock(state, dispatch) {
10
13
  var nodes = state.schema.nodes,
11
14
  tr = state.tr;
@@ -113,4 +116,74 @@ export var resetShouldIgnoreFollowingMutations = function resetShouldIgnoreFollo
113
116
  dispatch(ignoreFollowingMutationsTr);
114
117
  }
115
118
  return true;
116
- };
119
+ };
120
+
121
+ /**
122
+ * This function creates a new transaction that inserts a code block,
123
+ * if there is text selected it will wrap the current selection if not it will
124
+ * append the codeblock to the end of the document.
125
+ */
126
+ export function createInsertCodeBlockTransaction(_ref) {
127
+ var _state$selection$$fro;
128
+ var state = _ref.state;
129
+ var tr = state.tr;
130
+ var from = state.selection.from;
131
+ var codeBlock = state.schema.nodes.codeBlock;
132
+ var grandParentNodeType = (_state$selection$$fro = state.selection.$from.node(-1)) === null || _state$selection$$fro === void 0 ? void 0 : _state$selection$$fro.type;
133
+ var parentNodeType = state.selection.$from.parent.type;
134
+
135
+ /** We always want to append a codeBlock unless we're inserting into a paragraph
136
+ * AND it's a valid child of the grandparent node.
137
+ * Insert the current selection as codeBlock content unless it contains nodes other
138
+ * than paragraphs and inline.
139
+ */
140
+ var canInsertCodeBlock = shouldSplitSelectedNodeOnNodeInsertion({
141
+ parentNodeType: parentNodeType,
142
+ grandParentNodeType: grandParentNodeType,
143
+ content: codeBlock.createAndFill()
144
+ }) && contentAllowedInCodeBlock(state);
145
+ if (canInsertCodeBlock) {
146
+ tr = transformToCodeBlockAction(state, from);
147
+ } else {
148
+ safeInsert(codeBlock.createAndFill())(tr).scrollIntoView();
149
+ }
150
+ return tr;
151
+ }
152
+
153
+ /**
154
+ * Check if the current selection contains any nodes that are not permitted
155
+ * as codeBlock child nodes. Note that this allows paragraphs and inline nodes
156
+ * as we extract their text content.
157
+ */
158
+ function contentAllowedInCodeBlock(state) {
159
+ var _state$selection = state.selection,
160
+ $from = _state$selection.$from,
161
+ $to = _state$selection.$to;
162
+ var isAllowedChild = true;
163
+ state.doc.nodesBetween($from.pos, $to.pos, function (node) {
164
+ if (!isAllowedChild) {
165
+ return false;
166
+ }
167
+ return isAllowedChild = node.type === state.schema.nodes.listItem || node.type === state.schema.nodes.bulletList || node.type === state.schema.nodes.orderedList || node.type === state.schema.nodes.paragraph || node.isInline || node.isText;
168
+ });
169
+ return isAllowedChild;
170
+ }
171
+ export function insertCodeBlockWithAnalytics(inputMethod, analyticsAPI) {
172
+ return withAnalytics(analyticsAPI, {
173
+ action: ACTION.INSERTED,
174
+ actionSubject: ACTION_SUBJECT.DOCUMENT,
175
+ actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
176
+ attributes: {
177
+ inputMethod: inputMethod
178
+ },
179
+ eventType: EVENT_TYPE.TRACK
180
+ })(function (state, dispatch) {
181
+ var tr = createInsertCodeBlockTransaction({
182
+ state: state
183
+ });
184
+ if (dispatch) {
185
+ dispatch(tr);
186
+ }
187
+ return true;
188
+ });
189
+ }
@@ -12,10 +12,8 @@ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, INPUT_METHOD, EVENT_TYPE } f
12
12
  import { IconCode } from '@atlaskit/editor-common/quick-insert';
13
13
  import { messages } from '../block-type/messages';
14
14
  import refreshBrowserSelectionOnChange from './refresh-browser-selection';
15
- // Theres an existing interelationship between these files, where the imported function is being called for code-block
16
- // Insertions via the drop down menu
17
- // tslint-ignore-next-line
18
- import { createInsertCodeBlockTransaction } from '../block-type/commands/block-type';
15
+ import { createInsertCodeBlockTransaction, insertCodeBlockWithAnalytics } from './actions';
16
+ import { createCodeBlockInputRule } from './pm-plugins/input-rule';
19
17
  var codeBlockPlugin = function codeBlockPlugin(_ref) {
20
18
  var options = _ref.config,
21
19
  api = _ref.api;
@@ -38,6 +36,13 @@ var codeBlockPlugin = function codeBlockPlugin(_ref) {
38
36
  appearance: (_options$appearance = options === null || options === void 0 ? void 0 : options.appearance) !== null && _options$appearance !== void 0 ? _options$appearance : 'comment'
39
37
  }));
40
38
  }
39
+ }, {
40
+ name: 'codeBlockInputRule',
41
+ plugin: function plugin(_ref3) {
42
+ var _api$analytics;
43
+ var schema = _ref3.schema;
44
+ return createCodeBlockInputRule(schema, api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions);
45
+ }
41
46
  }, {
42
47
  name: 'codeBlockIDEKeyBindings',
43
48
  plugin: function plugin() {
@@ -45,8 +50,8 @@ var codeBlockPlugin = function codeBlockPlugin(_ref) {
45
50
  }
46
51
  }, {
47
52
  name: 'codeBlockKeyMap',
48
- plugin: function plugin(_ref3) {
49
- var schema = _ref3.schema;
53
+ plugin: function plugin(_ref4) {
54
+ var schema = _ref4.schema;
50
55
  return keymap(schema);
51
56
  }
52
57
  }, {
@@ -61,9 +66,18 @@ var codeBlockPlugin = function codeBlockPlugin(_ref) {
61
66
  onEditorViewStateUpdated: function onEditorViewStateUpdated(props) {
62
67
  refreshBrowserSelectionOnChange(props.originalTransaction, props.newEditorState);
63
68
  },
69
+ actions: {
70
+ /*
71
+ * Function will insert code block at current selection if block is empty or below current selection and set focus on it.
72
+ */
73
+ insertCodeBlock: function insertCodeBlock(inputMethod) {
74
+ var _api$analytics2;
75
+ return insertCodeBlockWithAnalytics(inputMethod, api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions);
76
+ }
77
+ },
64
78
  pluginsOptions: {
65
- quickInsert: function quickInsert(_ref4) {
66
- var formatMessage = _ref4.formatMessage;
79
+ quickInsert: function quickInsert(_ref5) {
80
+ var formatMessage = _ref5.formatMessage;
67
81
  return [{
68
82
  id: 'codeblock',
69
83
  title: formatMessage(messages.codeblock),
@@ -74,12 +88,12 @@ var codeBlockPlugin = function codeBlockPlugin(_ref) {
74
88
  icon: function icon() {
75
89
  return /*#__PURE__*/React.createElement(IconCode, null);
76
90
  },
77
- action: function action(insert, state) {
78
- var _api$analytics;
91
+ action: function action(_insert, state) {
92
+ var _api$analytics3;
79
93
  var tr = createInsertCodeBlockTransaction({
80
94
  state: state
81
95
  });
82
- api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions.attachAnalyticsEvent({
96
+ api === null || api === void 0 ? void 0 : (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions.attachAnalyticsEvent({
83
97
  action: ACTION.INSERTED,
84
98
  actionSubject: ACTION_SUBJECT.DOCUMENT,
85
99
  actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
@@ -0,0 +1,62 @@
1
+ import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
2
+ import { createRule, createPlugin, leafNodeReplacementCharacter } from '@atlaskit/prosemirror-input-rules';
3
+ import { INPUT_METHOD, ACTION, ACTION_SUBJECT, EVENT_TYPE, ACTION_SUBJECT_ID } from '@atlaskit/editor-common/analytics';
4
+ import { inputRuleWithAnalytics } from '@atlaskit/editor-common/utils';
5
+ import { isConvertableToCodeBlock, transformToCodeBlockAction } from '../transform-to-code-block';
6
+ import { insertBlock } from '@atlaskit/editor-common/commands';
7
+ export function createCodeBlockInputRule(schema, editorAnalyticsAPI) {
8
+ var rules = getCodeBlockRules(editorAnalyticsAPI, schema);
9
+ return createPlugin('code-block-input-rule', rules, {
10
+ isBlockNodeRule: true
11
+ });
12
+ }
13
+
14
+ /**
15
+ * Get all code block input rules
16
+ *
17
+ * @param {Schema} schema
18
+ * @returns {InputRuleWithHandler[]}
19
+ */
20
+ function getCodeBlockRules(editorAnalyticsAPI, schema) {
21
+ var ruleAnalytics = inputRuleWithAnalytics({
22
+ action: ACTION.INSERTED,
23
+ actionSubject: ACTION_SUBJECT.DOCUMENT,
24
+ actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
25
+ attributes: {
26
+ inputMethod: INPUT_METHOD.FORMATTING
27
+ },
28
+ eventType: EVENT_TYPE.TRACK
29
+ }, editorAnalyticsAPI);
30
+ var validMatchLength = function validMatchLength(match) {
31
+ return match.length > 0 && match[0].length === 3;
32
+ };
33
+ var threeTildeRule = createRule(/(?!\s)(`{3,})$/, function (state, match, start, end) {
34
+ if (!validMatchLength(match)) {
35
+ return null;
36
+ }
37
+ var attributes = {};
38
+ if (match[4]) {
39
+ attributes.language = match[4];
40
+ }
41
+ if (isConvertableToCodeBlock(state)) {
42
+ return transformToCodeBlockAction(state, start, attributes);
43
+ }
44
+ var tr = state.tr;
45
+ tr.delete(start, end);
46
+ var codeBlock = tr.doc.type.schema.nodes.codeBlock.createChecked();
47
+ safeInsert(codeBlock)(tr);
48
+ return tr;
49
+ });
50
+ var leftNodeReplacementThreeTildeRule = createRule(new RegExp("((".concat(leafNodeReplacementCharacter, "`{3,})|^\\s(`{3,}))(\\S*)$")), function (state, match, start, end) {
51
+ if (!validMatchLength(match)) {
52
+ return null;
53
+ }
54
+ var attributes = {};
55
+ if (match[4]) {
56
+ attributes.language = match[4];
57
+ }
58
+ var inlineStart = Math.max(match.index + state.selection.$from.start(), 1);
59
+ return insertBlock(state, schema.nodes.codeBlock, inlineStart, end, attributes);
60
+ });
61
+ return [ruleAnalytics(threeTildeRule), ruleAnalytics(leftNodeReplacementThreeTildeRule)];
62
+ }