@blocknote/core 0.54.0 → 0.54.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 (62) hide show
  1. package/dist/blocknote.cjs +1 -1
  2. package/dist/blocknote.js +6 -6
  3. package/dist/{blocks-CzkqclGj.cjs → blocks-CTB_5Vyj.cjs} +4 -4
  4. package/dist/blocks-CTB_5Vyj.cjs.map +1 -0
  5. package/dist/{blocks-CzQLehlc.js → blocks-Dol85hpH.js} +337 -330
  6. package/dist/blocks-Dol85hpH.js.map +1 -0
  7. package/dist/blocks.cjs +1 -1
  8. package/dist/blocks.js +2 -2
  9. package/dist/extensions-B7Rsp_Ft.cjs +58 -0
  10. package/dist/extensions-B7Rsp_Ft.cjs.map +1 -0
  11. package/dist/{extensions-DGuhtOgB.js → extensions-D6JcJCwd.js} +389 -346
  12. package/dist/extensions-D6JcJCwd.js.map +1 -0
  13. package/dist/extensions.cjs +1 -1
  14. package/dist/extensions.js +3 -3
  15. package/dist/{src-BkfZVfaO.js → src-Buuo5l7X.js} +521 -516
  16. package/dist/{src-BkfZVfaO.js.map → src-Buuo5l7X.js.map} +1 -1
  17. package/dist/{src-DM3syMdf.cjs → src-C54Ok9E0.cjs} +10 -10
  18. package/dist/{src-DM3syMdf.cjs.map → src-C54Ok9E0.cjs.map} +1 -1
  19. package/dist/webpack-stats.json +1 -1
  20. package/dist/y.cjs +1 -1
  21. package/dist/y.cjs.map +1 -1
  22. package/dist/y.js +205 -205
  23. package/dist/y.js.map +1 -1
  24. package/dist/yjs.cjs +1 -1
  25. package/dist/yjs.js +21 -21
  26. package/package.json +1 -1
  27. package/src/api/getBlocksChangedByTransaction.test.ts +211 -0
  28. package/src/api/getBlocksChangedByTransaction.ts +48 -12
  29. package/src/api/getChangedRange.ts +54 -0
  30. package/src/blocks/index.ts +0 -1
  31. package/src/editor/performance.test.ts +45 -0
  32. package/src/exporter/Exporter.test.ts +148 -10
  33. package/src/exporter/Exporter.ts +20 -2
  34. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +21 -0
  35. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +5 -3
  36. package/src/extensions/TableHandles/TableHandles.browser.test.ts +305 -0
  37. package/src/extensions/TableHandles/TableHandles.ts +82 -41
  38. package/src/fonts/inter.css +9 -9
  39. package/src/schema/blocks/createSpec.ts +4 -5
  40. package/src/schema/inlineContent/createSpec.ts +4 -5
  41. package/src/schema/nodeViewMutations.test.ts +81 -56
  42. package/src/schema/nodeViewMutations.ts +30 -27
  43. package/src/schema/schema.test.ts +110 -0
  44. package/src/schema/schema.ts +13 -4
  45. package/src/y/extensions/AttributionExtension.test.ts +8 -9
  46. package/src/y/versioning/__test__/yhub.test.ts +92 -59
  47. package/src/y/versioning/yhub.ts +35 -29
  48. package/types/src/api/getChangedRange.d.ts +14 -0
  49. package/types/src/blocks/index.d.ts +0 -1
  50. package/types/src/exporter/Exporter.d.ts +9 -0
  51. package/types/src/extensions/TableHandles/TableHandles.browser.test.d.ts +1 -0
  52. package/types/src/extensions/TableHandles/TableHandles.d.ts +3 -0
  53. package/types/src/schema/nodeViewMutations.d.ts +2 -2
  54. package/types/src/schema/schema.test.d.ts +1 -0
  55. package/types/src/y/versioning/yhub.d.ts +3 -2
  56. package/dist/blocks-CzQLehlc.js.map +0 -1
  57. package/dist/blocks-CzkqclGj.cjs.map +0 -1
  58. package/dist/extensions-CSB9ZvuL.cjs +0 -58
  59. package/dist/extensions-CSB9ZvuL.cjs.map +0 -1
  60. package/dist/extensions-DGuhtOgB.js.map +0 -1
  61. package/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.ts +0 -20
  62. package/types/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.d.ts +0 -6
@@ -1,3 +1,4 @@
1
+ import type { Node } from "prosemirror-model";
1
2
  import { EditorState, Plugin, PluginKey, PluginView } from "prosemirror-state";
2
3
  import {
3
4
  CellSelection,
@@ -27,7 +28,7 @@ import {
27
28
  import { nodeToBlock } from "../../api/nodeConversions/nodeToBlock.js";
28
29
  import { getNodeById } from "../../api/nodeUtil.js";
29
30
  import {
30
- editorHasBlockWithType,
31
+ blockHasType,
31
32
  isTableCellNode,
32
33
  isTableCellSelection,
33
34
  } from "../../blocks/defaultBlockTypeGuards.js";
@@ -188,6 +189,17 @@ export class TableHandlesView implements PluginView {
188
189
  this.mouseState = "down";
189
190
  };
190
191
 
192
+ // Hides the handles if they're currently shown. Used whenever the mouse moves
193
+ // somewhere that shouldn't have table handles attached to it.
194
+ hideHandles = () => {
195
+ if (this.state?.show) {
196
+ this.state.show = false;
197
+ this.state.showAddOrRemoveRowsButton = false;
198
+ this.state.showAddOrRemoveColumnsButton = false;
199
+ this.emitUpdate();
200
+ }
201
+ };
202
+
191
203
  mouseUpHandler = (event: MouseEvent) => {
192
204
  this.mouseState = "up";
193
205
  this.mouseMoveHandler(event);
@@ -219,22 +231,12 @@ export class TableHandlesView implements PluginView {
219
231
  // hide draghandles when selecting text as they could be in the way of the user
220
232
  this.mouseState = "selecting";
221
233
 
222
- if (this.state?.show) {
223
- this.state.show = false;
224
- this.state.showAddOrRemoveRowsButton = false;
225
- this.state.showAddOrRemoveColumnsButton = false;
226
- this.emitUpdate();
227
- }
234
+ this.hideHandles();
228
235
  return;
229
236
  }
230
237
 
231
238
  if (!target || !this.editor.isEditable) {
232
- if (this.state?.show) {
233
- this.state.show = false;
234
- this.state.showAddOrRemoveRowsButton = false;
235
- this.state.showAddOrRemoveColumnsButton = false;
236
- this.emitUpdate();
237
- }
239
+ this.hideHandles();
238
240
  return;
239
241
  }
240
242
 
@@ -248,38 +250,40 @@ export class TableHandlesView implements PluginView {
248
250
  if (!blockEl) {
249
251
  return;
250
252
  }
251
- this.tableElement = blockEl.node;
252
-
253
- let tableBlock:
254
- | BlockFromConfigNoChildren<DefaultBlockSchema["table"], any, any>
255
- | undefined;
256
253
 
257
254
  const { pmNodeInfo, doc } = this.editor.transact((tr) => ({
258
255
  pmNodeInfo: getNodeById(blockEl.id, tr.doc),
259
256
  doc: tr.doc,
260
257
  }));
258
+
259
+ // The hovered cell may belong to a document other than this editor's, as a
260
+ // custom block can embed a nested editor which itself contains a table. The
261
+ // nested editor's DOM is inside this view's DOM, so its cells still reach
262
+ // this handler, but its block IDs are unknown here.
261
263
  if (!pmNodeInfo) {
262
- throw new Error(`Block with ID ${blockEl.id} not found`);
264
+ this.hideHandles();
265
+ return;
263
266
  }
264
267
 
265
- const block = nodeToBlock(
266
- pmNodeInfo.node,
267
- doc,
268
- ) as unknown as BlockFromConfigNoChildren<
268
+ const block = nodeToBlock(pmNodeInfo.node, doc);
269
+
270
+ // `domCellAround` matches any `TD`/`TH` in the DOM, so the hovered block may
271
+ // just as well be a custom block that renders a table of its own. Checking
272
+ // the block itself (rather than only whether the schema has a table block)
273
+ // keeps the handles off blocks that have no table content to work with.
274
+ if (!blockHasType(block, this.editor, "table")) {
275
+ this.hideHandles();
276
+ return;
277
+ }
278
+
279
+ const tableBlock = block as unknown as BlockFromConfigNoChildren<
269
280
  DefaultBlockSchema["table"],
270
281
  any,
271
282
  any
272
283
  >;
273
284
 
274
- if (editorHasBlockWithType(this.editor, "table")) {
275
- this.tablePos = pmNodeInfo.posBeforeNode + 1;
276
- tableBlock = block;
277
- }
278
-
279
- if (!tableBlock) {
280
- return;
281
- }
282
-
285
+ this.tablePos = pmNodeInfo.posBeforeNode + 1;
286
+ this.tableElement = blockEl.node;
283
287
  this.tableId = blockEl.id;
284
288
  const widgetContainer = target.domNode
285
289
  .closest(".tableWrapper")
@@ -527,6 +531,22 @@ export class TableHandlesView implements PluginView {
527
531
 
528
532
  return true;
529
533
  };
534
+
535
+ // Resolves the position just inside the hovered table, within `doc`. The
536
+ // position is looked up by ID on each use rather than read from a cached one,
537
+ // as anything changing before the table (a collaborator inserting a block
538
+ // above it, say) shifts it: a stale position resolves into the wrong node, or
539
+ // past the end of the document entirely.
540
+ getTablePos(doc: Node): number | undefined {
541
+ if (this.tableId === undefined) {
542
+ return undefined;
543
+ }
544
+
545
+ const pmNodeInfo = getNodeById(this.tableId, doc);
546
+
547
+ return pmNodeInfo && pmNodeInfo.posBeforeNode + 1;
548
+ }
549
+
530
550
  // Updates drag handles when the table is modified or removed.
531
551
  update() {
532
552
  if (!this.state || !this.state.show) {
@@ -544,6 +564,7 @@ export class TableHandlesView implements PluginView {
544
564
  ) {
545
565
  this.state = undefined;
546
566
  this.tableId = undefined;
567
+ this.tablePos = undefined;
547
568
  this.tableElement = undefined;
548
569
  this.emitUpdate();
549
570
 
@@ -551,6 +572,9 @@ export class TableHandlesView implements PluginView {
551
572
  }
552
573
  this.state.block = refreshedBlock as typeof this.state.block;
553
574
 
575
+ // The table may have shifted in this update, so re-resolve its position.
576
+ this.tablePos = this.getTablePos(this.pmView.state.doc);
577
+
554
578
  const { height: rowCount, width: colCount } = getDimensionsOfTable(
555
579
  this.state.block,
556
580
  );
@@ -647,12 +671,21 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
647
671
  if (
648
672
  view === undefined ||
649
673
  view.state === undefined ||
650
- view.state.draggingState === undefined ||
651
- view.tablePos === undefined
674
+ view.state.draggingState === undefined
652
675
  ) {
653
676
  return;
654
677
  }
655
678
 
679
+ // Resolved against the state being rendered rather than read from
680
+ // `view.tablePos`, as ProseMirror computes decorations for a
681
+ // transaction before it calls the plugin view's `update` - so the
682
+ // cached position is still the pre-transaction one here.
683
+ const tablePos = view.getTablePos(state.doc);
684
+
685
+ if (tablePos === undefined) {
686
+ return;
687
+ }
688
+
656
689
  const newIndex =
657
690
  view.state.draggingState.draggedCellOrientation === "row"
658
691
  ? view.state.rowIndex
@@ -683,7 +716,7 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
683
716
  }
684
717
 
685
718
  // Gets the table to show the drop cursor in.
686
- const tableResolvedPos = state.doc.resolve(view.tablePos + 1);
719
+ const tableResolvedPos = state.doc.resolve(tablePos + 1);
687
720
 
688
721
  if (view.state.draggingState.draggedCellOrientation === "row") {
689
722
  const cellsInRow = getCellsAtRowHandle(
@@ -916,11 +949,8 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
916
949
  * interfering with open submenus.
917
950
  */
918
951
  hideHandlesIfNotFrozen() {
919
- if (!view!.menuFrozen && view!.state?.show) {
920
- view!.state.show = false;
921
- view!.state.showAddOrRemoveRowsButton = false;
922
- view!.state.showAddOrRemoveColumnsButton = false;
923
- view!.emitUpdate();
952
+ if (!view!.menuFrozen) {
953
+ view!.hideHandles();
924
954
  }
925
955
  },
926
956
 
@@ -954,7 +984,18 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
954
984
  throw new Error("Table handles view not initialized");
955
985
  }
956
986
 
957
- const tableResolvedPos = state.doc.resolve(view.tablePos! + 1);
987
+ // Resolved against `state`, which may be several transactions ahead of
988
+ // the mouse move that attached the handles (a menu can stay open across
989
+ // edits from elsewhere, e.g. a collaborator).
990
+ const tablePos = view.getTablePos(state.doc);
991
+
992
+ if (tablePos === undefined) {
993
+ throw new Error(
994
+ "Attempted to select table cells, but the hovered table is no longer in the document.",
995
+ );
996
+ }
997
+
998
+ const tableResolvedPos = state.doc.resolve(tablePos + 1);
958
999
  const startRowResolvedPos = state.doc.resolve(
959
1000
  tableResolvedPos.posAtIndex(relativeStartCell.row) + 1,
960
1001
  );
@@ -9,7 +9,7 @@
9
9
  local(""),
10
10
  url("./inter-v12-latin/inter-v12-latin-100.woff2") format("woff2"),
11
11
  /* Chrome 26+, Opera 23+, Firefox 39+ */
12
- url("./inter-v12-latin/inter-v12-latin-100.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
12
+ url("./inter-v12-latin/inter-v12-latin-100.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
13
13
  }
14
14
  /* inter-200 - latin */
15
15
  @font-face {
@@ -20,7 +20,7 @@
20
20
  local(""),
21
21
  url("./inter-v12-latin/inter-v12-latin-200.woff2") format("woff2"),
22
22
  /* Chrome 26+, Opera 23+, Firefox 39+ */
23
- url("./inter-v12-latin/inter-v12-latin-200.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
23
+ url("./inter-v12-latin/inter-v12-latin-200.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
24
24
  }
25
25
  /* inter-300 - latin */
26
26
  @font-face {
@@ -31,7 +31,7 @@
31
31
  local(""),
32
32
  url("./inter-v12-latin/inter-v12-latin-300.woff2") format("woff2"),
33
33
  /* Chrome 26+, Opera 23+, Firefox 39+ */
34
- url("./inter-v12-latin/inter-v12-latin-300.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
34
+ url("./inter-v12-latin/inter-v12-latin-300.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
35
35
  }
36
36
  /* inter-regular - latin */
37
37
  @font-face {
@@ -42,7 +42,7 @@
42
42
  local(""),
43
43
  url("./inter-v12-latin/inter-v12-latin-regular.woff2") format("woff2"),
44
44
  /* Chrome 26+, Opera 23+, Firefox 39+ */
45
- url("./inter-v12-latin/inter-v12-latin-regular.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
45
+ url("./inter-v12-latin/inter-v12-latin-regular.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
46
46
  }
47
47
  /* inter-500 - latin */
48
48
  @font-face {
@@ -53,7 +53,7 @@
53
53
  local(""),
54
54
  url("./inter-v12-latin/inter-v12-latin-500.woff2") format("woff2"),
55
55
  /* Chrome 26+, Opera 23+, Firefox 39+ */
56
- url("./inter-v12-latin/inter-v12-latin-500.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
56
+ url("./inter-v12-latin/inter-v12-latin-500.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
57
57
  }
58
58
  /* inter-600 - latin */
59
59
  @font-face {
@@ -64,7 +64,7 @@
64
64
  local(""),
65
65
  url("./inter-v12-latin/inter-v12-latin-600.woff2") format("woff2"),
66
66
  /* Chrome 26+, Opera 23+, Firefox 39+ */
67
- url("./inter-v12-latin/inter-v12-latin-600.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
67
+ url("./inter-v12-latin/inter-v12-latin-600.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
68
68
  }
69
69
  /* inter-700 - latin */
70
70
  @font-face {
@@ -75,7 +75,7 @@
75
75
  local(""),
76
76
  url("./inter-v12-latin/inter-v12-latin-700.woff2") format("woff2"),
77
77
  /* Chrome 26+, Opera 23+, Firefox 39+ */
78
- url("./inter-v12-latin/inter-v12-latin-700.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
78
+ url("./inter-v12-latin/inter-v12-latin-700.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
79
79
  }
80
80
  /* inter-800 - latin */
81
81
  @font-face {
@@ -86,7 +86,7 @@
86
86
  local(""),
87
87
  url("./inter-v12-latin/inter-v12-latin-800.woff2") format("woff2"),
88
88
  /* Chrome 26+, Opera 23+, Firefox 39+ */
89
- url("./inter-v12-latin/inter-v12-latin-800.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
89
+ url("./inter-v12-latin/inter-v12-latin-800.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
90
90
  }
91
91
  /* inter-900 - latin */
92
92
  @font-face {
@@ -97,5 +97,5 @@
97
97
  local(""),
98
98
  url("./inter-v12-latin/inter-v12-latin-900.woff2") format("woff2"),
99
99
  /* Chrome 26+, Opera 23+, Firefox 39+ */
100
- url("./inter-v12-latin/inter-v12-latin-900.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
100
+ url("./inter-v12-latin/inter-v12-latin-900.woff") format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
101
101
  }
@@ -12,7 +12,7 @@ import {
12
12
  ExtensionFactoryInstance,
13
13
  } from "../../editor/BlockNoteExtension.js";
14
14
  import { nonFormattingMarks } from "../markGroups.js";
15
- import { ignoreNonContentMutations } from "../nodeViewMutations.js";
15
+ import { ignoreDarkReaderMutations } from "../nodeViewMutations.js";
16
16
  import { PropSchema } from "../propTypes.js";
17
17
  import {
18
18
  getBlockFromNodeView,
@@ -279,10 +279,9 @@ export function addNodeAndExtensionsToSpec<
279
279
  applyNonSelectableBlockFix(typedNodeView, this.editor);
280
280
  }
281
281
 
282
- // Ignores DOM mutations that don't affect the block's content, so
283
- // that browser extensions which rewrite the DOM (e.g. Dark Reader)
284
- // can't trigger an infinite re-render loop that freezes the tab.
285
- ignoreNonContentMutations(typedNodeView);
282
+ // Ignores Dark Reader's rewrites of the block's DOM, which would
283
+ // otherwise trigger an infinite re-render loop that freezes the tab.
284
+ ignoreDarkReaderMutations(typedNodeView);
286
285
 
287
286
  // See explanation for why `update` is not implemented for NodeViews
288
287
  // https://github.com/TypeCellOS/BlockNote/pull/1904#discussion_r2313461464
@@ -10,7 +10,7 @@ import {
10
10
  import { inlineContentToNodes } from "../../api/nodeConversions/blockToNode.js";
11
11
  import { nodeToCustomInlineContent } from "../../api/nodeConversions/nodeToBlock.js";
12
12
  import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
13
- import { ignoreNonContentMutations } from "../nodeViewMutations.js";
13
+ import { ignoreDarkReaderMutations } from "../nodeViewMutations.js";
14
14
  import { propsToAttributes } from "../blocks/internal.js";
15
15
  import { nonFormattingMarks } from "../markGroups.js";
16
16
  import { Props } from "../propTypes.js";
@@ -366,10 +366,9 @@ export function createInlineContentSpec<
366
366
  inlineContentConfig.propSchema,
367
367
  );
368
368
 
369
- // Ignores DOM mutations that don't affect the inline content, so that
370
- // browser extensions which rewrite the DOM (e.g. Dark Reader) can't
371
- // trigger an infinite re-render loop that freezes the tab.
372
- ignoreNonContentMutations(nodeView);
369
+ // Ignores Dark Reader's rewrites of the inline content's DOM, which
370
+ // would otherwise trigger an infinite re-render loop that freezes the tab.
371
+ ignoreDarkReaderMutations(nodeView);
373
372
 
374
373
  return nodeView;
375
374
  };
@@ -2,15 +2,20 @@ import { NodeView, ViewMutationRecord } from "@tiptap/pm/view";
2
2
  import { describe, expect, it } from "vite-plus/test";
3
3
 
4
4
  import {
5
- ignoreNonContentMutations,
6
- isNonContentMutation,
5
+ ignoreDarkReaderMutations,
6
+ isDarkReaderMutation,
7
7
  } from "./nodeViewMutations.js";
8
8
 
9
- function attributeMutation(target: Node): ViewMutationRecord {
9
+ function attributeMutation(
10
+ target: Element,
11
+ attributeName: string,
12
+ oldValue: string | null = null,
13
+ ): ViewMutationRecord {
10
14
  return {
11
15
  type: "attributes",
12
16
  target,
13
- attributeName: "style",
17
+ attributeName,
18
+ oldValue,
14
19
  } as unknown as ViewMutationRecord;
15
20
  }
16
21
 
@@ -25,97 +30,117 @@ function childListMutation(target: Node): ViewMutationRecord {
25
30
 
26
31
  const selectionMutation = { type: "selection" } as ViewMutationRecord;
27
32
 
28
- describe("isNonContentMutation", () => {
33
+ describe("isDarkReaderMutation", () => {
29
34
  it("never ignores selection mutations", () => {
30
- expect(isNonContentMutation(selectionMutation, null)).toBe(false);
31
- expect(
32
- isNonContentMutation(selectionMutation, document.createElement("div")),
33
- ).toBe(false);
35
+ expect(isDarkReaderMutation(selectionMutation)).toBe(false);
34
36
  });
35
37
 
36
- it("ignores everything for a node view without content", () => {
37
- const target = document.createElement("span");
38
- expect(isNonContentMutation(attributeMutation(target), null)).toBe(true);
39
- expect(isNonContentMutation(childListMutation(target), null)).toBe(true);
38
+ it("ignores the attributes Dark Reader stamps on recoloured elements", () => {
39
+ const span = document.createElement("span");
40
+ span.setAttribute("data-darkreader-inline-bgcolor", "");
41
+ expect(
42
+ isDarkReaderMutation(
43
+ attributeMutation(span, "data-darkreader-inline-bgcolor"),
44
+ ),
45
+ ).toBe(true);
40
46
  });
41
47
 
42
- it("ignores attribute mutations even inside the content DOM", () => {
43
- const contentDOM = document.createElement("div");
48
+ it("ignores inline style writes that add or remove Dark Reader declarations", () => {
44
49
  const span = document.createElement("span");
45
- contentDOM.appendChild(span);
46
-
47
- // e.g. Dark Reader rewriting the inline style of a highlight span.
48
- expect(isNonContentMutation(attributeMutation(span), contentDOM)).toBe(
49
- true,
50
+ span.setAttribute(
51
+ "style",
52
+ "color: #f00; --darkreader-inline-color: #ff8080;",
50
53
  );
54
+ // Added: the old value had none.
51
55
  expect(
52
- isNonContentMutation(attributeMutation(contentDOM), contentDOM),
56
+ isDarkReaderMutation(attributeMutation(span, "style", "color: #f00;")),
53
57
  ).toBe(true);
54
- });
55
58
 
56
- it("reads content mutations inside the content DOM", () => {
57
- const contentDOM = document.createElement("div");
58
- const textNode = document.createTextNode("hello");
59
- contentDOM.appendChild(textNode);
60
-
61
- // childList directly on the content DOM (e.g. a node inserted while typing).
59
+ // Removed (the extension toggled off): only the old value had them.
60
+ span.setAttribute("style", "color: #f00;");
62
61
  expect(
63
- isNonContentMutation(childListMutation(contentDOM), contentDOM),
64
- ).toBe(false);
65
- // characterData-style mutation on a text node inside the content DOM.
66
- expect(isNonContentMutation(childListMutation(textNode), contentDOM)).toBe(
62
+ isDarkReaderMutation(
63
+ attributeMutation(
64
+ span,
65
+ "style",
66
+ "color: #f00; --darkreader-inline-color: #ff8080;",
67
+ ),
68
+ ),
69
+ ).toBe(true);
70
+ });
71
+
72
+ it("reads every other attribute mutation", () => {
73
+ const span = document.createElement("span");
74
+ span.setAttribute("style", "color: #f00;");
75
+ expect(isDarkReaderMutation(attributeMutation(span, "style", null))).toBe(
76
+ false,
77
+ );
78
+ expect(isDarkReaderMutation(attributeMutation(span, "class", null))).toBe(
79
+ false,
80
+ );
81
+ expect(isDarkReaderMutation(attributeMutation(span, "data-id", null))).toBe(
67
82
  false,
68
83
  );
69
84
  });
70
85
 
71
- it("ignores content mutations outside the content DOM (node view chrome)", () => {
86
+ it("reads child list mutations anywhere in the node view", () => {
72
87
  const dom = document.createElement("div");
73
- const contentDOM = document.createElement("div");
74
- const chrome = document.createElement("button"); // e.g. toggle button
75
- dom.append(chrome, contentDOM);
88
+ const contentDOM = document.createElement("p");
89
+ dom.append(document.createElement("button"), contentDOM);
76
90
 
77
- expect(isNonContentMutation(childListMutation(dom), contentDOM)).toBe(true);
78
- expect(isNonContentMutation(childListMutation(chrome), contentDOM)).toBe(
79
- true,
80
- );
91
+ expect(isDarkReaderMutation(childListMutation(contentDOM))).toBe(false);
92
+ // A browser's native paragraph split on Android and iOS inserts the new
93
+ // paragraph next to the content DOM, not inside it.
94
+ expect(isDarkReaderMutation(childListMutation(dom))).toBe(false);
81
95
  });
82
96
  });
83
97
 
84
- describe("ignoreNonContentMutations", () => {
85
- it("ignores non-content mutations while reading content ones", () => {
86
- const contentDOM = document.createElement("div");
98
+ describe("ignoreDarkReaderMutations", () => {
99
+ it("ignores Dark Reader writes while reading everything else", () => {
100
+ const contentDOM = document.createElement("p");
87
101
  const span = document.createElement("span");
102
+ span.setAttribute("style", "--darkreader-inline-color: #ff8080;");
88
103
  contentDOM.appendChild(span);
89
104
  const nodeView: NodeView = {
90
105
  dom: document.createElement("div"),
91
106
  contentDOM,
92
107
  };
93
108
 
94
- ignoreNonContentMutations(nodeView);
109
+ ignoreDarkReaderMutations(nodeView);
95
110
 
96
- // Non-content (attribute) mutation is ignored...
97
- expect(nodeView.ignoreMutation!(attributeMutation(span))).toBe(true);
98
- // ...content mutation is read...
111
+ expect(nodeView.ignoreMutation!(attributeMutation(span, "style"))).toBe(
112
+ true,
113
+ );
99
114
  expect(nodeView.ignoreMutation!(childListMutation(contentDOM))).toBe(false);
100
- // ...and selection is read.
101
115
  expect(nodeView.ignoreMutation!(selectionMutation)).toBe(false);
102
116
  });
103
117
 
104
- it("still defers to an existing ignoreMutation for content mutations", () => {
105
- const contentDOM = document.createElement("div");
118
+ it("keeps prosemirror-view's default for a node view without a content DOM", () => {
119
+ const dom = document.createElement("div");
120
+ const nodeView: NodeView = { dom };
121
+
122
+ ignoreDarkReaderMutations(nodeView);
123
+
124
+ // Nothing to read back: every mutation but the selection is ignored, as
125
+ // prosemirror-view does when a node view defines no `ignoreMutation`.
126
+ expect(nodeView.ignoreMutation!(attributeMutation(dom, "class"))).toBe(
127
+ true,
128
+ );
129
+ expect(nodeView.ignoreMutation!(childListMutation(dom))).toBe(true);
130
+ expect(nodeView.ignoreMutation!(selectionMutation)).toBe(false);
131
+ });
132
+
133
+ it("still defers to an existing ignoreMutation", () => {
134
+ const contentDOM = document.createElement("p");
106
135
  const nodeView: NodeView = {
107
136
  dom: document.createElement("div"),
108
137
  contentDOM,
109
- // Pretend this node view wants to ignore all of its content mutations.
138
+ // Pretend this node view wants to ignore all of its mutations.
110
139
  ignoreMutation: () => true,
111
140
  };
112
141
 
113
- ignoreNonContentMutations(nodeView);
142
+ ignoreDarkReaderMutations(nodeView);
114
143
 
115
- // A content mutation the filter would read is still ignored by the
116
- // original `ignoreMutation`.
117
144
  expect(nodeView.ignoreMutation!(childListMutation(contentDOM))).toBe(true);
118
- // Non-content mutations are ignored by the filter regardless.
119
- expect(nodeView.ignoreMutation!(attributeMutation(contentDOM))).toBe(true);
120
145
  });
121
146
  });
@@ -1,46 +1,49 @@
1
1
  import { NodeView, ViewMutationRecord } from "@tiptap/pm/view";
2
2
 
3
- // Ignores all mutations, except those which modify content. ProseMirror by default allows for
4
- // bidirectional updates between state & view i.e., mutating the view can cause a state update.
5
- // This means that basically any DOM mutation in a node view will trigger a re-render, which can
6
- // cause issues with certain browser extensions and interactive elements which aren't linked to
7
- // the node or editor state.
8
- export function isNonContentMutation(
9
- mutation: ViewMutationRecord,
10
- contentDOM: HTMLElement | null | undefined,
11
- ): boolean {
12
- // Let ProseMirror handle selection changes.
13
- if (mutation.type === "selection") {
3
+ // Dark Reader recolours a page by rewriting inline styles (every declaration
4
+ // it adds starts with `--darkreader`) and stamping `data-darkreader-*`
5
+ // attributes on the elements it touched. ProseMirror re-reads a node view on
6
+ // any DOM mutation inside it and re-renders it, after which the extension
7
+ // rewrites it again: an endless loop that froze the tab on code blocks and
8
+ // toggles (#2818). Only those writes are ignored. Every other mutation must
9
+ // reach ProseMirror, including a browser's native paragraph split, which lands
10
+ // next to the content DOM on Android and iOS: ignoring it left the split
11
+ // unread and a stray paragraph in the block (#3001).
12
+ export function isDarkReaderMutation(mutation: ViewMutationRecord): boolean {
13
+ if (mutation.type !== "attributes" || !mutation.attributeName) {
14
14
  return false;
15
15
  }
16
-
17
- // Ignore all mutations for nodes without content.
18
- if (!contentDOM) {
16
+ if (mutation.attributeName.startsWith("data-darkreader")) {
19
17
  return true;
20
18
  }
21
-
22
- // Ignore all changes to DOM attributes. If a DOM attribute value depends on the value of a
23
- // ProseMirror node's attribute, the change should be made to the ProseMirror node directly,
24
- // which will trigger a re-render. We don't propagate changes to the DOM back to the node.
25
- if (mutation.type === "attributes") {
26
- return true;
19
+ if (mutation.attributeName !== "style") {
20
+ return false;
27
21
  }
28
-
29
- // Everything left is a `childList` or `characterData` mutation (i.e., a content mutation). Only
30
- // those inside the content DOM are actual content edits that ProseMirror needs to read.
31
- return !contentDOM.contains(mutation.target);
22
+ const style = (mutation.target as Element).getAttribute("style") ?? "";
23
+ return (
24
+ style.includes("--darkreader") ||
25
+ (mutation.oldValue ?? "").includes("--darkreader")
26
+ );
32
27
  }
33
28
 
34
- export function ignoreNonContentMutations(nodeView: NodeView): void {
29
+ export function ignoreDarkReaderMutations(nodeView: NodeView): void {
35
30
  const originalIgnoreMutation = nodeView.ignoreMutation?.bind(nodeView);
36
31
  const contentDOM = nodeView.contentDOM;
37
32
 
38
33
  nodeView.ignoreMutation = (mutation: ViewMutationRecord) => {
39
- if (isNonContentMutation(mutation, contentDOM)) {
34
+ if (isDarkReaderMutation(mutation)) {
40
35
  return true;
41
36
  }
42
37
 
43
38
  // Defer to the node view's own `ignoreMutation` for additional filtering.
44
- return originalIgnoreMutation ? originalIgnoreMutation(mutation) : false;
39
+ if (originalIgnoreMutation) {
40
+ return originalIgnoreMutation(mutation);
41
+ }
42
+
43
+ // Defining `ignoreMutation` replaces prosemirror-view's default, so keep
44
+ // it: a node view without a content DOM (an image block, say) has nothing
45
+ // to read back, and reading its own DOM changes (the selected-node class,
46
+ // for one) resets a node selection, which broke copying an image.
47
+ return !contentDOM && mutation.type !== "selection";
45
48
  };
46
49
  }