@tiptap/core 2.0.0-beta.163 → 2.0.0-beta.167

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);
@@ -722,7 +737,12 @@
722
737
  joinForward: joinForward
723
738
  });
724
739
 
725
- const mac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false;
740
+ function isMacOS() {
741
+ return typeof navigator !== 'undefined'
742
+ ? /Mac/.test(navigator.platform)
743
+ : false;
744
+ }
745
+
726
746
  function normalizeKeyName(name) {
727
747
  const parts = name.split(/-(?!$)/);
728
748
  let result = parts[parts.length - 1];
@@ -748,7 +768,7 @@
748
768
  shift = true;
749
769
  }
750
770
  else if (/^mod$/i.test(mod)) {
751
- if (mac) {
771
+ if (isiOS() || isMacOS()) {
752
772
  meta = true;
753
773
  }
754
774
  else {
@@ -1000,6 +1020,26 @@
1000
1020
  selectParentNode: selectParentNode
1001
1021
  });
1002
1022
 
1023
+ // @ts-ignore
1024
+ const selectTextblockEnd = () => ({ state, dispatch }) => {
1025
+ return prosemirrorCommands.selectTextblockEnd(state, dispatch);
1026
+ };
1027
+
1028
+ var selectTextblockEnd$1 = /*#__PURE__*/Object.freeze({
1029
+ __proto__: null,
1030
+ selectTextblockEnd: selectTextblockEnd
1031
+ });
1032
+
1033
+ // @ts-ignore
1034
+ const selectTextblockStart = () => ({ state, dispatch }) => {
1035
+ return prosemirrorCommands.selectTextblockStart(state, dispatch);
1036
+ };
1037
+
1038
+ var selectTextblockStart$1 = /*#__PURE__*/Object.freeze({
1039
+ __proto__: null,
1040
+ selectTextblockStart: selectTextblockStart
1041
+ });
1042
+
1003
1043
  function createDocument(content, schema, parseOptions = {}) {
1004
1044
  return createNodeFromContent(content, schema, { slice: false, parseOptions });
1005
1045
  }
@@ -1798,6 +1838,8 @@
1798
1838
  ...selectNodeBackward$1,
1799
1839
  ...selectNodeForward$1,
1800
1840
  ...selectParentNode$1,
1841
+ ...selectTextblockEnd$1,
1842
+ ...selectTextblockStart$1,
1801
1843
  ...setContent$1,
1802
1844
  ...setMark$1,
1803
1845
  ...setMeta$1,
@@ -2037,13 +2079,14 @@
2037
2079
  () => commands.joinForward(),
2038
2080
  () => commands.selectNodeForward(),
2039
2081
  ]);
2040
- return {
2041
- Enter: () => this.editor.commands.first(({ commands }) => [
2042
- () => commands.newlineInCode(),
2043
- () => commands.createParagraphNear(),
2044
- () => commands.liftEmptyBlock(),
2045
- () => commands.splitBlock(),
2046
- ]),
2082
+ const handleEnter = () => this.editor.commands.first(({ commands }) => [
2083
+ () => commands.newlineInCode(),
2084
+ () => commands.createParagraphNear(),
2085
+ () => commands.liftEmptyBlock(),
2086
+ () => commands.splitBlock(),
2087
+ ]);
2088
+ const baseKeymap = {
2089
+ Enter: handleEnter,
2047
2090
  'Mod-Enter': () => this.editor.commands.exitCode(),
2048
2091
  Backspace: handleBackspace,
2049
2092
  'Mod-Backspace': handleBackspace,
@@ -2052,6 +2095,26 @@
2052
2095
  'Mod-Delete': handleDelete,
2053
2096
  'Mod-a': () => this.editor.commands.selectAll(),
2054
2097
  };
2098
+ const pcKeymap = {
2099
+ ...baseKeymap,
2100
+ Home: () => this.editor.commands.selectTextblockStart(),
2101
+ End: () => this.editor.commands.selectTextblockStart(),
2102
+ };
2103
+ const macKeymap = {
2104
+ ...baseKeymap,
2105
+ 'Ctrl-h': handleBackspace,
2106
+ 'Alt-Backspace': handleBackspace,
2107
+ 'Ctrl-d': handleDelete,
2108
+ 'Ctrl-Alt-Backspace': handleDelete,
2109
+ 'Alt-Delete': handleDelete,
2110
+ 'Alt-d': handleDelete,
2111
+ 'Ctrl-a': () => this.editor.commands.selectTextblockStart(),
2112
+ 'Ctrl-e': () => this.editor.commands.selectTextblockEnd(),
2113
+ };
2114
+ if (isiOS() || isMacOS()) {
2115
+ return macKeymap;
2116
+ }
2117
+ return pcKeymap;
2055
2118
  },
2056
2119
  addProseMirrorPlugins() {
2057
2120
  return [
@@ -2449,21 +2512,45 @@
2449
2512
  */
2450
2513
  function pasteRulesPlugin(props) {
2451
2514
  const { editor, rules } = props;
2452
- let isProseMirrorHTML = false;
2515
+ let dragSourceElement = null;
2516
+ let isPastedFromProseMirror = false;
2517
+ let isDroppedFromProseMirror = false;
2453
2518
  const plugins = rules.map(rule => {
2454
2519
  return new prosemirrorState.Plugin({
2455
- props: {
2456
- handlePaste: (view, event) => {
2520
+ // we register a global drag handler to track the current drag source element
2521
+ view(view) {
2522
+ const handleDragstart = (event) => {
2457
2523
  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;
2524
+ dragSourceElement = ((_a = view.dom.parentElement) === null || _a === void 0 ? void 0 : _a.contains(event.target))
2525
+ ? view.dom.parentElement
2526
+ : null;
2527
+ };
2528
+ window.addEventListener('dragstart', handleDragstart);
2529
+ return {
2530
+ destroy() {
2531
+ window.removeEventListener('dragstart', handleDragstart);
2532
+ },
2533
+ };
2534
+ },
2535
+ props: {
2536
+ handleDOMEvents: {
2537
+ drop: view => {
2538
+ isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement;
2539
+ return false;
2540
+ },
2541
+ paste: (view, event) => {
2542
+ var _a;
2543
+ const html = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/html');
2544
+ isPastedFromProseMirror = !!(html === null || html === void 0 ? void 0 : html.includes('data-pm-slice'));
2545
+ return false;
2546
+ },
2461
2547
  },
2462
2548
  },
2463
2549
  appendTransaction: (transactions, oldState, state) => {
2464
2550
  const transaction = transactions[0];
2465
- // stop if there is not a paste event
2466
- if (!transaction.getMeta('paste') || isProseMirrorHTML) {
2551
+ const isPaste = transaction.getMeta('uiEvent') === 'paste' && !isPastedFromProseMirror;
2552
+ const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror;
2553
+ if (!isPaste && !isDrop) {
2467
2554
  return;
2468
2555
  }
2469
2556
  // stop if there is no changed range