@tiptap/core 2.0.0-beta.162 → 2.0.0-beta.166

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.
@@ -204,8 +204,12 @@
204
204
 
205
205
  const blur = () => ({ editor, view }) => {
206
206
  requestAnimationFrame(() => {
207
+ var _a;
207
208
  if (!editor.isDestroyed) {
208
209
  view.dom.blur();
210
+ // Browsers should remove the caret on blur but safari does not.
211
+ // See: https://github.com/ueberdosis/tiptap/issues/2405
212
+ (_a = window === null || window === void 0 ? void 0 : window.getSelection()) === null || _a === void 0 ? void 0 : _a.removeAllRanges();
209
213
  }
210
214
  });
211
215
  return true;
@@ -664,6 +668,7 @@
664
668
  let { from, to } = typeof position === 'number'
665
669
  ? { from: position, to: position }
666
670
  : position;
671
+ let isOnlyTextContent = true;
667
672
  let isOnlyBlockContent = true;
668
673
  const nodes = isFragment(content)
669
674
  ? content
@@ -671,6 +676,9 @@
671
676
  nodes.forEach(node => {
672
677
  // check if added node is valid
673
678
  node.check();
679
+ isOnlyTextContent = isOnlyTextContent
680
+ ? node.isText && node.marks.length === 0
681
+ : false;
674
682
  isOnlyBlockContent = isOnlyBlockContent
675
683
  ? node.isBlock
676
684
  : false;
@@ -690,7 +698,14 @@
690
698
  to += 1;
691
699
  }
692
700
  }
693
- tr.replaceWith(from, to, content);
701
+ // if there is only plain text we have to use `insertText`
702
+ // because this will keep the current marks
703
+ if (isOnlyTextContent) {
704
+ tr.insertText(value, from, to);
705
+ }
706
+ else {
707
+ tr.replaceWith(from, to, content);
708
+ }
694
709
  // set cursor at end of inserted content
695
710
  if (options.updateSelection) {
696
711
  selectionToInsertionEnd(tr, tr.steps.length - 1, -1);
@@ -2449,21 +2464,45 @@
2449
2464
  */
2450
2465
  function pasteRulesPlugin(props) {
2451
2466
  const { editor, rules } = props;
2452
- let isProseMirrorHTML = false;
2467
+ let dragSourceElement = null;
2468
+ let isPastedFromProseMirror = false;
2469
+ let isDroppedFromProseMirror = false;
2453
2470
  const plugins = rules.map(rule => {
2454
2471
  return new prosemirrorState.Plugin({
2455
- props: {
2456
- handlePaste: (view, event) => {
2472
+ // we register a global drag handler to track the current drag source element
2473
+ view(view) {
2474
+ const handleDragstart = (event) => {
2457
2475
  var _a;
2458
- const html = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/html');
2459
- isProseMirrorHTML = !!(html === null || html === void 0 ? void 0 : html.includes('data-pm-slice'));
2460
- return false;
2476
+ dragSourceElement = ((_a = view.dom.parentElement) === null || _a === void 0 ? void 0 : _a.contains(event.target))
2477
+ ? view.dom.parentElement
2478
+ : null;
2479
+ };
2480
+ window.addEventListener('dragstart', handleDragstart);
2481
+ return {
2482
+ destroy() {
2483
+ window.removeEventListener('dragstart', handleDragstart);
2484
+ },
2485
+ };
2486
+ },
2487
+ props: {
2488
+ handleDOMEvents: {
2489
+ drop: view => {
2490
+ isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement;
2491
+ return false;
2492
+ },
2493
+ paste: (view, event) => {
2494
+ var _a;
2495
+ const html = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/html');
2496
+ isPastedFromProseMirror = !!(html === null || html === void 0 ? void 0 : html.includes('data-pm-slice'));
2497
+ return false;
2498
+ },
2461
2499
  },
2462
2500
  },
2463
2501
  appendTransaction: (transactions, oldState, state) => {
2464
2502
  const transaction = transactions[0];
2465
- // stop if there is not a paste event
2466
- if (!transaction.getMeta('paste') || isProseMirrorHTML) {
2503
+ const isPaste = transaction.getMeta('uiEvent') === 'paste' && !isPastedFromProseMirror;
2504
+ const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror;
2505
+ if (!isPaste && !isDrop) {
2467
2506
  return;
2468
2507
  }
2469
2508
  // stop if there is no changed range
@@ -4077,6 +4116,11 @@ img.ProseMirror-separator {
4077
4116
  });
4078
4117
  }
4079
4118
 
4119
+ // source: https://stackoverflow.com/a/6969486
4120
+ function escapeForRegEx(string) {
4121
+ return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
4122
+ }
4123
+
4080
4124
  /**
4081
4125
  * Returns a new `Transform` based on all steps of the passed transactions.
4082
4126
  */
@@ -4332,6 +4376,7 @@ img.ProseMirror-separator {
4332
4376
  exports.callOrReturn = callOrReturn;
4333
4377
  exports.combineTransactionSteps = combineTransactionSteps;
4334
4378
  exports.defaultBlockAt = defaultBlockAt;
4379
+ exports.escapeForRegEx = escapeForRegEx;
4335
4380
  exports.extensions = extensions;
4336
4381
  exports.findChildren = findChildren;
4337
4382
  exports.findChildrenInRange = findChildrenInRange;